Esempio n. 1
0
        public void SynchronizeLocalFileChange(string fullPath, FileChangeType changeType, string oldFullPath)
        {
            lock (this)
            {
                Logger.LogDebug("SynchronizeLocalFileChange Path={0} ChangeType={1} OldPath={2}", fullPath, changeType, oldFullPath);

                OnSyncProgress(0, ProgressStatus.Analyzing);

                try
                {
                    var syncToRemote = (_configuration.Direction == SyncDirection.LocalToRemote || _configuration.Direction == SyncDirection.Both);
                    var syncToLocal  = _configuration.Direction == SyncDirection.RemoteToLocal || _configuration.Direction == SyncDirection.Both;

                    if (!syncToRemote)
                    {
                        OnSyncProgress(100, ProgressStatus.Completed);
                        return;
                    }

                    if (!_configuration.ShouldFileSync(fullPath))
                    {
                        OnSyncProgress(100, ProgressStatus.Completed);
                        return;
                    }

                    var localExtension = Path.GetExtension(fullPath);
                    if (localExtension == ".spsync")
                    {
                        OnSyncProgress(100, ProgressStatus.Completed);
                        return;
                    }

                    var isDirectory = false;

                    if (changeType != FileChangeType.Deleted)
                    {
                        try
                        {
                            if (File.GetAttributes(fullPath).HasFlag(FileAttributes.Hidden))
                            {
                                OnSyncProgress(100, ProgressStatus.Completed);
                                return;
                            }

                            if (File.GetAttributes(fullPath).HasFlag(FileAttributes.Directory))
                            {
                                isDirectory = true;
                            }

                            if (isDirectory)
                            {
                                if (Path.GetDirectoryName(fullPath) == MetadataStore.STOREFOLDER)
                                {
                                    OnSyncProgress(100, ProgressStatus.Completed);
                                    return;
                                }
                            }
                            else
                            {
                                if (Directory.GetParent(fullPath).Name == MetadataStore.STOREFOLDER)
                                {
                                    OnSyncProgress(100, ProgressStatus.Completed);
                                    return;
                                }
                            }
                        }
                        catch
                        {
                            OnSyncProgress(100, ProgressStatus.Completed);
                            return;
                        }
                    }

                    MetadataItem item = null;

                    if (string.IsNullOrEmpty(oldFullPath))
                    {
                        item = _metadataStore.GetByFileName(fullPath);
                    }
                    else
                    {
                        item = _metadataStore.GetByFileName(oldFullPath);
                        if (item == null)
                        {
                            changeType = FileChangeType.Changed;
                            item       = _metadataStore.GetByFileName(fullPath);
                        }
                    }

                    if (item == null)
                    {
                        if (changeType != FileChangeType.Deleted)
                        {
                            if (_metadataStore.GetByFileName(fullPath) == null)
                            {
                                _metadataStore.Add(new MetadataItem(fullPath, isDirectory ? ItemType.Folder : ItemType.File));
                            }
                        }
                    }
                    else
                    {
                        item.UpdateWithLocalInfo(_configuration.ConflictHandling, Guid.NewGuid());
                        if (item.Status == ItemStatus.Conflict)
                        {
                            item.Status = OnItemConflict(item);
                        }

                        if (changeType == FileChangeType.Renamed)
                        {
                            item.NewNameAfterRename = Path.GetFileName(fullPath); //works for directories as well
                            item.Status             = ItemStatus.RenamedLocal;

                            if (isDirectory)
                            {
                                foreach (var itemInFolder in _metadataStore.Items.Where(p => p.LocalFile.Contains(item.LocalFile)))
                                {
                                    if (itemInFolder.Id == item.Id)
                                    {
                                        continue;
                                    }
                                    itemInFolder.LocalFolder = itemInFolder.LocalFolder.Replace(item.LocalFile, fullPath);
                                }
                            }
                        }
                    }

                    if (changeType == FileChangeType.Deleted && item != null)
                    {
                        item.Status = ItemStatus.DeletedLocal;
                    }

                    _metadataStore.Save();

                    SyncChanges();

                    OnSyncProgress(100, ProgressStatus.Completed);
                }
                catch (Exception ex)
                {
                    //todo:
                    if (_configuration.AuthenticationType == AuthenticationType.ADFS) //&& ex is webexception 403
                    {
                        Adfs.AdfsHelper.InValidateCookie();
                    }
                    OnSyncProgress(100, ProgressStatus.Error, "An error has occured: " + ex.Message, ex);
                    return;
                }
            }
        }