Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
0
        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();
                }
            }
        }