✨ File opening response type overriding
This commit is contained in:
@ -20,8 +20,13 @@ public class FileController(
|
|||||||
public async Task<ActionResult> OpenFile(string id, [FromQuery] bool original = false)
|
public async Task<ActionResult> OpenFile(string id, [FromQuery] bool original = false)
|
||||||
{
|
{
|
||||||
// Support the file extension for client side data recognize
|
// Support the file extension for client side data recognize
|
||||||
|
String? fileExtension = null;
|
||||||
if (id.Contains("."))
|
if (id.Contains("."))
|
||||||
id = id.Split('.').First();
|
{
|
||||||
|
var splitedId = id.Split('.');
|
||||||
|
id = splitedId.First();
|
||||||
|
fileExtension = splitedId.Last();
|
||||||
|
}
|
||||||
|
|
||||||
var file = await fs.GetFileAsync(id);
|
var file = await fs.GetFileAsync(id);
|
||||||
if (file is null) return NotFound();
|
if (file is null) return NotFound();
|
||||||
@ -65,12 +70,24 @@ public class FileController(
|
|||||||
return BadRequest(
|
return BadRequest(
|
||||||
"Failed to configure client for remote destination, file got an invalid storage remote.");
|
"Failed to configure client for remote destination, file got an invalid storage remote.");
|
||||||
|
|
||||||
|
var headers = new Dictionary<string, string>();
|
||||||
|
if (fileExtension is not null)
|
||||||
|
{
|
||||||
|
if (MimeTypes.TryGetMimeType(fileExtension, out var mimeType))
|
||||||
|
headers.Add("Response-Content-Type", mimeType);
|
||||||
|
}
|
||||||
|
else if (file.MimeType is not null && !file.MimeType!.EndsWith("unknown"))
|
||||||
|
{
|
||||||
|
headers.Add("Response-Content-Type", file.MimeType);
|
||||||
|
}
|
||||||
|
|
||||||
var bucket = dest.Bucket;
|
var bucket = dest.Bucket;
|
||||||
var openUrl = await client.PresignedGetObjectAsync(
|
var openUrl = await client.PresignedGetObjectAsync(
|
||||||
new PresignedGetObjectArgs()
|
new PresignedGetObjectArgs()
|
||||||
.WithBucket(bucket)
|
.WithBucket(bucket)
|
||||||
.WithObject(fileName)
|
.WithObject(fileName)
|
||||||
.WithExpiry(3600)
|
.WithExpiry(3600)
|
||||||
|
.WithHeaders(headers)
|
||||||
);
|
);
|
||||||
|
|
||||||
return Redirect(openUrl);
|
return Redirect(openUrl);
|
||||||
|
Reference in New Issue
Block a user