mirror of https://github.com/Squidex/squidex.git
16 changed files with 346 additions and 311 deletions
@ -0,0 +1,74 @@ |
|||||
|
// ==========================================================================
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
||||
|
// All rights reserved. Licensed under the MIT license.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System; |
||||
|
using System.IO; |
||||
|
using System.Threading; |
||||
|
using System.Threading.Tasks; |
||||
|
|
||||
|
namespace Squidex.Infrastructure.Assets |
||||
|
{ |
||||
|
public static class AssetStoreExtensions |
||||
|
{ |
||||
|
public static string GeneratePublicUrl(this IAssetStore store, Guid id, long version, string suffix) |
||||
|
{ |
||||
|
return store.GeneratePublicUrl(id.ToString(), version, suffix); |
||||
|
} |
||||
|
|
||||
|
public static string GeneratePublicUrl(this IAssetStore store, string id, long version, string suffix) |
||||
|
{ |
||||
|
return store.GeneratePublicUrl(GetFileName(id, version, suffix)); |
||||
|
} |
||||
|
|
||||
|
public static Task CopyAsync(this IAssetStore store, string sourceFileName, Guid id, long version, string suffix, CancellationToken ct = default) |
||||
|
{ |
||||
|
return store.CopyAsync(sourceFileName, id.ToString(), version, suffix, ct); |
||||
|
} |
||||
|
|
||||
|
public static Task CopyAsync(this IAssetStore store, string sourceFileName, string id, long version, string suffix, CancellationToken ct = default) |
||||
|
{ |
||||
|
return store.CopyAsync(sourceFileName, GetFileName(id, version, suffix), ct); |
||||
|
} |
||||
|
|
||||
|
public static Task DownloadAsync(this IAssetStore store, Guid id, long version, string suffix, Stream stream, CancellationToken ct = default) |
||||
|
{ |
||||
|
return store.DownloadAsync(id.ToString(), version, suffix, stream, ct); |
||||
|
} |
||||
|
|
||||
|
public static Task DownloadAsync(this IAssetStore store, string id, long version, string suffix, Stream stream, CancellationToken ct = default) |
||||
|
{ |
||||
|
return store.DownloadAsync(GetFileName(id, version, suffix), stream, ct); |
||||
|
} |
||||
|
|
||||
|
public static Task UploadAsync(this IAssetStore store, Guid id, long version, string suffix, Stream stream, bool overwrite = false, CancellationToken ct = default) |
||||
|
{ |
||||
|
return store.UploadAsync(id.ToString(), version, suffix, stream, overwrite, ct); |
||||
|
} |
||||
|
|
||||
|
public static Task UploadAsync(this IAssetStore store, string id, long version, string suffix, Stream stream, bool overwrite = false, CancellationToken ct = default) |
||||
|
{ |
||||
|
return store.UploadAsync(GetFileName(id, version, suffix), stream, overwrite, ct); |
||||
|
} |
||||
|
|
||||
|
public static Task DeleteAsync(this IAssetStore store, Guid id, long version, string suffix) |
||||
|
{ |
||||
|
return store.DeleteAsync(id.ToString(), version, suffix); |
||||
|
} |
||||
|
|
||||
|
public static Task DeleteAsync(this IAssetStore store, string id, long version, string suffix) |
||||
|
{ |
||||
|
return store.DeleteAsync(GetFileName(id, version, suffix)); |
||||
|
} |
||||
|
|
||||
|
public static string GetFileName(string id, long version, string suffix = null) |
||||
|
{ |
||||
|
Guard.NotNullOrEmpty(id, nameof(id)); |
||||
|
|
||||
|
return StringExtensions.JoinNonEmpty("_", id, version.ToString(), suffix); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,112 @@ |
|||||
|
// ==========================================================================
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
||||
|
// All rights reserved. Licensed under the MIT license.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System; |
||||
|
using System.IO; |
||||
|
using FakeItEasy; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Squidex.Infrastructure.Assets |
||||
|
{ |
||||
|
public class AssetExtensionTests |
||||
|
{ |
||||
|
private readonly IAssetStore sut = A.Fake<IAssetStore>(); |
||||
|
private readonly Guid id = Guid.NewGuid(); |
||||
|
private readonly Stream stream = new MemoryStream(); |
||||
|
private readonly string fileName = Guid.NewGuid().ToString(); |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_copy_with_id_and_version() |
||||
|
{ |
||||
|
sut.CopyAsync(fileName, id, 1, string.Empty); |
||||
|
|
||||
|
A.CallTo(() => sut.CopyAsync(fileName, $"{id}_1", default)) |
||||
|
.MustHaveHappened(); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_copy_with_id_and_version_and_suffix() |
||||
|
{ |
||||
|
sut.CopyAsync(fileName, id, 1, "Crop"); |
||||
|
|
||||
|
A.CallTo(() => sut.CopyAsync(fileName, $"{id}_1_Crop", default)) |
||||
|
.MustHaveHappened(); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_upload_with_id_and_version() |
||||
|
{ |
||||
|
sut.UploadAsync(id, 1, string.Empty, stream, true); |
||||
|
|
||||
|
A.CallTo(() => sut.UploadAsync($"{id}_1", stream, true, default)) |
||||
|
.MustHaveHappened(); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_upload_with_id_and_version_and_suffix() |
||||
|
{ |
||||
|
sut.UploadAsync(id, 1, "Crop", stream, true); |
||||
|
|
||||
|
A.CallTo(() => sut.UploadAsync($"{id}_1_Crop", stream, true, default)) |
||||
|
.MustHaveHappened(); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_download_with_id_and_version() |
||||
|
{ |
||||
|
sut.DownloadAsync(id, 1, string.Empty, stream); |
||||
|
|
||||
|
A.CallTo(() => sut.DownloadAsync($"{id}_1", stream, default)) |
||||
|
.MustHaveHappened(); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_download_with_id_and_version_and_suffix() |
||||
|
{ |
||||
|
sut.DownloadAsync(id, 1, "Crop", stream); |
||||
|
|
||||
|
A.CallTo(() => sut.DownloadAsync($"{id}_1_Crop", stream, default)) |
||||
|
.MustHaveHappened(); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_delete_with_id_and_version() |
||||
|
{ |
||||
|
sut.DeleteAsync(id, 1, string.Empty); |
||||
|
|
||||
|
A.CallTo(() => sut.DeleteAsync($"{id}_1")) |
||||
|
.MustHaveHappened(); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_delete_with_id_and_version_and_suffix() |
||||
|
{ |
||||
|
sut.DeleteAsync(id, 1, "Crop"); |
||||
|
|
||||
|
A.CallTo(() => sut.DeleteAsync($"{id}_1_Crop")) |
||||
|
.MustHaveHappened(); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_generate_url_with_id_and_version() |
||||
|
{ |
||||
|
sut.GeneratePublicUrl(id, 1, string.Empty); |
||||
|
|
||||
|
A.CallTo(() => sut.GeneratePublicUrl($"{id}_1")) |
||||
|
.MustHaveHappened(); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_generate_url_with_id_and_version_and_suffix() |
||||
|
{ |
||||
|
sut.GeneratePublicUrl(id, 1, "Crop"); |
||||
|
|
||||
|
A.CallTo(() => sut.GeneratePublicUrl($"{id}_1_Crop")) |
||||
|
.MustHaveHappened(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue