コード例 #1
0
        public async Task ApplyChangesAsync()
        {
            StorageLibraryChangeTracker?tracker = RootFolder?.TryGetChangeTracker();

            if (tracker != null)
            {
                tracker.Enable();
                StorageLibraryChangeReader changeReader = tracker.GetChangeReader();
                var changes = await changeReader.ReadBatchAsync();

                await changeReader.AcceptChangesAsync();

                if (changes.Count > 0)
                {
                    Task applyChangesTask = ApplyChangesToTemporaryFolderAsync(changes);

                    StorageLibraryChanged?.Invoke(this, new StorageLibraryChangedEventArgs(changes));

                    await applyChangesTask;

                    await LoadSolutionAsync();
                }
            }
        }
コード例 #2
0
ファイル: LibraryEngine.cs プロジェクト: ahmed605/Reborn-Zune
        private static async Task <List <KeyValuePair <StorageLibraryChangeType, object> > > LoadChanges()
        {
            List <KeyValuePair <StorageLibraryChangeType, object> > changes = new List <KeyValuePair <StorageLibraryChangeType, object> >();

            try
            {
                StorageLibrary musicLibray = await StorageLibrary.GetLibraryAsync(KnownLibraryId.Music);

                musicLibray.ChangeTracker.Enable();
                StorageLibraryChangeReader           musicChangeReader = musicLibray.ChangeTracker.GetChangeReader();
                IReadOnlyList <StorageLibraryChange> changeSet         = await musicChangeReader.ReadBatchAsync();


                //Below this line is for the blog post. Above the line is for the magazine
                foreach (StorageLibraryChange change in changeSet)
                {
                    if (change.ChangeType == StorageLibraryChangeType.ChangeTrackingLost)
                    {
                        //We are in trouble. Nothing else is going to be valid.
                        Debug.WriteLine("Tracking lost");
                        musicLibray.ChangeTracker.Reset();
                        return(changes);
                    }
                    if (change.IsOfType(StorageItemTypes.Folder))
                    {
                        Debug.WriteLine("Folder changes detected");
                    }
                    else if (change.IsOfType(StorageItemTypes.File))
                    {
                        Debug.WriteLine("File changes detected");
                        switch (change.ChangeType)
                        {
                        case StorageLibraryChangeType.ContentsChanged:
                            StorageFile file = await change.GetStorageItemAsync() as StorageFile;

                            changes.Add(new KeyValuePair <StorageLibraryChangeType, object>(change.ChangeType, file));
                            break;

                        case StorageLibraryChangeType.MovedOrRenamed:
                            changes.Add(new KeyValuePair <StorageLibraryChangeType, object>(change.ChangeType,
                                                                                            new KeyValuePair <string, string>(change.Path, change.PreviousPath)));
                            break;

                        case StorageLibraryChangeType.MovedOutOfLibrary:
                            changes.Add(new KeyValuePair <StorageLibraryChangeType, object>(change.ChangeType, change.Path));
                            break;

                        case StorageLibraryChangeType.MovedIntoLibrary:
                            StorageFile File = await change.GetStorageItemAsync() as StorageFile;

                            changes.Add(new KeyValuePair <StorageLibraryChangeType, object>(change.ChangeType, File));
                            break;
                        }
                    }
                }
                await musicChangeReader.AcceptChangesAsync();

                Debug.WriteLine("Get changes succeed");
                return(changes);
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.ToString());
                return(changes);
            }
        }
