Skip to content

Commit

Permalink
Liquid filter to choose redirect or public blob url #87
Browse files Browse the repository at this point in the history
  • Loading branch information
apexdodge committed Apr 8, 2023
1 parent cf38324 commit db626d8
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 28 deletions.
Expand Up @@ -8,7 +8,7 @@ public interface IFileStorageProvider

Task<string> GetDownloadUrlAsync(string key, DateTime expiresAt, bool inline = true);

Task<string> GetDownloadUrlAsync(string key, string fileName, string contentType, DateTime expiresAt, bool inline = true);
Task<string> GetDownloadUrlAsync(string key);

Task DeleteAsync(string key);

Expand Down
Expand Up @@ -43,26 +43,20 @@ public async Task<string> GetDownloadUrlAsync(string key, DateTime expiresAt, bo
Protocol = SasProtocol.HttpsAndHttp
};


downloadUrl = blobClient.GenerateSasUri(sasPermissions).AbsoluteUri;

downloadUrl = UseCustomDomain(downloadUrl);
return downloadUrl;
}

public async Task<string> GetDownloadUrlAsync(string key, string fileName, string contentType, DateTime expiresAt, bool inline = true)
public async Task<string> GetDownloadUrlAsync(string key)
{
BlobClient blobClient = _client.GetBlobClient(key);

string downloadUrl;

BlobSasBuilder sasPermissions = new BlobSasBuilder(BlobSasPermissions.Read, expiresAt)
{
ContentDisposition = inline ? $"inline" : $"attachment; filename={key}",
ContentType = contentType,
Protocol = SasProtocol.HttpsAndHttp
};

downloadUrl = blobClient.GenerateSasUri(sasPermissions).AbsoluteUri;
string downloadUrl = blobClient.Uri.AbsoluteUri;
downloadUrl = UseCustomDomain(downloadUrl);

return downloadUrl;
}

Expand Down Expand Up @@ -92,6 +86,7 @@ public async Task<string> SaveAndGetDownloadUrlAsync(byte[] data, string key, st
ContentType = contentType,
Protocol = SasProtocol.HttpsAndHttp
};

downloadUrl = blobClient.GenerateSasUri(sasPermissions).AbsoluteUri;

using (var ms = new MemoryStream())
Expand Down
Expand Up @@ -25,7 +25,7 @@ public async Task<string> GetDownloadUrlAsync(string key, DateTime expiresAt, bo
return _relativeUrlBuilder.MediaFileLocalStorageUrl(key);
}

public async Task<string> GetDownloadUrlAsync(string key, string fileName, string contentType, DateTime expiresAt, bool inline = true)
public async Task<string> GetDownloadUrlAsync(string key)
{
return _relativeUrlBuilder.MediaFileLocalStorageUrl(key);
}
Expand Down
15 changes: 3 additions & 12 deletions src/Raytha.Infrastructure/FileStorage/S3FileStorageProvider.cs
Expand Up @@ -55,19 +55,10 @@ public async Task<string> GetDownloadUrlAsync(string key, DateTime expiresAt, bo
return _client.GetPreSignedURL(request1);
}

public async Task<string> GetDownloadUrlAsync(string key, string fileName, string contentType, DateTime expiresAt, bool inline = true)
public async Task<string> GetDownloadUrlAsync(string key)
{
GetPreSignedUrlRequest request = new GetPreSignedUrlRequest
{
BucketName = _bucket,
Key = key,
Expires = expiresAt,
Protocol = Protocol.HTTPS,
Verb = HttpVerb.GET
};
request.ResponseHeaderOverrides.ContentType = contentType;
request.ResponseHeaderOverrides.ContentDisposition = inline ? $"inline; filename={key}" : $"attachment; filename={key}";
return _client.GetPreSignedURL(request);
string url = $"https://{_bucket}.s3.amazonaws.com/{key}";
return url;
}

public async Task<string> GetUploadUrlAsync(string key, string fileName, string contentType, DateTime expiresAt, bool inline = true)
Expand Down
19 changes: 16 additions & 3 deletions src/Raytha.Web/Services/RenderEngine.cs
Expand Up @@ -20,12 +20,18 @@ public class RenderEngine : IRenderEngine
private readonly IRelativeUrlBuilder _relativeUrlBuilder;
private readonly ICurrentOrganization _currentOrganization;
private readonly IMediator _mediator;
private readonly IFileStorageProvider _fileStorageProvider;

public RenderEngine(IMediator mediator, IRelativeUrlBuilder relativeUrlBuilder, ICurrentOrganization currentOrganization)
public RenderEngine(
IMediator mediator,
IRelativeUrlBuilder relativeUrlBuilder,
ICurrentOrganization currentOrganization,
IFileStorageProvider fileStorageProvider)
{
_relativeUrlBuilder = relativeUrlBuilder;
_currentOrganization = currentOrganization;
_mediator = mediator;
_fileStorageProvider = fileStorageProvider;
}

public string RenderAsHtml(string source, object entity)
Expand All @@ -35,7 +41,9 @@ public string RenderAsHtml(string source, object entity)
var options = new TemplateOptions();
options.MemberAccessStrategy = new UnsafeMemberAccessStrategy();
options.TimeZone = DateTimeExtensions.GetTimeZoneInfo(_currentOrganization.TimeZone);
options.Filters.AddFilter("raytha_attachment_url", RaythaAttachmentUrl);
options.Filters.AddFilter("raytha_attachment_url", AttachmentRedirectUrl); //deprecated
options.Filters.AddFilter("attachment_redirect_url", AttachmentRedirectUrl);
options.Filters.AddFilter("attachment_public_url", AttachmentPublicUrl);
options.Filters.AddFilter("organization_time", LocalDateFilter);
options.Filters.AddFilter("groupby", GroupBy);
options.Filters.AddFilter("json", JsonFilter);
Expand All @@ -53,11 +61,16 @@ public string RenderAsHtml(string source, object entity)
}
}

public ValueTask<FluidValue> RaythaAttachmentUrl(FluidValue input, FilterArguments arguments, TemplateContext context)
public ValueTask<FluidValue> AttachmentRedirectUrl(FluidValue input, FilterArguments arguments, TemplateContext context)
{
return new StringValue(_relativeUrlBuilder.MediaRedirectToFileUrl(input.ToStringValue()));
}

public ValueTask<FluidValue> AttachmentPublicUrl(FluidValue input, FilterArguments arguments, TemplateContext context)
{
return new StringValue(_fileStorageProvider.GetDownloadUrlAsync(input.ToStringValue()).Result);
}

public ValueTask<FluidValue> GroupBy(FluidValue input, FilterArguments property, TemplateContext context)
{
var groupByProperty = property.At(0).ToStringValue();
Expand Down

0 comments on commit db626d8

Please sign in to comment.