Example #1
0
        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);
                    }
                }
            }
        }
Example #2
0
        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);
                }
            }
        }
Example #3
0
        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);
        }