2022-03-15 18:00:53 +00:00
|
|
|
// (c) Copyright Ascensio System SIA 2010-2022
|
|
|
|
//
|
|
|
|
// This program is a free software product.
|
|
|
|
// You can redistribute it and/or modify it under the terms
|
|
|
|
// of the GNU Affero General Public License (AGPL) version 3 as published by the Free Software
|
|
|
|
// Foundation. In accordance with Section 7(a) of the GNU AGPL 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 details, see
|
|
|
|
// the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
|
|
|
|
//
|
|
|
|
// You can contact Ascensio System SIA at Lubanas st. 125a-25, Riga, Latvia, EU, LV-1021.
|
|
|
|
//
|
|
|
|
// The interactive user interfaces in modified source and object code versions of the Program must
|
|
|
|
// display Appropriate Legal Notices, as required under Section 5 of the GNU AGPL version 3.
|
|
|
|
//
|
|
|
|
// Pursuant to Section 7(b) of the License you must retain the original Product logo when
|
|
|
|
// distributing the program. Pursuant to Section 7(e) we decline to grant you any rights under
|
|
|
|
// trademark law for use of our trademarks.
|
|
|
|
//
|
|
|
|
// All the Product's GUI elements, including illustrations and icon sets, as well as technical writing
|
|
|
|
// content are licensed under the terms of the Creative Commons Attribution-ShareAlike 4.0
|
|
|
|
// International. See the License terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
|
|
|
|
|
2022-02-10 11:24:16 +00:00
|
|
|
namespace ASC.Data.Storage.DiscStorage;
|
2022-01-11 15:37:19 +00:00
|
|
|
|
2022-02-10 11:24:16 +00:00
|
|
|
public class StorageHandler
|
|
|
|
{
|
|
|
|
private readonly string _path;
|
|
|
|
private readonly string _module;
|
|
|
|
private readonly string _domain;
|
|
|
|
private readonly bool _checkAuth;
|
|
|
|
|
2022-03-25 09:23:28 +00:00
|
|
|
public StorageHandler(string path, string module, string domain, bool checkAuth = true)
|
2022-02-10 11:24:16 +00:00
|
|
|
{
|
|
|
|
_path = path;
|
|
|
|
_module = module;
|
|
|
|
_domain = domain;
|
|
|
|
_checkAuth = checkAuth;
|
|
|
|
}
|
|
|
|
|
2022-03-25 09:23:28 +00:00
|
|
|
public Task Invoke(HttpContext context, TenantManager tenantManager, SecurityContext securityContext, StorageFactory storageFactory, EmailValidationKeyProvider emailValidationKeyProvider)
|
2022-02-10 11:24:16 +00:00
|
|
|
{
|
|
|
|
if (_checkAuth && !securityContext.IsAuthenticated)
|
|
|
|
{
|
|
|
|
context.Response.StatusCode = (int)HttpStatusCode.Forbidden;
|
2022-02-16 09:47:11 +00:00
|
|
|
return Task.CompletedTask;
|
2022-02-10 09:36:42 +00:00
|
|
|
}
|
2022-02-10 11:24:16 +00:00
|
|
|
|
2022-02-21 09:51:45 +00:00
|
|
|
var storage = storageFactory.GetStorage(tenantManager.GetCurrentTenant().Id.ToString(CultureInfo.InvariantCulture), _module);
|
2022-02-16 09:47:11 +00:00
|
|
|
var path = CrossPlatform.PathCombine(_path, GetRouteValue("pathInfo", context).Replace('/', Path.DirectorySeparatorChar));
|
2022-02-10 11:24:16 +00:00
|
|
|
var header = context.Request.Query[Constants.QueryHeader].FirstOrDefault() ?? "";
|
|
|
|
|
|
|
|
var auth = context.Request.Query[Constants.QueryAuth].FirstOrDefault() ?? "";
|
|
|
|
var storageExpire = storage.GetExpire(_domain);
|
|
|
|
|
|
|
|
if (storageExpire != TimeSpan.Zero && storageExpire != TimeSpan.MinValue && storageExpire != TimeSpan.MaxValue || !string.IsNullOrEmpty(auth))
|
|
|
|
{
|
|
|
|
var expire = context.Request.Query[Constants.QueryExpire];
|
2022-03-17 15:01:39 +00:00
|
|
|
if (string.IsNullOrEmpty(expire))
|
|
|
|
{
|
|
|
|
expire = storageExpire.TotalMinutes.ToString(CultureInfo.InvariantCulture);
|
|
|
|
}
|
2022-02-10 11:24:16 +00:00
|
|
|
|
|
|
|
var validateResult = emailValidationKeyProvider.ValidateEmailKey(path + "." + header + "." + expire, auth ?? "", TimeSpan.FromMinutes(Convert.ToDouble(expire)));
|
|
|
|
if (validateResult != EmailValidationKeyProvider.ValidationResult.Ok)
|
2021-09-05 19:59:14 +00:00
|
|
|
{
|
2022-02-10 11:24:16 +00:00
|
|
|
context.Response.StatusCode = (int)HttpStatusCode.Forbidden;
|
2022-01-11 15:37:19 +00:00
|
|
|
return Task.CompletedTask;
|
2021-09-05 19:59:14 +00:00
|
|
|
}
|
2022-02-10 11:24:16 +00:00
|
|
|
}
|
|
|
|
|
2022-03-25 09:23:28 +00:00
|
|
|
return InternalInvoke(context, storage, path, header);
|
|
|
|
}
|
2022-02-16 12:57:37 +00:00
|
|
|
|
2022-03-25 09:23:28 +00:00
|
|
|
private async Task InternalInvoke(HttpContext context, IDataStore storage, string path, string header)
|
|
|
|
{
|
|
|
|
if (!await storage.IsFileAsync(_domain, path))
|
|
|
|
{
|
2022-02-10 11:24:16 +00:00
|
|
|
context.Response.StatusCode = (int)HttpStatusCode.NotFound;
|
2022-03-25 09:23:28 +00:00
|
|
|
return;
|
2022-02-10 11:24:16 +00:00
|
|
|
}
|
|
|
|
|
2022-02-16 09:47:11 +00:00
|
|
|
var headers = header.Length > 0 ? header.Split('&').Select(HttpUtility.UrlDecode) : Array.Empty<string>();
|
2022-02-10 11:24:16 +00:00
|
|
|
|
|
|
|
if (storage.IsSupportInternalUri)
|
|
|
|
{
|
2022-03-25 09:23:28 +00:00
|
|
|
var uri = await storage.GetInternalUriAsync(_domain, path, TimeSpan.FromMinutes(15), headers);
|
2022-02-10 11:24:16 +00:00
|
|
|
|
|
|
|
//TODO
|
|
|
|
//context.Response.Cache.SetAllowResponseInBrowserHistory(false);
|
|
|
|
//context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
|
|
|
|
|
|
|
|
context.Response.Redirect(uri.ToString());
|
2022-03-25 09:23:28 +00:00
|
|
|
return;
|
2022-02-10 11:24:16 +00:00
|
|
|
}
|
2021-08-03 14:05:49 +00:00
|
|
|
|
2022-02-10 11:24:16 +00:00
|
|
|
string encoding = null;
|
2022-03-25 09:23:28 +00:00
|
|
|
if (storage is DiscDataStore && await storage.IsFileAsync(_domain, path + ".gz"))
|
2022-02-10 11:24:16 +00:00
|
|
|
{
|
|
|
|
path += ".gz";
|
|
|
|
encoding = "gzip";
|
2022-01-11 15:37:19 +00:00
|
|
|
}
|
2022-02-10 11:24:16 +00:00
|
|
|
|
|
|
|
var headersToCopy = new List<string> { "Content-Disposition", "Cache-Control", "Content-Encoding", "Content-Language", "Content-Type", "Expires" };
|
|
|
|
foreach (var h in headers)
|
2022-01-11 15:37:19 +00:00
|
|
|
{
|
2022-02-10 11:24:16 +00:00
|
|
|
var toCopy = headersToCopy.Find(x => h.StartsWith(x));
|
|
|
|
if (string.IsNullOrEmpty(toCopy))
|
2022-02-10 11:06:37 +00:00
|
|
|
{
|
2022-02-10 11:24:16 +00:00
|
|
|
continue;
|
2021-08-03 14:05:49 +00:00
|
|
|
}
|
|
|
|
|
2022-02-10 11:24:16 +00:00
|
|
|
context.Response.Headers[toCopy] = h.Substring(toCopy.Length + 1);
|
2022-01-11 15:37:19 +00:00
|
|
|
}
|
|
|
|
|
2022-02-10 11:24:16 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
context.Response.ContentType = MimeMapping.GetMimeMapping(path);
|
|
|
|
}
|
|
|
|
catch (Exception)
|
2022-01-11 15:37:19 +00:00
|
|
|
{
|
2020-10-28 12:26:27 +00:00
|
|
|
|
2022-02-10 11:24:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (encoding != null)
|
2020-07-29 21:57:58 +00:00
|
|
|
{
|
2022-02-10 11:24:16 +00:00
|
|
|
context.Response.Headers["Content-Encoding"] = encoding;
|
2020-08-24 18:41:06 +00:00
|
|
|
}
|
2020-07-29 21:57:58 +00:00
|
|
|
|
2022-02-23 19:42:34 +00:00
|
|
|
using (var stream = await storage.GetReadStreamAsync(_domain, path))
|
|
|
|
{
|
2022-02-10 11:24:16 +00:00
|
|
|
await stream.CopyToAsync(context.Response.Body);
|
2020-08-24 18:41:06 +00:00
|
|
|
}
|
2022-02-10 11:24:16 +00:00
|
|
|
|
|
|
|
await context.Response.Body.FlushAsync();
|
|
|
|
await context.Response.CompleteAsync();
|
2022-02-16 09:47:11 +00:00
|
|
|
}
|
2022-02-10 11:24:16 +00:00
|
|
|
|
2022-02-16 09:47:11 +00:00
|
|
|
private string GetRouteValue(string name, HttpContext context)
|
|
|
|
{
|
|
|
|
return (context.GetRouteValue(name) ?? "").ToString();
|
2022-02-10 11:24:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static class StorageHandlerExtensions
|
|
|
|
{
|
|
|
|
public static IEndpointRouteBuilder RegisterStorageHandler(this IEndpointRouteBuilder builder, string module, string domain, bool publicRoute = false)
|
|
|
|
{
|
|
|
|
var pathUtils = builder.ServiceProvider.GetService<PathUtils>();
|
|
|
|
var virtPath = pathUtils.ResolveVirtualPath(module, domain);
|
|
|
|
virtPath = virtPath.TrimStart('/');
|
|
|
|
|
2022-03-25 09:23:28 +00:00
|
|
|
var handler = new StorageHandler(string.Empty, module, domain, !publicRoute);
|
2022-02-10 11:24:16 +00:00
|
|
|
var url = virtPath + "{*pathInfo}";
|
|
|
|
|
|
|
|
if (!builder.DataSources.Any(r => r.Endpoints.Any(e => e.DisplayName == url)))
|
2020-08-31 08:18:07 +00:00
|
|
|
{
|
2022-03-25 09:23:28 +00:00
|
|
|
builder.MapGet(url, handler.Invoke);
|
2022-02-10 11:24:16 +00:00
|
|
|
|
|
|
|
var newUrl = url.Replace("{0}", "{t1}/{t2}/{t3}");
|
|
|
|
|
|
|
|
if (newUrl != url)
|
|
|
|
{
|
2022-03-25 09:23:28 +00:00
|
|
|
builder.MapGet(url, handler.Invoke);
|
2022-02-10 11:24:16 +00:00
|
|
|
}
|
2020-08-31 08:18:07 +00:00
|
|
|
}
|
2022-02-10 11:24:16 +00:00
|
|
|
|
|
|
|
return builder;
|
|
|
|
}
|
|
|
|
}
|