示例#1
0
        static public async Task <IRandomAccessStream> RetrieveStorageFileAsStreamAsync(StorageFile file, bool parentFolder)
        {
            var coverStream = await CoverExtractor.GetCoverStreamFromFileAsync(file);

            if (coverStream != null)
            {
                return(coverStream);
            }

            if (parentFolder)
            {
                var parent = await file.GetParentAsync();

                if (parent != null)
                {
                    var f =
                        await parent.TryGetItemAsync(CommonSharedStrings.FolderJpg) as StorageFile ??
                        await parent.TryGetItemAsync(CommonSharedStrings.CoverJpg) as StorageFile ??
                        await parent.TryGetItemAsync(CommonSharedStrings.FolderPng) as StorageFile ??
                        await parent.TryGetItemAsync(CommonSharedStrings.CoverPng) as StorageFile;

                    if (f != null)
                    {
                        return(await f.OpenReadAsync());
                    }
                }
            }
            return(null);
        }
示例#2
0
        static private async Task <BitmapImage> RetrieveStorageFileAsync(IStorageFile file, bool parentFolder)
        {
            BitmapImage img = null;

            using (var coverStream = await CoverExtractor.GetCoverStreamFromFileAsync(file))
            {
                if (coverStream != null)
                {
                    img = await CreateBitmapFromStreamAsync(coverStream);
                }
            }

            if (img == null && file is IStorageItem2 && parentFolder)
            {
                // The file itself does not contain a cover image.
                // Search in parent folder instead.
                var parent = await(file as IStorageItem2).GetParentAsync();
                if (parent != null)
                {
                    var f =
                        await parent.TryGetItemAsync(CommonSharedStrings.FolderJpg) as IStorageFile ??
                        await parent.TryGetItemAsync(CommonSharedStrings.CoverJpg) as IStorageFile ??
                        await parent.TryGetItemAsync(CommonSharedStrings.FolderPng) as IStorageFile ??
                        await parent.TryGetItemAsync(CommonSharedStrings.CoverPng) as IStorageFile;

                    if (f != null)
                    {
                        using (var s = await f.OpenReadAsync())
                            img = await CreateBitmapFromStreamAsync(s);
                    }
                }
            }

            // Cache even if null.
            _bitmapDiskCache.TryAdd(file.Path, img);
            return(img);
        }
示例#3
0
        private static async Task InternalFetchAlbumAsync(string artist, string album, string filePath, bool online)
        {
            if (filePath != null)
            {
                var file = await NativeMethods.GetStorageFileFromPathAsync(filePath);

                if (file != null)
                {
                    using (var coverStream = await CoverExtractor.GetCoverStreamFromFileAsync(file))
                    {
                        if (coverStream != null)
                        {
                            using (var sr = new BinaryReader(coverStream.AsStream()))
                            {
                                var content = sr.ReadBytes((int)coverStream.Size);
                                await ThumbnailManager.AddAsync(artist, album, content, true);

                                return;
                            }
                        }
                    }

                    // The file itself does not contain a cover image.
                    // Search in parent folder instead.
                    if (file is IStorageItem2)
                    {
                        var parent = await(file as IStorageItem2).GetParentAsync();
                        if (parent != null)
                        {
                            var f =
                                await parent.TryGetItemAsync(CommonSharedStrings.FolderJpg) as IStorageFile ??
                                await parent.TryGetItemAsync(CommonSharedStrings.CoverJpg) as IStorageFile ??
                                await parent.TryGetItemAsync(CommonSharedStrings.FolderPng) as IStorageFile ??
                                await parent.TryGetItemAsync(CommonSharedStrings.CoverPng) as IStorageFile;

                            if (f != null)
                            {
                                using (var s = await f.OpenReadAsync())
                                    using (var sr = new BinaryReader(s.AsStream()))
                                    {
                                        var content = sr.ReadBytes((int)s.Size);
                                        await ThumbnailManager.AddAsync(artist, album, content, true);

                                        return;
                                    }
                            }
                        }
                    }
                }
            }

            if (online && AggreatedOnlineMetadata.Availability)
            {
                // There is no cover image in the parent directory, or the parent directory cannot be retrieved.
                // Search online
                try
                {
                    var searchResult = (await AggreatedOnlineMetadata.GetAlbumsAsync(album, artist)).FirstOrDefault();

                    if (searchResult != null)
                    {
                        using (var client = new HttpClient())
                        {
                            using (var response = await client.GetAsync(searchResult.Thumbnail))
                            {
                                response.EnsureSuccessStatusCode();
                                response.Content.Headers.TryGetValues("Content-Length", out var ctlength);
                                var lengthValue = ctlength.FirstOrDefault();

                                if (lengthValue == null ||
                                    !ulong.TryParse(lengthValue, out ulong length))
                                {
                                    length = 0;
                                }

                                if (length <= 10 * 1024 * 1024)
                                {
                                    var content = await response.Content.ReadAsByteArrayAsync();

                                    if (content.Length >= (int)length)
                                    {
                                        await ThumbnailManager.AddAsync(artist, album, content, true);

                                        return;
                                    }
                                }
                            }
                        }
                    }
                }
                catch { }
                // Only write placeholder file when we don't have online results.
                await ThumbnailManager.AddAsync(artist, album, new byte[0], true);
            }
        }