Ejemplo n.º 1
0
        // file saving process
        public static int processSave(Dictionary <string, object> fileData, string fileName, string userAddress)
        {
            if (fileData["url"].Equals(null))
            {
                throw new Exception("DownloadUrl is null");
            }
            var    downloadUri = (string)fileData["url"];
            string curExt      = Path.GetExtension(fileName).ToLower(); // get current file extension

            var downloadExt = fileData.ContainsKey("filetype")
                ? "." + (string)fileData["filetype"]
                : Path.GetExtension(downloadUri).ToLower() ?? ""; // TODO: Delete in version 7.0 or higher. Support for versions below 7.0

            var newFileName = fileName;

            // convert downloaded file to the file with the current extension if these extensions aren't equal
            if (!curExt.Equals(downloadExt, StringComparison.InvariantCultureIgnoreCase))
            {
                try
                {
                    // convert file and give url to a new file
                    string newFileUri;
                    var    result = ServiceConverter.GetConvertedUri(downloadUri, downloadExt, curExt, ServiceConverter.GenerateRevisionId(downloadUri), false, out newFileUri);
                    if (string.IsNullOrEmpty(newFileUri))
                    {
                        // get the correct file name if it already exists
                        newFileName = DocManagerHelper.GetCorrectName(Path.GetFileNameWithoutExtension(fileName) + downloadExt, userAddress);
                    }
                    else
                    {
                        downloadUri = newFileUri;
                    }
                }
                catch (Exception)
                {
                    newFileName = DocManagerHelper.GetCorrectName(Path.GetFileNameWithoutExtension(fileName) + downloadExt, userAddress);
                }
            }

            DocManagerHelper.VerifySSL();

            var storagePath = DocManagerHelper.StoragePath(newFileName, userAddress); // get the file path
            var histDir     = DocManagerHelper.HistoryDir(storagePath);               // get the path to the history directory

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

            var versionDir = DocManagerHelper.VersionDir(histDir, DocManagerHelper.GetFileVersion(histDir));  // get the path to the file version

            if (!Directory.Exists(versionDir))
            {
                Directory.CreateDirectory(versionDir);                                 // if the path doesn't exist, create it
            }
            // get the path to the previous file version and move it to the storage directory
            File.Move(DocManagerHelper.StoragePath(fileName, userAddress), Path.Combine(versionDir, "prev" + curExt));

            DownloadToFile(downloadUri, storagePath);                                             // save file to the storage directory
            DownloadToFile((string)fileData["changesurl"], Path.Combine(versionDir, "diff.zip")); // save file changes to the diff.zip archive

            var hist = fileData.ContainsKey("changeshistory") ? (string)fileData["changeshistory"] : null;

            if (string.IsNullOrEmpty(hist) && fileData.ContainsKey("history"))
            {
                var jss = new JavaScriptSerializer();
                hist = jss.Serialize(fileData["history"]);
            }

            if (!string.IsNullOrEmpty(hist))
            {
                File.WriteAllText(Path.Combine(versionDir, "changes.json"), hist);  // write the history changes to the changes.json file
            }

            File.WriteAllText(Path.Combine(versionDir, "key.txt"), (string)fileData["key"]);        // write the key value to the key.txt file

            string forcesavePath = DocManagerHelper.ForcesavePath(newFileName, userAddress, false); // get the path to the forcesaved file version

            if (!forcesavePath.Equals(""))                                                          // if the forcesaved file version exists
            {
                File.Delete(forcesavePath);                                                         // remove it
            }

            return(0);
        }
        public static int processSave(Dictionary <string, object> fileData, string fileName, string userAddress)
        {
            var    downloadUri = (string)fileData["url"];
            string curExt      = Path.GetExtension(fileName);
            string downloadExt = Path.GetExtension(downloadUri) ?? "";
            var    newFileName = fileName;

            if (!curExt.Equals(downloadExt, StringComparison.InvariantCultureIgnoreCase))
            {
                try
                {
                    string newFileUri;
                    var    result = ServiceConverter.GetConvertedUri(downloadUri, downloadExt, curExt, ServiceConverter.GenerateRevisionId(downloadUri), false, out newFileUri);
                    if (string.IsNullOrEmpty(newFileUri))
                    {
                        newFileName = DocManagerHelper.GetCorrectName(Path.GetFileNameWithoutExtension(fileName) + downloadExt, userAddress);
                    }
                    else
                    {
                        downloadUri = newFileUri;
                    }
                }
                catch (Exception)
                {
                    newFileName = DocManagerHelper.GetCorrectName(Path.GetFileNameWithoutExtension(fileName) + downloadExt, userAddress);
                }
            }

            var storagePath = DocManagerHelper.StoragePath(newFileName, userAddress);
            var histDir     = DocManagerHelper.HistoryDir(storagePath);

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

            var versionDir = DocManagerHelper.VersionDir(histDir, DocManagerHelper.GetFileVersion(histDir));

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

            File.Move(DocManagerHelper.StoragePath(fileName, userAddress), Path.Combine(versionDir, "prev" + curExt));

            DownloadToFile(downloadUri, storagePath);
            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"))
            {
                var jss = new JavaScriptSerializer();
                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"]);

            string forcesavePath = DocManagerHelper.ForcesavePath(newFileName, userAddress, false);

            if (!forcesavePath.Equals(""))
            {
                File.Delete(forcesavePath);
            }

            return(0);
        }