/// <summary> /// Tries to create a movie action item for a scan from a file. /// </summary> /// <param name="file">The file to create movie action from</param> /// <param name="item">The resulting movie action</param> /// <returns>Whether the file was matched to a movie</returns> private bool CreateMovieAction(OrgPath file, string matchString, out OrgItem item, bool threaded, bool fast, bool skipMatching, Movie knownMovie) { // Initialize item item = new OrgItem(OrgAction.None, file.Path, FileCategory.MovieVideo, file.OrgFolder); // Check if sample! if (matchString.ToLower().Contains("sample")) { item.Action = OrgAction.Delete; return(false); } // Try to match file to movie string search = Path.GetFileNameWithoutExtension(matchString); // Search for match to movie Movie searchResult = null; bool searchSucess = false; if (!skipMatching) { searchSucess = SearchHelper.MovieSearch.ContentMatch(search, string.Empty, string.Empty, fast, threaded, out searchResult, knownMovie); } // Add closest match item if (searchSucess) { // Get root folder ContentRootFolder rootFolder; string path; if (Settings.GetMovieFolderForContent(searchResult, out rootFolder)) { searchResult.RootFolder = rootFolder.FullPath; } else { searchResult.RootFolder = NO_MOVIE_FOLDER; item.Action = OrgAction.NoRootFolder; } if (item.Action != OrgAction.NoRootFolder) { item.Action = file.Copy ? OrgAction.Copy : OrgAction.Move; } item.DestinationPath = searchResult.BuildFilePath(file.Path); if (File.Exists(item.DestinationPath)) { item.Action = OrgAction.AlreadyExists; } searchResult.Path = searchResult.BuildFolderPath(); item.Movie = searchResult; if (item.Action == OrgAction.AlreadyExists || item.Action == OrgAction.NoRootFolder) { item.Enable = false; } else { item.Enable = true; } return(true); } return(false); }
/// <summary> /// Recursively run a scan on a set of movie folders and their sub-folders /// </summary> /// <param name="folders">Movie folder to perform scan on</param> /// <param name="scanResults">Scan results to build on.</param> /// <param name="queuedItems">Items already in the queue</param> private void RunScan(List <ContentRootFolder> folders, List <OrgItem> scanResults, List <OrgItem> queuedItems) { // Get unknown files (files not in content folder) from root folders OnProgressChange(ScanProcess.FileCollect, string.Empty, 0); List <OrgPath> files = GetUnknownFiles(folders); // Initialize item numbering int number = 0; // Go through unknwon files and try to match each file to a movie for (int i = 0; i < files.Count; i++) { // Check for cancellation if (scanCanceled) { break; } // Update progress OnProgressChange(ScanProcess.Movie, files[i].Path, (int)Math.Round((double)i / files.Count * 70)); // Categorize the file FileCategory fileCat = FileHelper.CategorizeFile(files[i], files[i].Path); // Check that video file (tv is okay, may match incorrectly) if (fileCat != FileCategory.MovieVideo && fileCat != FileCategory.TvVideo && fileCat != FileCategory.Trash) { continue; } // Check that file is not already in the queue bool alreadyQueued = false; foreach (OrgItem queued in queuedItems) { if (queued.SourcePath == files[i].Path) { OrgItem newItem = new OrgItem(queued); newItem.Action = OrgAction.Queued; newItem.Number = number++; scanResults.Add(newItem); alreadyQueued = true; break; } } if (alreadyQueued) { continue; } // Check for trash if (fileCat == FileCategory.Trash) { OrgItem delItem = new OrgItem(OrgAction.Delete, files[i].Path, fileCat, files[i].OrgFolder); scanResults.Add(delItem); continue; } // Try to match file to movie string search = Path.GetFileNameWithoutExtension(files[i].Path); Movie searchResult; bool searchSucess = SearchHelper.MovieSearch.ContentMatch(search, files[i].RootFolder.FullPath, string.Empty, false, true, out searchResult, null); // Add closest match item OrgItem item = new OrgItem(OrgAction.None, files[i].Path, fileCat, files[i].OrgFolder); item.Number = number++; if (searchSucess && !string.IsNullOrEmpty(searchResult.DatabaseName)) { item.Action = OrgAction.Move; item.DestinationPath = searchResult.BuildFilePath(files[i].Path); searchResult.Path = searchResult.BuildFolderPath(); item.Movie = searchResult; item.Enable = true; } scanResults.Add(item); } // Get knwon movie files (files from within content folders) files = GetKnownFiles(folders); // Go through each known file and check if renaming is needed for (int i = 0; i < files.Count; i++) { // Check for cancellation if (scanCanceled) { break; } // Update progress OnProgressChange(ScanProcess.Movie, files[i].Path, (int)Math.Round((double)i / files.Count * 20) + 70); // Categorize the file FileCategory fileCat = FileHelper.CategorizeFile(files[i], files[i].Path); // Check that video file (tv is okay, may match incorrectly) if (fileCat != FileCategory.MovieVideo && fileCat != FileCategory.TvVideo && fileCat != FileCategory.Trash) { continue; } // Check that movie is valide Movie movie = (Movie)files[i].Content; if (movie == null || string.IsNullOrEmpty(movie.DatabaseName)) { continue; } // Check that file is not already in the queue bool alreadyQueued = false; foreach (OrgItem queued in queuedItems) { if (queued.SourcePath == files[i].Path) { alreadyQueued = true; break; } } if (alreadyQueued) { continue; } // Check for trash if (fileCat == FileCategory.Trash) { OrgItem delItem = new OrgItem(OrgAction.Delete, files[i].Path, fileCat, files[i].OrgFolder); scanResults.Add(delItem); continue; } // Check if file needs to be renamed string newPath = movie.BuildFilePathNoFolderChanges(files[i].Path); if (newPath != files[i].Path && !File.Exists(newPath)) { // Add rename to results OrgItem item = new OrgItem(OrgAction.Rename, files[i].Path, FileCategory.MovieVideo, files[i].OrgFolder); item.Number = number++; if (Path.GetDirectoryName(newPath) != Path.GetDirectoryName(files[i].Path)) { item.Action = OrgAction.Move; } item.DestinationPath = newPath; item.Movie = movie; item.Enable = true; scanResults.Add(item); } } // Check if any movie folders need to be renamed! foreach (Movie movie in Organization.GetContentFromRootFolders(folders)) { if (!string.IsNullOrEmpty(movie.DatabaseName) && movie.Path != movie.BuildFolderPath()) { OrgItem item = new OrgItem(OrgAction.Rename, movie.Path, FileCategory.Folder, movie, movie.BuildFolderPath(), null); item.Enable = true; item.Number = number++; scanResults.Add(item); } } // Update progress OnProgressChange(ScanProcess.Movie, string.Empty, 100); }