コード例 #3
0
ファイル: LibraryService.cs プロジェクト: Losses/DarkPlayer
        private static async void OnPeriodicCheckTimerFired(object sender, object e)
        {
            if (m_isIndexing || m_libChangeReader == null)
            {
                return;
            }

            TelemetryHelper.LogEvent();
            m_logger.LogInformation("Enter periodic check.");

            try
            {
                var isIndexingRequired = false;
                var changes            = await m_libChangeReader.ReadBatchAsync();

                foreach (var change in changes)
                {
                    // For files, validate extension.
                    if (change.IsOfType(StorageItemTypes.File) && change.Path != null)
                    {
                        if (!FileIndexer.SupportedFormats.Contains(Path.GetExtension(change.Path)))
                        {
                            continue;
                        }
                    }

                    // Determine eligibility
                    switch (change.ChangeType)
                    {
                    case StorageLibraryChangeType.Created:
                    case StorageLibraryChangeType.Deleted:
                    case StorageLibraryChangeType.MovedIntoLibrary:
                    case StorageLibraryChangeType.MovedOrRenamed:
                    case StorageLibraryChangeType.MovedOutOfLibrary:
                    case StorageLibraryChangeType.ContentsChanged:
                    case StorageLibraryChangeType.ContentsReplaced:
                        isIndexingRequired = true;
                        break;
                    }

                    // Exit the loop
                    if (isIndexingRequired)
                    {
                        break;
                    }
                }

                m_logger.LogInformation($"Periodic check result: Reindex: {isIndexingRequired}.");

                if (isIndexingRequired)
                {
                    var updatedCount = await IndexInternalAsync(new ThumbnailOperations());

                    m_logger.LogInformation($"{updatedCount} files have been added.");
                }
                await m_libChangeReader.AcceptChangesAsync();

                m_logger.LogInformation("Periodic check has been completed.");
            }
            catch (Exception ex) when((uint)ex.HResult == 0x80080222)
            {
                await DisableAutoTrackChangesAndNotifyUserAsync();
            }
            catch (Exception ex)
            {
                // Log exceptions to ETW channel
                m_logger.LogError($"Type of {ex.GetType()} exception occurred in {ex.StackTrace} with message {ex.Message}");
            }
        }
コード例 #4
0
        public async Task <List <BaseItem> > GetChangesFromChangeTracker(KnownLibraryId libraryId, FolderAssociation association, List <StorageLibraryChangeType> supportedChangeTypes)
        {
            var result  = new List <BaseItem>();
            var library = await StorageLibrary.GetLibraryAsync(libraryId);

            library.ChangeTracker.Enable();
            _changeReader = library.ChangeTracker.GetChangeReader();
            var changes = await _changeReader.ReadBatchAsync();

            foreach (var change in changes)
            {
                if (change.ChangeType == StorageLibraryChangeType.ChangeTrackingLost)
                {
                    await LogHelper.Write($"Changetracking lost: {change.Path}");

                    library.ChangeTracker.Reset();
                    return(null);
                }
                try
                {
                    if (supportedChangeTypes.Contains(change.ChangeType))
                    {
                        Debug.WriteLine($"File: {change.Path} ChangeType: {change.ChangeType}");
                        if (change.Path.EndsWith("thumb"))
                        {
                            continue;
                        }
                        var file = await change.GetStorageItemAsync();

                        if (file == null && change.ChangeType == StorageLibraryChangeType.Deleted)
                        {
                            var localItem = new LocalItem();
                            localItem.Association = association;
                            localItem.EntityId    = change.Path;
                            result.Add(localItem);
                        }
                        else if (file == null || file.IsOfType(StorageItemTypes.Folder))
                        {
                            await LogHelper.Write($"Skipping {change.Path}");
                        }
                        else
                        {
                            try
                            {
                                //check if the file is currently in use, for example a video currently being recorded
                                var stream = await((StorageFile)file).OpenStreamForWriteAsync();
                                stream.Dispose();
                            }
                            catch (Exception)
                            {
                                continue;
                            }
                            var props = await file.GetBasicPropertiesAsync();

                            var item = new LocalItem(association, file, props);

                            if (change.ChangeType == StorageLibraryChangeType.ContentsChanged)
                            {
                                //for some reason something is writing to the files after the upload, but without making changes
                                var existingItem = ItemTableModel.GetDefault().GetItem(item);
                                if (existingItem?.Size == item.Size)
                                {
                                    continue;
                                }
                            }
                            result.Add(item);
                        }
                    }
                }
                catch (Exception e)
                {
                    await LogHelper.Write($"InstantUpload: {e.Message} File: {change.Path} ChangeType: {change.ChangeType}");

                    await LogHelper.Write(e.StackTrace);
                }
            }
            return(result);
        }