mirror of https://github.com/Squidex/squidex.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
112 lines
4.0 KiB
112 lines
4.0 KiB
// ==========================================================================
|
|
// Squidex Headless CMS
|
|
// ==========================================================================
|
|
// Copyright (c) Squidex UG (haftungsbeschränkt)
|
|
// All rights reserved. Licensed under the MIT license.
|
|
// ==========================================================================
|
|
|
|
using System;
|
|
using System.IO;
|
|
using System.Net;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using Google;
|
|
using Google.Cloud.Storage.V1;
|
|
|
|
namespace Squidex.Infrastructure.Assets
|
|
{
|
|
public sealed class GoogleCloudAssetStore : IAssetStore, IInitializable
|
|
{
|
|
private static readonly UploadObjectOptions IfNotExists = new UploadObjectOptions { IfGenerationMatch = 0 };
|
|
private static readonly CopyObjectOptions IfNotExistsCopy = new CopyObjectOptions { IfGenerationMatch = 0 };
|
|
private readonly string bucketName;
|
|
private StorageClient storageClient;
|
|
|
|
public GoogleCloudAssetStore(string bucketName)
|
|
{
|
|
Guard.NotNullOrEmpty(bucketName, nameof(bucketName));
|
|
|
|
this.bucketName = bucketName;
|
|
}
|
|
|
|
public async Task InitializeAsync(CancellationToken ct = default)
|
|
{
|
|
try
|
|
{
|
|
storageClient = StorageClient.Create();
|
|
|
|
await storageClient.GetBucketAsync(bucketName, cancellationToken: ct);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new ConfigurationException($"Cannot connect to google cloud bucket '${bucketName}'.", ex);
|
|
}
|
|
}
|
|
|
|
public string GeneratePublicUrl(string fileName)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
public async Task CopyAsync(string sourceFileName, string targetFileName, CancellationToken ct = default)
|
|
{
|
|
Guard.NotNullOrEmpty(sourceFileName, nameof(sourceFileName));
|
|
Guard.NotNullOrEmpty(targetFileName, nameof(targetFileName));
|
|
|
|
try
|
|
{
|
|
await storageClient.CopyObjectAsync(bucketName, sourceFileName, bucketName, targetFileName, IfNotExistsCopy, ct);
|
|
}
|
|
catch (GoogleApiException ex) when (ex.HttpStatusCode == HttpStatusCode.NotFound)
|
|
{
|
|
throw new AssetNotFoundException(sourceFileName, ex);
|
|
}
|
|
catch (GoogleApiException ex) when (ex.HttpStatusCode == HttpStatusCode.PreconditionFailed)
|
|
{
|
|
throw new AssetAlreadyExistsException(targetFileName);
|
|
}
|
|
}
|
|
|
|
public async Task DownloadAsync(string fileName, Stream stream, CancellationToken ct = default)
|
|
{
|
|
Guard.NotNullOrEmpty(fileName, nameof(fileName));
|
|
|
|
try
|
|
{
|
|
await storageClient.DownloadObjectAsync(bucketName, fileName, stream, cancellationToken: ct);
|
|
}
|
|
catch (GoogleApiException ex) when (ex.HttpStatusCode == HttpStatusCode.NotFound)
|
|
{
|
|
throw new AssetNotFoundException(fileName, ex);
|
|
}
|
|
}
|
|
|
|
public async Task UploadAsync(string fileName, Stream stream, bool overwrite = false, CancellationToken ct = default)
|
|
{
|
|
Guard.NotNullOrEmpty(fileName, nameof(fileName));
|
|
|
|
try
|
|
{
|
|
await storageClient.UploadObjectAsync(bucketName, fileName, "application/octet-stream", stream, overwrite ? null : IfNotExists, ct);
|
|
}
|
|
catch (GoogleApiException ex) when (ex.HttpStatusCode == HttpStatusCode.PreconditionFailed)
|
|
{
|
|
throw new AssetAlreadyExistsException(fileName);
|
|
}
|
|
}
|
|
|
|
public async Task DeleteAsync(string fileName)
|
|
{
|
|
Guard.NotNullOrEmpty(fileName, nameof(fileName));
|
|
|
|
try
|
|
{
|
|
await storageClient.DeleteObjectAsync(bucketName, fileName);
|
|
}
|
|
catch (GoogleApiException ex) when (ex.HttpStatusCode == HttpStatusCode.NotFound)
|
|
{
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|