public async ValueTask <FolderContainerType> GetFolderContainerType(StorageFolder folder)
        {
            var containerType = _folderContainerTypeRepository.GetContainerType(folder.Path);

            if (containerType != null)
            {
                return(containerType.Value);
            }

            var query = folder.CreateFileQueryWithOptions(new Windows.Storage.Search.QueryOptions(Windows.Storage.Search.CommonFileQuery.DefaultQuery, SupportedFileTypesHelper.GetAllSupportedFileExtensions())
            {
                FolderDepth = Windows.Storage.Search.FolderDepth.Shallow
            });
            var count = await query.GetItemCountAsync();

            if (count == 0)
            {
                _folderContainerTypeRepository.SetContainerType(folder.Path, FolderContainerType.Other);
                return(FolderContainerType.Other);
            }

            var items = await query.GetFilesAsync(0, count);

            containerType = items.All(x => SupportedFileTypesHelper.IsSupportedImageFileExtension(x.FileType))
                ? FolderContainerType.OnlyImages
                : FolderContainerType.Other
            ;
            _folderContainerTypeRepository.SetContainerType(folder.Path, containerType.Value);
            return(containerType.Value);
        }
Beispiel #2
0
        public async Task <StorageFile> GetFileThumbnailImageAsync(StorageFile file, CancellationToken ct = default)
        {
            var tempFolder = await GetTempFolderAsync();

            var itemId = GetStorageItemId(file);

            if (await ApplicationData.Current.TemporaryFolder.FileExistsAsync(itemId))
            {
                return(await ApplicationData.Current.TemporaryFolder.GetFileAsync(itemId));
            }
            else
            {
                var thumbnailFile = await tempFolder.CreateFileAsync(itemId, CreationCollisionOption.ReplaceExisting);

                if (SupportedFileTypesHelper.IsSupportedArchiveFileExtension(file.FileType)
                    )
                {
                    return(await GenerateThumbnailImageAsync(file, thumbnailFile, EncodingForFolderOrArchiveFileThumbnailBitmap));
                }
                else
                {
                    return(await GenerateThumbnailImageAsync(file, thumbnailFile, EncodingForImageFileThumbnailBitmap));
                }
            }
        }
        private async void Grid_Drop(object sender, DragEventArgs e)
        {
            var defferal = e.GetDeferral();

            try
            {
                string       token           = null;
                IStorageItem openStorageItem = null;
                var          dropItems       = await e.DataView.GetStorageItemsAsync();

                foreach (var storageItem in dropItems)
                {
                    if (storageItem is StorageFolder)
                    {
                        token = await _viewModel.SourceStorageItemsRepository.AddItemPersistantAsync(storageItem, SourceOriginConstants.DragAndDrop);
                    }
                    else if (storageItem is StorageFile file)
                    {
                        token = await _viewModel.SourceStorageItemsRepository.AddFileTemporaryAsync(file, SourceOriginConstants.DragAndDrop);
                    }

                    openStorageItem = storageItem;
                }

                if (dropItems.Count == 1)
                {
                    if (openStorageItem is StorageFolder)
                    {
                        await _viewModel.NavigationService.NavigateAsync(nameof(Views.FolderListupPage), new NavigationParameters((PageNavigationConstants.Path, openStorageItem.Path)));
                    }
                    else if (openStorageItem is StorageFile fileItem)
                    {
                        if (SupportedFileTypesHelper.IsSupportedArchiveFileExtension(fileItem.FileType) ||
                            SupportedFileTypesHelper.IsSupportedImageFileExtension(fileItem.FileType)
                            )
                        {
                            await _viewModel.NavigationService.NavigateAsync(nameof(Views.ImageViewerPage), new NavigationParameters((PageNavigationConstants.Path, openStorageItem.Path)));
                        }
                        else if (SupportedFileTypesHelper.IsSupportedEBookFileExtension(fileItem.FileType))
                        {
                            await _viewModel.NavigationService.NavigateAsync(nameof(Views.EBookReaderPage), new NavigationParameters((PageNavigationConstants.Path, openStorageItem.Path)));
                        }
                    }
                }
            }
            finally
            {
                defferal.Complete();
            }
        }
