mirror of https://github.com/Squidex/squidex.git
11 changed files with 325 additions and 7 deletions
@ -0,0 +1,158 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschränkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using System.IO; |
|||
using System.Threading; |
|||
using System.Threading.Tasks; |
|||
using FluentFTP; |
|||
using Squidex.Infrastructure.Log; |
|||
|
|||
namespace Squidex.Infrastructure.Assets |
|||
{ |
|||
public sealed class FTPAssetStore : IAssetStore, IInitializable |
|||
{ |
|||
private readonly string path; |
|||
private readonly ISemanticLog log; |
|||
private readonly Func<IFtpClient> factory; |
|||
|
|||
public FTPAssetStore(Func<IFtpClient> factory, string path, ISemanticLog log) |
|||
{ |
|||
Guard.NotNull(factory, nameof(factory)); |
|||
Guard.NotNullOrEmpty(path, nameof(path)); |
|||
Guard.NotNull(log, nameof(log)); |
|||
|
|||
this.factory = factory; |
|||
this.path = path; |
|||
|
|||
this.log = log; |
|||
} |
|||
|
|||
public string GeneratePublicUrl(string fileName) |
|||
{ |
|||
return null; |
|||
} |
|||
|
|||
public async Task InitializeAsync(CancellationToken ct = default) |
|||
{ |
|||
using (var client = factory()) |
|||
{ |
|||
await client.ConnectAsync(ct); |
|||
|
|||
if (!await client.DirectoryExistsAsync(path, ct)) |
|||
{ |
|||
await client.CreateDirectoryAsync(path, ct); |
|||
} |
|||
} |
|||
|
|||
log.LogInformation(w => w |
|||
.WriteProperty("action", "FTPAssetStoreConfigured") |
|||
.WriteProperty("path", path)); |
|||
} |
|||
|
|||
public async Task CopyAsync(string sourceFileName, string targetFileName, CancellationToken ct = default) |
|||
{ |
|||
Guard.NotNullOrEmpty(sourceFileName, nameof(sourceFileName)); |
|||
Guard.NotNullOrEmpty(targetFileName, nameof(targetFileName)); |
|||
|
|||
using (var client = GetFtpClient()) |
|||
{ |
|||
var tempPath = Path.Combine(Path.GetTempPath(), Path.GetTempFileName()); |
|||
|
|||
using (var stream = new FileStream(tempPath, FileMode.Create, FileAccess.ReadWrite, FileShare.None, 4096, FileOptions.DeleteOnClose)) |
|||
{ |
|||
await DownloadAsync(client, sourceFileName, stream, ct); |
|||
await UploadAsync(client, targetFileName, stream, false, ct); |
|||
} |
|||
} |
|||
} |
|||
|
|||
public async Task DownloadAsync(string fileName, Stream stream, CancellationToken ct = default) |
|||
{ |
|||
Guard.NotNullOrEmpty(fileName, nameof(fileName)); |
|||
Guard.NotNull(stream, nameof(stream)); |
|||
|
|||
using (var client = GetFtpClient()) |
|||
{ |
|||
await DownloadAsync(client, fileName, stream, ct); |
|||
} |
|||
} |
|||
|
|||
public async Task UploadAsync(string fileName, Stream stream, bool overwrite = false, CancellationToken ct = default) |
|||
{ |
|||
Guard.NotNullOrEmpty(fileName, nameof(fileName)); |
|||
Guard.NotNull(stream, nameof(stream)); |
|||
|
|||
using (var client = GetFtpClient()) |
|||
{ |
|||
await UploadAsync(client, fileName, stream, overwrite, ct); |
|||
} |
|||
} |
|||
|
|||
private static async Task DownloadAsync(IFtpClient client, string fileName, Stream stream, CancellationToken ct) |
|||
{ |
|||
try |
|||
{ |
|||
await client.DownloadAsync(stream, fileName, token: ct); |
|||
} |
|||
catch (FtpException ex) when (IsNotFound(ex)) |
|||
{ |
|||
throw new AssetNotFoundException(fileName, ex); |
|||
} |
|||
} |
|||
|
|||
private static async Task UploadAsync(IFtpClient client, string fileName, Stream stream, bool overwrite, CancellationToken ct) |
|||
{ |
|||
if (!overwrite && await client.FileExistsAsync(fileName, ct)) |
|||
{ |
|||
throw new AssetAlreadyExistsException(fileName); |
|||
} |
|||
|
|||
await client.UploadAsync(stream, fileName, overwrite ? FtpExists.Overwrite : FtpExists.Skip, true, null, ct); |
|||
} |
|||
|
|||
public async Task DeleteAsync(string fileName) |
|||
{ |
|||
Guard.NotNullOrEmpty(fileName, nameof(fileName)); |
|||
|
|||
using (var client = GetFtpClient()) |
|||
{ |
|||
try |
|||
{ |
|||
await client.DeleteFileAsync(fileName); |
|||
} |
|||
catch (FtpException ex) |
|||
{ |
|||
if (!IsNotFound(ex)) |
|||
{ |
|||
throw ex; |
|||
} |
|||
} |
|||
} |
|||
} |
|||
|
|||
private IFtpClient GetFtpClient() |
|||
{ |
|||
var client = factory(); |
|||
|
|||
client.SetWorkingDirectory(path); |
|||
client.Connect(); |
|||
|
|||
return client; |
|||
} |
|||
|
|||
private static bool IsNotFound(Exception exception) |
|||
{ |
|||
if (exception is FtpCommandException command) |
|||
{ |
|||
return command.CompletionCode == "550"; |
|||
} |
|||
|
|||
return exception.InnerException != null ? IsNotFound(exception.InnerException) : false; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,29 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using FakeItEasy; |
|||
using FluentFTP; |
|||
using Squidex.Infrastructure.Log; |
|||
|
|||
namespace Squidex.Infrastructure.Assets |
|||
{ |
|||
public sealed class FTPAssetStoreFixture : IDisposable |
|||
{ |
|||
public FTPAssetStore AssetStore { get; } |
|||
|
|||
public FTPAssetStoreFixture() |
|||
{ |
|||
AssetStore = new FTPAssetStore(() => new FtpClient("localhost", 21, "test", "test"), "assets", A.Fake<ISemanticLog>()); |
|||
AssetStore.InitializeAsync().Wait(); |
|||
} |
|||
|
|||
public void Dispose() |
|||
{ |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,35 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschränkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using Xunit; |
|||
|
|||
namespace Squidex.Infrastructure.Assets |
|||
{ |
|||
[Trait("Category", "Dependencies")] |
|||
public class FTPAssetStoreTests : AssetStoreTests<FTPAssetStore>, IClassFixture<FTPAssetStoreFixture> |
|||
{ |
|||
private readonly FTPAssetStoreFixture fixture; |
|||
|
|||
public FTPAssetStoreTests(FTPAssetStoreFixture fixture) |
|||
{ |
|||
this.fixture = fixture; |
|||
} |
|||
|
|||
public override FTPAssetStore CreateStore() |
|||
{ |
|||
return fixture.AssetStore; |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_calculate_source_url() |
|||
{ |
|||
var url = Sut.GeneratePublicUrl(FileName); |
|||
|
|||
Assert.Null(url); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue