Exemple #1
0
        private async Task SaveIdolImagesAsync(IEnumerable <IdolImage> images)
        {
            var imgDic = images.ToDictionary(_ => _.Hash);

            using (var db = new ImasCDDbContext(_ConnectionString))
            {
                db.Database.CommandTimeout = _Timeout;
                var dbImages = await db.IdolImages.ToDictionaryAsync(_ => _.Hash);

                foreach (var kv in imgDic)
                {
                    IdolImage d;
                    if (dbImages.TryGetValue(kv.Key, out d))
                    {
                        // 更新しない

                        //var s = kv.Value;

                        //d.Headline = !string.IsNullOrEmpty(s.Headline) ? s.Headline : d.Headline;
                        //d.Rarity = s.Rarity > 0 ? s.Rarity : d.Rarity;
                        //d.Type = s.Type > 0 ? s.Type : d.Type;
                        //d.BloodType = s.BloodType > 0 ? s.BloodType : d.BloodType;
                        //d.Height = s.Height > 0 ? s.Height : d.Height;
                        //d.Weight = s.Weight > 0 ? s.Weight : d.Weight;
                        //d.Bust = s.Bust > 0 ? s.Bust : d.Bust;
                        //d.Waist = s.Waist > 0 ? s.Waist : d.Waist;
                        //d.Hip = s.Hip > 0 ? s.Hip : d.Hip;
                        //d.Age = s.Age > 0 ? s.Age : d.Age;
                        //d.BirthMonth = s.BirthMonth > 0 ? s.BirthMonth : d.BirthMonth;
                        //d.BirthDay = s.BirthDay > 0 ? s.BirthDay : d.BirthDay;
                        //d.SunSign = s.SunSign > 0 ? s.SunSign : d.SunSign;
                        //d.Birthplace = !string.IsNullOrEmpty(s.Birthplace) ? s.Birthplace : d.Birthplace;
                        //d.Hobby = !string.IsNullOrEmpty(s.Hobby) ? s.Hobby : d.Hobby;
                        //d.Handedness = s.Handedness > 0 ? s.Handedness : d.Handedness;
                    }
                    else
                    {
                        if (kv.Value.Idol != null)
                        {
                            db.Idols.Attach(kv.Value.Idol);
                        }
                        db.IdolImages.Add(kv.Value);
                    }
                }

                await db.SaveChangesAsync();
            }
        }
Exemple #2
0
        public async Task <HttpResponseMessage> GetImageAsync(string hash, IdolImageDataType type)
        {
            using (var db = new ImasCDDbContext())
            {
                var d = await db.IdolImageData.FirstOrDefaultAsync(_ => _.Hash == hash && _.Type == type);

                if (d != null)
                {
                    if (d.Data == null)
                    {
                        throw new HttpResponseException(HttpStatusCode.NotFound);
                    }
                    return(CreateFileResult(d.Data, "image/jpeg"));
                }

                var u = IdolImageData.GetImageUrl(hash, type);

                byte[] data;
                using (var wc = new WebClient())
                {
                    try
                    {
                        data = await wc.DownloadDataTaskAsync(u);
                    }
                    catch
                    {
                        data = null;
                    }
                }

                db.IdolImageData.Add(new IdolImageData()
                {
                    Hash = hash,
                    Type = type,
                    Data = data
                });

                await db.SaveChangesAsync();

                if (data == null)
                {
                    throw new HttpResponseException(HttpStatusCode.NotFound);
                }
                return(CreateFileResult(data, "image/jpeg"));
            }
        }
Exemple #3
0
        private async Task InsertImageDataAsync(string hash, IdolImageDataType type, byte[] data)
        {
            using (var db = new ImasCDDbContext(_ConnectionString))
            {
                db.Database.CommandTimeout = _Timeout;
                var d = await db.IdolImageData.FirstOrDefaultAsync(_ => _.Hash == hash && _.Type == type);

                if (d == null)
                {
                    d      = new IdolImageData();
                    d.Hash = hash;
                    d.Type = type;
                    db.IdolImageData.Add(d);
                }
                d.Data = data;

                await db.SaveChangesAsync();
            }
        }