예제 #1
0
        public async Task CreateDBCacheIfNeededAsync(IEnumerable <string> filePaths)
        {
            // DBに存在しないものだけキャッシュ作成を行う
            foreach (var filePath in filePaths.Except(await _context.Photos.Select(p => p.FilePath).ToListAsync().ConfigureAwait(true)))
            {
                var photo = new Photo
                {
                    FilePath = filePath
                };

                // サムネイル作成
                // サムネイルは初表示時に作成する
                //photo.Thumbnail = await ImageHelper.CreateThumbnailAsync(filePath);

                // metaデータ読み込み
                if (!VrcMetaDataReader.TryRead(filePath, out VrcMetaData meta))
                {
                    // ファイル名から日付を取得
                    photo.Date = MetaDataHelper.GetDateTimeFromPhotoName(filePath);
                }
                else
                {
                    // 日付情報設定
                    photo.Date = meta.Date;

                    // ワールド情報設定
                    if (string.IsNullOrEmpty(meta.World))
                    {
                        photo.World = null;
                    }
                    else
                    {
                        if (!ExistsWorldByWorldName(meta.World, out var world))
                        {
                            photo.World = CreateWorld(meta.World);
                        }
                        else
                        {
                            photo.World = world;
                        }
                    }

                    // 撮影者情報設定
                    if (string.IsNullOrEmpty(meta.Photographer))
                    {
                        photo.Photographer = null;
                    }
                    else
                    {
                        var(exists, photographer) = await ExistsUserByUserNameAsync(meta.Photographer).ConfigureAwait(true);

                        if (!exists)
                        {
                            photo.Photographer = await CreateUserAsync(meta.Photographer).ConfigureAwait(true);
                        }
                        else
                        {
                            photo.Photographer = photographer;
                        }
                    }

                    // ユーザ情報設定
                    if (meta.Users.Any())
                    {
                        foreach (var metaUser in meta.Users)
                        {
                            var(exists, user) = await ExistsUserByUserNameAsync(metaUser.UserName).ConfigureAwait(true);

                            // TODO:TwitterのDisplaynameを入れていない
                            if (!exists)
                            {
                                user = await CreateUserAsync(metaUser.UserName).ConfigureAwait(true);
                            }

                            var photoUser = CreatePhotoUser(photo, user);

                            photo.PhotoUsers.Add(photoUser);
                        }
                    }
                }

                await _context.Photos.AddAsync(photo);

                await _context.SaveChangesAsync().ConfigureAwait(true);
            }
        }