protected virtual async Task GetChanges()
        {
            var associations = FolderAssociationTableModel.GetDefault().GetAllItems();

            foreach (FolderAssociation association in associations)
            {
                if (association.SupportsInstantUpload)
                {
                    continue;
                }
                await LogHelper.Write($"Scanning {association.LocalFolderPath} and {association.RemoteFolderFolderPath} BackgroundTask: {_isBackgroundTask}");

                if (_watch.Elapsed.Minutes >= 9)
                {
                    break;
                }
                await SetExecutionStatus(ExecutionStatus.Scanning);

                var getUpdatedTargetTask = _targetEntityAdapter.GetUpdatedItems(association);
                var getUpdatedSourceTask = _sourceEntityAdapter.GetUpdatedItems(association);
                var getDeletedTargetTask = _targetEntityAdapter.GetDeletedItemsAsync(association);
                var getDeletedSourceTask = _sourceEntityAdapter.GetDeletedItemsAsync(association);

                var deletedItems = await getDeletedTargetTask;
                deletedItems.AddRange(await getDeletedSourceTask);

                var items = await getUpdatedSourceTask;
                items.AddRange(await getUpdatedTargetTask);

                await _UpdateFileIndexes(association, items);
                await ProcessDeletions(deletedItems);

                await LogHelper.Write($"Updating: {items.Count} Deleting: {deletedItems.Count} BackgroundTask: {_isBackgroundTask}");
            }
        }
        protected override async Task GetChanges()
        {
            _itemsToAdd    = new List <BaseItem>();
            _itemsToUpdate = new List <BaseItem>();
            foreach (var association in FolderAssociationTableModel.GetDefault().GetAllItems())
            {
                if (association.SupportsInstantUpload)
                {
                    var adds = await _adapter.GetChangesFromChangeTracker(KnownLibraryId.Pictures, association, new List <StorageLibraryChangeType> {
                        StorageLibraryChangeType.Created, StorageLibraryChangeType.MovedIntoLibrary, StorageLibraryChangeType.MovedOrRenamed
                    });

                    _itemsToAdd.AddRange(adds);
                    var updated = await _adapter.GetChangesFromChangeTracker(KnownLibraryId.Pictures, association, new List <StorageLibraryChangeType> {
                        StorageLibraryChangeType.ContentsChanged, StorageLibraryChangeType.ContentsReplaced
                    });

                    _itemsToUpdate.AddRange(updated);
                    var deletions = await _adapter.GetChangesFromChangeTracker(KnownLibraryId.Pictures, association, new List <StorageLibraryChangeType> {
                        StorageLibraryChangeType.Deleted, StorageLibraryChangeType.MovedOrRenamed, StorageLibraryChangeType.MovedOutOfLibrary
                    });

                    if (adds == null || updated == null || deletions == null)//changetracking was reset in this case
                    {
                        return;
                    }

                    await _UpdateFileIndexes(association, adds);
                    await _UpdateFileIndexes(association, updated);
                    await ProcessDeletions(deletions);

                    await _adapter.AcceptChangesFromChangeTracker();
                }
            }
        }
        protected async Task _UpdateFileIndexes(FolderAssociation association, List <BaseItem> items)
        {
            await SetExecutionStatus(ExecutionStatus.UpdatingIndex);

            var itemTableModel = ItemTableModel.GetDefault();

            foreach (BaseItem t in items)
            {
                t.Association = association;
                var foundItem = itemTableModel.GetItem(t);
                if (foundItem == null)
                {
                    itemTableModel.InsertItem(t);
                    t.Id = itemTableModel.GetLastInsertItem().Id;
                }
                else
                {
                    if (foundItem.ChangeKey != t.ChangeKey)
                    {
                        t.ChangeNumber = foundItem.ChangeNumber + 1;
                        itemTableModel.UpdateItem(t, foundItem.Id);
                    }
                    t.Id = foundItem.Id;
                }
            }
            association.LastSync = DateTime.Now;
            FolderAssociationTableModel.GetDefault().UpdateItem(association, association.Id);
        }
 public void RemoveInstantUploadAssociations()
 {
     foreach (var association in GetInstantUploadAssociations())
     {
         FolderAssociationTableModel.GetDefault().DeleteItem(association.Id);
         LinkStatusTableModel.GetDefault().DeleteLinksFromAssociation(association);
         ItemTableModel.GetDefault().DeleteItem(association.RemoteFolderId);
         ItemTableModel.GetDefault().DeleteItem(association.LocalFolderId);
         ItemTableModel.GetDefault().DeleteItemsFromAssociation(association);
     }
 }
        public async Task <FolderAssociation> AddFolderToSyncAsync(StorageFolder folder, DavItem remoteFolderItem, bool allowInstantUpload = false)
        {
            StorageApplicationPermissions.FutureAccessList.Add(folder);
            var properties = await folder.Properties.RetrievePropertiesAsync(new List <string> {
                "System.DateModified"
            });

            FolderAssociation fa = new FolderAssociation
            {
                IsActive       = true,
                LocalFolderId  = 0,
                RemoteFolderId = 0,
                SyncDirection  = SyncDirection.FullSync,
                LastSync       = DateTime.MinValue
            };

            if (allowInstantUpload)
            {
                fa.SyncDirection         = SyncDirection.UploadOnly;
                fa.SupportsInstantUpload = true;
            }
            FolderAssociationTableModel.GetDefault().InsertItem(fa);
            fa = FolderAssociationTableModel.GetDefault().GetLastInsertItem();

            BaseItem li = new LocalItem
            {
                IsCollection = true,
                LastModified = ((DateTimeOffset)properties["System.DateModified"]).LocalDateTime,
                EntityId     = folder.Path,
                Association  = fa,
            };
            var testFolder = await StorageFolder.GetFolderFromPathAsync(folder.Path);

            if (testFolder.Path != folder.Path)
            {
                li.EntityId = testFolder.Path;
            }
            ItemTableModel.GetDefault().InsertItem(li);
            li = ItemTableModel.GetDefault().GetLastInsertItem();

            remoteFolderItem.Association = fa;
            ItemTableModel.GetDefault().InsertItem(remoteFolderItem);
            var ri = ItemTableModel.GetDefault().GetLastInsertItem();

            fa.RemoteFolderId = ri.Id;
            fa.LocalFolderId  = li.Id;
            FolderAssociationTableModel.GetDefault().UpdateItem(fa, fa.Id);
            var link = new LinkStatus(li, ri);

            LinkStatusTableModel.GetDefault().InsertItem(link);
            return(fa);
        }
        public async Task <FolderAssociation> AddFolderToSyncAsync(StorageFolder folder, DavItem remoteFolderItem, SyncDirection direction = SyncDirection.FullSync)
        {
            StorageApplicationPermissions.FutureAccessList.Add(folder);
            var properties = await folder.Properties.RetrievePropertiesAsync(new List <string> {
                "System.DateModified"
            });

            FolderAssociation fa = new FolderAssociation
            {
                IsActive       = true,
                LocalFolderId  = 0,
                RemoteFolderId = 0,
                SyncDirection  = direction,
                LastSync       = DateTime.MinValue
            };

            FolderAssociationTableModel.GetDefault().InsertItem(fa);
            fa = FolderAssociationTableModel.GetDefault().GetLastInsertItem();
            var      path = folder.Path.Replace("\\USERS\\", "\\Users\\");
            BaseItem li   = new LocalItem
            {
                IsCollection = true,
                LastModified = ((DateTimeOffset)properties["System.DateModified"]).LocalDateTime,
                EntityId     = path,
                Association  = fa,
            };

            ItemTableModel.GetDefault().InsertItem(li);
            li = ItemTableModel.GetDefault().GetLastInsertItem();

            remoteFolderItem.Association = fa;
            ItemTableModel.GetDefault().InsertItem(remoteFolderItem);
            var ri = ItemTableModel.GetDefault().GetLastInsertItem();

            fa.RemoteFolderId = ri.Id;
            fa.LocalFolderId  = li.Id;
            FolderAssociationTableModel.GetDefault().UpdateItem(fa, fa.Id);
            var link = new LinkStatus(li, ri);

            LinkStatusTableModel.GetDefault().InsertItem(link);
            return(fa);
        }
 public void RemoveFromSyncedFolders(FolderAssociation association)
 {
     ItemTableModel.GetDefault().DeleteItemsFromAssociation(association);
     LinkStatusTableModel.GetDefault().DeleteLinksFromAssociation(association);
     FolderAssociationTableModel.GetDefault().DeleteItem(association.Id);
 }
 public List <FolderAssociation> GetAllSyncedFolders()
 {
     return(FolderAssociationTableModel.GetDefault().GetAllItems().ToList());
 }
