Ejemplo n.º 1
0
        /// <summary>
        /// Executes the scan
        /// </summary>
        public void Process()
        {
            Stopped = false;
            SetStatus();

            if (MainForm.WatchFolders.Count == 0)
            {
                MainForm.MusicFileRepo.DeleteAll().Wait();
                MainForm.PlaylistFileRepo.DeleteAll().Wait();
                MainForm.SetDiscoveredFilesLabel("0");
                MainForm.SetIssuesLabel("0");
                MainForm.SetUploadedLabel("0");
            }

            CurrentMusicFiles = MainForm.MusicFileRepo.LoadAll().Result;
            foreach (var musicFile in CurrentMusicFiles)
            {
                CurrentProcessingFilesHash.Add(musicFile.Path);
            }

            CurrentPlaylistFiles = MainForm.PlaylistFileRepo.LoadAll().Result;
            foreach (var playlistFile in CurrentPlaylistFiles)
            {
                CurrentProcessingFilesHash.Add(playlistFile.Path);
            }

            //
            // Get files to add - Cross reference with the DB
            //
            foreach (var watchFolder in MainForm.WatchFolders)
            {
                if (MainFormAborting())
                {
                    return;
                }

                if (Directory.Exists(watchFolder.Path))
                {
                    foreach (var file in FastDirectoryEnumerator.EnumerateFiles(watchFolder.Path,
                                                                                "*.*",
                                                                                SearchOption.AllDirectories))
                    {
                        try
                        {
                            if (MainFormAborting())
                            {
                                return;
                            }

                            if (!CurrentProcessingFilesHash.Contains(file.Path))
                            {
                                if (Path.GetExtension(file.Name.ToLower()).In(Global.SupportedMusicFiles) ||
                                    Path.GetExtension(file.Name.ToLower()).In(Global.SupportedPlaylistFiles))
                                {
                                    NewFiles.Add(file);
                                    NewFilesHash.Add(file.Path);
                                }
                            }

                            if (MainFormAborting())
                            {
                                return;
                            }

                            if (Path.GetExtension(file.Name.ToLower()).In(Global.SupportedMusicFiles) ||
                                Path.GetExtension(file.Name.ToLower()).In(Global.SupportedPlaylistFiles))
                            {
                                DiscoveredFilesHash.Add(file.Path);
                            }
                        }
                        catch (Exception e)
                        {
                            Logger.Log(
                                e,
                                "FileScanner.Process - Error reading file (possibly removed): " +
                                file.Path,
                                Log.LogTypeEnum.Error,
                                false);
                        }
                    }
                }
                else
                {
                    if (MainFormAborting())
                    {
                        return;
                    }

                    SetStatus("Error: Watch folder directory does not exists: " + watchFolder.Path.EllipsisPath(100), "Directory ");
                    ThreadHelper.SafeSleep(5000);
                }
            }

            //
            // Get files to delete - Cross reference with the DB
            //

            foreach (var musicFile in CurrentMusicFiles)
            {
                if (MainFormAborting())
                {
                    return;
                }

                if (!DiscoveredFilesHash.Contains(musicFile.Path))
                {
                    MusicFilesToDelete.Add(musicFile);
                }
            }

            foreach (var playlistFile in CurrentPlaylistFiles)
            {
                if (MainFormAborting())
                {
                    return;
                }

                if (!DiscoveredFilesHash.Contains(playlistFile.Path))
                {
                    PlaylistFilesToDelete.Add(playlistFile);
                }
            }

            using (var conn = new SQLiteConnection("Data Source=" + Global.DbLocation + ";cache=shared"))
            {
                SetStatus();
                conn.Open();

                try
                {
                    int count = 0;
                    foreach (var file in NewFiles)
                    {
                        if (MainFormAborting(conn))
                        {
                            return;
                        }

                        count++;
                        if (count % 100 == 0)
                        {
                            MainForm.SetDiscoveredFilesLabel(count.ToString());
                        }

                        SetStatus();

                        if (Path.GetExtension(file.Path).ToLower().In(Global.SupportedMusicFiles))
                        {
                            AddMusiFileToDB(conn, new MusicFile(file.Path));
                        }

                        else if (Path.GetExtension(file.Path).ToLower().In(Global.SupportedPlaylistFiles))
                        {
                            AddPlaylistFileToDB(conn, new PlaylistFile(file.Path, file.LastWriteTime));
                        }
                    }

                    if (MainFormAborting(conn))
                    {
                        return;
                    }

                    foreach (var musicFile in MusicFilesToDelete)
                    {
                        if (MainFormAborting(conn))
                        {
                            return;
                        }

                        RemoveMusicFileFromDB(conn, musicFile.Path);
                    }
                    ;

                    if (MainFormAborting(conn))
                    {
                        return;
                    }

                    foreach (var playlistFile in PlaylistFilesToDelete)
                    {
                        if (MainFormAborting(conn))
                        {
                            return;
                        }

                        RemovePlaylistFileFromDb(conn, playlistFile.Path);
                    }
                    ;

                    MainForm.SetDiscoveredFilesLabel(MainForm.MusicFileRepo.CountAll().Result.ToString());
                }
                catch { }

                conn.Close();
            }

            SetStatus(MainForm.ConnectedToYTMusic ? "Ready" : "Waiting for YouTube Music connection", "Waiting for YouTube Music connection");
            Stopped = true;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Executes the scan
        /// </summary>
        public void Process()
        {
            SetStatus();

            CurrentMusicFiles = MainForm.MusicFileRepo.LoadAll().Result;
            foreach (var musicFile in CurrentMusicFiles)
            {
                CurrentMusicFilesHash.Add(musicFile.Path);
            }

            if (MainForm.WatchFolders.Count == 0)
            {
                MainForm.MusicFileRepo.DeleteAll().Wait();
            }

            //
            // Get files to add - Cross reference with the DB
            //
            foreach (var watchFolder in MainForm.WatchFolders)
            {
                if (MainForm.Aborting)
                {
                    SetStatus("Idle", "Idle");
                    return;
                }

                foreach (var file in FastDirectoryEnumerator.EnumerateFiles(watchFolder.Path,
                                                                            "*.*",
                                                                            SearchOption.AllDirectories))
                {
                    if (MainForm.Aborting)
                    {
                        SetStatus("Idle", "Idle");
                        return;
                    }

                    if (!CurrentMusicFilesHash.Contains(file.Path))
                    {
                        if (Path.GetExtension(file.Name.ToLower()).In(Global.SupportedFiles))
                        {
                            NewFiles.Add(file);
                            NewFilesHash.Add(file.Path);
                        }
                    }

                    if (Path.GetExtension(file.Name.ToLower()).In(Global.SupportedFiles))
                    {
                        DiscoveredFilesHash.Add(file.Path);
                    }
                }
            }

            //
            // Get files to delete - Cross reference with the DB
            //
            foreach (var musicFile in CurrentMusicFiles)
            {
                if (MainForm.Aborting)
                {
                    SetStatus("Idle", "Idle");
                    return;
                }

                if (!DiscoveredFilesHash.Contains(musicFile.Path))
                {
                    MusicFilesToDelete.Add(musicFile);
                }
            }

            using (var conn = new SQLiteConnection("Data Source=" + Global.DbLocation))
            {
                SetStatus();
                conn.Open();
                int count = 0;
                foreach (var file in NewFiles)
                {
                    if (MainForm.Aborting)
                    {
                        MainForm.SetStatusMessage("Idle", "Idle");
                        return;
                    }

                    count++;
                    if (count > MainForm.InitialFilesCount)
                    {
                        if (count % 100 == 0)
                        {
                            MainForm.SetDiscoveredFilesLabel(count.ToString());
                        }
                    }

                    SetStatus();
                    AddToDB(conn, new MusicFile(file.Path));
                }

                if (MainForm.Aborting)
                {
                    SetStatus("Idle", "Idle");
                    return;
                }

                count = 0;
                foreach (var musicFile in MusicFilesToDelete)
                {
                    if (MainForm.Aborting)
                    {
                        MainForm.SetStatusMessage("Idle", "Idle");
                        return;
                    }

                    count++;
                    RemoveFromDB(conn, musicFile.Path);
                }
                ;

                MainForm.SetDiscoveredFilesLabel(MainForm.MusicFileRepo.CountAll().Result.ToString());
            }

            SetStatus(MainForm.ConnectedToYTMusic ? "Ready" : "Waiting for YouTube Music connection", "Waiting for YouTube Music connection");
        }