public static void BackupIfNotExists(cmSite site, string path, string relativePath, string name, bool nameAsComments = false) { if (File.Exists(path)) { // if last revision not exist, backup first RevisionAccessor ra = DataAccessor.CreateInstance <RevisionAccessor>(); cmRevision revision = ra.GetLastRevision(site.ID, relativePath); if (revision == null || !File.Exists(Revisions.GetLocalPath(revision.FilePath))) { SqlQuery <cmRevision> query = new SqlQuery <cmRevision>(); string localFile, filePath; localFile = Revisions.GetNewFilePath(out filePath); File.Copy(path, localFile); revision = new cmRevision(); if (nameAsComments) { revision.Comments = name; } else { revision.Comments = string.Format("No revision found for [{0}], make a backup.", name); } revision.SiteID = site.ID; revision.FilePath = filePath; revision.Ins = DateTime.Now; revision.RelativePath = relativePath; revision.UserID = CustomProfile.Current.UserID; query.Insert(revision); } } }
public static void Backup(cmSite site, string path, string relativePath, string name, bool nameAsComments = false) { string localFile, filePath; SqlQuery <cmRevision> query = new SqlQuery <cmRevision>(); // copy the file to backup localFile = Revisions.GetNewFilePath(out filePath); File.Copy(path, localFile); // save to cmRevision cmRevision revision = new cmRevision(); if (nameAsComments) { revision.Comments = name; } else { revision.Comments = string.Format("Update the config for [{0}]", name); } revision.SiteID = site.ID; revision.FilePath = filePath; revision.Ins = DateTime.Now; revision.RelativePath = relativePath; revision.UserID = CustomProfile.Current.UserID; query.Insert(revision); }
public static void RollbackRevision(int revisionID, int userID = 0) { if (userID == 0) { userID = CustomProfile.Current.UserID; } RevisionAccessor ra = DataAccessor.CreateInstance <RevisionAccessor>(); cmRevision revision = ra.GetByID(revisionID); string file = null; if (revision != null) { file = GetBaseDirectory() + revision.FilePath; } if (string.IsNullOrEmpty(file) || !File.Exists(file)) { throw new Exception("Error, can't find this revision."); } if (revision.RelativePath.StartsWith("/.config/", StringComparison.InvariantCultureIgnoreCase)) { throw new Exception("Error, can't rollback this revision."); } string realPath = HostingEnvironment.MapPath(string.Format("~/Views/{0}{1}", revision.DomainDistinctName, revision.RelativePath)); SqlQuery <cmRevision> query = new SqlQuery <cmRevision>(); string filePath; string localFile = Revisions.GetNewFilePath(out filePath); cmRevision newRevision = new cmRevision(); newRevision.Comments = string.Format("Rollback to Rev.{0}", revisionID); newRevision.SiteID = revision.SiteID; newRevision.FilePath = filePath; newRevision.Ins = DateTime.Now; newRevision.RelativePath = revision.RelativePath; newRevision.UserID = userID; query.Insert(newRevision); global::System.IO.File.Copy(file, realPath, true); global::System.IO.File.Copy(file, localFile, true); }
/// <summary> /// Write the metadata entry /// </summary> /// <param name="site"></param> /// <param name="path"></param> /// <param name="lang">If language is null, then default value is written</param> /// <param name="name"></param> /// <param name="value"></param> /// <returns></returns> public static bool Save(cmSite site, string path, string lang, string name, string value) { string postfix = string.IsNullOrWhiteSpace(lang) ? string.Empty : "." + lang; string physicalPath = HostingEnvironment.MapPath( string.Format("~/Views/{0}/{1}/.{2}{3}", site.DistinctName, path.TrimStart('/').TrimEnd('/'), name, postfix) ); ContentTree.EnsureDirectoryExistsForFile(site, physicalPath); SqlQuery <cmRevision> query = new SqlQuery <cmRevision>(); string filePath; string localFile; string metadataPath = path.TrimEnd('/') + "/." + name + postfix; if (File.Exists(physicalPath)) { // nothing changed if (WinFileIO.ReadWithoutLock(physicalPath) == value) { return(false); } // if last revision not exist, backup first { RevisionAccessor ra = DataAccessor.CreateInstance <RevisionAccessor>(); cmRevision revision = ra.GetLastRevision(site.ID, metadataPath); if (revision == null || !File.Exists(Revisions.GetLocalPath(revision.FilePath))) { localFile = Revisions.GetNewFilePath(out filePath); File.Copy(physicalPath, localFile); revision = new cmRevision(); revision.Comments = string.Format("No revision found for [{0}], language=[{1}], make a backup.", name, lang.DefaultIfNullOrEmpty("default")); revision.SiteID = site.ID; revision.FilePath = filePath; revision.Ins = DateTime.Now; revision.RelativePath = metadataPath; revision.UserID = CustomProfile.Current.UserID; query.Insert(revision); } } } else if (!string.IsNullOrWhiteSpace(site.TemplateDomainDistinctName)) { string inheritedPath = HostingEnvironment.MapPath( string.Format("~/Views/{0}/{1}/.{2}{3}", site.TemplateDomainDistinctName, path.TrimStart('/').TrimEnd('/'), name, postfix) ); if (File.Exists(inheritedPath)) { // nothing changed if (WinFileIO.ReadWithoutLock(inheritedPath) == value) { return(false); } } else if (string.IsNullOrWhiteSpace(value)) { return(false); } } else if (string.IsNullOrWhiteSpace(value)) { return(false); } using (FileStream fs = new FileStream(physicalPath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Delete | FileShare.ReadWrite)) { fs.SetLength(0); using (StreamWriter sw = new StreamWriter(fs, Encoding.UTF8)) { sw.Write(value); } } // copy the file to backup localFile = Revisions.GetNewFilePath(out filePath); File.Copy(physicalPath, localFile); // save to cmRevision { cmRevision revision = new cmRevision(); revision.Comments = string.Format("update the entry [{0}], language=[{1}].", name, lang.DefaultIfNullOrEmpty("default")); revision.SiteID = site.ID; revision.FilePath = filePath; revision.Ins = DateTime.Now; revision.RelativePath = metadataPath; revision.UserID = CustomProfile.Current.UserID; query.Insert(revision); } return(true); }