Exemple #1
0
        public async Task <TagImage> ShowRandomImage(StorageFolder imageFolder, string tag)
        {
            CommonFileQuery query        = CommonFileQuery.DefaultQuery;
            var             queryOptions = new QueryOptions(query, new[] { ".jpg", ".jpeg" })
            {
                FolderDepth = FolderDepth.Deep
            };

            _ = imageFolder.CreateFileQueryWithOptions(queryOptions);

            var images = await imageFolder.GetFilesAsync();

            TagImage image = await GenerateImage(images, tag);

            while (image.Tags.Contains(tag) && taggedImages.Count < images.Count)
            {
                if (taggedImages.Contains(image.GetHashCode()) == false)
                {
                    taggedImages.Add(image.GetHashCode());
                }
                image = await GenerateImage(images, tag);
            }

            return(image);
        }
        /// <summary>
        /// Get files from <see cref="storageFolder"/>.
        /// </summary>
        /// <param name="fileQuery">Define the logic for sorting the files.</param>
        /// <param name="filterByExtension">Defines a file extension. This method will return only files with this extension.</param>
        /// <param name="top">Define how many files to return. This can be useful when the directory has a lot of files, in that case
        /// GetFilesAsync will have a performance hit.</param>
        private IEnumerable <StorageFile> GetFiles(CommonFileQuery fileQuery, string filterByExtension, uint top)
        {
            IEnumerable <StorageFile> files = new List <StorageFile>();

            try
            {
                if (this.StorageFolder != null)
                {
                    files = this.StorageFolder
                            .GetFilesAsync(CommonFileQuery.DefaultQuery, 0, top)
                            .AsTask()
                            .ConfigureAwait(false)
                            .GetAwaiter()
                            .GetResult();

                    // a low 'top' value might cause a bug if there are more then 50 tmp files. This is a trade off,
                    // because reading all the files (no top) has a performance hit and there is no expectation to have 50 tmp files.
                    return(files.Where((file) => Path.GetExtension(file.Name).Equals(filterByExtension, StringComparison.OrdinalIgnoreCase)));
                }
            }
            catch (Exception e)
            {
                string msg = string.Format(CultureInfo.InvariantCulture, "Peek failed while get files from storage. Exception: " + e);
                CoreEventSource.Log.LogVerbose(msg);
            }

            return(files);
        }
        /// <summary>
        /// Gets an index-based range of files from the list of all files in the current folder. Also gets the files from the subfolders of the current folder when the value of the query argument is something other than <see cref="CommonFileQuery.DefaultQuery"/>. Files are sorted based on the specified value from the <see cref="CommonFileQuery"/> enumeration.
        /// </summary>
        /// <param name="query">One of the enumeration values that specifies how to sort the files and determines whether the query is shallow or deep.</param>
        /// <param name="startIndex">The zero-based index of the first file in the range to retrieve.</param>
        /// <param name="maxItemsToRetrieve">The maximum number of files to retrieve.</param>
        /// <returns>
        /// When this method completes successfully, it returns a flat list of files, sorted as specified by query.
        /// The list is of type <see cref="StorageFile"/>. Each file in the list is represented by a <see cref="StorageFile"/> object.
        /// </returns>
        ///<remarks>
        /// This method is exclusive of nanoFramework and it's not available in the UWP API.
        /// The equivalent method would be GetFilesAsync(CommonFileQuery, UInt32, UInt32).
        ///</remarks>
        public StorageFile[] GetFiles(CommonFileQuery query, UInt32 startIndex, UInt32 maxItemsToRetrieve)
        {
            // check CommonFileQuery
            if (query != CommonFileQuery.DefaultQuery)
            {
#pragma warning disable S3928 // Parameter names used into ArgumentException constructors should match an existing one
                throw new ArgumentException();
#pragma warning restore S3928 // Parameter names used into ArgumentException constructors should match an existing one
            }

            return(GetStorageFilesNative(startIndex, maxItemsToRetrieve));
        }
