Exemplo n.º 1
0
        public async Task <Score> GetScore(
            [FromRoute(Name = "score_name")] string scoreName)
        {
            var metaFileOperator = new MetaFileOperator(S3Client, BucketName);

            var urlConvertor = new MinioUrlConvertor(S3Client.Config.ServiceURL, BucketName);
            var metas        = await metaFileOperator.GetScoreMetaAsync(scoreName);

            Uri CreateVersionUrl(string name, int v)
            {
                return(new Uri($"{Request.Scheme}://{Request.Host}/api/v1/score/{name}/version/{v}"));
            }

            var meta = metas.GetLastScoreContent();

            var score = new Score()
            {
                Name            = meta.Name,
                Title           = meta.Title,
                Description     = meta.Description,
                VersionMetaUrls = meta.VersionFileKeys
                                  .OrderBy(x => x.Key)
                                  .Select(x => int.Parse(x.Key))
                                  .Select(version => new ScoreVersionMetaUrl()
                {
                    Version = version,
                    Url     = CreateVersionUrl(meta.Name, version),
                }).ToArray(),
            };

            return(score);
        }
Exemplo n.º 2
0
        public async Task <ScoreVersion> GetVersion(
            [FromRoute(Name = "score_name")] string scoreName,
            [FromRoute(Name = "version")] int version)
        {
            var metaFileOperator = new MetaFileOperator(S3Client, BucketName);

            var scoreMeta = await metaFileOperator.GetScoreMetaAsync(scoreName);

            var scoreContentMeta = scoreMeta.GetLastScoreContent();

            var versionText = version.ToString("00000");

            if (scoreContentMeta.VersionFileKeys.TryGetValue(versionText, out var versionFileKey) == false)
            {
                throw new InvalidOperationException($"'{scoreName}' にバージョン '{version}' は存在しません");
            }

            var versionMeta = await metaFileOperator.GetVersionMetaAsync(versionFileKey);

            var urlConvertor = new MinioUrlConvertor(S3Client.Config.ServiceURL, BucketName);

            var metaConvertor = new ScoreMetaConvertor();

            var scoreVersion = metaConvertor.ConvertTo(versionMeta, urlConvertor);

            return(scoreVersion);
        }
Exemplo n.º 3
0
        public async Task <IActionResult> CreateScoreAsync(
            [FromRoute(Name = "score_name")] string scoreName,
            [FromBody] PatchScore patchScore)
        {
            var convertor        = new ScoreMetaConvertor();
            var metaFileOperator = new MetaFileOperator(S3Client, BucketName);

            var key = $"{scoreName}/{ScoreMeta.FileName}";


            ScoreMeta scoreMeta;

            try
            {
                scoreMeta = await metaFileOperator.GetScoreMetaAsync(scoreName);
            }
            catch (AmazonS3Exception e)
            {
                Logger.LogError(e, e.Message);
                throw;
            }

            var newCurrentScoreMeta = convertor.ConvertToContent(scoreMeta.GetLastScoreContent(), patchScore);

            var newScoreMetaKey = ScoreMeta.CreateKey();

            scoreMeta[newScoreMetaKey] = newCurrentScoreMeta;

            try
            {
                await metaFileOperator.SaveScoreMetaAsync(scoreName, scoreMeta, newScoreMetaKey);
            }
            catch (Exception e)
            {
                Logger.LogError(e, e.Message);
                throw;
            }

            return(Ok());
        }
