Browse Source

DL parameter to prevent download.

pull/370/head
Sebastian 7 years ago
parent
commit
6f10534af3
  1. 21
      src/Squidex/Areas/Api/Controllers/Assets/AssetContentController.cs

21
src/Squidex/Areas/Api/Controllers/Assets/AssetContentController.cs

@ -52,6 +52,7 @@ namespace Squidex.Areas.Api.Controllers.Assets
/// <param name="idOrSlug">The id or slug of the asset.</param> /// <param name="idOrSlug">The id or slug of the asset.</param>
/// <param name="more">Optional suffix that can be used to seo-optimize the link to the image Has not effect.</param> /// <param name="more">Optional suffix that can be used to seo-optimize the link to the image Has not effect.</param>
/// <param name="version">The optional version of the asset.</param> /// <param name="version">The optional version of the asset.</param>
/// <param name="dl">Set it to 0 to prevent download.</param>
/// <param name="width">The target width of the asset, if it is an image.</param> /// <param name="width">The target width of the asset, if it is an image.</param>
/// <param name="height">The target height of the asset, if it is an image.</param> /// <param name="height">The target height of the asset, if it is an image.</param>
/// <param name="quality">Optional image quality, it is is an jpeg image.</param> /// <param name="quality">Optional image quality, it is is an jpeg image.</param>
@ -67,6 +68,7 @@ namespace Squidex.Areas.Api.Controllers.Assets
[AllowAnonymous] [AllowAnonymous]
public async Task<IActionResult> GetAssetContentBySlug(string app, string idOrSlug, string more, public async Task<IActionResult> GetAssetContentBySlug(string app, string idOrSlug, string more,
[FromQuery] long version = EtagVersion.Any, [FromQuery] long version = EtagVersion.Any,
[FromQuery] int dl = 1,
[FromQuery] int? width = null, [FromQuery] int? width = null,
[FromQuery] int? height = null, [FromQuery] int? height = null,
[FromQuery] int? quality = null, [FromQuery] int? quality = null,
@ -83,7 +85,7 @@ namespace Squidex.Areas.Api.Controllers.Assets
entity = await assetRepository.FindAssetBySlugAsync(App.Id, idOrSlug); entity = await assetRepository.FindAssetBySlugAsync(App.Id, idOrSlug);
} }
return DeliverAsset(entity, version, width, height, quality, mode); return DeliverAsset(entity, version, width, height, quality, mode, dl);
} }
/// <summary> /// <summary>
@ -92,6 +94,7 @@ namespace Squidex.Areas.Api.Controllers.Assets
/// <param name="id">The id of the asset.</param> /// <param name="id">The id of the asset.</param>
/// <param name="more">Optional suffix that can be used to seo-optimize the link to the image Has not effect.</param> /// <param name="more">Optional suffix that can be used to seo-optimize the link to the image Has not effect.</param>
/// <param name="version">The optional version of the asset.</param> /// <param name="version">The optional version of the asset.</param>
/// <param name="dl">Set it to 0 to prevent download.</param>
/// <param name="width">The target width of the asset, if it is an image.</param> /// <param name="width">The target width of the asset, if it is an image.</param>
/// <param name="height">The target height of the asset, if it is an image.</param> /// <param name="height">The target height of the asset, if it is an image.</param>
/// <param name="quality">Optional image quality, it is is an jpeg image.</param> /// <param name="quality">Optional image quality, it is is an jpeg image.</param>
@ -106,6 +109,7 @@ namespace Squidex.Areas.Api.Controllers.Assets
[ApiCosts(0.5)] [ApiCosts(0.5)]
public async Task<IActionResult> GetAssetContent(Guid id, string more, public async Task<IActionResult> GetAssetContent(Guid id, string more,
[FromQuery] long version = EtagVersion.Any, [FromQuery] long version = EtagVersion.Any,
[FromQuery] int dl = 1,
[FromQuery] int? width = null, [FromQuery] int? width = null,
[FromQuery] int? height = null, [FromQuery] int? height = null,
[FromQuery] int? quality = null, [FromQuery] int? quality = null,
@ -113,10 +117,10 @@ namespace Squidex.Areas.Api.Controllers.Assets
{ {
var entity = await assetRepository.FindAssetAsync(id); var entity = await assetRepository.FindAssetAsync(id);
return DeliverAsset(entity, version, width, height, quality, mode); return DeliverAsset(entity, version, width, height, quality, mode, dl);
} }
private IActionResult DeliverAsset(IAssetEntity entity, long version, int? width, int? height, int? quality, string mode) private IActionResult DeliverAsset(IAssetEntity entity, long version, int? width, int? height, int? quality, string mode, int download = 1)
{ {
if (entity == null || entity.FileVersion < version || width == 0 || height == 0 || quality == 0) if (entity == null || entity.FileVersion < version || width == 0 || height == 0 || quality == 0)
{ {
@ -125,7 +129,7 @@ namespace Squidex.Areas.Api.Controllers.Assets
Response.Headers[HeaderNames.ETag] = entity.FileVersion.ToString(); Response.Headers[HeaderNames.ETag] = entity.FileVersion.ToString();
return new FileCallbackResult(entity.MimeType, entity.FileName, true, async bodyStream => var handler = new Func<Stream, Task>(async bodyStream =>
{ {
var assetId = entity.Id.ToString(); var assetId = entity.Id.ToString();
@ -179,6 +183,15 @@ namespace Squidex.Areas.Api.Controllers.Assets
await assetStore.DownloadAsync(assetId, entity.FileVersion, null, bodyStream); await assetStore.DownloadAsync(assetId, entity.FileVersion, null, bodyStream);
} }
}); });
if (download == 1)
{
return new FileCallbackResult(entity.MimeType, entity.FileName, true, handler);
}
else
{
return new FileCallbackResult(entity.MimeType, null, true, handler);
}
} }
private static FileStream GetTempStream() private static FileStream GetTempStream()

Loading…
Cancel
Save