private static void Track(HttpContext context)
        {
            var userAddress = context.Request["userAddress"];
            var fileName    = context.Request["fileName"];

            string body;

            try
            {
                using (var receiveStream = context.Request.InputStream)
                    using (var readStream = new StreamReader(receiveStream))
                    {
                        body = readStream.ReadToEnd();
                    }
            }
            catch (Exception e)
            {
                throw new HttpException((int)HttpStatusCode.BadRequest, e.Message);
            }

            var jss = new JavaScriptSerializer();

            if (string.IsNullOrEmpty(body))
            {
                return;
            }
            var fileData = jss.Deserialize <Dictionary <string, object> >(body);

            if (JwtManager.Enabled)
            {
                if (fileData.ContainsKey("token"))
                {
                    fileData = jss.Deserialize <Dictionary <string, object> >(JwtManager.Decode(fileData["token"].ToString()));
                }
                else if (context.Request.Headers.AllKeys.Contains("Authorization", StringComparer.InvariantCultureIgnoreCase))
                {
                    var headerToken = context.Request.Headers.Get("Authorization").Substring("Bearer ".Length);
                    fileData = (Dictionary <string, object>)jss.Deserialize <Dictionary <string, object> >(JwtManager.Decode(headerToken))["payload"];
                }
                else
                {
                    throw new Exception("Expected JWT");
                }
            }

            var status = (TrackerStatus)(int)fileData["status"];

            switch (status)
            {
            case TrackerStatus.MustSave:
            case TrackerStatus.Corrupted:
                var downloadUri = (string)fileData["url"];

                var curExt      = Path.GetExtension(fileName);
                var downloadExt = Path.GetExtension(downloadUri) ?? "";
                if (!downloadExt.Equals(curExt, StringComparison.InvariantCultureIgnoreCase))
                {
                    var key = ServiceConverter.GenerateRevisionId(downloadUri);

                    try
                    {
                        string newFileUri;
                        ServiceConverter.GetConvertedUri(downloadUri, downloadExt, curExt, key, false, out newFileUri);
                        downloadUri = newFileUri;
                    }
                    catch (Exception ex)
                    {
                        fileName = _Default.GetCorrectName(Path.GetFileNameWithoutExtension(fileName) + downloadExt, userAddress);
                    }
                }

                // hack. http://ubuntuforums.org/showthread.php?t=1841740
                if (_Default.IsMono)
                {
                    ServicePointManager.ServerCertificateValidationCallback += (s, ce, ca, p) => true;
                }

                var saved = 1;
                try
                {
                    var storagePath = _Default.StoragePath(fileName, userAddress);
                    var histDir     = _Default.HistoryDir(storagePath);
                    var versionDir  = _Default.VersionDir(histDir, _Default.GetFileVersion(histDir) + 1);

                    if (!Directory.Exists(versionDir))
                    {
                        Directory.CreateDirectory(versionDir);
                    }

                    File.Copy(storagePath, Path.Combine(versionDir, "prev" + curExt));

                    DownloadToFile(downloadUri, _Default.StoragePath(fileName, userAddress));
                    DownloadToFile((string)fileData["changesurl"], Path.Combine(versionDir, "diff.zip"));

                    var hist = fileData.ContainsKey("changeshistory") ? (string)fileData["changeshistory"] : null;
                    if (string.IsNullOrEmpty(hist) && fileData.ContainsKey("history"))
                    {
                        hist = jss.Serialize(fileData["history"]);
                    }

                    if (!string.IsNullOrEmpty(hist))
                    {
                        File.WriteAllText(Path.Combine(versionDir, "changes.json"), hist);
                    }

                    File.WriteAllText(Path.Combine(versionDir, "key.txt"), (string)fileData["key"]);
                }
                catch (Exception)
                {
                    saved = 0;
                }

                break;
            }
            context.Response.Write("{\"error\":0}");
        }
Esempio n. 2
0
        protected void Page_Load(object sender, EventArgs e)
        {
            var externalUrl = Request["fileUrl"];

            if (!string.IsNullOrEmpty(externalUrl))
            {
                FileName = _Default.DoUpload(externalUrl, Request);
            }
            else
            {
                FileName = 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 jss = new JavaScriptSerializer();

            var actionLink = Request.GetOrDefault("actionLink", null);
            var actionData = string.IsNullOrEmpty(actionLink) ? null : jss.DeserializeObject(actionLink);

            var config = new Dictionary <string, object>
            {
                { "type", Request.GetOrDefault("editorsType", "desktop") },
                { "documentType", _Default.DocumentType(FileName) },
                {
                    "document", new Dictionary <string, object>
                    {
                        { "title", FileName },
                        { "url", FileUri },
                        { "fileType", ext.Trim('.') },
                        { "key", Key },
                        {
                            "info", new Dictionary <string, object>
                            {
                                { "author", "Me" },
                                { "created", DateTime.Now.ToShortDateString() }
                            }
                        },
                        {
                            "permissions", new Dictionary <string, object>
                            {
                                { "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" }
                            }
                        }
                    }
                },
                {
                    "editorConfig", new Dictionary <string, object>
                    {
                        { "actionLink", actionData },
                        { "mode", mode },
                        { "lang", Request.Cookies.GetOrDefault("ulang", "en") },
                        { "callbackUrl", CallbackUrl },
                        {
                            "user", new Dictionary <string, object>
                            {
                                { "id", Request.Cookies.GetOrDefault("uid", "uid-1") },
                                { "name", Request.Cookies.GetOrDefault("uname", "John Smith") }
                            }
                        },
                        {
                            "embedded", new Dictionary <string, object>
                            {
                                { "saveUrl", FileUri },
                                { "embedUrl", FileUri },
                                { "shareUrl", FileUri },
                                { "toolbarDocked", "top" }
                            }
                        },
                        {
                            "customization", new Dictionary <string, object>
                            {
                                { "about", true },
                                { "feedback", true },
                                {
                                    "goback", new Dictionary <string, object>
                                    {
                                        { "url", _Default.Host + "default.aspx" }
                                    }
                                }
                            }
                        }
                    }
                }
            };

            if (JwtManager.Enabled)
            {
                var token = JwtManager.Encode(config);
                config.Add("token", token);
            }

            DocConfig = jss.Serialize(config);

            try
            {
                Dictionary <string, object> hist;
                Dictionary <string, object> histData;
                GetHistory(out hist, out histData);
                if (hist != null && histData != null)
                {
                    History     = jss.Serialize(hist);
                    HistoryData = jss.Serialize(histData);
                }
            }
            catch { }
        }