filehandler: added preview images

This commit is contained in:
Alexey Bannov 2023-03-13 20:11:15 +03:00
parent cad10c140b
commit 2ff1ba77a3
2 changed files with 48 additions and 14 deletions

View File

@ -24,7 +24,7 @@ map $request_uri $header_x_frame_options {
map $request_uri $cache_control {
default "no-cache, no-store, no-transform";
~*\/(filehandler\.ashx\?action=thumb)|\/(storage\/room_logos\/root\/|storage\/userPhotos\/root\/) "must-revalidate, no-transform, immutable, max-age=31536000";
~*\/(filehandler\.ashx\?action=(thumb|preview))|\/(storage\/room_logos\/root\/|storage\/userPhotos\/root\/) "must-revalidate, no-transform, immutable, max-age=31536000";
~*\/(api\/2\.0.*|storage|login\.ashx|filehandler\.ashx|ChunkedUploader.ashx|ThirdPartyAppHandler|apisystem|sh|remoteEntry\.js|debuginfo\.md|static\/scripts.*) "no-cache, no-store, no-transform";
~*\/(images|favicon.ico.*)|\.(js|woff|woff2|css)|(locales.*\.json) "must-revalidate, no-transform, immutable, max-age=31536000";
}

View File

@ -26,6 +26,12 @@
using System;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using static Dropbox.Api.UsersCommon.AccountType;
using Image = SixLabors.ImageSharp.Image;
using JsonException = System.Text.Json.JsonException;
using MimeMapping = ASC.Common.Web.MimeMapping;
@ -190,7 +196,10 @@ public class FileHandlerService
await DifferenceFile(context);
break;
case "thumb":
await ThumbnailFile(context);
await ThumbnailFile(context, false);
break;
case "preview":
await ThumbnailFile(context, true);
break;
case "track":
await TrackFile(context);
@ -1011,21 +1020,21 @@ public class FileHandlerService
}
}
private async Task ThumbnailFile(HttpContext context)
private async Task ThumbnailFile(HttpContext context, bool force)
{
var q = context.Request.Query[FilesLinkUtility.FileId];
if (int.TryParse(q, out var id))
{
await ThumbnailFile(context, id);
await ThumbnailFile(context, id, force);
}
else
{
await ThumbnailFileFromThirdparty(context, q.FirstOrDefault() ?? "");
await ThumbnailFileFromThirdparty(context, q.FirstOrDefault() ?? "", force);
}
}
private async Task ThumbnailFile(HttpContext context, int id)
private async Task ThumbnailFile(HttpContext context, int id, bool force)
{
try
{
@ -1072,20 +1081,45 @@ public class FileHandlerService
return;
}
if (file.ThumbnailStatus != Thumbnail.Created)
if (file.ThumbnailStatus != Thumbnail.Created && !force)
{
context.Response.StatusCode = (int)HttpStatusCode.NoContent;
return;
}
context.Response.Headers.Add("Content-Disposition", ContentDispositionUtil.GetHeaderValue("." + _global.ThumbnailExtension));
context.Response.ContentType = MimeMapping.GetMimeMapping("." + _global.ThumbnailExtension);
using (var stream = await _globalStore.GetStore().GetReadStreamAsync(fileDao.GetUniqThumbnailPath(file, width, height)))
if (force)
{
context.Response.Headers.Add("Content-Length", stream.Length.ToString(CultureInfo.InvariantCulture));
await stream.CopyToAsync(context.Response.Body);
context.Response.ContentType = MimeMapping.GetMimeMapping(".jpeg");
context.Response.Headers.Add("Content-Disposition", ContentDispositionUtil.GetHeaderValue(".jpeg", true));
using (var stream = await fileDao.GetFileStreamAsync(file))
{
var processedImage = await Image.LoadAsync(stream);
processedImage.Mutate(x => x.Resize(new ResizeOptions
{
Size = new Size(width, height),
Mode = ResizeMode.Crop
}));
// save as jpeg more fast, then webp
await processedImage.SaveAsJpegAsync(context.Response.Body);
}
}
else
{
context.Response.ContentType = MimeMapping.GetMimeMapping("." + _global.ThumbnailExtension);
context.Response.Headers.Add("Content-Disposition", ContentDispositionUtil.GetHeaderValue("." + _global.ThumbnailExtension));
var thumbnailFilePath = fileDao.GetUniqThumbnailPath(file, width, height);
using (var stream = await _globalStore.GetStore().GetReadStreamAsync(thumbnailFilePath))
{
context.Response.Headers.Add("Content-Length", stream.Length.ToString(CultureInfo.InvariantCulture));
await stream.CopyToAsync(context.Response.Body);
}
}
}
catch (FileNotFoundException ex)
{
@ -1113,7 +1147,7 @@ public class FileHandlerService
}
}
private async Task ThumbnailFileFromThirdparty(HttpContext context, string id)
private async Task ThumbnailFileFromThirdparty(HttpContext context, string id, bool force)
{
try
{