示例#9
0
        private async Task _GetChangesFromSearchIndex(StorageFolder folder, long associationId,
                                                      List <BaseItem> result)
        {
            var association = FolderAssociationTableModel.GetDefault().GetItem(associationId);
            var files       = new List <IStorageItem>();
            var options     = new QueryOptions();

            options.FolderDepth   = FolderDepth.Deep;
            options.IndexerOption = IndexerOption.UseIndexerWhenAvailable;
            //details about filesystem queries using the indexer
            //https://msdn.microsoft.com/en-us/magazine/mt620012.aspx
            string timeFilter = "System.Search.GatherTime:>=" + association.LastSync;

            options.ApplicationSearchFilter = timeFilter;
            var prefetchedProperties = new List <string> {
                "System.DateModified", "System.Size"
            };

            options.SetPropertyPrefetch(PropertyPrefetchOptions.BasicProperties, prefetchedProperties);
            if (!folder.AreQueryOptionsSupported(options))
            {
                throw new Exception($"Windows Search Index has to be enabled for {folder.Path}");
            }

            var queryResult = folder.CreateItemQueryWithOptions(options);

            queryResult.ApplyNewQueryOptions(options);
            files.AddRange(await queryResult.GetItemsAsync());

            foreach (var file in files)
            {
                try
                {
                    IDictionary <string, object> propertyResult = null;
                    if (file.IsOfType(StorageItemTypes.File))
                    {
                        propertyResult =
                            await((StorageFile)file).Properties.RetrievePropertiesAsync(prefetchedProperties);
                    }
                    else if (file.IsOfType(StorageItemTypes.Folder))
                    {
                        propertyResult =
                            await((StorageFolder)file).Properties.RetrievePropertiesAsync(prefetchedProperties);
                    }
                    var item = new LocalItem(new FolderAssociation {
                        Id = associationId
                    }, file, propertyResult);

                    var existingItem = ItemTableModel.GetDefault().GetItem(item);
                    if (existingItem != null)
                    {
                        if (!item.IsCollection)
                        {
                            //additional check if the file has changed:
                            //even though the size not the best way to make sure if a file has changed
                            //its very unlikely that after a change they have the exact same byte count
                            //so its the best option we have
                            if ((ulong)propertyResult["System.Size"] == existingItem.Size)
                            {
                                continue;
                            }
                        }
                    }
                    result.Add(item);
                }
                catch (Exception)
                {
                    Debug.WriteLine(file);
                    throw;
                }
            }

            if (files.Count == 0)
            {
                await _GetChangedFilesRecursive(folder, association, result);
            }

            if (!IsBackgroundSync)
            {
                var unsynced =
                    ItemTableModel.GetDefault()
                    .GetPostponedItems()
                    .Where(x => x.AdapterType == typeof(FileSystemAdapter));
                foreach (var abstractItem in unsynced)
                {
                    abstractItem.SyncPostponed = false;
                }
                result.AddRange(unsynced);
            }
        }
 private void Save()
 {
     FolderAssociationTableModel.GetDefault().UpdateItem(_association, _association.Id);
 }
 public List <FolderAssociation> GetInstantUploadAssociations()
 {
     return(FolderAssociationTableModel.GetDefault().GetAllItems().ToList().Where(x => x.SupportsInstantUpload).ToList());
 }
 public List <FolderAssociation> GetConfiguredFolders()
 {
     return(FolderAssociationTableModel.GetDefault().GetAllItems().ToList().Where(x => x.SupportsInstantUpload == false).ToList());
 }