Exemple #4
0
        public async Task <List <PreviewImage> > ShowImages(StorageFolder imageFolder)
        {
            CommonFileQuery query        = CommonFileQuery.DefaultQuery;
            var             queryOptions = new QueryOptions(query, new[] { ".png", ".jpg" })
            {
                FolderDepth = FolderDepth.Deep
            };

            _ = imageFolder.CreateFileQueryWithOptions(queryOptions);


            return(await GenerateImages(await imageFolder.GetFilesAsync()));
        }
 public async Task <IReadOnlyList <IStorageItem> > GetAllItemsInFolder(StorageFolder folder, CommonFileQuery query)
 {
     return(await folder.GetItemsAsync());
 }
Exemple #6
0
 IAsyncOperation <IReadOnlyList <StorageFile> > IStorageFolderQueryOperations.GetFilesAsync(CommonFileQuery query)
 {
     return(AsyncInfo.Run <IReadOnlyList <StorageFile> >(async(cancellationToken) =>
     {
         return await Task.WhenAll((await GetFilesAsync(query)).Select(x => x.ToStorageFileAsync().AsTask()));
     }));
 }
        //        public IAsyncOperation<IReadOnlyList<StorageFolder>> GetFoldersAsync(CommonFolderQuery query)
        //        { }

        //        public IAsyncOperation<IReadOnlyList<StorageFolder>> GetFoldersAsync(CommonFolderQuery query, UInt32 startIndex, UInt32 maxItemsToRetrieve)
        //        { }

        //        public IAsyncOperation<IndexedState> GetIndexedStateAsync()
        //        { }

        //        public IAsyncOperation<IStorageItem> GetItemAsync(String name)
        //        { }

        //        public IAsyncOperation<IReadOnlyList<IStorageItem>> GetItemsAsync()
        //        { }

        //        public IAsyncOperation<IReadOnlyList<IStorageItem>> GetItemsAsync(UInt32 startIndex, UInt32 maxItemsToRetrieve)
        //        { }

        //        public IAsyncOperation<StorageFolder> GetParentAsync()
        //        { }

        //        public IAsyncOperation<StorageItemThumbnail> GetScaledImageAsThumbnailAsync(ThumbnailMode mode)
        //        { }

        //        public IAsyncOperation<StorageItemThumbnail> GetScaledImageAsThumbnailAsync(ThumbnailMode mode, UInt32 requestedSize)
        //        { }

        //        public IAsyncOperation<StorageItemThumbnail> GetScaledImageAsThumbnailAsync(ThumbnailMode mode, UInt32 requestedSize, ThumbnailOptions options)
        //        { }

        //        public IAsyncOperation<StorageItemThumbnail> GetThumbnailAsync(ThumbnailMode mode)
        //        { }

        //        public IAsyncOperation<StorageItemThumbnail> GetThumbnailAsync(ThumbnailMode mode, UInt32 requestedSize)
        //        { }

        //        public IAsyncOperation<StorageItemThumbnail> GetThumbnailAsync(ThumbnailMode mode, UInt32 requestedSize, ThumbnailOptions options)
        //        { }

        /// <summary>
        /// Indicates whether the current folder supports the specified <see cref="CommonFileQuery"/>.
        /// </summary>
        /// <param name="query">The value to test.</param>
        /// <returns>
        /// True if the folder supports the specified <see cref="CommonFileQuery"/> otherwise, false.
        /// </returns>
        public bool IsCommonFileQuerySupported(CommonFileQuery query)
        {
            return(query == CommonFileQuery.DefaultQuery);
        }
Exemple #8
0
 public abstract bool IsCommonFileQuerySupported(CommonFileQuery query);
