/** * * (c) Copyright Ascensio System SIA 2020 * * 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; using System.Collections.Generic; using System.IO; using System.Web; using System.Web.Configuration; using System.Web.Script.Serialization; using System.Web.UI; using ASC.Api.DocumentConverter; namespace OnlineEditorsExample { public partial class DocEditor : Page { public static string FileName; public static string FileUri { get { return _Default.FileUri(FileName, true); } } public static string FileUriUser { get { return _Default.FileUri(FileName, false); } } protected string Key { get { return ServiceConverter.GenerateRevisionId(_Default.CurUserHostAddress(null) + "/" + Path.GetFileName(FileUri) + "/" + File.GetLastWriteTime(_Default.StoragePath(FileName, null)).GetHashCode()); } } protected string DocServiceApiUri { get { return (WebConfigurationManager.AppSettings["files.docservice.url.site"] ?? string.Empty) + (WebConfigurationManager.AppSettings["files.docservice.url.api"] ?? string.Empty); } } protected string DocConfig { get; private set; } protected string History { get; private set; } protected string HistoryData { get; private set; } protected string InsertImageConfig { get; private set; } protected string compareFileData { get; private set; } protected string dataMailMergeRecipients { get; private set; } protected string documentType { get { return _Default.DocumentType(FileName); } } public static string CallbackUrl { get { var callbackUrl = new UriBuilder(_Default.GetServerUrl(true)); callbackUrl.Path = HttpRuntime.AppDomainAppVirtualPath + (HttpRuntime.AppDomainAppVirtualPath.EndsWith("/") ? "" : "/") + "webeditor.ashx"; callbackUrl.Query = "type=track" + "&fileName=" + HttpUtility.UrlEncode(FileName) + "&userAddress=" + HttpUtility.UrlEncode(HttpContext.Current.Request.UserHostAddress); return callbackUrl.ToString(); } } protected void Page_Load(object sender, EventArgs e) { var externalUrl = Request["fileUrl"]; if (!string.IsNullOrEmpty(externalUrl)) { FileName = _Default.DoUpload(externalUrl, Request); } else { FileName = Path.GetFileName(Request["fileID"]); } var type = Request["type"]; if (!string.IsNullOrEmpty(type)) { Try(type, Request["sample"], Request); Response.Redirect("doceditor.aspx?fileID=" + HttpUtility.UrlEncode(FileName)); } var ext = Path.GetExtension(FileName); var editorsMode = Request.GetOrDefault("editorsMode", "edit"); var canEdit = _Default.EditedExts.Contains(ext); var mode = canEdit && editorsMode != "view" ? "edit" : "view"; var submitForm = canEdit && (editorsMode.Equals("edit") || editorsMode.Equals("fillForms")); var userId = Request.Cookies.GetOrDefault("uid", "uid-1"); var uname = userId.Equals("uid-0") ? null : Request.Cookies.GetOrDefault("uname", "John Smith"); string userGroup = null; List reviewGroups = null; if (userId.Equals("uid-2")) { userGroup = "group-2"; reviewGroups = new List() { "group-2", "" }; } if (userId.Equals("uid-3")) { userGroup = "group-3"; reviewGroups = new List() { "group-2" }; } var jss = new JavaScriptSerializer(); object favorite = null; if (!string.IsNullOrEmpty(Request.Cookies.GetOrDefault("uid", null))) { favorite = Request.Cookies.GetOrDefault("uid", null).Equals("uid-2"); } var actionLink = Request.GetOrDefault("actionLink", null); var actionData = string.IsNullOrEmpty(actionLink) ? null : jss.DeserializeObject(actionLink); var config = new Dictionary { { "type", Request.GetOrDefault("editorsType", "desktop") }, { "documentType", documentType }, { "document", new Dictionary { { "title", FileName }, { "url", FileUri }, { "fileType", ext.Trim('.') }, { "key", Key }, { "info", new Dictionary { { "owner", "Me" }, { "uploaded", DateTime.Now.ToShortDateString() }, { "favorite", favorite } } }, { "permissions", new Dictionary { { "comment", editorsMode != "view" && editorsMode != "fillForms" && editorsMode != "embedded" && editorsMode != "blockcontent"}, { "download", true }, { "edit", canEdit && (editorsMode == "edit" || editorsMode == "filter") || editorsMode == "blockcontent" }, { "fillForms", editorsMode != "view" && editorsMode != "comment" && editorsMode != "embedded" && editorsMode != "blockcontent" }, { "modifyFilter", editorsMode != "filter" }, { "modifyContentControl", editorsMode != "blockcontent" }, { "review", editorsMode == "edit" || editorsMode == "review" }, { "reviewGroups", reviewGroups } } } } }, { "editorConfig", new Dictionary { { "actionLink", actionData }, { "mode", mode }, { "lang", Request.Cookies.GetOrDefault("ulang", "en") }, { "callbackUrl", CallbackUrl }, { "user", new Dictionary { { "id", userId }, { "name", uname }, { "group", userGroup } } }, { "embedded", new Dictionary { { "saveUrl", FileUriUser }, { "embedUrl", FileUriUser }, { "shareUrl", FileUriUser }, { "toolbarDocked", "top" } } }, { "customization", new Dictionary { { "about", true }, { "feedback", true }, { "forcesave", true }, { "submitForm", submitForm }, { "goback", new Dictionary { { "url", _Default.GetServerUrl(false) + "default.aspx" } } } } } } } }; if (JwtManager.Enabled) { var token = JwtManager.Encode(config); config.Add("token", token); } DocConfig = jss.Serialize(config); try { Dictionary logoConfig = GetLogoConfig(); InsertImageConfig = jss.Serialize(logoConfig).Replace("{", "").Replace("}", ""); Dictionary compareFile = GetCompareFile(); compareFileData = jss.Serialize(compareFile); Dictionary mailMergeConfig = GetMailMergeConfig(); dataMailMergeRecipients = jss.Serialize(mailMergeConfig); Dictionary hist; Dictionary histData; GetHistory(out hist, out histData); if (hist != null && histData != null) { History = jss.Serialize(hist); HistoryData = jss.Serialize(histData); } } catch { } } private void GetHistory(out Dictionary history, out Dictionary historyData) { var jss = new JavaScriptSerializer(); var histDir = _Default.HistoryDir(_Default.StoragePath(FileName, null)); history = null; historyData = null; if (_Default.GetFileVersion(histDir) > 0) { var currentVersion = _Default.GetFileVersion(histDir); var hist = new List>(); var histData = new Dictionary(); for (var i = 1; i <= currentVersion; i++) { var obj = new Dictionary(); var dataObj = new Dictionary(); var verDir = _Default.VersionDir(histDir, i); var key = i == currentVersion ? Key : File.ReadAllText(Path.Combine(verDir, "key.txt")); obj.Add("key", key); obj.Add("version", i); if (i == 1) { var infoPath = Path.Combine(histDir, "createdInfo.json"); if (File.Exists(infoPath)) { var info = jss.Deserialize>(File.ReadAllText(infoPath)); obj.Add("created", info["created"]); obj.Add("user", new Dictionary() { { "id", info["id"] }, { "name", info["name"] }, }); } } dataObj.Add("key", key); dataObj.Add("url", i == currentVersion ? FileUri : MakePublicUrl(Directory.GetFiles(verDir, "prev.*")[0])); dataObj.Add("version", i); if (i > 1) { var changes = jss.Deserialize>(File.ReadAllText(Path.Combine(_Default.VersionDir(histDir, i - 1), "changes.json"))); var change = ((Dictionary)((ArrayList)changes["changes"])[0]); obj.Add("changes", changes["changes"]); obj.Add("serverVersion", changes["serverVersion"]); obj.Add("created", change["created"]); obj.Add("user", change["user"]); var prev = (Dictionary)histData[(i - 2).ToString()]; dataObj.Add("previous", new Dictionary() { { "key", prev["key"] }, { "url", prev["url"] }, }); dataObj.Add("changesUrl", MakePublicUrl(Path.Combine(_Default.VersionDir(histDir, i - 1), "diff.zip"))); } if (JwtManager.Enabled) { var token = JwtManager.Encode(dataObj); dataObj.Add("token", token); } hist.Add(obj); histData.Add((i - 1).ToString(), dataObj); } history = new Dictionary() { { "currentVersion", currentVersion }, { "history", hist } }; historyData = histData; } } private Dictionary GetLogoConfig() { var InsertImageUrl = new UriBuilder(_Default.GetServerUrl(true)); InsertImageUrl.Path = HttpRuntime.AppDomainAppVirtualPath + (HttpRuntime.AppDomainAppVirtualPath.EndsWith("/") ? "" : "/") + "App_Themes\\images\\logo.png"; Dictionary logoConfig = new Dictionary { { "fileType", "png"}, { "url", InsertImageUrl.ToString()} }; if (JwtManager.Enabled) { var insImageToken = JwtManager.Encode(logoConfig); logoConfig.Add("token", insImageToken); } return logoConfig; } private Dictionary GetCompareFile() { var compareFileUrl = new UriBuilder(_Default.GetServerUrl(true)); compareFileUrl.Path = HttpRuntime.AppDomainAppVirtualPath + (HttpRuntime.AppDomainAppVirtualPath.EndsWith("/") ? "" : "/") + "webeditor.ashx"; compareFileUrl.Query = "type=assets&fileName=" + HttpUtility.UrlEncode("sample.docx"); Dictionary dataCompareFile = new Dictionary { { "fileType", "docx" }, { "url", compareFileUrl.ToString() } }; if (JwtManager.Enabled) { var compareFileToken = JwtManager.Encode(dataCompareFile); dataCompareFile.Add("token", compareFileToken); } return dataCompareFile; } private Dictionary GetMailMergeConfig() { var mailmergeUrl = new UriBuilder(_Default.GetServerUrl(true)); mailmergeUrl.Path = HttpRuntime.AppDomainAppVirtualPath + (HttpRuntime.AppDomainAppVirtualPath.EndsWith("/") ? "" : "/") + "webeditor.ashx"; mailmergeUrl.Query = "type=csv"; Dictionary mailMergeConfig = new Dictionary { { "fileType", "csv" }, { "url", mailmergeUrl.ToString() } }; if (JwtManager.Enabled) { var mailmergeToken = JwtManager.Encode(mailMergeConfig); mailMergeConfig.Add("token", mailmergeToken); } return mailMergeConfig; } private string MakePublicUrl(string fullPath) { var root = HttpRuntime.AppDomainAppPath + WebConfigurationManager.AppSettings["storage-path"]; return _Default.GetServerUrl(true) + fullPath.Substring(root.Length).Replace(Path.DirectorySeparatorChar, '/'); } private static void Try(string type, string sample, HttpRequest request) { string ext; switch (type) { case "word": ext = ".docx"; break; case "cell": ext = ".xlsx"; break; case "slide": ext = ".pptx"; break; default: return; } var demoName = (string.IsNullOrEmpty(sample) ? "new" : "sample") + ext; var demoPath = "assets\\" + (string.IsNullOrEmpty(sample) ? "new\\" : "sample\\"); FileName = _Default.GetCorrectName(demoName); var filePath = _Default.StoragePath(FileName, null); File.Copy(HttpRuntime.AppDomainAppPath + demoPath + demoName, filePath); CreateMeta(FileName, request.Cookies.GetOrDefault("uid", "uid-1"), request.Cookies.GetOrDefault("uname", "John Smith"), null); } public static void CreateMeta(string fileName, string uid, string uname, string userAddress) { var histDir = _Default.HistoryDir(_Default.StoragePath(fileName, userAddress)); Directory.CreateDirectory(histDir); File.WriteAllText(Path.Combine(histDir, "createdInfo.json"), new JavaScriptSerializer().Serialize(new Dictionary { { "created", DateTime.Now.ToString("yyyy'-'MM'-'dd HH':'mm':'ss") }, { "id", uid }, { "name", uname } })); } } }