Beispiel #1
0
        public static Task ExecuteUpdateScriptAsync(this AssetOperation operation, UpdateAsset update)
        {
            var script = operation.App.AssetScripts?.Update;

            if (string.IsNullOrWhiteSpace(script))
            {
                return(Task.CompletedTask);
            }

            // Tags and metadata are mutable and can be changed from the scripts, but not replaced.
            var vars = new ScriptVars
            {
                // Use a dictionary for better performance, because no reflection is involved.
                [ScriptKeys.Command] = new Dictionary <string, object?>
                {
                    [ScriptKeys.Metadata] = update.Metadata.Mutable(),
                    [ScriptKeys.FileHash] = update.FileHash,
                    [ScriptKeys.FileName] = update.File.FileName,
                    [ScriptKeys.FileSize] = update.File.FileSize,
                    [ScriptKeys.MimeType] = update.File.MimeType,
                    [ScriptKeys.Tags]     = update.Tags
                },
                [ScriptKeys.Operation] = "Update"
            };

            return(ExecuteScriptAsync(operation, script, vars));
        }
Beispiel #2
0
        public async Task Update_should_create_events()
        {
            var command = new UpdateAsset {
                File = file, ImageInfo = image, FileHash = fileHash
            };

            await ExecuteCreateAsync();

            var result = await sut.ExecuteAsync(CreateAssetCommand(command));

            result.ShouldBeEquivalent(sut.Snapshot);

            Assert.Equal(1, sut.Snapshot.FileVersion);
            Assert.Equal(fileHash, sut.Snapshot.FileHash);

            LastEvents
            .ShouldHaveSameEvents(
                CreateAssetEvent(new AssetUpdated
            {
                IsImage     = true,
                FileSize    = file.FileSize,
                FileHash    = fileHash,
                FileVersion = 1,
                MimeType    = file.MimeType,
                PixelWidth  = image.PixelWidth,
                PixelHeight = image.PixelHeight
            })
                );
        }
Beispiel #3
0
        public async Task Update_should_create_events_and_update_file_state()
        {
            var command = new UpdateAsset {
                File = file, FileHash = "NewHash"
            };

            await ExecuteCreateAsync();

            var result = await PublishAsync(command);

            result.ShouldBeEquivalent(sut.Snapshot);

            Assert.Equal(1, sut.Snapshot.FileVersion);
            Assert.Equal(command.FileHash, sut.Snapshot.FileHash);

            LastEvents
            .ShouldHaveSameEvents(
                CreateAssetEvent(new AssetUpdated
            {
                FileSize    = file.FileSize,
                FileHash    = command.FileHash,
                FileVersion = 1,
                Metadata    = new AssetMetadata(),
                MimeType    = file.MimeType
            })
                );
        }
Beispiel #4
0
        public static void Configure(UpdateRelease[] releases)
        {
            _releases = releases;
            var buffer = new StringBuilder();

            foreach (var release in _releases)
            {
                buffer.Append("## ");
                buffer.Append(release.tag_name);
                if (release.prerelease)
                {
                    buffer.Append("<sup>(预览版)</sup>");
                }
                buffer.Append(" `");
                buffer.Append(DateTime.Parse(release.created_at).ToString("yyyy-MM-dd"));
                buffer.AppendLine("`");
                buffer.AppendLine(release.body);
                buffer.AppendLine();
            }
            _version = typeof(UpdateManager).Assembly.GetName().Version;
            var assets    = _releases.Where(x => !x.prerelease).SelectMany(x => x.assets).ToArray();
            var preassets = _releases.Where(x => x.prerelease).SelectMany(x => x.assets).ToArray();

            _full   = assets.FirstOrDefault(x => x.IsFull);
            _update = ResolveUpdate(assets)?.Reverse().ToArray();

            _prefull   = preassets.FirstOrDefault(x => x.IsFull);
            _preupdate = ResolveUpdate(preassets)?.Reverse().ToArray();

            _releaseDescription = buffer.ToString();
        }
Beispiel #5
0
        public void CanUpdate_should_not_throw_exception()
        {
            var command = new UpdateAsset {
                AppId = appId
            };

            GuardAsset.CanUpdate(command);
        }
Beispiel #6
0
        private async Task UpdateCore(UpdateAsset update, AssetOperation operation)
        {
            if (!update.DoNotScript)
            {
                await operation.ExecuteUpdateScriptAsync(update);
            }

            Update(update);
        }
Beispiel #7
0
 private void Update(UpdateAsset command)
 {
     Raise(command, new AssetUpdated
     {
         MimeType    = command.File.MimeType,
         FileVersion = Snapshot.FileVersion + 1,
         FileSize    = command.File.FileSize
     });
 }
Beispiel #8
0
        public object Put(UpdateAsset request)
        {
            var entity = request.ConvertTo <Asset>();

            return(InTransaction(db =>
            {
                Logic.Update(entity);
                return new CommonResponse(Logic.GetById(entity.Id));
            }));
        }
Beispiel #9
0
        public async Task <IActionResult> PutAssetContent(string app, DomainId id, IFormFile file)
        {
            var command = new UpdateAsset {
                File = await CheckAssetFileAsync(file), AssetId = id
            };

            var response = await InvokeCommandAsync(command);

            return(Ok(response));
        }
        protected async Task On(UpdateAsset command, CommandContext context)
        {
            await handler.UpdateAsync <AssetDomainObject>(context, async c =>
            {
                command.ImageInfo = await assetThumbnailGenerator.GetImageInfoAsync(command.File.OpenRead());

                c.Update(command);

                await assetStore.UploadAsync(c.Id.ToString(), c.FileVersion, null, command.File.OpenRead());
            });
        }
