DocSpace-buildtools/web/ASC.Web.Core/Files/FileUtility.cs

572 lines
21 KiB
C#
Raw Normal View History

2019-06-07 08:59:07 +00:00
/*
*
* (c) Copyright Ascensio System Limited 2010-2018
*
* This program is freeware. You can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) version 3 as published by the Free Software Foundation (https://www.gnu.org/copyleft/gpl.html).
* In accordance with Section 7(a) of the GNU GPL its Section 15 shall be amended to the effect that
* Ascensio System SIA expressly excludes the warranty of non-infringement of any third-party rights.
*
* THIS PROGRAM IS DISTRIBUTED WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR
* FITNESS FOR A PARTICULAR PURPOSE. For more details, see GNU GPL at https://www.gnu.org/copyleft/gpl.html
*
* You can contact Ascensio System SIA by email at sales@onlyoffice.com
*
* The interactive user interfaces in modified source and object code versions of ONLYOFFICE must display
* Appropriate Legal Notices, as required under Section 5 of the GNU GPL version 3.
*
* Pursuant to Section 7 § 3(b) of the GNU GPL you must retain the original ONLYOFFICE logo which contains
* relevant author attributions when distributing the software. If the display of the logo in its graphic
* form is not reasonably feasible for technical reasons, you must include the words "Powered by ONLYOFFICE"
* in every copy of the program you distribute.
* Pursuant to Section 7 § 3(e) we decline to grant you any rights under trademark law for use of our trademarks.
*
*/
namespace ASC.Web.Core.Files
{
2021-12-08 10:14:44 +00:00
[Singletone]
public class FileUtilityConfiguration
{
private IConfiguration Configuration { get; }
public FileUtilityConfiguration(IConfiguration configuration)
{
Configuration = configuration;
}
private List<string> extsIndexing;
public List<string> ExtsIndexing { get => extsIndexing ??= (Configuration.GetSection("files:index").Get<string[]>() ?? new string[] { }).ToList(); }
private List<string> extsImagePreviewed;
public List<string> ExtsImagePreviewed { get => extsImagePreviewed ??= (Configuration.GetSection("files:viewed-images").Get<string[]>() ?? new string[] { }).ToList(); }
private List<string> extsMediaPreviewed;
public List<string> ExtsMediaPreviewed { get => extsMediaPreviewed ??= (Configuration.GetSection("files:viewed-media").Get<string[]>() ?? new string[] { }).ToList(); }
private List<string> extsWebPreviewed;
public List<string> ExtsWebPreviewed
{
get
{
return extsWebPreviewed ??= (Configuration.GetSection("files:docservice:viewed-docs").Get<string[]>() ?? new string[] { }).ToList();
}
}
private List<string> extsWebEdited;
public List<string> ExtsWebEdited
{
get
{
return extsWebEdited ??= (Configuration.GetSection("files:docservice:edited-docs").Get<string[]>() ?? new string[] { }).ToList();
}
}
private List<string> extsWebEncrypt;
public List<string> ExtsWebEncrypt { get => extsWebEncrypt ??= (Configuration.GetSection("files:docservice:encrypted-docs").Get<string[]>() ?? new string[] { }).ToList(); }
private List<string> extsWebReviewed;
public List<string> ExtsWebReviewed
{
get
{
return extsWebReviewed ??= (Configuration.GetSection("files:docservice:reviewed-docs").Get<string[]>() ?? new string[] { }).ToList();
}
}
private List<string> extsWebCustomFilterEditing;
public List<string> ExtsWebCustomFilterEditing
{
get
{
return extsWebCustomFilterEditing ??= (Configuration.GetSection("files:docservice:customfilter-docs").Get<string[]>() ?? new string[] { }).ToList();
}
}
private List<string> extsWebRestrictedEditing;
public List<string> ExtsWebRestrictedEditing
{
get
{
return extsWebRestrictedEditing ??= (Configuration.GetSection("files:docservice:formfilling-docs").Get<string[]>() ?? new string[] { }).ToList();
}
}
private List<string> extsWebCommented;
public List<string> ExtsWebCommented
{
get
{
return extsWebCommented ??= (Configuration.GetSection("files:docservice:commented-docs").Get<string[]>() ?? new string[] { }).ToList();
}
}
private List<string> extsWebTemplate;
public List<string> ExtsWebTemplate
{
get
{
return extsWebTemplate ??= (Configuration.GetSection("files:docservice:template-docs").Get<string[]>() ?? new string[] { }).ToList();
}
}
private List<string> extsMustConvert;
public List<string> ExtsMustConvert
{
get
{
return extsMustConvert ??= (Configuration.GetSection("files:docservice:convert-docs").Get<string[]>() ?? new string[] { }).ToList();
}
}
private List<string> extsCoAuthoring;
public List<string> ExtsCoAuthoring
{
get => extsCoAuthoring ??= (Configuration.GetSection("files:docservice:coauthor-docs").Get<string[]>() ?? new string[] { }).ToList();
}
2021-12-28 15:40:42 +00:00
private string masterFormExtension;
public string MasterFormExtension
{
get => masterFormExtension ??= Configuration["files:docservice:internal-form"] ?? ".docxf";
}
2021-12-08 10:14:44 +00:00
public Dictionary<FileType, string> InternalExtension
{
get => new Dictionary<FileType, string>
{
{ FileType.Document, Configuration["files:docservice:internal-doc"] ?? ".docx" },
{ FileType.Spreadsheet, Configuration["files:docservice:internal-xls"] ?? ".xlsx" },
{ FileType.Presentation, Configuration["files:docservice:internal-ppt"] ?? ".pptx" }
};
}
internal string GetSignatureSecret()
{
var result = Configuration["files:docservice:secret:value"] ?? "";
var regex = new Regex(@"^\s+$");
if (regex.IsMatch(result))
result = "";
return result;
}
internal string GetSignatureHeader()
{
var result = (Configuration["files:docservice:secret:header"] ?? "").Trim();
if (string.IsNullOrEmpty(result))
result = "Authorization";
return result;
}
internal bool GetCanForcesave()
{
return !bool.TryParse(Configuration["files:docservice:forcesave"] ?? "", out var canForcesave) || canForcesave;
}
}
2020-10-19 15:53:15 +00:00
[Scope]
2019-09-23 12:20:08 +00:00
public class FileUtility
2019-06-07 08:59:07 +00:00
{
2021-07-15 10:07:47 +00:00
private Lazy<FilesDbContext> LazyFilesDbContext { get; }
private FilesDbContext FilesDbContext { get => LazyFilesDbContext.Value; }
2020-02-13 12:19:25 +00:00
public FileUtility(
2021-12-08 10:14:44 +00:00
FileUtilityConfiguration fileUtilityConfiguration,
2020-02-13 12:19:25 +00:00
FilesLinkUtility filesLinkUtility,
DbContextManager<FilesDbContext> dbContextManager)
2019-09-23 12:20:08 +00:00
{
2021-12-08 10:14:44 +00:00
FileUtilityConfiguration = fileUtilityConfiguration;
2019-09-23 12:20:08 +00:00
FilesLinkUtility = filesLinkUtility;
2021-07-15 10:07:47 +00:00
LazyFilesDbContext = new Lazy<FilesDbContext>(() => dbContextManager.Get("files"));
2020-01-22 13:59:49 +00:00
CanForcesave = GetCanForcesave();
2019-09-23 12:20:08 +00:00
}
2019-06-07 08:59:07 +00:00
#region method
public static string GetFileExtension(string fileName)
{
if (string.IsNullOrEmpty(fileName)) return string.Empty;
string extension = null;
try
{
extension = Path.GetExtension(fileName);
}
catch (Exception)
{
var position = fileName.LastIndexOf('.');
if (0 <= position)
extension = fileName.Substring(position).Trim().ToLower();
}
return extension == null ? string.Empty : extension.Trim().ToLower();
}
2019-09-23 12:20:08 +00:00
public string GetInternalExtension(string fileName)
2019-06-07 08:59:07 +00:00
{
var extension = GetFileExtension(fileName);
2019-08-15 13:05:50 +00:00
return InternalExtension.TryGetValue(GetFileTypeByExtention(extension), out var internalExtension)
2019-06-07 08:59:07 +00:00
? internalExtension
: extension;
}
2019-09-23 12:20:08 +00:00
public string GetGoogleDownloadableExtension(string googleExtension)
2019-06-07 08:59:07 +00:00
{
googleExtension = GetFileExtension(googleExtension);
if (googleExtension.Equals(".gdraw")) return ".pdf";
return GetInternalExtension(googleExtension);
}
public static string ReplaceFileExtension(string fileName, string newExtension)
{
newExtension = string.IsNullOrEmpty(newExtension) ? string.Empty : newExtension;
return Path.GetFileNameWithoutExtension(fileName) + newExtension;
}
public static FileType GetFileTypeByFileName(string fileName)
{
return GetFileTypeByExtention(GetFileExtension(fileName));
}
public static FileType GetFileTypeByExtention(string extension)
{
extension = extension.ToLower();
if (ExtsDocument.Contains(extension)) return FileType.Document;
if (ExtsSpreadsheet.Contains(extension)) return FileType.Spreadsheet;
if (ExtsPresentation.Contains(extension)) return FileType.Presentation;
if (ExtsImage.Contains(extension)) return FileType.Image;
if (ExtsArchive.Contains(extension)) return FileType.Archive;
if (ExtsAudio.Contains(extension)) return FileType.Audio;
if (ExtsVideo.Contains(extension)) return FileType.Video;
return FileType.Unknown;
}
2019-09-23 12:20:08 +00:00
public bool CanImageView(string fileName)
2019-06-07 08:59:07 +00:00
{
2021-12-09 17:23:06 +00:00
var ext = GetFileExtension(fileName);
return ExtsImagePreviewed.Exists(r => r.Equals(ext, StringComparison.OrdinalIgnoreCase));
2019-06-07 08:59:07 +00:00
}
2019-09-23 12:20:08 +00:00
public bool CanMediaView(string fileName)
2019-06-07 08:59:07 +00:00
{
2021-12-09 17:23:06 +00:00
var ext = GetFileExtension(fileName);
return ExtsMediaPreviewed.Exists(r => r.Equals(ext, StringComparison.OrdinalIgnoreCase));
2019-06-07 08:59:07 +00:00
}
2019-09-23 12:20:08 +00:00
public bool CanWebView(string fileName)
2019-06-07 08:59:07 +00:00
{
2021-12-09 17:23:06 +00:00
var ext = GetFileExtension(fileName);
return ExtsWebPreviewed.Exists(r => r.Equals(ext, StringComparison.OrdinalIgnoreCase));
2019-06-07 08:59:07 +00:00
}
2019-09-23 12:20:08 +00:00
public bool CanWebEdit(string fileName)
2019-06-07 08:59:07 +00:00
{
2021-12-09 17:23:06 +00:00
var ext = GetFileExtension(fileName);
return ExtsWebEdited.Exists(r => r.Equals(ext, StringComparison.OrdinalIgnoreCase));
2019-06-07 08:59:07 +00:00
}
2019-09-23 12:20:08 +00:00
public bool CanWebReview(string fileName)
2019-06-07 08:59:07 +00:00
{
2021-12-09 17:23:06 +00:00
var ext = GetFileExtension(fileName);
return ExtsWebReviewed.Exists(r => r.Equals(ext, StringComparison.OrdinalIgnoreCase));
2019-06-07 08:59:07 +00:00
}
2020-09-18 07:59:23 +00:00
public bool CanWebCustomFilterEditing(string fileName)
{
2021-12-09 17:23:06 +00:00
var ext = GetFileExtension(fileName);
return ExtsWebCustomFilterEditing.Exists(r => r.Equals(ext, StringComparison.OrdinalIgnoreCase));
2020-09-18 07:59:23 +00:00
}
2019-09-23 12:20:08 +00:00
public bool CanWebRestrictedEditing(string fileName)
2019-06-07 08:59:07 +00:00
{
2021-12-09 17:23:06 +00:00
var ext = GetFileExtension(fileName);
return ExtsWebRestrictedEditing.Exists(r => r.Equals(ext, StringComparison.OrdinalIgnoreCase));
2019-06-07 08:59:07 +00:00
}
2019-09-23 12:20:08 +00:00
public bool CanWebComment(string fileName)
2019-06-07 08:59:07 +00:00
{
2021-12-09 17:23:06 +00:00
var ext = GetFileExtension(fileName);
return ExtsWebCommented.Exists(r => r.Equals(ext, StringComparison.OrdinalIgnoreCase));
2019-06-07 08:59:07 +00:00
}
2019-09-23 12:20:08 +00:00
public bool CanCoAuhtoring(string fileName)
2019-06-07 08:59:07 +00:00
{
2021-12-09 17:23:06 +00:00
var ext = GetFileExtension(fileName);
return ExtsCoAuthoring.Exists(r => r.Equals(ext, StringComparison.OrdinalIgnoreCase));
2019-06-07 08:59:07 +00:00
}
2019-09-23 12:20:08 +00:00
public bool CanIndex(string fileName)
2019-06-07 08:59:07 +00:00
{
2021-12-09 17:23:06 +00:00
var ext = GetFileExtension(fileName);
return ExtsIndexing.Exists(r => r.Equals(ext, StringComparison.OrdinalIgnoreCase));
2019-06-07 08:59:07 +00:00
}
#endregion
#region member
2019-09-23 12:20:08 +00:00
private Dictionary<string, List<string>> _extsConvertible;
2019-06-07 08:59:07 +00:00
2019-09-23 12:20:08 +00:00
public Dictionary<string, List<string>> ExtsConvertible
2019-06-07 08:59:07 +00:00
{
get
{
if (_extsConvertible == null)
{
_extsConvertible = new Dictionary<string, List<string>>();
if (string.IsNullOrEmpty(FilesLinkUtility.DocServiceConverterUrl)) return _extsConvertible;
2021-07-15 10:07:47 +00:00
var dbManager = FilesDbContext;
2020-09-28 12:45:26 +00:00
var list = dbManager.FilesConverts.Select(r => new { r.Input, r.Output }).ToList();
2019-08-15 15:08:40 +00:00
list.ForEach(item =>
{
2019-12-09 11:59:22 +00:00
var input = item.Input;
2020-09-28 12:45:26 +00:00
var output = item.Output;
2019-08-15 15:08:40 +00:00
if (string.IsNullOrEmpty(input) || string.IsNullOrEmpty(output))
return;
input = input.ToLower().Trim();
output = output.ToLower().Trim();
if (!_extsConvertible.ContainsKey(input))
_extsConvertible[input] = new List<string>();
_extsConvertible[input].Add(output);
});
2019-06-07 08:59:07 +00:00
}
return _extsConvertible;
}
}
2019-09-23 12:20:08 +00:00
private List<string> _extsUploadable;
2019-06-07 08:59:07 +00:00
2019-09-23 12:20:08 +00:00
public List<string> ExtsUploadable
2019-06-07 08:59:07 +00:00
{
get
{
if (_extsUploadable == null)
{
_extsUploadable = new List<string>();
_extsUploadable.AddRange(ExtsWebPreviewed);
_extsUploadable.AddRange(ExtsWebEdited);
_extsUploadable.AddRange(ExtsImagePreviewed);
_extsUploadable = _extsUploadable.Distinct().ToList();
}
return _extsUploadable;
}
}
2021-12-08 10:14:44 +00:00
private List<string> ExtsIndexing { get => FileUtilityConfiguration.ExtsIndexing; }
2019-06-07 08:59:07 +00:00
2021-12-08 10:14:44 +00:00
public List<string> ExtsImagePreviewed { get => FileUtilityConfiguration.ExtsImagePreviewed; }
2019-06-07 08:59:07 +00:00
2021-12-08 10:14:44 +00:00
public List<string> ExtsMediaPreviewed { get => FileUtilityConfiguration.ExtsMediaPreviewed; }
2019-06-07 08:59:07 +00:00
2019-09-23 12:20:08 +00:00
public List<string> ExtsWebPreviewed
2019-06-07 08:59:07 +00:00
{
2020-05-07 11:02:29 +00:00
get
{
if (string.IsNullOrEmpty(FilesLinkUtility.DocServiceApiUrl))
{
return new List<string>();
}
2021-12-08 10:14:44 +00:00
return FileUtilityConfiguration.ExtsWebPreviewed;
2020-05-07 11:02:29 +00:00
}
2019-06-07 08:59:07 +00:00
}
2019-09-23 12:20:08 +00:00
public List<string> ExtsWebEdited
2019-06-07 08:59:07 +00:00
{
2020-05-07 11:02:29 +00:00
get
{
if (string.IsNullOrEmpty(FilesLinkUtility.DocServiceApiUrl))
{
return new List<string>();
}
2021-12-08 10:14:44 +00:00
return FileUtilityConfiguration.ExtsWebEdited;
2020-05-07 11:02:29 +00:00
}
2019-06-07 08:59:07 +00:00
}
2021-12-08 10:14:44 +00:00
public List<string> ExtsWebEncrypt { get => FileUtilityConfiguration.ExtsWebEncrypt; }
2019-06-07 08:59:07 +00:00
2019-09-23 12:20:08 +00:00
public List<string> ExtsWebReviewed
2019-06-07 08:59:07 +00:00
{
2020-05-07 11:02:29 +00:00
get
{
if (string.IsNullOrEmpty(FilesLinkUtility.DocServiceApiUrl))
{
return new List<string>();
}
2021-12-08 10:14:44 +00:00
return FileUtilityConfiguration.ExtsWebReviewed;
2020-05-07 11:02:29 +00:00
}
2019-06-07 08:59:07 +00:00
}
2020-09-18 07:59:23 +00:00
public List<string> ExtsWebCustomFilterEditing
{
get
{
if (string.IsNullOrEmpty(FilesLinkUtility.DocServiceApiUrl))
{
return new List<string>();
}
2021-12-08 10:14:44 +00:00
return FileUtilityConfiguration.ExtsWebCustomFilterEditing;
2020-09-18 07:59:23 +00:00
}
}
2019-09-23 12:20:08 +00:00
public List<string> ExtsWebRestrictedEditing
2019-06-07 08:59:07 +00:00
{
2020-05-07 11:02:29 +00:00
get
{
if (string.IsNullOrEmpty(FilesLinkUtility.DocServiceApiUrl))
{
return new List<string>();
}
2021-12-08 10:14:44 +00:00
return FileUtilityConfiguration.ExtsWebRestrictedEditing;
2020-05-07 11:02:29 +00:00
}
2019-06-07 08:59:07 +00:00
}
2019-09-23 12:20:08 +00:00
public List<string> ExtsWebCommented
2019-06-07 08:59:07 +00:00
{
2020-05-07 11:02:29 +00:00
get
{
if (string.IsNullOrEmpty(FilesLinkUtility.DocServiceApiUrl))
{
return new List<string>();
}
2021-12-08 10:14:44 +00:00
return FileUtilityConfiguration.ExtsWebCommented;
2020-05-07 11:02:29 +00:00
}
2019-06-07 08:59:07 +00:00
}
2020-10-22 14:17:20 +00:00
public List<string> ExtsWebTemplate
{
2021-12-08 10:14:44 +00:00
get => FileUtilityConfiguration.ExtsWebTemplate;
2020-10-22 14:17:20 +00:00
}
2020-09-18 07:59:23 +00:00
2019-09-23 12:20:08 +00:00
public List<string> ExtsMustConvert
2019-06-07 08:59:07 +00:00
{
2020-05-07 11:02:29 +00:00
get
{
if (string.IsNullOrEmpty(FilesLinkUtility.DocServiceConverterUrl))
{
return new List<string>();
}
2021-12-08 10:14:44 +00:00
return FileUtilityConfiguration.ExtsMustConvert;
2020-05-07 11:02:29 +00:00
}
}
public List<string> ExtsCoAuthoring
{
2021-12-08 10:14:44 +00:00
get => FileUtilityConfiguration.ExtsCoAuthoring;
2019-06-07 08:59:07 +00:00
}
2021-12-08 10:14:44 +00:00
private FileUtilityConfiguration FileUtilityConfiguration { get; }
2020-08-12 09:58:08 +00:00
private FilesLinkUtility FilesLinkUtility { get; }
2019-06-07 08:59:07 +00:00
public static readonly List<string> ExtsArchive = new List<string>
{
".zip", ".rar", ".ace", ".arc", ".arj",
".bh", ".cab", ".enc", ".gz", ".ha",
".jar", ".lha", ".lzh", ".pak", ".pk3",
".tar", ".tgz", ".gz", ".uu", ".uue", ".xxe",
".z", ".zoo"
};
public static readonly List<string> ExtsVideo = new List<string>
{
".3gp", ".asf", ".avi", ".f4v",
".fla", ".flv", ".m2ts", ".m4v",
".mkv", ".mov", ".mp4", ".mpeg",
".mpg", ".mts", ".ogv", ".svi",
".vob", ".webm", ".wmv"
};
public static readonly List<string> ExtsAudio = new List<string>
{
".aac", ".ac3", ".aiff", ".amr",
".ape", ".cda", ".flac", ".m4a",
".mid", ".mka", ".mp3", ".mpc",
".oga", ".ogg", ".pcm", ".ra",
".raw", ".wav", ".wma"
};
public static readonly List<string> ExtsImage = new List<string>
{
".bmp", ".cod", ".gif", ".ief", ".jpe", ".jpeg", ".jpg",
".jfif", ".tiff", ".tif", ".cmx", ".ico", ".pnm", ".pbm",
".png", ".ppm", ".rgb", ".svg", ".xbm", ".xpm", ".xwd",
2020-09-18 07:59:23 +00:00
".svgt", ".svgy", ".gdraw", ".webp"
2019-06-07 08:59:07 +00:00
};
public static readonly List<string> ExtsSpreadsheet = new List<string>
{
".xls", ".xlsx", ".xlsm",
".xlt", ".xltx", ".xltm",
".ods", ".fods", ".ots", ".csv",
".xlst", ".xlsy", ".xlsb",
".gsheet"
};
public static readonly List<string> ExtsPresentation = new List<string>
{
".pps", ".ppsx", ".ppsm",
".ppt", ".pptx", ".pptm",
".pot", ".potx", ".potm",
".odp", ".fodp", ".otp",
".pptt", ".ppty",
".gslides"
};
public static readonly List<string> ExtsDocument = new List<string>
{
".doc", ".docx", ".docm",
".dot", ".dotx", ".dotm",
".odt", ".fodt", ".ott", ".rtf", ".txt",
".html", ".htm", ".mht",
".pdf", ".djvu", ".fb2", ".epub", ".xps",
".doct", ".docy",
2021-11-16 17:40:15 +00:00
".gdoc",
".docxf", ".oform"
2019-06-07 08:59:07 +00:00
};
2020-09-18 07:59:23 +00:00
public static readonly List<string> ExtsTemplate = new List<string>
{
".ott", ".ots", ".otp",
".dot", ".dotm", ".dotx",
".xlt", ".xltm", ".xltx",
".pot", ".potm", ".potx",
};
2021-12-08 10:14:44 +00:00
public Dictionary<FileType, string> InternalExtension => FileUtilityConfiguration.InternalExtension;
2019-06-07 08:59:07 +00:00
2021-12-28 15:40:42 +00:00
public string MasterFormExtension { get => FileUtilityConfiguration.MasterFormExtension; }
2019-06-07 08:59:07 +00:00
public enum CsvDelimiter
{
None = 0,
Tab = 1,
Semicolon = 2,
Colon = 3,
Comma = 4,
Space = 5
}
2019-09-23 12:20:08 +00:00
public string SignatureSecret { get => GetSignatureSecret(); }
public string SignatureHeader { get => GetSignatureHeader(); }
2019-06-07 08:59:07 +00:00
2021-12-08 10:14:44 +00:00
private string GetSignatureSecret() => FileUtilityConfiguration.GetSignatureSecret();
2019-06-07 08:59:07 +00:00
2021-12-08 10:14:44 +00:00
private string GetSignatureHeader() => FileUtilityConfiguration.GetSignatureHeader();
2019-06-07 08:59:07 +00:00
2020-01-22 13:59:49 +00:00
public readonly bool CanForcesave;
2021-12-08 10:14:44 +00:00
private bool GetCanForcesave() => FileUtilityConfiguration.GetCanForcesave();
2020-01-22 13:59:49 +00:00
2019-06-07 08:59:07 +00:00
#endregion
}
}