Exemplo n.º 4
0
        public async IAsyncEnumerable <Score> GetScores()
        {
            var metaFileOperator = new MetaFileOperator(S3Client, BucketName);

            var scoreNames = await metaFileOperator.GetScoreNamesAsync();

            var urlConvertor = new MinioUrlConvertor(S3Client.Config.ServiceURL, BucketName);

            var tasks = scoreNames.AsParallel()
                        .Select(name => metaFileOperator.GetScoreMetaAsync(name))
                        .ToArray();

            var scoreMetas = await Task.WhenAll(tasks);

            Uri CreateVersionUrl(string name, int v)
            {
                return(new Uri($"{Request.Scheme}://{Request.Host}/api/v1/score/{name}/version/{v}"));
            }

            var scores = scoreMetas.Select(metas => metas.GetLastScoreContent())
                         .Select(meta => new Score()
            {
                Name            = meta.Name,
                Title           = meta.Title,
                Description     = meta.Description,
                VersionMetaUrls = meta.VersionFileKeys
                                  .OrderBy(x => x.Key)
                                  .Select(x => int.Parse(x.Key))
                                  .Select(version => new ScoreVersionMetaUrl()
                {
                    Version = version,
                    Url     = CreateVersionUrl(meta.Name, version),
                }).ToArray(),
            });

            foreach (var score in scores)
            {
                yield return(score);
            }
        }
Exemplo n.º 5
0
        public async Task <IActionResult> CreateVersion(
            [FromRoute(Name = "score_name")] string scoreName,
            [FromForm] NewScoreVersion newScoreVersion)
        {
            var metaFileOperator = new MetaFileOperator(S3Client, BucketName);

            var images  = newScoreVersion.Images;
            var nosText = newScoreVersion.Nos;

            var keyCount = images.GroupBy(x => x.FileName, x => x)
                           .Select(x => (filename: x.Key, count: x.Count()))
                           .Where(x => 2 <= x.count)
                           .ToImmutableArray();

            if (keyCount.Any())
            {
                var errorMessage = string.Join(Environment.NewLine, new string[]
                {
                    "次のファイルが重複しています"
                }.Concat(keyCount.Select(x => $"'{x.filename}'")));
                throw new InvalidOperationException(errorMessage);
            }

            var nos = JsonSerializer.Deserialize <Dictionary <string, double> >(nosText);

            var notContainsNos = images.Select(x => x.FileName)
                                 .Where(x => !nos.ContainsKey(x))
                                 .ToImmutableArray();

            if (notContainsNos.Any())
            {
                var errorMessage = string.Join(Environment.NewLine, new string[]
                {
                    "次のファイルの No が指定されていません"
                }.Concat(notContainsNos.Select(x => $"'{x}'")));
                throw new InvalidOperationException(errorMessage);
            }

            var now = DateTimeOffset.UtcNow;

            var nextVersion = await metaFileOperator.NewVersionNumber(scoreName);

            var versionFileKey = metaFileOperator.CreateVersionFileKey(scoreName, nextVersion, now);

            var versionMeta = new ScoreVersionMeta()
            {
                Version = int.Parse(nextVersion),
            };

            foreach (var formFile in images)
            {
                var no = nos[formFile.FileName].ToString("F").TrimEnd('0').TrimEnd('.');

                var commentPrefix = metaFileOperator.CreatePageCommentPrefix(scoreName, nextVersion, now, no);
                try
                {
                    var key = await metaFileOperator.SaveImage(scoreName, formFile);

                    versionMeta.Pages[no] = new ScoreVersionPageMeta()
                    {
                        No            = no,
                        ImageFileKey  = key,
                        CommentPrefix = commentPrefix,
                    };
                }
                catch (AmazonS3Exception e)
                {
                    Logger.LogError(e, e.Message);
                    throw;
                }
            }

            await metaFileOperator.SaveVersionMetaAsync(versionFileKey, versionMeta);

            var scoreMeta = await metaFileOperator.GetScoreMetaAsync(scoreName);

            var scoreContentMeta = await scoreMeta.GetLastScoreContent().DeepCopyAsync();

            scoreContentMeta.VersionFileKeys[nextVersion] = versionFileKey;

            var scoreMetaKey = ScoreMeta.CreateKey();

            scoreMeta[scoreMetaKey] = scoreContentMeta;

            await metaFileOperator.SaveScoreMetaAsync(scoreName, scoreMeta, scoreMetaKey);

            return(Ok());
        }