Beispiel #11
0
        public void Update(UpdateAsset command)
        {
            var @event = SimpleMapper.Map(command, new AssetUpdated
            {
                FileVersion = Snapshot.FileVersion + 1,
                FileSize    = command.File.FileSize,
                MimeType    = command.File.MimeType
            });

            RaiseEvent(@event);
        }
Beispiel #12
0
        public async Task <IActionResult> PutAssetContent(string app, Guid id, [SwaggerIgnore] List <IFormFile> file)
        {
            var assetFile = await CheckAssetFileAsync(file);

            var command = new UpdateAsset {
                File = assetFile, AssetId = id
            };

            var response = await InvokeCommandAsync(app, command);

            return(Ok(response));
        }
Beispiel #13
0
        public void Update(UpdateAsset command)
        {
            var @event = SimpleMapper.Map(command, new AssetUpdated
            {
                FileVersion = Snapshot.FileVersion + 1,
                FileSize    = command.File.FileSize,
                MimeType    = command.File.MimeType,
                PixelWidth  = command.ImageInfo?.PixelWidth,
                PixelHeight = command.ImageInfo?.PixelHeight,
                IsImage     = command.ImageInfo != null
            });

            RaiseEvent(@event);
        }
        public async Task <IActionResult> PutAssetContent(string app, Guid id, [SwaggerIgnore] List <IFormFile> file)
        {
            var assetFile = await CheckAssetFileAsync(file);

            var command = new UpdateAsset {
                File = assetFile, AssetId = id
            };
            var context = await CommandBus.PublishAsync(command);

            var result   = context.Result <AssetSavedResult>();
            var response = AssetReplacedDto.Create(command, result);

            return(StatusCode(201, response));
        }
Beispiel #15
0
        public static AssetReplacedDto FromCommand(UpdateAsset command, AssetSavedResult result)
        {
            var response = new AssetReplacedDto
            {
                FileSize    = command.File.FileSize,
                FileVersion = result.FileVersion,
                MimeType    = command.File.MimeType,
                IsImage     = command.ImageInfo != null,
                PixelWidth  = command.ImageInfo?.PixelWidth,
                PixelHeight = command.ImageInfo?.PixelHeight,
                Version     = result.Version
            };

            return(response);
        }
        public async Task Update_should_calculate_hash()
        {
            var command = new UpdateAsset {
                AssetId = assetId, File = file
            };
            var context = CreateContextForCommand(command);

            SetupImageInfo();

            await ExecuteCreateAsync();

            await sut.HandleAsync(context);

            Assert.True(command.FileHash.Length > 10);
        }
        public async Task Update_should_update_domain_object()
        {
            var command = new UpdateAsset {
                AssetId = assetId, File = file
            };
            var context = CreateContextForCommand(command);

            SetupImageInfo();

            await ExecuteCreateAsync();

            await sut.HandleAsync(context);

            AssertAssetHasBeenUploaded(1, context.ContextId);
            AssertAssetImageChecked();
        }
Beispiel #18
0
        public AssetDomainObject Update(UpdateAsset command)
        {
            VerifyCreatedAndNotDeleted();

            var @event = SimpleMapper.Map(command, new AssetUpdated
            {
                FileVersion = Snapshot.FileVersion + 1,
                FileSize    = command.File.FileSize,
                MimeType    = command.File.MimeType,
                PixelWidth  = command.ImageInfo?.PixelWidth,
                PixelHeight = command.ImageInfo?.PixelHeight,
                IsImage     = command.ImageInfo != null
            });

            RaiseEvent(@event);

            return(this);
        }
Beispiel #19
0
        protected async Task On(UpdateAsset command, CommandContext context)
        {
            command.ImageInfo = await assetThumbnailGenerator.GetImageInfoAsync(command.File.OpenRead());

            try
            {
                var asset = await handler.UpdateAsync <AssetDomainObject>(context, async a =>
                {
                    a.Update(command);

                    await assetStore.UploadTemporaryAsync(context.ContextId.ToString(), command.File.OpenRead());

                    context.Complete(new AssetSavedResult(a.Version, a.FileVersion));
                });

                await assetStore.CopyTemporaryAsync(context.ContextId.ToString(), asset.Id.ToString(), asset.FileVersion, null);
            }
            finally
            {
                await assetStore.DeleteTemporaryAsync(context.ContextId.ToString());
            }
        }
Beispiel #20
0
        public object Update(int id, UpdateAsset updateAsset, IMapper mapper)
        {
            var asset = _context.Assets.AsNoTracking()
                        .Where(p => p.Id == id)
                        .FirstOrDefault();

            if (asset == null)
            {
                var error = new ErrorResponse();
                error.ErrorMessages.Add(MessageHelper.RecordToBeUpdatedNotFound);

                return(error);
            }
            else
            {
                var mappedAsset = mapper.Map(updateAsset, asset);
                mappedAsset.DateUpdated = DateTime.Now;

                _context.Update(mappedAsset);
                _context.SaveChanges();

                return(new SuccessResponse());
            }
        }
Beispiel #21
0
 public static void CanUpdate(UpdateAsset command)
 {
     Guard.NotNull(command, nameof(command));
 }
Beispiel #22
0
        public IActionResult Update(int id, [FromBody] UpdateAsset asset)
        {
            var response = _service.Update(id, asset, _mapper);

            return(Ok(response));
        }
Beispiel #23
0
 private void UpdateCore(UpdateAsset update)
 {
     Update(update);
 }
Beispiel #24
0
        private void UpdateCore(UpdateAsset update)
        {
            GuardAsset.CanUpdate(update);

            Update(update);
        }