DocSpace-client/common/ASC.Data.Storage/WebPath.cs

254 lines
10 KiB
C#
Raw Normal View History

2019-06-04 14:43:20 +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.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
2020-01-21 12:44:05 +00:00
2020-02-17 08:58:14 +00:00
using ASC.Common;
2019-10-18 08:48:27 +00:00
using ASC.Common.Logging;
2019-08-15 12:04:42 +00:00
using ASC.Core;
using ASC.Core.Common.Settings;
2019-06-06 08:44:38 +00:00
using ASC.Data.Storage.Configuration;
2020-01-21 12:44:05 +00:00
2019-09-13 11:18:27 +00:00
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Hosting;
2019-10-18 08:48:27 +00:00
using Microsoft.Extensions.Options;
2019-06-04 14:43:20 +00:00
namespace ASC.Data.Storage
{
2019-09-13 11:18:27 +00:00
public class WebPathSettings
2019-06-04 14:43:20 +00:00
{
2019-09-13 11:18:27 +00:00
private readonly IEnumerable<Appender> Appenders;
2019-06-04 14:43:20 +00:00
2019-10-18 08:48:27 +00:00
public WebPathSettings(Configuration.Storage storage)
2019-06-04 14:43:20 +00:00
{
2019-10-18 08:48:27 +00:00
var section = storage;
2019-06-04 14:43:20 +00:00
if (section != null)
{
Appenders = section.Appender;
}
}
2019-11-06 15:03:09 +00:00
public string GetRelativePath(HttpContext httpContext, IOptionsMonitor<ILog> options, string absolutePath)
2019-06-04 14:43:20 +00:00
{
if (!Uri.IsWellFormedUriString(absolutePath, UriKind.Absolute))
{
throw new ArgumentException(string.Format("bad path format {0} is not absolute", absolutePath));
}
var appender = Appenders.FirstOrDefault(x => absolutePath.Contains(x.Append) || (absolutePath.Contains(x.AppendSecure) && !string.IsNullOrEmpty(x.AppendSecure)));
if (appender == null)
{
return absolutePath;
}
2019-10-18 08:48:27 +00:00
return SecureHelper.IsSecure(httpContext, options) && !string.IsNullOrEmpty(appender.AppendSecure) ?
2019-06-04 14:43:20 +00:00
absolutePath.Remove(0, appender.AppendSecure.Length) :
absolutePath.Remove(0, appender.Append.Length);
}
2019-11-06 15:03:09 +00:00
public string GetPath(HttpContext httpContext, IOptionsMonitor<ILog> options, string relativePath)
2019-06-04 14:43:20 +00:00
{
2019-08-08 13:35:22 +00:00
if (!string.IsNullOrEmpty(relativePath) && relativePath.IndexOf('~') == 0)
2019-06-04 14:43:20 +00:00
{
throw new ArgumentException(string.Format("bad path format {0} remove '~'", relativePath), "relativePath");
}
var result = relativePath;
var ext = Path.GetExtension(relativePath).ToLowerInvariant();
if (Appenders.Any())
{
2019-08-15 13:16:39 +00:00
var avaliableAppenders = Appenders.Where(x => x.Extensions != null && x.Extensions.Split('|').Contains(ext) || string.IsNullOrEmpty(ext)).ToList();
2019-06-04 14:43:20 +00:00
var avaliableAppendersCount = avaliableAppenders.LongCount();
Appender appender;
if (avaliableAppendersCount > 1)
{
appender = avaliableAppenders[(int)(relativePath.Length % avaliableAppendersCount)];
}
else if (avaliableAppendersCount == 1)
{
appender = avaliableAppenders.First();
}
else
{
appender = Appenders.First();
}
2019-08-08 13:35:22 +00:00
if (appender.Append.IndexOf('~') == 0)
2019-06-04 14:43:20 +00:00
{
var query = string.Empty;
//Rel path
if (relativePath.IndexOfAny(new[] { '?', '=', '&' }) != -1)
{
//Cut it
query = relativePath.Substring(relativePath.IndexOf('?'));
relativePath = relativePath.Substring(0, relativePath.IndexOf('?'));
}
//if (HostingEnvironment.IsHosted)
//{
// result = VirtualPathUtility.ToAbsolute(string.Format("{0}/{1}{2}", appender.Append.TrimEnd('/'), relativePath.TrimStart('/'), query));
//}
//else
//{
2019-08-15 12:04:42 +00:00
result = string.Format("{0}/{1}{2}", appender.Append.TrimEnd('/').TrimStart('~'), relativePath.TrimStart('/'), query);
2019-06-04 14:43:20 +00:00
//}
}
else
{
//TODO HostingEnvironment.IsHosted
2019-10-18 08:48:27 +00:00
if (SecureHelper.IsSecure(httpContext, options) && !string.IsNullOrEmpty(appender.AppendSecure))
2019-06-04 14:43:20 +00:00
{
result = string.Format("{0}/{1}", appender.AppendSecure.TrimEnd('/'), relativePath.TrimStart('/'));
}
else
{
//Append directly
result = string.Format("{0}/{1}", appender.Append.TrimEnd('/'), relativePath.TrimStart('/'));
}
}
}
2020-01-21 12:44:05 +00:00
return result;
2019-06-04 14:43:20 +00:00
}
2019-09-13 11:18:27 +00:00
}
public class WebPath
{
private static readonly IDictionary<string, bool> Existing = new ConcurrentDictionary<string, bool>();
public WebPathSettings WebPathSettings { get; }
public StaticUploader StaticUploader { get; }
public SettingsManager SettingsManager { get; }
public StorageSettingsHelper StorageSettingsHelper { get; }
2019-09-13 11:18:27 +00:00
public IHttpContextAccessor HttpContextAccessor { get; }
public IHostEnvironment HostEnvironment { get; }
2019-09-18 15:19:30 +00:00
public CoreBaseSettings CoreBaseSettings { get; }
2019-11-06 15:03:09 +00:00
public IOptionsMonitor<ILog> Options { get; }
2019-09-13 11:18:27 +00:00
public WebPath(
2019-10-18 08:48:27 +00:00
WebPathSettings webPathSettings,
StaticUploader staticUploader,
SettingsManager settingsManager,
StorageSettingsHelper storageSettingsHelper,
2019-09-13 11:18:27 +00:00
IHttpContextAccessor httpContextAccessor,
IHostEnvironment hostEnvironment,
2019-10-18 08:48:27 +00:00
CoreBaseSettings coreBaseSettings,
2019-11-06 15:03:09 +00:00
IOptionsMonitor<ILog> options)
2019-09-13 11:18:27 +00:00
{
WebPathSettings = webPathSettings;
StaticUploader = staticUploader;
SettingsManager = settingsManager;
StorageSettingsHelper = storageSettingsHelper;
2019-09-13 11:18:27 +00:00
HttpContextAccessor = httpContextAccessor;
HostEnvironment = hostEnvironment;
2019-09-18 15:19:30 +00:00
CoreBaseSettings = coreBaseSettings;
2019-10-18 08:48:27 +00:00
Options = options;
2019-09-13 11:18:27 +00:00
}
public string GetPath(string relativePath)
{
if (!string.IsNullOrEmpty(relativePath) && relativePath.IndexOf('~') == 0)
{
throw new ArgumentException(string.Format("bad path format {0} remove '~'", relativePath), "relativePath");
}
2019-09-18 15:19:30 +00:00
if (CoreBaseSettings.Standalone && StaticUploader.CanUpload())
2019-09-13 11:18:27 +00:00
{
try
{
var result = StorageSettingsHelper.DataStore(SettingsManager.Load<CdnStorageSettings>()).GetInternalUri("", relativePath, TimeSpan.Zero, null).AbsoluteUri.ToLower();
2019-09-13 11:18:27 +00:00
if (!string.IsNullOrEmpty(result)) return result;
}
catch (Exception)
{
}
}
2019-10-18 08:48:27 +00:00
return WebPathSettings.GetPath(HttpContextAccessor.HttpContext, Options, relativePath);
2019-09-13 11:18:27 +00:00
}
2019-06-04 14:43:20 +00:00
2019-09-13 11:18:27 +00:00
public bool Exists(string relativePath)
2019-06-04 14:43:20 +00:00
{
var path = GetPath(relativePath);
if (!Existing.ContainsKey(path))
{
2019-09-13 11:18:27 +00:00
if (Uri.IsWellFormedUriString(path, UriKind.Relative) && HttpContextAccessor?.HttpContext != null)
2019-06-04 14:43:20 +00:00
{
//Local
Existing[path] = File.Exists(Path.Combine(HostEnvironment.ContentRootPath, path));
2019-06-04 14:43:20 +00:00
}
if (Uri.IsWellFormedUriString(path, UriKind.Absolute))
{
//Make request
Existing[path] = CheckWebPath(path);
}
}
return Existing[path];
}
2019-09-13 11:18:27 +00:00
private bool CheckWebPath(string path)
2019-06-04 14:43:20 +00:00
{
try
{
var request = (HttpWebRequest)WebRequest.Create(path);
request.Method = "HEAD";
2019-08-15 15:08:40 +00:00
using var resp = (HttpWebResponse)request.GetResponse();
return resp.StatusCode == HttpStatusCode.OK;
2019-06-04 14:43:20 +00:00
}
catch (Exception)
{
return false;
}
}
}
2019-10-31 11:28:30 +00:00
public static class WebPathExtension
{
2020-02-17 08:58:14 +00:00
public static DIHelper AddWebPathService(this DIHelper services)
2019-10-31 11:28:30 +00:00
{
services.TryAddScoped<WebPath>();
return services
.AddStaticUploaderService()
.AddCdnStorageSettingsService()
.AddWebPathSettingsService()
2020-02-17 08:58:14 +00:00
.AddCoreBaseSettingsService();
2019-10-31 11:28:30 +00:00
}
2020-02-17 08:58:14 +00:00
public static DIHelper AddWebPathSettingsService(this DIHelper services)
2019-10-31 11:28:30 +00:00
{
services.TryAddSingleton<WebPathSettings>();
return services.AddStorage();
}
}
2019-06-04 14:43:20 +00:00
}