DocSpace-client/products/ASC.Files/Service/Thumbnail/Builder.cs

353 lines
13 KiB
C#
Raw Normal View History

2021-05-30 18:52:32 +00:00
/*
*
* (c) Copyright Ascensio System Limited 2010-2021
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
namespace ASC.Files.ThumbnailBuilder
{
[Singletone]
internal class BuilderQueue<T>
{
private readonly ThumbnailSettings _config;
private readonly ILog _logger;
private readonly IServiceProvider _serviceProvider;
2021-05-30 18:52:32 +00:00
2022-01-21 08:26:25 +00:00
public BuilderQueue(IServiceProvider serviceProvider, IOptionsMonitor<ILog> log, ThumbnailSettings settings)
2021-05-30 18:52:32 +00:00
{
_logger = log.Get("ASC.Files.ThumbnailBuilder");
_serviceProvider = serviceProvider;
_config = settings;
2021-05-30 18:52:32 +00:00
}
public void BuildThumbnails(IEnumerable<FileData<T>> filesWithoutThumbnails)
{
try
{
Parallel.ForEach(
filesWithoutThumbnails,
new ParallelOptions { MaxDegreeOfParallelism = _config.MaxDegreeOfParallelism },
2021-10-29 14:21:51 +00:00
(fileData) =>
{
using var scope = _serviceProvider.CreateScope();
2021-05-30 18:52:32 +00:00
var commonLinkUtilitySettings = scope.ServiceProvider.GetService<CommonLinkUtilitySettings>();
commonLinkUtilitySettings.ServerUri = fileData.BaseUri;
var builder = scope.ServiceProvider.GetService<Builder<T>>();
2021-10-29 14:21:51 +00:00
builder.BuildThumbnail(fileData);
2021-05-30 18:52:32 +00:00
}
);
}
catch (Exception exception)
{
_logger.Error(string.Format("BuildThumbnails: filesWithoutThumbnails.Count: {0}.", filesWithoutThumbnails.Count()), exception);
2021-05-30 18:52:32 +00:00
}
}
}
[Scope]
internal class Builder<T>
{
private readonly ThumbnailSettings _config;
private readonly ILog _logger;
private readonly TenantManager _tenantManager;
private readonly IDaoFactory _daoFactory;
private readonly DocumentServiceConnector _documentServiceConnector;
private readonly DocumentServiceHelper _documentServiceHelper;
private readonly Global _global;
private readonly PathProvider _pathProvider;
private readonly IHttpClientFactory _clientFactory;
2021-05-30 18:52:32 +00:00
public Builder(
2022-01-21 08:26:25 +00:00
ThumbnailSettings settings,
2021-05-30 18:52:32 +00:00
TenantManager tenantManager,
IDaoFactory daoFactory,
DocumentServiceConnector documentServiceConnector,
DocumentServiceHelper documentServiceHelper,
Global global,
PathProvider pathProvider,
IOptionsMonitor<ILog> log,
2022-01-13 11:19:39 +00:00
IHttpClientFactory clientFactory)
2021-05-30 18:52:32 +00:00
{
this._config = settings;
_tenantManager = tenantManager;
_daoFactory = daoFactory;
_documentServiceConnector = documentServiceConnector;
_documentServiceHelper = documentServiceHelper;
_global = global;
_pathProvider = pathProvider;
_logger = log.Get("ASC.Files.ThumbnailBuilder");
_clientFactory = clientFactory;
2021-05-30 18:52:32 +00:00
}
internal void BuildThumbnail(FileData<T> fileData)
{
try
{
_tenantManager.SetCurrentTenant(fileData.TenantId);
2021-05-30 18:52:32 +00:00
var fileDao = _daoFactory.GetFileDao<T>();
2021-05-30 18:52:32 +00:00
if (fileDao == null)
{
_logger.ErrorFormat("BuildThumbnail: TenantId: {0}. FileDao could not be null.", fileData.TenantId);
2021-05-30 18:52:32 +00:00
return;
}
GenerateThumbnail(fileDao, fileData);
}
catch (Exception exception)
{
_logger.Error(string.Format("BuildThumbnail: TenantId: {0}.", fileData.TenantId), exception);
2021-05-30 18:52:32 +00:00
}
finally
{
Launcher.Queue.TryRemove(fileData.FileId, out _);
}
}
private void GenerateThumbnail(IFileDao<T> fileDao, FileData<T> fileData)
{
File<T> file = null;
try
{
2021-08-26 12:32:44 +00:00
file = fileDao.GetFileAsync(fileData.FileId).Result;
2021-05-30 18:52:32 +00:00
if (file == null)
{
_logger.ErrorFormat("GenerateThumbnail: FileId: {0}. File not found.", fileData.FileId);
2021-05-30 18:52:32 +00:00
return;
}
if (file.ThumbnailStatus != Thumbnail.Waiting)
{
_logger.InfoFormat("GenerateThumbnail: FileId: {0}. Thumbnail already processed.", fileData.FileId);
2021-05-30 18:52:32 +00:00
return;
}
var ext = FileUtility.GetFileExtension(file.Title);
if (!_config.FormatsArray.Contains(ext) || file.Encrypted || file.RootFolderType == FolderType.TRASH || file.ContentLength > _config.AvailableFileSize)
2021-05-30 18:52:32 +00:00
{
file.ThumbnailStatus = Thumbnail.NotRequired;
fileDao.SaveThumbnailAsync(file, null).Wait();
2021-05-30 18:52:32 +00:00
return;
}
if (IsImage(file))
{
CropImage(fileDao, file);
}
else
{
MakeThumbnail(fileDao, file);
}
}
catch (Exception exception)
{
_logger.Error(string.Format("GenerateThumbnail: FileId: {0}.", fileData.FileId), exception);
2021-05-30 18:52:32 +00:00
if (file != null)
{
file.ThumbnailStatus = Thumbnail.Error;
2021-12-23 11:46:21 +00:00
fileDao.SaveThumbnailAsync(file, null).Wait();
2021-05-30 18:52:32 +00:00
}
}
}
private void MakeThumbnail(IFileDao<T> fileDao, File<T> file)
{
_logger.DebugFormat("MakeThumbnail: FileId: {0}.", file.ID);
2021-05-30 18:52:32 +00:00
string thumbnailUrl = null;
var attempt = 1;
do
{
try
{
if (GetThumbnailUrl(file, _global.ThumbnailExtension, out thumbnailUrl))
2021-05-30 18:52:32 +00:00
{
break;
}
}
catch (Exception exception)
{
if (exception.InnerException != null)
{
var documentServiceException = exception.InnerException as DocumentService.DocumentServiceException;
if (documentServiceException != null)
{
if (documentServiceException.Code == DocumentService.DocumentServiceException.ErrorCode.ConvertPassword)
{
throw new Exception(string.Format("MakeThumbnail: FileId: {0}. Encrypted file.", file.ID));
}
if (documentServiceException.Code == DocumentService.DocumentServiceException.ErrorCode.Convert)
{
throw new Exception(string.Format("MakeThumbnail: FileId: {0}. Could not convert.", file.ID));
}
}
}
}
if (attempt >= _config.AttemptsLimit)
2021-05-30 18:52:32 +00:00
{
throw new Exception(string.Format("MakeThumbnail: FileId: {0}. Attempts limmit exceeded.", file.ID));
}
else
{
_logger.DebugFormat("MakeThumbnail: FileId: {0}. Sleep {1} after attempt #{2}. ", file.ID, _config.AttemptWaitInterval, attempt);
2021-05-30 18:52:32 +00:00
attempt++;
}
Thread.Sleep(_config.AttemptWaitInterval);
2021-05-30 18:52:32 +00:00
}
while (string.IsNullOrEmpty(thumbnailUrl));
SaveThumbnail(fileDao, file, thumbnailUrl);
}
private bool GetThumbnailUrl(File<T> file, string toExtension, out string url)
{
var fileUri = _pathProvider.GetFileStreamUrl(file);
fileUri = _documentServiceConnector.ReplaceCommunityAdress(fileUri);
2021-05-30 18:52:32 +00:00
var fileExtension = file.ConvertedExtension;
var docKey = _documentServiceHelper.GetDocKey(file);
2021-05-30 18:52:32 +00:00
var thumbnail = new DocumentService.ThumbnailData
{
Aspect = 2,
First = true,
//Height = config.ThumbnaillHeight,
//Width = config.ThumbnaillWidth
};
var spreadsheetLayout = new DocumentService.SpreadsheetLayout
{
IgnorePrintArea = true,
//Orientation = "landscape", // "297mm" x "210mm"
FitToHeight = 0,
FitToWidth = 1,
Headings = false,
GridLines = false,
2021-11-16 17:40:15 +00:00
Margins = new DocumentService.SpreadsheetLayout.LayoutMargins
2021-05-30 18:52:32 +00:00
{
Top = "0mm",
Right = "0mm",
Bottom = "0mm",
Left = "0mm"
},
2021-11-16 17:40:15 +00:00
PageSize = new DocumentService.SpreadsheetLayout.LayoutPageSize
2021-05-30 18:52:32 +00:00
{
Width = (_config.ThumbnaillWidth * 1.5) + "mm", // 192 * 1.5 = "288mm",
Height = (_config.ThumbnaillHeight * 1.5) + "mm" // 128 * 1.5 = "192mm"
2021-05-30 18:52:32 +00:00
}
};
2022-02-10 18:25:40 +00:00
(var operationResultProgress, url) = _documentServiceConnector.GetConvertedUriAsync(fileUri, fileExtension, toExtension, docKey, null, thumbnail, spreadsheetLayout, false).Result;
operationResultProgress = Math.Min(operationResultProgress, 100);
2021-05-30 18:52:32 +00:00
return operationResultProgress == 100;
}
private void SaveThumbnail(IFileDao<T> fileDao, File<T> file, string thumbnailUrl)
{
_logger.DebugFormat("SaveThumbnail: FileId: {0}. ThumbnailUrl {1}.", file.ID, thumbnailUrl);
2021-10-12 10:14:33 +00:00
var request = new HttpRequestMessage();
request.RequestUri = new Uri(thumbnailUrl);
2021-05-30 18:52:32 +00:00
var httpClient = _clientFactory.CreateClient();
2021-11-24 19:34:39 +00:00
using var response = httpClient.Send(request);
using (var stream = new ResponseStream(response))
2021-05-30 18:52:32 +00:00
{
Crop(fileDao, file, stream);
}
_logger.DebugFormat("SaveThumbnail: FileId: {0}. Successfully saved.", file.ID);
2021-05-30 18:52:32 +00:00
}
private bool IsImage(File<T> file)
{
var extension = FileUtility.GetFileExtension(file.Title);
2021-05-30 18:52:32 +00:00
return FileUtility.ExtsImage.Contains(extension);
}
private void CropImage(IFileDao<T> fileDao, File<T> file)
{
_logger.DebugFormat("CropImage: FileId: {0}.", file.ID);
2021-05-30 18:52:32 +00:00
2021-11-15 11:31:47 +00:00
using (var stream = fileDao.GetFileStreamAsync(file).Result)
2021-05-30 18:52:32 +00:00
{
Crop(fileDao, file, stream);
}
_logger.DebugFormat("CropImage: FileId: {0}. Successfully saved.", file.ID);
2021-05-30 18:52:32 +00:00
}
private void Crop(IFileDao<T> fileDao, File<T> file, Stream stream)
{
using (var sourceImg = Image.Load(stream))
2021-05-30 18:52:32 +00:00
{
using (var targetImg = GetImageThumbnail(sourceImg))
2021-05-30 18:52:32 +00:00
{
using (var targetStream = new MemoryStream())
{
targetImg.Save(targetStream, PngFormat.Instance);
2021-12-23 11:46:21 +00:00
fileDao.SaveThumbnailAsync(file, targetStream).Wait();
2021-05-30 18:52:32 +00:00
}
}
2021-11-06 20:11:18 +00:00
}
2021-11-06 20:11:18 +00:00
GC.Collect();
2021-05-30 18:52:32 +00:00
}
private Image GetImageThumbnail(Image sourceBitmap)
2021-05-30 18:52:32 +00:00
{
//bad for small or disproportionate images
//return sourceBitmap.GetThumbnailImage(config.ThumbnaillWidth, config.ThumbnaillHeight, () => false, IntPtr.Zero);
var targetSize = new Size(Math.Min(sourceBitmap.Width, _config.ThumbnaillWidth), Math.Min(sourceBitmap.Height, _config.ThumbnaillHeight));
2021-05-30 18:52:32 +00:00
var point = new Point(0, 0);
var size = targetSize;
if (sourceBitmap.Width > _config.ThumbnaillWidth && sourceBitmap.Height > _config.ThumbnaillHeight)
2021-05-30 18:52:32 +00:00
{
if (sourceBitmap.Width > sourceBitmap.Height)
{
var width = (int)(_config.ThumbnaillWidth * (sourceBitmap.Height / (1.0 * _config.ThumbnaillHeight)));
2021-05-30 18:52:32 +00:00
size = new Size(width, sourceBitmap.Height);
}
else
{
var height = (int)(_config.ThumbnaillHeight * (sourceBitmap.Width / (1.0 * _config.ThumbnaillWidth)));
2021-05-30 18:52:32 +00:00
size = new Size(sourceBitmap.Width, height);
}
}
if (sourceBitmap.Width > sourceBitmap.Height)
{
point.X = (sourceBitmap.Width - size.Width) / 2;
}
var targetThumbnailSettings = new UserPhotoThumbnailSettings(point, size);
return UserPhotoThumbnailManager.GetImage(sourceBitmap, targetSize, targetThumbnailSettings);
2021-05-30 18:52:32 +00:00
}
}
}