public static void DiskFileRename(SiteDb SiteDb, string OldPath, string NewPath) { NonRoutableObject NonRoutable = null; IRepository repo = null; NonRoutable = DiskPathService.GetNonRoutableObject(OldPath); if (NonRoutable != null) { var NewNonRoutable = DiskPathService.GetNonRoutableObject(NewPath); if (NewNonRoutable != null && !string.IsNullOrEmpty(NonRoutable.Name) && !string.IsNullOrEmpty(NewNonRoutable.Name)) { repo = SiteDb.GetRepository(NonRoutable.StoreName); var siteobject = repo?.GetByNameOrId(NonRoutable.Name); if (siteobject == null) { return; } var oldid = siteobject.Id; siteobject.Name = NewNonRoutable.Name; siteobject.Id = default(Guid); repo.Delete(oldid); repo.AddOrUpdate(siteobject); } } else { string OldRelative = DiskPathService.GetRelativeUrl(SiteDb.WebSite, OldPath); string NewRelative = DiskPathService.GetRelativeUrl(SiteDb.WebSite, NewPath); if (!string.IsNullOrEmpty(OldRelative) && !string.IsNullOrEmpty(NewRelative)) { SiteDb.Routes.ChangeRoute(OldRelative, NewRelative); } } }
public static NonRoutableObject GetNonRoutableObject(string FullOrRelativePath) { FullOrRelativePath = FullOrRelativePath.Replace("/", "\\"); string path = "\\" + PathPrefix + "\\"; int index = FullOrRelativePath.IndexOf(path, StringComparison.OrdinalIgnoreCase); if (index == -1) { if (FullOrRelativePath.StartsWith(PathPrefix + "\\", StringComparison.OrdinalIgnoreCase)) { FullOrRelativePath = "\\" + FullOrRelativePath; index = 0; } } if (index > -1) { string substring = FullOrRelativePath.Substring(index + path.Length); if (substring.StartsWith("\\")) { substring = substring.Substring(1); } string[] splited = substring.Split('\\'); if (splited.Length > 1) { var storename = splited[0]; if (!string.IsNullOrEmpty(storename)) { NonRoutableObject item = new NonRoutableObject(); item.StoreName = storename; string fullname = splited[1]; if (fullname.EndsWith(DefaultExtension)) { fullname = fullname.Substring(0, fullname.Length - DefaultExtension.Length); } else { int lastdot = fullname.LastIndexOf("."); if (lastdot > 0) { string extension = fullname.Substring(lastdot + 1); fullname = fullname.Substring(0, lastdot); item.Extension = extension; } } Guid id; if (System.Guid.TryParse(fullname, out id)) { item.Id = id; } else { item.Name = fullname; } return(item); } } } return(null); }