|
|
|
@ -7,8 +7,10 @@ |
|
|
|
// ==========================================================================
|
|
|
|
|
|
|
|
using System.IO; |
|
|
|
using System.Linq; |
|
|
|
using System.Threading.Tasks; |
|
|
|
using Squidex.Infrastructure.Log; |
|
|
|
using Squidex.Infrastructure.Tasks; |
|
|
|
|
|
|
|
namespace Squidex.Infrastructure.Assets |
|
|
|
{ |
|
|
|
@ -49,6 +51,26 @@ namespace Squidex.Infrastructure.Assets |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public async Task UploadTemporaryAsync(string name, Stream stream) |
|
|
|
{ |
|
|
|
var file = GetFile(name); |
|
|
|
|
|
|
|
using (var fileStream = file.OpenWrite()) |
|
|
|
{ |
|
|
|
await stream.CopyToAsync(fileStream); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public async Task UploadAsync(string id, long version, string suffix, Stream stream) |
|
|
|
{ |
|
|
|
var file = GetFile(id, version, suffix); |
|
|
|
|
|
|
|
using (var fileStream = file.OpenWrite()) |
|
|
|
{ |
|
|
|
await stream.CopyToAsync(fileStream); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public async Task DownloadAsync(string id, long version, string suffix, Stream stream) |
|
|
|
{ |
|
|
|
var file = GetFile(id, version, suffix); |
|
|
|
@ -66,28 +88,60 @@ namespace Squidex.Infrastructure.Assets |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public async Task UploadAsync(string id, long version, string suffix, Stream stream) |
|
|
|
public Task CopyTemporaryAsync(string name, string id, long version, string suffix) |
|
|
|
{ |
|
|
|
var file = GetFile(id, version, suffix); |
|
|
|
try |
|
|
|
{ |
|
|
|
var file = GetFile(name); |
|
|
|
|
|
|
|
using (var fileStream = file.OpenWrite()) |
|
|
|
file.CopyTo(GetPath(id, version, suffix)); |
|
|
|
|
|
|
|
return TaskHelper.Done; |
|
|
|
} |
|
|
|
catch (FileNotFoundException ex) |
|
|
|
{ |
|
|
|
await stream.CopyToAsync(fileStream); |
|
|
|
throw new AssetNotFoundException($"Asset {name} not found.", ex); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private FileInfo GetFile(string id, long version, string suffix) |
|
|
|
public Task DeleteTemporaryAsync(string name) |
|
|
|
{ |
|
|
|
Guard.NotNullOrEmpty(id, nameof(id)); |
|
|
|
try |
|
|
|
{ |
|
|
|
var file = GetFile(name); |
|
|
|
|
|
|
|
var path = Path.Combine(directory.FullName, $"{id}_{version}"); |
|
|
|
file.Delete(); |
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(suffix)) |
|
|
|
return TaskHelper.Done; |
|
|
|
} |
|
|
|
catch (FileNotFoundException ex) |
|
|
|
{ |
|
|
|
path += "_" + suffix; |
|
|
|
throw new AssetNotFoundException($"Asset {name} not found.", ex); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return new FileInfo(path); |
|
|
|
private FileInfo GetFile(string id, long version, string suffix) |
|
|
|
{ |
|
|
|
Guard.NotNullOrEmpty(id, nameof(id)); |
|
|
|
|
|
|
|
return GetFile(GetPath(id, version, suffix)); |
|
|
|
} |
|
|
|
|
|
|
|
private FileInfo GetFile(string name) |
|
|
|
{ |
|
|
|
Guard.NotNullOrEmpty(name, nameof(name)); |
|
|
|
|
|
|
|
return new FileInfo(GetPath(name)); |
|
|
|
} |
|
|
|
|
|
|
|
private string GetPath(string name) |
|
|
|
{ |
|
|
|
return Path.Combine(directory.FullName, name); |
|
|
|
} |
|
|
|
|
|
|
|
private string GetPath(string id, long version, string suffix) |
|
|
|
{ |
|
|
|
return Path.Combine(directory.FullName, string.Join("_", new[] { id, version.ToString(), suffix }.Where(x => !string.IsNullOrWhiteSpace(x)))); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|