Exemple #9
0
 IAsyncOperation <IReadOnlyList <StorageFile> > IStorageFolderQueryOperations.GetFilesAsync(CommonFileQuery query, uint startIndex, uint maxItemsToRetrieve)
 {
     return(AsyncInfo.Run <IReadOnlyList <StorageFile> >(async(cancellationToken) =>
     {
         return await Task.WhenAll((await GetFilesAsync(query, startIndex, maxItemsToRetrieve)).Select(x => x.ToStorageFileAsync().AsTask()));
     }));
 }
        /// <summary>
        /// Get files from <see cref="storageFolder"/>.
        /// </summary>
        /// <param name="fileQuery">Define the logic for sorting the files.</param>
        /// <param name="filterByExtension">Defines a file extension. This method will return only files with this extension.</param>
        /// <param name="top">Define how many files to return. This can be useful when the directory has a lot of files, in that case 
        /// GetFilesAsync will have a performance hit.</param>
        private IEnumerable<StorageFile> GetFiles(CommonFileQuery fileQuery, string filterByExtension, uint top)
        {
            IEnumerable<StorageFile> files = new List<StorageFile>();

            try
            {
                if (this.StorageFolder != null)
                {
                    files = this.StorageFolder
                                .GetFilesAsync(CommonFileQuery.DefaultQuery, 0, top)
                                .AsTask()
                                .ConfigureAwait(false)
                                .GetAwaiter()
                                .GetResult();

                    // a low 'top' value might cause a bug if there are more then 50 tmp files. This is a trade off, 
                    // because reading all the files (no top) has a performance hit and there is no expectation to have 50 tmp files. 
                    return files.Where((file) => Path.GetExtension(file.Name).Equals(filterByExtension, StringComparison.OrdinalIgnoreCase));
                }
            }
            catch (Exception e)
            {
                string msg = string.Format(CultureInfo.InvariantCulture, "Peek failed while get files from storage. Exception: " + e);
                CoreEventSource.Log.LogVerbose(msg);
            }

            return files;
        }
Exemple #11
0
 public abstract IAsyncOperation <IReadOnlyList <BaseStorageFile> > GetFilesAsync(CommonFileQuery query);
Exemple #12
0
 public override StorageFileQueryResult CreateFileQuery(CommonFileQuery query) => throw new NotSupportedException();
Exemple #13
0
 public override IAsyncOperation <IReadOnlyList <BaseStorageFile> > GetFilesAsync(CommonFileQuery query)
 {
     return(AsyncInfo.Run <IReadOnlyList <BaseStorageFile> >(async(cancellationToken) =>
     {
         return await GetFilesAsync();
     }));
 }
Exemple #14
0
        private static async Task <IReadOnlyList <StorageFile> > GetMediaFromFolder(StorageFolder folder,
                                                                                    CommonFileQuery query)
        {
            IReadOnlyList <StorageFile> files = null;
            StorageFileQueryResult      fileQuery;
            var queryOptions = new QueryOptions(query,
                                                new List <string>
            {
                ".3g2",
                ".3gp",
                ".3gp2",
                ".3gpp",
                ".amv",
                ".asf",
                ".avi",
                ".divx",
                ".drc",
                ".dv",
                ".f4v",
                ".flv",
                ".gvi",
                ".gxf",
                ".ismv",
                ".iso",
                ".m1v",
                ".m2v",
                ".m2t",
                ".m2ts",
                ".m3u8",
                ".mkv",
                ".mov",
                ".mp2",
                ".mp2v",
                ".mp4",
                ".mp4v",
                ".mpe",
                ".mpeg",
                ".mpeg1",
                ".mpeg2",
                ".mpeg4",
                ".mpg",
                ".mpv2",
                ".mts",
                ".mtv",
                ".mxf",
                ".mxg",
                ".nsv",
                ".nut",
                ".nuv",
                ".ogm",
                ".ogv",
                ".ogx",
                ".ps",
                ".rec",
                ".rm",
                ".rmvb",
                ".tob",
                ".ts",
                ".tts",
                ".vob",
                ".vro",
                ".webm",
                ".wm",
                ".wmv",
                ".wtv",
                ".xesc",
            });

            try
            {
                fileQuery = folder.CreateFileQueryWithOptions(queryOptions);

                files = await fileQuery.GetFilesAsync();
            }
            catch (Exception ex)
            {
                Debug.WriteLine("exception listing files");
                Debug.WriteLine(ex.ToString());
            }
            // DLNA folders don't support advanced file listings, us a basic file query
            if (files == null)
            {
                fileQuery = folder.CreateFileQuery(CommonFileQuery.OrderByName);
                files     = await fileQuery.GetFilesAsync();
            }

            return(files);
        }
