public static void ChangeRoute(SiteDb SiteDb, string OldRelative, string NewRelative) { if (SiteDb.WebSite.EnableDiskSync) { var OldFullPath = DiskPathService.GetFullDiskPath(SiteDb.WebSite, OldRelative); var NewFullPath = DiskPathService.GetFullDiskPath(SiteDb.WebSite, NewRelative); if (System.IO.File.Exists(OldFullPath) && !System.IO.File.Exists(NewFullPath)) { var manager = GetSyncManager(SiteDb.WebSite.Id); IOHelper.EnsureFileDirectoryExists(NewFullPath); var allbytes = Lib.Helper.IOHelper.ReadAllBytes(OldFullPath); manager.SyncMediator.AbsoluteLock(NewFullPath); manager.WriteBytes(NewFullPath, allbytes); manager.SyncMediator.LockDisk3Seconds(NewFullPath); manager.SyncMediator.ReleaseAbsoluteLock(NewFullPath); manager.SyncMediator.AbsoluteLock(OldFullPath); System.IO.File.Delete(OldFullPath); manager.SyncMediator.LockDisk3Seconds(OldFullPath); manager.SyncMediator.ReleaseAbsoluteLock(OldFullPath); } } }
public static void ChangeRoute(SiteDb SiteDb, Route newRoute, string OldRelative, string NewRelative) { if (SiteDb.WebSite.EnableDiskSync) { if (newRoute.DestinationConstType == ConstObjectType.Code) { // code does not change file location, instead, change the textbody. var code = SiteDb.Code.Get(newRoute.objectId); if (code != null) { SyncManager ma = new SyncManager(SiteDb.Id); ma.SyncToDisk(SiteDb, code, ChangeType.Update, SiteDb.Code.StoreName); } } else { var OldFullPath = DiskPathService.GetFullDiskPath(SiteDb.WebSite, OldRelative); var NewFullPath = DiskPathService.GetFullDiskPath(SiteDb.WebSite, NewRelative); if (System.IO.File.Exists(OldFullPath) && !System.IO.File.Exists(NewFullPath)) { var manager = new SyncManager(SiteDb.WebSite.Id); IOHelper.EnsureFileDirectoryExists(NewFullPath); var allbytes = Lib.Helper.IOHelper.ReadAllBytes(OldFullPath); manager.WriteBytes(NewFullPath, allbytes); manager.Delete(OldFullPath); } } } }
public static void DeleteDiskFolder(string FullPath, SiteDb SiteDb) { string nonroutable = DiskPathService.GetNonRoutableFolder(FullPath); if (!string.IsNullOrEmpty(nonroutable)) { // delete all routable.. var repo = SiteDb.GetRepository(nonroutable); if (repo != null) { var all = repo.All(); foreach (var item in all) { repo.Delete(item.Id); } } } else { // delete all. var relative = DiskPathService.GetRelativeUrl(SiteDb.WebSite, FullPath); if (!string.IsNullOrEmpty(relative)) { relative = relative.ToLower(); if (!relative.EndsWith("/")) { relative = relative + "/"; } List <Route> routesToRemove = new List <Route>(); foreach (var item in SiteDb.Routes.All()) { if (item.Name.ToLower().StartsWith(relative)) { routesToRemove.Add(item); } } foreach (var item in routesToRemove) { if (item.objectId != default(Guid)) { var repo = SiteDb.GetRepository(item.DestinationConstType); repo.Delete(item.objectId); SiteDb.Routes.Delete(item.Id); } } } } }
public static void DiskFolderRename(SiteDb sitedb, string oldFolder, string NewFolder) { string nonroutable = DiskPathService.GetNonRoutableFolder(NewFolder); if (!string.IsNullOrEmpty(nonroutable)) { return; } string oldrelative = DiskPathService.GetRelativeUrl(sitedb.WebSite, oldFolder); string newrelative = DiskPathService.GetRelativeUrl(sitedb.WebSite, NewFolder); if (!oldrelative.EndsWith("/")) { oldrelative = oldrelative + "/"; } if (!newrelative.EndsWith("/")) { newrelative = newrelative + "/"; } var loweroldrelative = oldrelative.ToLower(); var lowernewrelative = newrelative.ToLower(); List <Route> ChangeRoutes = new List <Route>(); foreach (var item in sitedb.Routes.All()) { if (item.Name.StartsWith(loweroldrelative, StringComparison.OrdinalIgnoreCase)) { ChangeRoutes.Add(item); } } int oldlen = loweroldrelative.Length; foreach (var item in ChangeRoutes) { string newroute = newrelative + item.Name.Substring(oldlen); sitedb.Routes.ChangeRoute(item.Name, newroute); } }
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 void DeleteFromDb(string DiskFullPath, SiteDb sitedb) { var NonRoutable = DiskPathService.GetNonRoutableObject(DiskFullPath); if (NonRoutable != null) { var repo = sitedb.GetRepository(NonRoutable.StoreName); string name = string.IsNullOrWhiteSpace(NonRoutable.Name) ? NonRoutable.Id.ToString() : NonRoutable.Name; var result = repo.GetByNameOrId(name) as ISiteObject; if (result != null) { this.SyncMediator.AcquireDbLock(result.Id); repo.Delete(result.Id); this.SyncMediator.ReleaseDbLock(result.Id); } } else { var RelativeUrl = DiskPathService.GetRelativeUrl(sitedb.WebSite, DiskFullPath); var route = sitedb.Routes.GetByUrl(RelativeUrl); if (route != null) { var repo = sitedb.GetRepository(route.DestinationConstType); if (repo != null) { var result = repo.Get(route.objectId) as ISiteObject; if (result != null) { this.SyncMediator.AcquireDbLock(result.Id); repo.Delete(result.Id); this.SyncMediator.ReleaseDbLock(result.Id); } } } } }
public string SyncToDisk(SiteDb SiteDb, ISiteObject Value, ChangeType ChangeType, string StoreName) { string diskpath = null; if (Attributes.AttributeHelper.IsDiskable(Value) && !IsEmbedded(Value) && !string.IsNullOrEmpty(StoreName)) { if (!this.SyncMediator.CheckDbLocked(Value.Id)) { var value = Value as ISiteObject; string relativeurl = DiskPathService.GetObjectRelativeUrl(value, SiteDb, StoreName); if (!string.IsNullOrEmpty(relativeurl)) { string fullpath = DiskPathService.GetFullDiskPath(SiteDb.WebSite, relativeurl); if (ChangeType == ChangeType.Delete) { if (File.Exists(fullpath)) { diskpath = fullpath; // this.SyncMediator.AcquireDeletionLock(fullpath); this.SyncMediator.AbsoluteLock(fullpath); File.Delete(fullpath); this.SyncMediator.LockDisk3Seconds(fullpath); this.SyncMediator.ReleaseAbsoluteLock(fullpath); } } else { var coreobject = value as ICoreObject; if (coreobject != null) { bool hasPast = false; var logs = SiteDb.Synchronization.Query.Where(o => o.SyncSettingId == SiteDb.Synchronization.DiskSyncSettingId && o.ObjectId == value.Id).SelectAll(); foreach (var item in logs) { if (StringHelper.IsSameValue(StoreName, item.StoreName)) { if (item.Version >= coreobject.Version) { hasPast = true; } } } if (!hasPast) { var contentbytes = SyncService.GetObjectBytes(value); if (File.Exists(fullpath)) { var bytes = IOHelper.ReadAllBytes(fullpath); diskpath = fullpath; if (!IOHelper.IsEqualBytes(bytes, contentbytes)) { this.SyncMediator.AbsoluteLock(fullpath); this.SyncMediator.ContentHashLock(fullpath, contentbytes); this.WriteBytes(fullpath, contentbytes); this.SyncMediator.LockDisk3Seconds(fullpath); this.SyncMediator.ReleaseAbsoluteLock(fullpath); } } else { // this.SyncMediator.AcquireDiskWriteLock(fullpath, contentbytes); // this.SyncMediator.LockDisk3Seconds(fullpath); this.SyncMediator.AbsoluteLock(fullpath); this.SyncMediator.ContentHashLock(fullpath, contentbytes); this.WriteBytes(fullpath, contentbytes); this.SyncMediator.LockDisk3Seconds(fullpath); this.SyncMediator.ReleaseAbsoluteLock(fullpath); diskpath = fullpath; } } } } } } } var core = Value as ICoreObject; if (core != null) { SiteDb.Synchronization.AddOrUpdate(new Synchronization { SyncSettingId = SiteDb.Synchronization.DiskSyncSettingId, ObjectId = Value.Id, Version = core.Version, StoreName = StoreName }); } return(diskpath); }
public void SyncToDb(string FullPath, SiteDb SiteDb, byte[] diskbytes = null, bool logSync = true) { if (diskbytes == null) { diskbytes = this.ReadAllBytes(FullPath); } if (diskbytes == null) { return; } if (this.SyncMediator.IsContentHashLock(FullPath, diskbytes)) { return; } //if (!this.SyncMediator.CheckAndAcquireDiskLock(FullPath, diskbytes)) //{ // return; //} string OldRelativeUrl = null; string RelativeUrl = null; IRepository repo = null; ISiteObject result = null; Routing.Route route = null; string NameFromFile = null; string extension = UrlHelper.FileExtension(FullPath); if (!string.IsNullOrEmpty(extension) && !extension.StartsWith(".")) { extension = "." + extension; } if (!string.IsNullOrEmpty(extension)) { extension = extension.ToLower(); } var NonRoutable = DiskPathService.GetNonRoutableObject(FullPath); if (NonRoutable != null) { repo = SiteDb.GetRepository(NonRoutable.StoreName); NameFromFile = NonRoutable.Name; string name = string.IsNullOrWhiteSpace(NonRoutable.Name) ? NonRoutable.Id.ToString() : NonRoutable.Name; if (!string.IsNullOrEmpty(NonRoutable.Extension)) { extension = NonRoutable.Extension.ToLower(); if (!extension.StartsWith(".")) { extension = "." + extension; } } result = repo.GetByNameOrId(name) as ISiteObject; if (result == null) { if (name.ToLower().EndsWith(extension)) { name = name.Substring(0, name.Length - extension.Length); result = repo.GetByNameOrId(name); } else { name = name + extension; result = repo.GetByNameOrId(name); } } } else { OldRelativeUrl = DiskPathService.GetRelativeUrl(SiteDb.WebSite, FullPath); RelativeUrl = Kooboo.Sites.Helper.RouteHelper.ToValidRoute(OldRelativeUrl); route = SiteDb.Routes.GetByUrl(RelativeUrl); if (route != null) { repo = SiteDb.GetRepository(route.DestinationConstType); result = repo.Get(route.objectId) as ISiteObject; } else { var ModelType = Service.ConstTypeService.GetModelTypeByUrl(RelativeUrl); if (ModelType == null) { return; } repo = SiteDb.GetRepository(ModelType); } NameFromFile = UrlHelper.FileName(RelativeUrl); } if (result == null) { result = Activator.CreateInstance(repo.ModelType) as ISiteObject; } if (!CheckAssignObject(ref result, diskbytes)) { return; } if (result is IExtensionable) { var extensionfile = result as IExtensionable; extensionfile.Extension = extension; } if (string.IsNullOrEmpty(result.Name)) { result.Name = Lib.Helper.StringHelper.ToValidFileName(NameFromFile); } if (!string.IsNullOrEmpty(RelativeUrl)) { SiteDb.Routes.AddOrUpdate(RelativeUrl, result as SiteObject); } if (!isSameName(result.Name, NameFromFile, extension) || OldRelativeUrl != RelativeUrl) { if (File.Exists(FullPath)) { this.SyncMediator.AbsoluteLock(FullPath); File.Delete(FullPath); this.SyncMediator.LockDisk3Seconds(FullPath); this.SyncMediator.ReleaseAbsoluteLock(FullPath); } repo.AddOrUpdate(result); } else { this.SyncMediator.AcquireDbLock(result.Id); repo.AddOrUpdate(result); this.SyncMediator.ReleaseDbLock(result.Id); } if (logSync) { var coreobject = result as CoreObject; SiteDb.Synchronization.AddOrUpdate(new Synchronization { SyncSettingId = SiteDb.Synchronization.DiskSyncSettingId, ObjectId = coreobject.Id, Version = coreobject.Version, StoreName = repo.StoreName }); } }
internal void ProcessFromDisk() { if (this.CanAccept) { Interlocked.Increment(ref this.CurrentThreadCount); DiskChangeEvent task = PeekTask(); if (task != null) { try { Data.Models.WebSite website = Data.GlobalDb.WebSites.Get(WebSiteId); var sitedb = website.SiteDb(); if (task.ChangeType == DiskChangeType.Rename) { if (DiskPathService.IsDirectory(website, task.OldFullPath) && DiskPathService.IsDirectory(website, task.FullPath)) { SyncService.DiskFolderRename(sitedb, task.OldFullPath, task.FullPath); } else { SyncService.DiskFileRename(sitedb, task.OldFullPath, task.FullPath); } } else if (task.ChangeType == DiskChangeType.Deleted) { if (DiskPathService.IsDirectory(website, task.FullPath)) { SyncService.DeleteDiskFolder(task.FullPath, sitedb); } else { this.DeleteFromDb(task.FullPath, sitedb); } } else { //if (task.ChangeType == DiskChangeType.Created) //{ // contentbytes = new byte[0]; //} this.SyncToDb(task.FullPath, sitedb, null); } } catch (Exception ex) { var error = ex.Message; } this.RemoveTask(task); } Interlocked.Decrement(ref this.CurrentThreadCount); if (task != null) { startNewFromDisk(); } } }