diff --git a/src/Squidex.Read.MongoDb/Assets/MongoAssetRepository.cs b/src/Squidex.Read.MongoDb/Assets/MongoAssetRepository.cs index 1d7ddc2bd..964f523b4 100644 --- a/src/Squidex.Read.MongoDb/Assets/MongoAssetRepository.cs +++ b/src/Squidex.Read.MongoDb/Assets/MongoAssetRepository.cs @@ -33,7 +33,7 @@ namespace Squidex.Read.MongoDb.Assets protected override Task SetupCollectionAsync(IMongoCollection collection) { - return Collection.Indexes.CreateOneAsync(IndexKeys.Descending(x => x.LastModified).Ascending(x => x.AppId).Ascending(x => x.FileName).Ascending(x => x.MimeType)); + return collection.Indexes.CreateOneAsync(IndexKeys.Descending(x => x.LastModified).Ascending(x => x.AppId).Ascending(x => x.FileName).Ascending(x => x.MimeType)); } public async Task> QueryAsync(Guid appId, HashSet mimeTypes = null, string query = null, int take = 10, int skip = 0) diff --git a/src/Squidex.Write/Assets/Commands/CreateAsset.cs b/src/Squidex.Write/Assets/Commands/CreateAsset.cs index 66f321237..36f53b7db 100644 --- a/src/Squidex.Write/Assets/Commands/CreateAsset.cs +++ b/src/Squidex.Write/Assets/Commands/CreateAsset.cs @@ -6,6 +6,7 @@ // All rights reserved. // ========================================================================== +using System; using Squidex.Infrastructure.Assets; namespace Squidex.Write.Assets.Commands @@ -15,5 +16,10 @@ namespace Squidex.Write.Assets.Commands public AssetFile File { get; set; } public ImageInfo ImageInfo { get; set; } + + public CreateAsset() + { + AssetId = Guid.NewGuid(); + } } } diff --git a/src/Squidex/Controllers/Api/Assets/AssetController.cs b/src/Squidex/Controllers/Api/Assets/AssetController.cs index e69f62578..7d4e4b9b2 100644 --- a/src/Squidex/Controllers/Api/Assets/AssetController.cs +++ b/src/Squidex/Controllers/Api/Assets/AssetController.cs @@ -57,7 +57,8 @@ namespace Squidex.Controllers.Api.Assets /// The query to limit the files by name. /// Comma separated list of mime types to get. /// - /// 200 => assets returned. + /// 200 => Assets returned. + /// 404 => App not found. /// [HttpGet] [Route("apps/{app}/assets/")] @@ -123,7 +124,7 @@ namespace Squidex.Controllers.Api.Assets /// The file to upload. /// /// 201 => Asset updated. - /// 404 => App or Asset not found. + /// 404 => Asset or app not found. /// 400 => Asset exceeds the maximum size. /// [HttpPut] @@ -133,10 +134,14 @@ namespace Squidex.Controllers.Api.Assets public async Task PutAssetContent(string app, Guid id, List file) { var assetFile = GetAssetFile(file); - - await CommandBus.PublishAsync(new UpdateAsset { File = assetFile }); - return NoContent(); + var command = new UpdateAsset { File = assetFile, AssetId = id }; + var context = await CommandBus.PublishAsync(command); + + var result = context.Result(); + var response = AssetUpdatedDto.Create(command, result); + + return StatusCode(201, response); } /// @@ -146,14 +151,13 @@ namespace Squidex.Controllers.Api.Assets /// The id of the asset. /// The asset object that needs to updated. /// - /// 201 => Asset updated. - /// 404 => App or Asset not found. + /// 204 => Asset updated. + /// 404 => Asset or app not found. /// - [HttpPost] - [Route("apps/{app}/assets/{id}/content")] - [ProducesResponseType(typeof(AssetDto), 201)] + [HttpPut] + [Route("apps/{app}/assets/{id}")] [ProducesResponseType(typeof(ErrorDto), 400)] - public async Task PutAsset(string app, Guid id, [FromBody] AssetUpdateDto request) + public async Task PutAsset(string app, Guid id, [FromBody] AssetUpdateDto request) { var command = SimpleMapper.Map(request, new RenameAsset()); @@ -169,6 +173,7 @@ namespace Squidex.Controllers.Api.Assets /// The id of the asset to delete. /// /// 204 => Asset has been deleted. + /// 404 => Asset or app not found. /// [HttpDelete] [Route("apps/{app}/schemas/{name}/")] @@ -183,7 +188,7 @@ namespace Squidex.Controllers.Api.Assets { if (file.Count != 1) { - var error = new ValidationError($"Can only upload one file, found ${file.Count}."); + var error = new ValidationError($"Can only upload one file, found {file.Count}."); throw new ValidationException("Cannot create asset.", error); } diff --git a/src/Squidex/Controllers/Api/Assets/Models/AssetUpdatedDto.cs b/src/Squidex/Controllers/Api/Assets/Models/AssetUpdatedDto.cs new file mode 100644 index 000000000..1010b7acb --- /dev/null +++ b/src/Squidex/Controllers/Api/Assets/Models/AssetUpdatedDto.cs @@ -0,0 +1,80 @@ +// ========================================================================== +// AssetUpdatedDto.cs +// Squidex Headless CMS +// ========================================================================== +// Copyright (c) Squidex Group +// All rights reserved. +// ========================================================================== + +using System.ComponentModel.DataAnnotations; +using NodaTime; +using Squidex.Infrastructure; +using Squidex.Infrastructure.CQRS.Commands; +using Squidex.Write.Assets.Commands; + +namespace Squidex.Controllers.Api.Assets.Models +{ + public sealed class AssetUpdatedDto + { + /// + /// The mime type. + /// + [Required] + public string MimeType { get; set; } + + /// + /// The size of the file in bytes. + /// + public long FileSize { get; set; } + + /// + /// Determines of the created file is an image. + /// + public bool IsImage { get; set; } + + /// + /// The width of the image in pixels if the asset is an image. + /// + public int? PixelWidth { get; set; } + + /// + /// The height of the image in pixels if the asset is an image. + /// + public int? PixelHeight { get; set; } + + /// + /// The user that has updated the asset. + /// + [Required] + public RefToken LastModifiedBy { get; set; } + + /// + /// The date and time when the asset has been modified last. + /// + public Instant LastModified { get; set; } + + /// + /// The version of the asset. + /// + public long Version { get; set; } + + public static AssetUpdatedDto Create(UpdateAsset command, EntitySavedResult result) + { + var now = SystemClock.Instance.GetCurrentInstant(); + + var response = new AssetUpdatedDto + { + Version = result.Version, + LastModified = now, + LastModifiedBy = command.Actor, + FileSize = command.File.FileSize, + MimeType = command.File.MimeType, + IsImage = command.ImageInfo != null, + PixelWidth = command.ImageInfo?.PixelWidth, + PixelHeight = command.ImageInfo?.PixelHeight + }; + + return response; + } + } +} diff --git a/src/Squidex/app/features/assets/pages/asset.component.html b/src/Squidex/app/features/assets/pages/asset.component.html index 56ac6f642..6416e7000 100644 --- a/src/Squidex/app/features/assets/pages/asset.component.html +++ b/src/Squidex/app/features/assets/pages/asset.component.html @@ -1,24 +1,29 @@
-
-
-
{{fileType}}
- -
- +
+
+
+
{{fileType}}
+ +
+ +
+
+ +
-
- +
+
-