|
|
|
@ -57,44 +57,47 @@ namespace Squidex.Infrastructure.Assets |
|
|
|
return new Uri(blobContainer.StorageUri.PrimaryUri, $"/{containerName}/{blobName}").ToString(); |
|
|
|
} |
|
|
|
|
|
|
|
public async Task CopyAsync(string name, string id, long version, string suffix, CancellationToken ct = default(CancellationToken)) |
|
|
|
public async Task CopyAsync(string sourceFileName, string id, long version, string suffix, CancellationToken ct = default(CancellationToken)) |
|
|
|
{ |
|
|
|
var blobName = GetObjectName(id, version, suffix); |
|
|
|
var blobRef = blobContainer.GetBlobReference(blobName); |
|
|
|
var targetName = GetObjectName(id, version, suffix); |
|
|
|
var targetBlob = blobContainer.GetBlobReference(targetName); |
|
|
|
|
|
|
|
var tempBlob = blobContainer.GetBlockBlobReference(name); |
|
|
|
var sourceBlob = blobContainer.GetBlockBlobReference(sourceFileName); |
|
|
|
|
|
|
|
try |
|
|
|
{ |
|
|
|
await blobRef.StartCopyAsync(tempBlob.Uri, null, null, null, null, ct); |
|
|
|
await targetBlob.StartCopyAsync(sourceBlob.Uri, null, AccessCondition.GenerateIfNotExistsCondition(), null, null, ct); |
|
|
|
|
|
|
|
while (blobRef.CopyState.Status == CopyStatus.Pending) |
|
|
|
while (targetBlob.CopyState.Status == CopyStatus.Pending) |
|
|
|
{ |
|
|
|
ct.ThrowIfCancellationRequested(); |
|
|
|
|
|
|
|
await Task.Delay(50); |
|
|
|
await blobRef.FetchAttributesAsync(null, null, null, ct); |
|
|
|
await targetBlob.FetchAttributesAsync(null, null, null, ct); |
|
|
|
} |
|
|
|
|
|
|
|
if (blobRef.CopyState.Status != CopyStatus.Success) |
|
|
|
if (targetBlob.CopyState.Status != CopyStatus.Success) |
|
|
|
{ |
|
|
|
throw new StorageException($"Copy of temporary file failed: {blobRef.CopyState.Status}"); |
|
|
|
throw new StorageException($"Copy of temporary file failed: {targetBlob.CopyState.Status}"); |
|
|
|
} |
|
|
|
} |
|
|
|
catch (StorageException ex) when (ex.RequestInformation.HttpStatusCode == 409) |
|
|
|
{ |
|
|
|
throw new AssetAlreadyExistsException(targetName); |
|
|
|
} |
|
|
|
catch (StorageException ex) when (ex.RequestInformation.HttpStatusCode == 404) |
|
|
|
{ |
|
|
|
throw new AssetNotFoundException(name, ex); |
|
|
|
throw new AssetNotFoundException(sourceFileName, ex); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public async Task DownloadAsync(string id, long version, string suffix, Stream stream, CancellationToken ct = default(CancellationToken)) |
|
|
|
{ |
|
|
|
var blobName = GetObjectName(id, version, suffix); |
|
|
|
var blobRef = blobContainer.GetBlockBlobReference(blobName); |
|
|
|
var blob = blobContainer.GetBlockBlobReference(GetObjectName(id, version, suffix)); |
|
|
|
|
|
|
|
try |
|
|
|
{ |
|
|
|
await blobRef.DownloadToStreamAsync(stream, null, null, null, ct); |
|
|
|
await blob.DownloadToStreamAsync(stream, null, null, null, ct); |
|
|
|
} |
|
|
|
catch (StorageException ex) when (ex.RequestInformation.HttpStatusCode == 404) |
|
|
|
{ |
|
|
|
@ -102,37 +105,45 @@ namespace Squidex.Infrastructure.Assets |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public async Task UploadAsync(string id, long version, string suffix, Stream stream, CancellationToken ct = default(CancellationToken)) |
|
|
|
public Task UploadAsync(string id, long version, string suffix, Stream stream, CancellationToken ct = default(CancellationToken)) |
|
|
|
{ |
|
|
|
var blobName = GetObjectName(id, version, suffix); |
|
|
|
var blobRef = blobContainer.GetBlockBlobReference(blobName); |
|
|
|
|
|
|
|
blobRef.Metadata[AssetVersion] = version.ToString(); |
|
|
|
blobRef.Metadata[AssetId] = id; |
|
|
|
return UploadCoreAsync(GetObjectName(id, version, suffix), stream, ct); |
|
|
|
} |
|
|
|
|
|
|
|
await blobRef.UploadFromStreamAsync(stream, null, null, null, ct); |
|
|
|
await blobRef.SetMetadataAsync(); |
|
|
|
public Task UploadAsync(string fileName, Stream stream, CancellationToken ct = default(CancellationToken)) |
|
|
|
{ |
|
|
|
return UploadCoreAsync(fileName, stream, ct); |
|
|
|
} |
|
|
|
|
|
|
|
public Task UploadAsync(string name, Stream stream, CancellationToken ct = default(CancellationToken)) |
|
|
|
public Task DeleteAsync(string id, long version, string suffix) |
|
|
|
{ |
|
|
|
var tempBlob = blobContainer.GetBlockBlobReference(name); |
|
|
|
return DeleteCoreAsync(GetObjectName(id, version, suffix)); |
|
|
|
} |
|
|
|
|
|
|
|
return tempBlob.UploadFromStreamAsync(stream, null, null, null, ct); |
|
|
|
public Task DeleteAsync(string fileName) |
|
|
|
{ |
|
|
|
return DeleteCoreAsync(fileName); |
|
|
|
} |
|
|
|
|
|
|
|
public Task DeleteAsync(string name) |
|
|
|
private Task DeleteCoreAsync(string blobName) |
|
|
|
{ |
|
|
|
var tempBlob = blobContainer.GetBlockBlobReference(name); |
|
|
|
var blob = blobContainer.GetBlockBlobReference(blobName); |
|
|
|
|
|
|
|
return tempBlob.DeleteIfExistsAsync(); |
|
|
|
return blob.DeleteIfExistsAsync(); |
|
|
|
} |
|
|
|
|
|
|
|
public Task DeleteAsync(string id, long version, string suffix) |
|
|
|
private async Task UploadCoreAsync(string blobName, Stream stream, CancellationToken ct) |
|
|
|
{ |
|
|
|
var tempBlob = blobContainer.GetBlockBlobReference(GetObjectName(id, version, suffix)); |
|
|
|
try |
|
|
|
{ |
|
|
|
var tempBlob = blobContainer.GetBlockBlobReference(blobName); |
|
|
|
|
|
|
|
return tempBlob.DeleteIfExistsAsync(); |
|
|
|
await tempBlob.UploadFromStreamAsync(stream, AccessCondition.GenerateIfNotExistsCondition(), null, null, ct); |
|
|
|
} |
|
|
|
catch (StorageException ex) when (ex.RequestInformation.HttpStatusCode == 409) |
|
|
|
{ |
|
|
|
throw new AssetAlreadyExistsException(blobName); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private string GetObjectName(string id, long version, string suffix) |
|
|
|
|