Exemple #1
0
        public async Task <CreateAssetResult> Handle(CreateAssetCommand request, CancellationToken cancellationToken)
        {
            var release = await _db.Releases.SingleOrDefaultAsync(e => e.Uuid == request.ReleaseId);

            if (release == null)
            {
                throw new AppException(AppErrorCode.NotFound, "Release was not found");
            }

            string filename = Regex.Match(request.FileName, _filenamePattern).Groups[0].Value;

            filename = Regex.Replace(filename, _illegalFilenamePattern, "");

            if (string.IsNullOrEmpty(filename))
            {
                throw new ValidationException("Invalid file name format");
            }

            string uuid = Uuid.NewUuid();

            using var sha256       = SHA256.Create();
            using var cryptoStream = new CryptoStream(request.FileStream, sha256, CryptoStreamMode.Read);
            string storagePath = $"assets/{uuid}";
            string tag         = string.IsNullOrEmpty(request.Tag) ? null: request.Tag;

            var uploadedObject = await _objectStorage.Upload(storagePath, cryptoStream, cacheExpiry : _cacheExpiry, cancellationToken : cancellationToken);

            var asset = new Asset {
                Uuid                 = uuid,
                CreatedAt            = _clock.UtcNowMilliseconds,
                Name                 = request.FileName,
                ContentSize          = uploadedObject.Size,
                ContentHashAlgorithm = HashAlgorithm.Sha256,
                ContentHash          = sha256.Hash,
                StoragePath          = storagePath,
                Tag = tag
            };

            release.Assets.Add(asset);

            return(new CreateAssetResult {
                Id = asset.Uuid
            });
        }