Beispiel #4
0
        public async Task <BitmapImage> GenerateBitmapImageAsync(CancellationToken ct)
        {
            if (StorageItem is StorageFile file &&
                SupportedFileTypesHelper.IsSupportedImageFileExtension(file.FileType))
            {
                using (var stream = await file.OpenReadAsync().AsTask(ct))
                {
                    var bitmapImage = new BitmapImage();
                    await bitmapImage.SetSourceAsync(stream).AsTask(ct);

                    ct.ThrowIfCancellationRequested();

                    return(bitmapImage);
                }
            }
        public StorageItemViewModel(IImageSource item, string token, SourceStorageItemsRepository sourceStorageItemsRepository, FolderListingSettings folderListingSettings, BookmarkManager bookmarkManager)
            : this(sourceStorageItemsRepository, folderListingSettings, bookmarkManager)
        {
            Item        = item;
            DateCreated = Item.DateCreated;
            Token       = token;

            Name = Item.Name;
            Type = SupportedFileTypesHelper.StorageItemToStorageItemTypes(item);
            if (item is StorageItemImageSource storageItemImageSource)
            {
                Path = storageItemImageSource.Path;
            }

            UpdateLastReadPosition();
        }
        public async Task <GetImagesFromArchiveResult> GetImagesFromTarFileAsync(StorageFile file)
        {
            CompositeDisposable disposables = new CompositeDisposable();
            var stream = await file.OpenStreamForReadAsync()
                         .AddTo(disposables);

            var zipArchive = TarArchive.Open(stream)
                             .AddTo(disposables);

            var supportedEntries = zipArchive.Entries
                                   .OrderBy(x => x.Key)
                                   .Where(x => SupportedFileTypesHelper.IsSupportedImageFileExtension(x.Key))
                                   .Select(x => (IImageSource) new ArchiveEntryImageSource(x, file, _recyclableMemoryStreamManager))
                                   .ToArray();

            return new GetImagesFromArchiveResult()
                   {
                       ItemsCount = (uint)supportedEntries.Length,
                       Disposer = disposables,
                       Images = supportedEntries,
                   };
        }
        private async void Grid_DragEnter(object sender, DragEventArgs e)
        {
            var deferral = e.GetDeferral();

            try
            {
                if (e.DataView.Contains(StandardDataFormats.StorageItems))
                {
                    var items = await e.DataView.GetStorageItemsAsync();

                    var isAllAcceptableItem = items.All(item => item is StorageFolder ||
                                                        (item is StorageFile file && SupportedFileTypesHelper.IsSupportedFileExtension(file.FileType))
                                                        );
                    if (isAllAcceptableItem)
                    {
                        e.AcceptedOperation = DataPackageOperation.Link;
                    }
                }
            }
            finally
            {
                deferral.Complete();
            }
        }
Beispiel #8
0
 /// <summary>
 /// Tokenで取得されたファイルやフォルダ
 /// </summary>
 /// <param name="storageItem"></param>
 /// <param name="thumbnailManager"></param>
 public StorageItemImageSource(IStorageItem storageItem, ThumbnailManager thumbnailManager)
 {
     StorageItem       = storageItem;
     _thumbnailManager = thumbnailManager;
     ItemTypes         = SupportedFileTypesHelper.StorageItemToStorageItemTypes(StorageItem);
 }
        private async Task <ImageCollectionResult> GetImageSourcesAsync(IStorageItem storageItem, bool folderReturnOnlyImages, CancellationToken ct = default)
        {
            if (storageItem is StorageFile file)
            {
                // 画像ファイルを指定された場合だけ特殊対応として、その親フォルダの内容を列挙して返す
                if (SupportedFileTypesHelper.IsSupportedImageFileExtension(file.FileType))
                {
                    // Note: 親フォルダへのアクセス権限無い場合が想定されるが
                    //    アプリとしてユーザーに選択可能としているのはフォルダのみなので無視できる?

                    // TODO: 外部からアプリに画像が渡された時、親フォルダへのアクセス権が無いケースに対応する
                    var parentFolder = await file.GetParentAsync();

                    // 画像ファイルが選ばれた時、そのファイルの所属フォルダをコレクションとして表示する
                    var result = await Task.Run(async() => await GetFolderImagesAsync(parentFolder, ct));

                    try
                    {
                        var images             = new IImageSource[result.ItemsCount];
                        int firstSelectedIndex = 0;
                        if (result.Images != null)
                        {
                            int index = 0;
                            await foreach (var item in result.Images?.WithCancellation(ct))
                            {
                                images[index] = item;
                                if (item.Name == file.Name)
                                {
                                    firstSelectedIndex = index;
                                }
                                index++;
                            }
                        }
                        else
                        {
                            images = new IImageSource[] { new StorageItemImageSource(file, _thumbnailManager) };
                        }

                        return(new ImageCollectionResult()
                        {
                            Images = images,
                            ItemsEnumeratorDisposer = Disposable.Empty,
                            FirstSelectedIndex = firstSelectedIndex,
                            ParentFolderOrArchiveName = parentFolder?.Name
                        });
                    }
                    catch (OperationCanceledException)
                    {
                        return(null);
                    }
                }
                // 圧縮ファイルを展開した中身を列挙して返す
                else if (SupportedFileTypesHelper.IsSupportedArchiveFileExtension(file.FileType))
                {
                    try
                    {
                        var result = await Task.Run(async() => await GetImagesFromArchiveFileAsync(file, ct));

                        return(new ImageCollectionResult()
                        {
                            Images = result.Images,
                            ItemsEnumeratorDisposer = result.Disposer,
                            FirstSelectedIndex = 0,
                            ParentFolderOrArchiveName = file.Name
                        });
                    }
                    catch (OperationCanceledException)
                    {
                        return(null);
                    }
                }
            }
            // フォルダ内のフォルダ、画像ファイル、圧縮ファイルを列挙して返す
            else if (storageItem is StorageFolder folder)
            {
                var result = folderReturnOnlyImages
                    ? await Task.Run(async() => await GetFolderImagesAsync(folder, ct))
                    : await Task.Run(async() => await GetFolderItemsAsync(folder, ct))
                ;

                try
                {
                    var images = new IImageSource[result.ItemsCount];
                    int index  = 0;

                    bool isAllImageFile = true;
                    await foreach (var item in result.Images.WithCancellation(ct))
                    {
                        images[index] = item;
                        index++;

                        isAllImageFile &= (item as StorageItemImageSource)?.ItemTypes == StorageItemTypes.Image;
                    }

                    // フォルダが画像のみを保持しているかどうかをローカルDBに設定する
                    _folderContainerTypeManager.SetContainerType(folder, isAllImageFile ? FolderContainerType.OnlyImages : FolderContainerType.Other);

                    return(new ImageCollectionResult()
                    {
                        Images = images,
                        ItemsEnumeratorDisposer = Disposable.Empty,
                        FirstSelectedIndex = 0,
                        ParentFolderOrArchiveName = String.Empty,
                    });
                }
                catch (OperationCanceledException)
                {
                    return(null);
                }
            }

            throw new NotSupportedException();
        }
        public async Task <(uint ItemsCount, IAsyncEnumerable <IImageSource> Images)> GetFolderItemsAsync(StorageFolder storageFolder, CancellationToken ct)
        {
#if WINDOWS_UWP
            var query      = storageFolder.CreateItemQueryWithOptions(new QueryOptions(CommonFileQuery.DefaultQuery, SupportedFileTypesHelper.GetAllSupportedFileExtensions()));
            var itemsCount = await query.GetItemCountAsync();

            return(itemsCount, AsyncEnumerableItems(itemsCount, query, ct));
#else
            return (itemsCount, AsyncEnumerableImages(
#endif
        }