/** * * (c) Copyright Ascensio System SIA 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. * */ using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text.RegularExpressions; using System.Web; using System.Web.Configuration; using System.Web.Script.Serialization; using OnlineEditorsExampleMVC.Models; namespace OnlineEditorsExampleMVC.Helpers { public class DocManagerHelper { public static long MaxFileSize { get { long size; long.TryParse(WebConfigurationManager.AppSettings["filesize-max"], out size); return size > 0 ? size : 5 * 1024 * 1024; } } public static List FileExts { get { return ViewedExts.Concat(EditedExts).Concat(ConvertExts).ToList(); } } public static List ViewedExts { get { return (WebConfigurationManager.AppSettings["files.docservice.viewed-docs"] ?? "").Split(new char[] { '|', ',' }, StringSplitOptions.RemoveEmptyEntries).ToList(); } } public static List EditedExts { get { return (WebConfigurationManager.AppSettings["files.docservice.edited-docs"] ?? "").Split(new char[] { '|', ',' }, StringSplitOptions.RemoveEmptyEntries).ToList(); } } public static List ConvertExts { get { return (WebConfigurationManager.AppSettings["files.docservice.convert-docs"] ?? "").Split(new char[] { '|', ',' }, StringSplitOptions.RemoveEmptyEntries).ToList(); } } public static string CurUserHostAddress(string userAddress = null) { return Regex.Replace(userAddress ?? HttpContext.Current.Request.UserHostAddress, "[^0-9a-zA-Z.=]", "_"); } public static string StoragePath(string fileName, string userAddress = null) { var directory = HttpRuntime.AppDomainAppPath + CurUserHostAddress(userAddress) + "\\"; if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } return directory + Path.GetFileName(fileName); } public static string ForcesavePath(string fileName, string userAddress, Boolean create) { var directory = HttpRuntime.AppDomainAppPath + CurUserHostAddress(userAddress) + "\\"; if (!Directory.Exists(directory)) { return ""; } directory = directory + Path.GetFileName(fileName) + "-hist" + "\\"; if (!Directory.Exists(directory)) { if (create) { Directory.CreateDirectory(directory); } else { return ""; } } directory = directory + Path.GetFileName(fileName); if (!File.Exists(directory)) { if (!create) { return ""; } } return directory; } public static string HistoryDir(string storagePath) { return storagePath += "-hist"; } public static string VersionDir(string histPath, int version) { return Path.Combine(histPath, version.ToString()); } public static string VersionDir(string fileName, string userAddress, int version) { return VersionDir(HistoryDir(StoragePath(fileName, userAddress)), version); } public static int GetFileVersion(string historyPath) { if (!Directory.Exists(historyPath)) return 0; return Directory.EnumerateDirectories(historyPath).Count() + 1; } public static int GetFileVersion(string fileName, string userAddress) { return GetFileVersion(HistoryDir(StoragePath(fileName, userAddress))); } public static string GetCorrectName(string fileName, string userAddress = null) { var baseName = Path.GetFileNameWithoutExtension(fileName); var ext = Path.GetExtension(fileName).ToLower(); var name = baseName + ext; for (var i = 1; File.Exists(StoragePath(name, userAddress)); i++) { name = baseName + " (" + i + ")" + ext; } return name; } public static List GetStoredFiles() { var directory = HttpRuntime.AppDomainAppPath + WebConfigurationManager.AppSettings["storage-path"] + CurUserHostAddress(null) + "\\"; if (!Directory.Exists(directory)) return new List(); var directoryInfo = new DirectoryInfo(directory); List storedFiles = directoryInfo.GetFiles("*.*", SearchOption.TopDirectoryOnly).ToList(); return storedFiles; } public static string CreateDemo(string fileExt, bool withContent) { var demoName = (withContent ? "sample." : "new.") + fileExt; var demoPath = "assets\\" + (withContent ? "sample\\" : "new\\"); var fileName = GetCorrectName(demoName); File.Copy(HttpRuntime.AppDomainAppPath + demoPath + demoName, StoragePath(fileName)); return fileName; } public static void CreateMeta(string fileName, string uid, string uname, string userAddress = null) { var histDir = HistoryDir(StoragePath(fileName, userAddress)); Directory.CreateDirectory(histDir); var id = string.IsNullOrEmpty(uid) ? "uid-1" : uid; var name = string.IsNullOrEmpty(uname) ? "John Smith" : uname; File.WriteAllText(Path.Combine(histDir, "createdInfo.json"), new JavaScriptSerializer().Serialize(new Dictionary { { "created", DateTime.Now.ToString("yyyy'-'MM'-'dd HH':'mm':'ss") }, { "id", id }, { "name", id.Equals("uid-0") ? null : name } })); } public static string GetFileUri(string fileName, Boolean forDocumentServer) { var uri = new UriBuilder(GetServerUrl(forDocumentServer)) { Path = HttpRuntime.AppDomainAppVirtualPath + "/" + CurUserHostAddress() + "/" + fileName, Query = "" }; return uri.ToString(); } public static string GetPathUri(string path) { var uri = new UriBuilder(GetServerUrl(true)) { Path = HttpRuntime.AppDomainAppVirtualPath + "/" + path, Query = "" }; return uri.ToString(); } public static string GetServerUrl(Boolean forDocumentServer) { if (forDocumentServer && !WebConfigurationManager.AppSettings["files.docservice.url.example"].Equals("")) { return WebConfigurationManager.AppSettings["files.docservice.url.example"]; } else { var uri = new UriBuilder(HttpContext.Current.Request.Url) { Query = "" }; var requestHost = HttpContext.Current.Request.Headers["Host"]; if (!string.IsNullOrEmpty(requestHost)) uri = new UriBuilder(uri.Scheme + "://" + requestHost); return uri.ToString(); } } public static string GetCallback(string fileName) { var callbackUrl = new UriBuilder(GetServerUrl(true)) { Path = HttpRuntime.AppDomainAppVirtualPath + (HttpRuntime.AppDomainAppVirtualPath.EndsWith("/") ? "" : "/") + "webeditor.ashx", Query = "type=track" + "&fileName=" + HttpUtility.UrlEncode(fileName) + "&userAddress=" + HttpUtility.UrlEncode(HttpContext.Current.Request.UserHostAddress) }; return callbackUrl.ToString(); } public static string GetCreateUrl(FileUtility.FileType fileType) { var createUrl = new UriBuilder(GetServerUrl(false)) { Path = HttpRuntime.AppDomainAppVirtualPath + (HttpRuntime.AppDomainAppVirtualPath.EndsWith("/") ? "" : "/") + "Sample", Query = "fileExt=" + DocManagerHelper.GetInternalExtension(fileType).Trim('.') }; return createUrl.ToString(); } public static string GetInternalExtension(FileUtility.FileType fileType) { switch (fileType) { case FileUtility.FileType.Word: return ".docx"; case FileUtility.FileType.Cell: return ".xlsx"; case FileUtility.FileType.Slide: return ".pptx"; default: return ".docx"; } } public static List> GetFilesInfo(string fileId = null) { var files = new List>(); foreach (var file in GetStoredFiles()) { var dictionary = new Dictionary(); dictionary.Add("version", GetFileVersion(file.Name, null)); dictionary.Add("id", ServiceConverter.GenerateRevisionId(DocManagerHelper.CurUserHostAddress() + "/" + file.Name + "/" + File.GetLastWriteTime(DocManagerHelper.StoragePath(file.Name, null)).GetHashCode())); dictionary.Add("contentLength", Math.Round(file.Length / 1024.0, 2) + " KB"); dictionary.Add("pureContentLength", file.Length); dictionary.Add("title", file.Name); dictionary.Add("updated", file.LastWriteTime.ToString()); if (fileId != null) { if (fileId.Equals(dictionary["id"])) { files.Add(dictionary); break; } } else { files.Add(dictionary); } } return files; } } }