Exemple #15
0
 public override bool IsCommonFileQuerySupported(CommonFileQuery query)
 {
     return(Folder.IsCommonFileQuerySupported(query));
 }
Exemple #16
0
 public override IAsyncOperation <IReadOnlyList <BaseStorageFile> > GetFilesAsync(CommonFileQuery query)
 {
     return(AsyncInfo.Run <IReadOnlyList <BaseStorageFile> >(async(cancellationToken) =>
     {
         var items = await Folder.GetFilesAsync(query);
         return items.Select(x => new SystemStorageFile(x)).ToList();
     }));
 }
Exemple #17
0
 public override StorageFileQueryResult CreateFileQuery(CommonFileQuery query)
 {
     return(Folder.CreateFileQuery(query));
 }
        private static async Task<IReadOnlyList<StorageFile>> GetMediaFromFolder(StorageFolder folder,
                                                                            CommonFileQuery query)
        {
            IReadOnlyList<StorageFile> files = null;
            StorageFileQueryResult fileQuery;
            var queryOptions = new QueryOptions(query,
                                               new List<string>
                                               {
                                                   ".3g2",
                                                   ".3gp",
                                                   ".3gp2",
                                                   ".3gpp",
                                                   ".amv",
                                                   ".asf",
                                                   ".avi",
                                                   ".divx",
                                                   ".drc",
                                                   ".dv",
                                                   ".f4v",
                                                   ".flv",
                                                   ".gvi",
                                                   ".gxf",
                                                   ".ismv",
                                                   ".iso",
                                                   ".m1v",
                                                   ".m2v",
                                                   ".m2t",
                                                   ".m2ts",
                                                   ".m3u8",
                                                   ".mkv",
                                                   ".mov",
                                                   ".mp2",
                                                   ".mp2v",
                                                   ".mp4",
                                                   ".mp4v",
                                                   ".mpe",
                                                   ".mpeg",
                                                   ".mpeg1",
                                                   ".mpeg2",
                                                   ".mpeg4",
                                                   ".mpg",
                                                   ".mpv2",
                                                   ".mts",
                                                   ".mtv",
                                                   ".mxf",
                                                   ".mxg",
                                                   ".nsv",
                                                   ".nut",
                                                   ".nuv",
                                                   ".ogm",
                                                   ".ogv",
                                                   ".ogx",
                                                   ".ps",
                                                   ".rec",
                                                   ".rm",
                                                   ".rmvb",
                                                   ".tob",
                                                   ".ts",
                                                   ".tts",
                                                   ".vob",
                                                   ".vro",
                                                   ".webm",
                                                   ".wm",
                                                   ".wmv",
                                                   ".wtv",
                                                   ".xesc",
                                               });

            try
            {
                fileQuery = folder.CreateFileQueryWithOptions(queryOptions);

                files = await fileQuery.GetFilesAsync();
            }
            catch (Exception ex)
            {
                Debug.WriteLine("exception listing files");
                Debug.WriteLine(ex.ToString());
            }
            // DLNA folders don't support advanced file listings, us a basic file query
            if (files == null)
            {
                fileQuery = folder.CreateFileQuery(CommonFileQuery.OrderByName);
                files = await fileQuery.GetFilesAsync();
            }

            return files;
        }
Exemple #19
0
 public override IAsyncOperation <IReadOnlyList <BaseStorageFile> > GetFilesAsync(CommonFileQuery query, uint startIndex, uint maxItemsToRetrieve)
 {
     return(AsyncInfo.Run <IReadOnlyList <BaseStorageFile> >(async(cancellationToken) =>
     {
         var items = await GetFilesAsync();
         return items.Skip((int)startIndex).Take((int)maxItemsToRetrieve).ToList();
     }));
 }
Exemple #20
0
 public abstract StorageFileQueryResult CreateFileQuery(CommonFileQuery query);
Exemple #21
0
 public override bool IsCommonFileQuerySupported(CommonFileQuery query) => false;
Exemple #22
0
 public abstract IAsyncOperation <IReadOnlyList <BaseStorageFile> > GetFilesAsync(CommonFileQuery query, uint startIndex, uint maxItemsToRetrieve);