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

451 lines
17 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.
*
*/
using System;
using System.Text.RegularExpressions;
using System.Threading;
using System.Web;
2020-02-04 08:41:04 +00:00
2020-02-17 08:58:14 +00:00
using ASC.Common;
2019-06-07 08:59:07 +00:00
using ASC.Core;
2020-02-04 08:41:04 +00:00
using ASC.Core.Common;
2019-06-07 08:59:07 +00:00
using ASC.Security.Cryptography;
using ASC.Web.Studio.Utility;
2020-02-04 08:41:04 +00:00
2019-09-23 12:20:08 +00:00
using Microsoft.Extensions.Configuration;
2019-06-07 08:59:07 +00:00
namespace ASC.Web.Core.Files
{
2019-09-23 12:20:08 +00:00
public class FilesLinkUtility
2019-06-07 08:59:07 +00:00
{
public const string FilesBaseVirtualPath = "~/products/files/";
2020-06-22 18:58:16 +00:00
public const string EditorPage = "doceditor";
2019-09-23 12:20:08 +00:00
private readonly string FilesUploaderURL;
2020-08-12 09:58:08 +00:00
private CommonLinkUtility CommonLinkUtility { get; set; }
private BaseCommonLinkUtility BaseCommonLinkUtility { get; }
private CoreBaseSettings CoreBaseSettings { get; set; }
private CoreSettings CoreSettings { get; set; }
private IConfiguration Configuration { get; }
private InstanceCrypto InstanceCrypto { get; }
2019-09-23 12:20:08 +00:00
2020-02-05 13:33:09 +00:00
public FilesLinkUtility(
CommonLinkUtility commonLinkUtility,
BaseCommonLinkUtility baseCommonLinkUtility,
CoreBaseSettings coreBaseSettings,
CoreSettings coreSettings,
IConfiguration configuration,
InstanceCrypto instanceCrypto)
2019-09-23 12:20:08 +00:00
{
CommonLinkUtility = commonLinkUtility;
2020-02-05 13:33:09 +00:00
BaseCommonLinkUtility = baseCommonLinkUtility;
2019-09-23 12:20:08 +00:00
CoreBaseSettings = coreBaseSettings;
CoreSettings = coreSettings;
Configuration = configuration;
2019-10-09 15:04:46 +00:00
InstanceCrypto = instanceCrypto;
2020-04-15 15:39:59 +00:00
FilesUploaderURL = Configuration["files:uploader:url"] ?? "~";
2019-09-23 12:20:08 +00:00
}
public string FilesBaseAbsolutePath
2019-06-07 08:59:07 +00:00
{
2020-02-04 08:41:04 +00:00
get { return BaseCommonLinkUtility.ToAbsolute(FilesBaseVirtualPath); }
2019-06-07 08:59:07 +00:00
}
2020-06-22 18:58:16 +00:00
public const string FileId = "fileId";
2019-06-07 08:59:07 +00:00
public const string FolderId = "folderid";
public const string Version = "version";
public const string FileUri = "fileuri";
public const string FileTitle = "title";
public const string Action = "action";
public const string DocShareKey = "doc";
public const string TryParam = "try";
public const string FolderUrl = "folderurl";
public const string OutType = "outputtype";
public const string AuthKey = "stream_auth";
2020-02-05 15:37:31 +00:00
public const string Anchor = "anchor";
2019-06-07 08:59:07 +00:00
2019-09-23 12:20:08 +00:00
public string FileHandlerPath
2019-06-07 08:59:07 +00:00
{
get { return FilesBaseAbsolutePath + "httphandlers/filehandler.ashx"; }
}
2019-09-23 12:20:08 +00:00
public string DocServiceUrl
2019-06-07 08:59:07 +00:00
{
get
{
var url = GetUrlSetting("public");
if (!string.IsNullOrEmpty(url) && url != "/")
{
url = url.TrimEnd('/') + "/";
}
return url;
}
set
{
SetUrlSetting("api", null);
value = (value ?? "").Trim().ToLowerInvariant();
if (!string.IsNullOrEmpty(value))
{
value = value.TrimEnd('/') + "/";
if (!new Regex(@"(^https?:\/\/)|^\/", RegexOptions.CultureInvariant).IsMatch(value))
{
value = "http://" + value;
}
}
SetUrlSetting("public", value);
}
}
2019-09-23 12:20:08 +00:00
public string DocServiceUrlInternal
2019-06-07 08:59:07 +00:00
{
get
{
var url = GetUrlSetting("internal");
if (string.IsNullOrEmpty(url))
{
url = DocServiceUrl;
}
else
{
url = url.TrimEnd('/') + "/";
}
return url;
}
set
{
SetUrlSetting("converter", null);
SetUrlSetting("storage", null);
SetUrlSetting("command", null);
SetUrlSetting("docbuilder", null);
value = (value ?? "").Trim().ToLowerInvariant();
if (!string.IsNullOrEmpty(value))
{
value = value.TrimEnd('/') + "/";
if (!new Regex(@"(^https?:\/\/)", RegexOptions.CultureInvariant).IsMatch(value))
{
value = "http://" + value;
}
}
SetUrlSetting("internal", value);
}
}
2019-09-23 12:20:08 +00:00
public string DocServiceApiUrl
2019-06-07 08:59:07 +00:00
{
get
{
var url = GetUrlSetting("api");
if (string.IsNullOrEmpty(url))
{
url = DocServiceUrl;
if (!string.IsNullOrEmpty(url))
{
url += "web-apps/apps/api/documents/api.js";
}
}
return url;
}
}
2019-09-23 12:20:08 +00:00
public string DocServiceConverterUrl
2019-06-07 08:59:07 +00:00
{
get
{
var url = GetUrlSetting("converter");
if (string.IsNullOrEmpty(url))
{
url = DocServiceUrlInternal;
if (!string.IsNullOrEmpty(url))
{
url += "ConvertService.ashx";
}
}
return url;
}
}
2019-09-23 12:20:08 +00:00
public string DocServiceCommandUrl
2019-06-07 08:59:07 +00:00
{
get
{
var url = GetUrlSetting("command");
if (string.IsNullOrEmpty(url))
{
url = DocServiceUrlInternal;
if (!string.IsNullOrEmpty(url))
{
url += "coauthoring/CommandService.ashx";
}
}
return url;
}
}
2019-09-23 12:20:08 +00:00
public string DocServiceDocbuilderUrl
2019-06-07 08:59:07 +00:00
{
get
{
var url = GetUrlSetting("docbuilder");
if (string.IsNullOrEmpty(url))
{
url = DocServiceUrlInternal;
if (!string.IsNullOrEmpty(url))
{
url += "docbuilder";
}
}
return url;
}
}
2019-09-23 12:20:08 +00:00
public string DocServiceHealthcheckUrl
2019-06-07 08:59:07 +00:00
{
get
{
var url = GetUrlSetting("healthcheck");
if (string.IsNullOrEmpty(url))
{
url = DocServiceUrlInternal;
if (!string.IsNullOrEmpty(url))
{
url += "healthcheck";
}
}
return url;
}
}
2019-09-23 12:20:08 +00:00
public string DocServicePortalUrl
2019-06-07 08:59:07 +00:00
{
get { return GetUrlSetting("portal"); }
set
{
value = (value ?? "").Trim().ToLowerInvariant();
if (!string.IsNullOrEmpty(value))
{
value = value.TrimEnd('/') + "/";
if (!new Regex(@"(^https?:\/\/)", RegexOptions.CultureInvariant).IsMatch(value))
{
value = "http://" + value;
}
}
SetUrlSetting("portal", value);
}
}
2019-09-23 12:20:08 +00:00
public string FileDownloadUrlString
2019-06-07 08:59:07 +00:00
{
get { return FileHandlerPath + "?" + Action + "=download&" + FileId + "={0}"; }
}
2019-09-23 12:20:08 +00:00
public string GetFileDownloadUrl(object fileId)
2019-06-07 08:59:07 +00:00
{
return GetFileDownloadUrl(fileId, 0, string.Empty);
}
2019-09-23 12:20:08 +00:00
public string GetFileDownloadUrl(object fileId, int fileVersion, string convertToExtension)
2019-06-07 08:59:07 +00:00
{
return string.Format(FileDownloadUrlString, HttpUtility.UrlEncode(fileId.ToString()))
+ (fileVersion > 0 ? "&" + Version + "=" + fileVersion : string.Empty)
+ (string.IsNullOrEmpty(convertToExtension) ? string.Empty : "&" + OutType + "=" + convertToExtension);
}
2019-09-23 12:20:08 +00:00
public string GetFileWebMediaViewUrl(object fileId)
2019-06-07 08:59:07 +00:00
{
return FilesBaseAbsolutePath + "#preview/" + HttpUtility.UrlEncode(fileId.ToString());
}
2019-09-23 12:20:08 +00:00
public string FileWebViewerUrlString
2019-06-07 08:59:07 +00:00
{
get { return FileWebEditorUrlString + "&" + Action + "=view"; }
}
2019-09-23 12:20:08 +00:00
public string GetFileWebViewerUrlForMobile(object fileId, int fileVersion)
2019-06-07 08:59:07 +00:00
{
var viewerUrl = CommonLinkUtility.ToAbsolute("~/../products/files/") + EditorPage + "?" + FileId + "={0}";
return string.Format(viewerUrl, HttpUtility.UrlEncode(fileId.ToString()))
+ (fileVersion > 0 ? "&" + Version + "=" + fileVersion : string.Empty);
}
2019-09-23 12:20:08 +00:00
public string FileWebViewerExternalUrlString
2019-06-07 08:59:07 +00:00
{
get { return FilesBaseAbsolutePath + EditorPage + "?" + FileUri + "={0}&" + FileTitle + "={1}&" + FolderUrl + "={2}"; }
}
2019-09-23 12:20:08 +00:00
public string GetFileWebViewerExternalUrl(string fileUri, string fileTitle, string refererUrl = "")
2019-06-07 08:59:07 +00:00
{
return string.Format(FileWebViewerExternalUrlString, HttpUtility.UrlEncode(fileUri), HttpUtility.UrlEncode(fileTitle), HttpUtility.UrlEncode(refererUrl));
}
2019-09-23 12:20:08 +00:00
public string FileWebEditorUrlString
2019-06-07 08:59:07 +00:00
{
get { return FilesBaseAbsolutePath + EditorPage + "?" + FileId + "={0}"; }
}
2019-09-23 12:20:08 +00:00
public string GetFileWebEditorUrl(object fileId)
2019-06-07 08:59:07 +00:00
{
return string.Format(FileWebEditorUrlString, HttpUtility.UrlEncode(fileId.ToString()));
}
2019-09-23 12:20:08 +00:00
public string GetFileWebEditorTryUrl(FileType fileType)
2019-06-07 08:59:07 +00:00
{
return FilesBaseAbsolutePath + EditorPage + "?" + TryParam + "=" + fileType;
}
2019-09-23 12:20:08 +00:00
public string FileWebEditorExternalUrlString
2019-06-07 08:59:07 +00:00
{
get { return FileHandlerPath + "?" + Action + "=create&" + FileUri + "={0}&" + FileTitle + "={1}"; }
}
2019-09-23 12:20:08 +00:00
public string GetFileWebEditorExternalUrl(string fileUri, string fileTitle)
2019-06-07 08:59:07 +00:00
{
return GetFileWebEditorExternalUrl(fileUri, fileTitle, false);
}
2019-09-23 12:20:08 +00:00
public string GetFileWebEditorExternalUrl(string fileUri, string fileTitle, bool openFolder)
2019-06-07 08:59:07 +00:00
{
var url = string.Format(FileWebEditorExternalUrlString, HttpUtility.UrlEncode(fileUri), HttpUtility.UrlEncode(fileTitle));
if (openFolder)
url += "&openfolder=true";
return url;
}
2019-09-23 12:20:08 +00:00
public string GetFileWebPreviewUrl(FileUtility fileUtility, string fileTitle, object fileId)
2019-06-07 08:59:07 +00:00
{
2019-09-23 12:20:08 +00:00
if (fileUtility.CanImageView(fileTitle) || fileUtility.CanMediaView(fileTitle))
2019-06-07 08:59:07 +00:00
return GetFileWebMediaViewUrl(fileId);
2019-09-23 12:20:08 +00:00
if (fileUtility.CanWebView(fileTitle))
2019-06-07 08:59:07 +00:00
{
2019-09-23 12:20:08 +00:00
if (fileUtility.ExtsMustConvert.Contains(FileUtility.GetFileExtension(fileTitle)))
2019-06-07 08:59:07 +00:00
return string.Format(FileWebViewerUrlString, HttpUtility.UrlEncode(fileId.ToString()));
return GetFileWebEditorUrl(fileId);
}
return GetFileDownloadUrl(fileId);
}
2019-09-23 12:20:08 +00:00
public string FileRedirectPreviewUrlString
2019-06-07 08:59:07 +00:00
{
get { return FileHandlerPath + "?" + Action + "=redirect"; }
}
2019-09-23 12:20:08 +00:00
public string GetFileRedirectPreviewUrl(object enrtyId, bool isFile)
2019-06-07 08:59:07 +00:00
{
return FileRedirectPreviewUrlString + "&" + (isFile ? FileId : FolderId) + "=" + HttpUtility.UrlEncode(enrtyId.ToString());
}
2020-01-22 13:59:49 +00:00
public string GetInitiateUploadSessionUrl(int tenantId, object folderId, object fileId, string fileName, long contentLength, bool encrypted, SecurityContext securityContext)
2019-06-07 08:59:07 +00:00
{
2020-01-22 13:59:49 +00:00
var queryString = string.Format("?initiate=true&{0}={1}&fileSize={2}&tid={3}&userid={4}&culture={5}&encrypted={6}",
2019-06-07 08:59:07 +00:00
FileTitle,
HttpUtility.UrlEncode(fileName),
contentLength,
2019-09-16 14:51:39 +00:00
tenantId,
2019-09-09 12:56:33 +00:00
HttpUtility.UrlEncode(InstanceCrypto.Encrypt(securityContext.CurrentAccount.ID.ToString())),
2020-01-22 13:59:49 +00:00
Thread.CurrentThread.CurrentUICulture.Name,
encrypted.ToString().ToLower());
2019-06-07 08:59:07 +00:00
if (fileId != null)
queryString = queryString + "&" + FileId + "=" + HttpUtility.UrlEncode(fileId.ToString());
if (folderId != null)
queryString = queryString + "&" + FolderId + "=" + HttpUtility.UrlEncode(folderId.ToString());
return CommonLinkUtility.GetFullAbsolutePath(GetFileUploaderHandlerVirtualPath() + queryString);
}
2019-09-23 12:20:08 +00:00
public string GetUploadChunkLocationUrl(string uploadId)
2019-06-07 08:59:07 +00:00
{
var queryString = "?uid=" + uploadId;
return CommonLinkUtility.GetFullAbsolutePath(GetFileUploaderHandlerVirtualPath() + queryString);
}
2019-09-23 12:20:08 +00:00
public bool IsLocalFileUploader
2019-06-07 08:59:07 +00:00
{
get { return !Regex.IsMatch(FilesUploaderURL, "^http(s)?://\\.*"); }
}
2019-09-23 12:20:08 +00:00
private string GetFileUploaderHandlerVirtualPath()
2019-06-07 08:59:07 +00:00
{
var virtualPath = FilesUploaderURL;
return virtualPath.EndsWith(".ashx") ? virtualPath : virtualPath.TrimEnd('/') + "/ChunkedUploader.ashx";
}
2019-09-23 12:20:08 +00:00
private string GetUrlSetting(string key, string appSettingsKey = null)
2019-06-07 08:59:07 +00:00
{
var value = string.Empty;
2019-09-20 12:20:03 +00:00
if (CoreBaseSettings.Standalone)
2019-06-07 08:59:07 +00:00
{
2019-09-20 12:20:03 +00:00
value = CoreSettings.GetSetting(GetSettingsKey(key));
2019-06-07 08:59:07 +00:00
}
if (string.IsNullOrEmpty(value))
{
2020-04-15 15:39:59 +00:00
value = Configuration["files:docservice:url:" + (appSettingsKey ?? key)];
2019-06-07 08:59:07 +00:00
}
return value;
}
2019-09-23 12:20:08 +00:00
private void SetUrlSetting(string key, string value)
2019-06-07 08:59:07 +00:00
{
2019-09-20 12:20:03 +00:00
if (!CoreBaseSettings.Standalone)
2019-06-07 08:59:07 +00:00
{
throw new NotSupportedException("Method for server edition only.");
}
value = (value ?? "").Trim();
if (string.IsNullOrEmpty(value)) value = null;
if (GetUrlSetting(key) != value)
2019-09-20 12:20:03 +00:00
CoreSettings.SaveSetting(GetSettingsKey(key), value);
2019-06-07 08:59:07 +00:00
}
2019-09-23 12:20:08 +00:00
private string GetSettingsKey(string key)
2019-06-07 08:59:07 +00:00
{
return "DocKey_" + key;
}
}
2020-02-12 13:50:38 +00:00
public static class FilesLinkUtilityExtention
{
2020-02-17 08:58:14 +00:00
public static DIHelper AddFilesLinkUtilityService(this DIHelper services)
2020-02-12 13:50:38 +00:00
{
2020-07-17 10:52:28 +00:00
if (services.TryAddScoped<FilesLinkUtility>())
{
return services
.AddCommonLinkUtilityService()
.AddBaseCommonLinkUtilityService()
.AddCoreBaseSettingsService()
.AddCoreSettingsService()
.AddInstanceCryptoService();
}
return services;
2020-02-12 13:50:38 +00:00
}
}
2019-06-07 08:59:07 +00:00
}