Ejemplo n.º 1
0
        public static async Task SaveSingleFileAlbumArtAsync(Mediafile mp3File, StorageFile file = null)
        {
            if (mp3File == null || mp3File.Path == null)
            {
                return;
            }

            try
            {
                if (file == null)
                {
                    file = await StorageFile.GetFileFromPathAsync(mp3File.Path);
                }

                var albumartFolder   = ApplicationData.Current.LocalFolder;
                var albumartLocation = albumartFolder.Path + @"\AlbumArts\" + (mp3File.Album + mp3File.LeadArtist).ToLower().ToSha1() + ".jpg";

                if (!SharedLogic.Instance.VerifyFileExists(albumartLocation, 300))
                {
                    bool albumSaved = await TagReaderHelper.SaveAlbumArtsAsync(file, mp3File);

                    mp3File.AttachedPicture = albumSaved ? albumartLocation : null;
                }
                file = null;
            }
            catch (Exception ex)
            {
                BLogger.E("Failed to save albumart.", ex);
                await SharedLogic.Instance.NotificationManager.ShowMessageAsync("Failed to save album art of " + mp3File.OrginalFilename);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Add folder to Library asynchronously.
        /// </summary>
        /// <param name="queryResult">The query result after querying in a specific folder.</param>
        /// <returns></returns>
        public static async Task <IEnumerable <Mediafile> > GetSongsFromFolderAsync(StorageFolder folder, bool useIndexer = true, uint stepSize = 20)
        {
            StorageFileQueryResult queryResult = folder.CreateFileQueryWithOptions(DirectoryWalker.GetQueryOptions(null, useIndexer));

            uint index = 0;
            IReadOnlyList <StorageFile> files = await queryResult.GetFilesAsync(index, stepSize);

            index += stepSize;

            var count = await queryResult.GetItemCountAsync();

            if (count <= 0)
            {
                BLogger.I("No songs found.");
                await SharedLogic.Instance.NotificationManager.ShowMessageAsync("No songs found! Please try again.");

                return(null);
            }
            var   tempList = new List <Mediafile>((int)count);
            short progress = 0;

            try
            {
                // Note that I'm paging in the files as described
                while (files.Count != 0)
                {
                    var fileTask = queryResult.GetFilesAsync(index, stepSize).AsTask();
                    for (int i = 0; i < files.Count; i++)
                    {
                        if (files[i]?.IsAvailable == true)
                        {
                            progress++;
                            Messenger.Instance.NotifyColleagues(MessageTypes.MsgUpdateSongCount, progress);
                            Mediafile mp3File = await TagReaderHelper.CreateMediafile(files[i], false).ConfigureAwait(false); //the core of the whole method.

                            if (mp3File != null)
                            {
                                mp3File.FolderPath = Path.GetDirectoryName(files[i].Path);
                                await SaveSingleFileAlbumArtAsync(mp3File, files[i]).ConfigureAwait(false);

                                SharedLogic.Instance.NotificationManager.ShowStaticMessage(progress + "\\" + count + " Song(s) Loaded");
                                tempList.Add(mp3File);
                            }
                        }
                    }
                    files  = await fileTask;
                    index += stepSize;
                }
            }
            catch (Exception ex)
            {
                string message = ex.Message + "||" + ex.InnerException;
                BLogger.E("Error while importing folder.", ex);
                await SharedLogic.Instance.NotificationManager.ShowMessageAsync(message);
            }
            return(tempList.DistinctBy(f => f.OrginalFilename));
        }
Ejemplo n.º 3
0
        private static async Task <IEnumerable <Mediafile> > GetSongsInFolderAsync(List <string> songs, StorageFolder folder)
        {
            StorageFileQueryResult queryResult = folder.CreateFileQueryWithOptions(DirectoryWalker.GetQueryOptions(null, true, FolderDepth.Shallow));
            uint index = 0, stepSize = 40;
            IReadOnlyList <StorageFile> files = await queryResult.GetFilesAsync(index, stepSize);

            index += stepSize;
            var tempList = new List <Mediafile>(songs.Count);

            // Note that I'm paging in the files as described
            while (files.Count != 0 && tempList.Count <= songs.Count)
            {
                var fileTask = queryResult.GetFilesAsync(index, stepSize).AsTask();
                for (int i = 0; i < files.Count; i++)
                {
                    try
                    {
                        if (songs.Contains(files[i].Path))
                        {
                            Mediafile mp3File = await TagReaderHelper.CreateMediafile(files[i], false).ConfigureAwait(false); //the core of the whole method.

                            if (mp3File != null)
                            {
                                mp3File.FolderPath = Path.GetDirectoryName(files[i].Path);
                                await LibraryHelper.SaveSingleFileAlbumArtAsync(mp3File, files[i]).ConfigureAwait(false);

                                tempList.Add(mp3File);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        BLogger.E("Loading of a playlist song in folder failed.", ex);
                    }
                }
                files = await fileTask.ConfigureAwait(false);

                index += stepSize;
            }

            return(tempList);
        }