public void Init(ImportFolder ifldr) { try { ServerInfo.Instance.RefreshFolderProviders(); importFldr = new ImportFolder() { ImportFolderID = ifldr.ImportFolderID, ImportFolderType = ifldr.ImportFolderType, ImportFolderLocation = ifldr.ImportFolderLocation, ImportFolderName = ifldr.ImportFolderName, IsDropSource = ifldr.IsDropSource, IsDropDestination = ifldr.IsDropDestination, CloudID = ifldr.CloudID.HasValue && ifldr.CloudID == 0 ? null : ifldr.CloudID, IsWatched = ifldr.IsWatched }; txtImportFolderLocation.Text = importFldr.ImportFolderLocation; chkDropDestination.IsChecked = importFldr.IsDropDestination == 1; chkDropSource.IsChecked = importFldr.IsDropSource == 1; chkIsWatched.IsChecked = importFldr.IsWatched == 1; if (ifldr.CloudID.HasValue) comboProvider.SelectedItem = ServerInfo.Instance.FolderProviders.FirstOrDefault(a => a.CloudID == ifldr.CloudID.Value); else comboProvider.SelectedIndex = 0; txtImportFolderLocation.Focus(); } catch (Exception ex) { Utils.ShowErrorMessage(ex); } }
public void Save(ImportFolder obj) { using (var session = JMMService.SessionFactory.OpenSession()) { // populate the database using (var transaction = session.BeginTransaction()) { session.SaveOrUpdate(obj); transaction.Commit(); } } }
public void Init(ImportFolder ifldr) { try { importFldr = ifldr; txtImportFolderLocation.Text = importFldr.ImportFolderLocation; chkDropDestination.IsChecked = importFldr.IsDropDestination == 1; chkDropSource.IsChecked = importFldr.IsDropSource == 1; chkIsWatched.IsChecked = importFldr.IsWatched == 1; txtImportFolderLocation.Focus(); } catch (Exception ex) { Utils.ShowErrorMessage(ex); } }
private static void CreateImportFolders2() { logger.Debug("Creating shares..."); ImportFolderRepository repNetShares = new ImportFolderRepository(); ImportFolder sn = repNetShares.GetByImportLocation(@"F:\Anime1"); if (sn == null) { sn = new ImportFolder(); sn.ImportFolderType = (int)ImportFolderType.HDD; sn.ImportFolderName = "Anime1"; sn.ImportFolderLocation = @"F:\Anime1"; repNetShares.Save(sn); } sn = repNetShares.GetByImportLocation(@"H:\Anime2"); if (sn == null) { sn = new ImportFolder(); sn.ImportFolderType = (int)ImportFolderType.HDD; sn.ImportFolderName = "Anime2"; sn.ImportFolderLocation = @"H:\Anime2"; repNetShares.Save(sn); } sn = repNetShares.GetByImportLocation(@"G:\Anime3"); if (sn == null) { sn = new ImportFolder(); sn.ImportFolderType = (int)ImportFolderType.HDD; sn.ImportFolderName = "Anime3"; sn.ImportFolderLocation = @"G:\Anime3"; repNetShares.Save(sn); } logger.Debug("Creating shares complete!"); }
private static void CreateImportFolders() { logger.Debug("Creating shares..."); ImportFolderRepository repNetShares = new ImportFolderRepository(); ImportFolder sn = repNetShares.GetByImportLocation(@"M:\[ Anime 2011 ]"); if (sn == null) { sn = new ImportFolder(); sn.ImportFolderType = (int)ImportFolderType.HDD; sn.ImportFolderName = "Anime 2011"; sn.ImportFolderLocation = @"M:\[ Anime 2011 ]"; repNetShares.Save(sn); } sn = repNetShares.GetByImportLocation(@"M:\[ Anime - DVD and Bluray IN PROGRESS ]"); if (sn == null) { sn = new ImportFolder(); sn.ImportFolderType = (int)ImportFolderType.HDD; sn.ImportFolderName = "Anime - DVD and Bluray IN PROGRESS"; sn.ImportFolderLocation = @"M:\[ Anime - DVD and Bluray IN PROGRESS ]"; repNetShares.Save(sn); } sn = repNetShares.GetByImportLocation(@"M:\[ Anime - DVD and Bluray COMPLETE ]"); if (sn == null) { sn = new ImportFolder(); sn.ImportFolderType = (int)ImportFolderType.HDD; sn.ImportFolderName = "Anime - DVD and Bluray COMPLETE"; sn.ImportFolderLocation = @"M:\[ Anime - DVD and Bluray COMPLETE ]"; repNetShares.Save(sn); } sn = repNetShares.GetByImportLocation(@"M:\[ Anime ]"); if (sn == null) { sn = new ImportFolder(); sn.ImportFolderType = (int)ImportFolderType.HDD; sn.ImportFolderName = "Anime"; sn.ImportFolderLocation = @"M:\[ Anime ]"; repNetShares.Save(sn); } logger.Debug("Creating shares complete!"); }
private static void CreateImportFolders_Test() { logger.Debug("Creating import folders..."); ImportFolderRepository repImportFolders = new ImportFolderRepository(); ImportFolder sn = repImportFolders.GetByImportLocation(@"M:\[ Anime Test ]"); if (sn == null) { sn = new ImportFolder(); sn.ImportFolderName = "Anime"; sn.ImportFolderType = (int)ImportFolderType.HDD; sn.ImportFolderLocation = @"M:\[ Anime Test ]"; repImportFolders.Save(sn); } logger.Debug("Complete!"); }
public Contract_ImportFolder_SaveResponse SaveImportFolder(Contract_ImportFolder contract) { Contract_ImportFolder_SaveResponse response = new Contract_ImportFolder_SaveResponse(); response.ErrorMessage = ""; response.ImportFolder = null; try { ImportFolderRepository repNS = new ImportFolderRepository(); ImportFolder ns = null; if (contract.ImportFolderID.HasValue) { // update ns = repNS.GetByID(contract.ImportFolderID.Value); if (ns == null) { response.ErrorMessage = "Could not find Import Folder ID: " + contract.ImportFolderID.Value.ToString(); return response; } } else { // create ns = new ImportFolder(); } if (string.IsNullOrEmpty(contract.ImportFolderName)) { response.ErrorMessage = "Must specify an Import Folder name"; return response; } if (string.IsNullOrEmpty(contract.ImportFolderLocation)) { response.ErrorMessage = "Must specify an Import Folder location"; return response; } if (!Directory.Exists(contract.ImportFolderLocation)) { response.ErrorMessage = "Cannot find Import Folder location"; return response; } if (!contract.ImportFolderID.HasValue) { ImportFolder nsTemp = repNS.GetByImportLocation(contract.ImportFolderLocation); if (nsTemp != null) { response.ErrorMessage = "An entry already exists for the specified Import Folder location"; return response; } } if (contract.IsDropDestination == 1 && contract.IsDropSource == 1) { response.ErrorMessage = "A folder cannot be a drop source and a drop destination at the same time"; return response; } // check to make sure we don't have multiple drop folders List<ImportFolder> allFolders = repNS.GetAll(); if (contract.IsDropDestination == 1) { foreach (ImportFolder imf in allFolders) { if (imf.IsDropDestination == 1 && (!contract.ImportFolderID.HasValue || (contract.ImportFolderID.Value != imf.ImportFolderID))) { imf.IsDropDestination = 0; repNS.Save(imf); } } } ns.ImportFolderName = contract.ImportFolderName; ns.ImportFolderLocation = contract.ImportFolderLocation; ns.IsDropDestination = contract.IsDropDestination; ns.IsDropSource = contract.IsDropSource; ns.IsWatched = contract.IsWatched; repNS.Save(ns); response.ImportFolder = ns.ToContract(); MainWindow.StopWatchingFiles(); MainWindow.StartWatchingFiles(); return response; } catch (Exception ex) { logger.ErrorException(ex.ToString(), ex); response.ErrorMessage = ex.Message; return response; } }
public Contract_VideoDetailed ToContractDetailed(int userID) { Contract_VideoDetailed contract = new Contract_VideoDetailed(); // get the cross ref episode List <CrossRef_File_Episode> xrefs = this.EpisodeCrossRefs; if (xrefs.Count == 0) { return(null); } contract.Percentage = xrefs[0].Percentage; contract.EpisodeOrder = xrefs[0].EpisodeOrder; contract.CrossRefSource = xrefs[0].CrossRefSource; contract.AnimeEpisodeID = xrefs[0].EpisodeID; contract.VideoLocal_FilePath = this.FilePath; contract.VideoLocal_Hash = this.Hash; contract.VideoLocal_FileSize = this.FileSize; contract.VideoLocalID = this.VideoLocalID; contract.VideoLocal_IsIgnored = this.IsIgnored; contract.VideoLocal_IsVariation = this.IsVariation; contract.VideoLocal_MD5 = this.MD5; contract.VideoLocal_SHA1 = this.SHA1; contract.VideoLocal_CRC32 = this.CRC32; contract.VideoLocal_HashSource = this.HashSource; VideoLocal_User userRecord = this.GetUserRecord(userID); if (userRecord == null) { contract.VideoLocal_IsWatched = 0; } else { contract.VideoLocal_IsWatched = 1; } // Import Folder ImportFolder ns = this.ImportFolder; // to prevent multiple db calls if (ns != null) { contract.ImportFolderID = ns.ImportFolderID; contract.ImportFolderLocation = ns.ImportFolderLocation; contract.ImportFolderName = ns.ImportFolderName; } // video info VideoInfo vi = this.VideoInfo; // to prevent multiple db calls if (vi != null) { contract.VideoInfo_AudioBitrate = vi.AudioBitrate; contract.VideoInfo_AudioCodec = vi.AudioCodec; contract.VideoInfo_Duration = vi.Duration; contract.VideoInfo_VideoBitrate = vi.VideoBitrate; contract.VideoInfo_VideoBitDepth = vi.VideoBitDepth; contract.VideoInfo_VideoCodec = vi.VideoCodec; contract.VideoInfo_VideoFrameRate = vi.VideoFrameRate; contract.VideoInfo_VideoResolution = vi.VideoResolution; contract.VideoInfo_VideoInfoID = vi.VideoInfoID; } // AniDB File AniDB_File anifile = this.GetAniDBFile(); // to prevent multiple db calls if (anifile != null) { contract.AniDB_Anime_GroupName = anifile.Anime_GroupName; contract.AniDB_Anime_GroupNameShort = anifile.Anime_GroupNameShort; contract.AniDB_AnimeID = anifile.AnimeID; contract.AniDB_CRC = anifile.CRC; contract.AniDB_Episode_Rating = anifile.Episode_Rating; contract.AniDB_Episode_Votes = anifile.Episode_Votes; contract.AniDB_File_AudioCodec = anifile.File_AudioCodec; contract.AniDB_File_Description = anifile.File_Description; contract.AniDB_File_FileExtension = anifile.File_FileExtension; contract.AniDB_File_LengthSeconds = anifile.File_LengthSeconds; contract.AniDB_File_ReleaseDate = anifile.File_ReleaseDate; contract.AniDB_File_Source = anifile.File_Source; contract.AniDB_File_VideoCodec = anifile.File_VideoCodec; contract.AniDB_File_VideoResolution = anifile.File_VideoResolution; contract.AniDB_FileID = anifile.FileID; contract.AniDB_GroupID = anifile.GroupID; contract.AniDB_MD5 = anifile.MD5; contract.AniDB_SHA1 = anifile.SHA1; contract.AniDB_File_FileVersion = anifile.FileVersion; // languages contract.LanguagesAudio = anifile.LanguagesRAW; contract.LanguagesSubtitle = anifile.SubtitlesRAW; } else { contract.AniDB_Anime_GroupName = ""; contract.AniDB_Anime_GroupNameShort = ""; contract.AniDB_CRC = ""; contract.AniDB_File_AudioCodec = ""; contract.AniDB_File_Description = ""; contract.AniDB_File_FileExtension = ""; contract.AniDB_File_Source = ""; contract.AniDB_File_VideoCodec = ""; contract.AniDB_File_VideoResolution = ""; contract.AniDB_MD5 = ""; contract.AniDB_SHA1 = ""; contract.AniDB_File_FileVersion = 1; // languages contract.LanguagesAudio = ""; contract.LanguagesSubtitle = ""; } AniDB_ReleaseGroup relGroup = this.ReleaseGroup; // to prevent multiple db calls if (relGroup != null) { contract.ReleaseGroup = relGroup.ToContract(); } else { contract.ReleaseGroup = null; } return(contract); }
public void MoveFileIfRequired() { // check if this file is in the drop folder // otherwise we don't need to move it if (this.ImportFolder.IsDropSource == 0) { return; } if (!File.Exists(this.FullServerPath)) { return; } // find the default destination ImportFolder destFolder = null; ImportFolderRepository repFolders = new ImportFolderRepository(); foreach (ImportFolder fldr in repFolders.GetAll()) { if (fldr.IsDropDestination == 1) { destFolder = fldr; break; } } if (destFolder == null) { return; } if (!Directory.Exists(destFolder.ImportFolderLocation)) { return; } // we can only move the file if it has an anime associated with it List <CrossRef_File_Episode> xrefs = this.EpisodeCrossRefs; if (xrefs.Count == 0) { return; } CrossRef_File_Episode xref = xrefs[0]; // find the series associated with this episode AnimeSeriesRepository repSeries = new AnimeSeriesRepository(); AnimeSeries series = repSeries.GetByAnimeID(xref.AnimeID); if (series == null) { return; } // find where the other files are stored for this series // if there are no other files except for this one, it means we need to create a new location bool foundLocation = false; string newFullPath = ""; // sort the episodes by air date, so that we will move the file to the location of the latest episode List <AnimeEpisode> allEps = series.GetAnimeEpisodes(); List <SortPropOrFieldAndDirection> sortCriteria = new List <SortPropOrFieldAndDirection>(); sortCriteria.Add(new SortPropOrFieldAndDirection("AniDB_EpisodeID", true, SortType.eInteger)); allEps = Sorting.MultiSort <AnimeEpisode>(allEps, sortCriteria); foreach (AnimeEpisode ep in allEps) { foreach (VideoLocal vid in ep.GetVideoLocals()) { if (vid.VideoLocalID != this.VideoLocalID) { // make sure this folder is not the drop source if (vid.ImportFolder.IsDropSource == 1) { continue; } string thisFileName = vid.FullServerPath; string folderName = Path.GetDirectoryName(thisFileName); if (Directory.Exists(folderName)) { newFullPath = folderName; foundLocation = true; break; } } } if (foundLocation) { break; } } if (!foundLocation) { // we need to create a new folder string newFolderName = Utils.RemoveInvalidFolderNameCharacters(series.GetAnime().MainTitle); newFullPath = Path.Combine(destFolder.ImportFolderLocation, newFolderName); if (!Directory.Exists(newFullPath)) { Directory.CreateDirectory(newFullPath); } } int newFolderID = 0; string newPartialPath = ""; string newFullServerPath = Path.Combine(newFullPath, Path.GetFileName(this.FullServerPath)); DataAccessHelper.GetShareAndPath(newFullServerPath, repFolders.GetAll(), ref newFolderID, ref newPartialPath); logger.Info("Moving file from {0} to {1}", this.FullServerPath, newFullServerPath); if (File.Exists(newFullServerPath)) { // if the file already exists, we can just delete the source file instead // this is safer than deleting and moving File.Delete(this.FullServerPath); this.ImportFolderID = newFolderID; this.FilePath = newPartialPath; VideoLocalRepository repVids = new VideoLocalRepository(); repVids.Save(this); } else { string originalFileName = this.FullServerPath; FileInfo fi = new FileInfo(originalFileName); // now move the file File.Move(this.FullServerPath, newFullServerPath); this.ImportFolderID = newFolderID; this.FilePath = newPartialPath; VideoLocalRepository repVids = new VideoLocalRepository(); repVids.Save(this); try { // move any subtitle files foreach (string subtitleFile in Utils.GetPossibleSubtitleFiles(originalFileName)) { if (File.Exists(subtitleFile)) { FileInfo fiSub = new FileInfo(subtitleFile); string newSubPath = Path.Combine(Path.GetDirectoryName(newFullServerPath), fiSub.Name); if (File.Exists(newSubPath)) { // if the file already exists, we can just delete the source file instead // this is safer than deleting and moving File.Delete(newSubPath); } else { File.Move(subtitleFile, newSubPath); } } } } catch (Exception ex) { logger.ErrorException(ex.ToString(), ex); } // check for any empty folders in drop folder // only for the drop folder if (this.ImportFolder.IsDropSource == 1) { foreach (string folderName in Directory.GetDirectories(this.ImportFolder.ImportFolderLocation, "*", SearchOption.AllDirectories)) { if (Directory.Exists(folderName)) { if (Directory.GetFiles(folderName, "*", SearchOption.AllDirectories).Length == 0) { try { Directory.Delete(folderName, true); } /*catch (IOException) * { * Thread.Sleep(0); * Directory.Delete(folderName, false); * }*/ catch (Exception ex) { logger.ErrorException(ex.ToString(), ex); } } } } } } }
public void Init(ImportFolder acc, string initialpath) { _account = acc.CloudAccount; PopulateMainDir(initialpath); }
public void MoveFileIfRequired() { try { logger.Trace("Attempting to MOVE file: {0}", this.FullServerPath); // check if this file is in the drop folder // otherwise we don't need to move it if (ImportFolder.IsDropSource == 0) { logger.Trace("Not moving file as it is NOT in the drop folder: {0}", this.FullServerPath); return; } IFileSystem f = this.ImportFolder.FileSystem; if (f == null) { logger.Trace("Unable to MOVE, filesystem not working: {0}", this.FullServerPath); return; } FileSystemResult <IObject> fsrresult = f.Resolve(FullServerPath); if (!fsrresult.IsOk) { logger.Error("Could not find the file to move: {0}", this.FullServerPath); return; } IFile source_file = fsrresult.Result as IFile; if (source_file == null) { logger.Error("Could not find the file to move: {0}", this.FullServerPath); return; } // find the default destination ImportFolder destFolder = null; foreach (ImportFolder fldr in RepoFactory.ImportFolder.GetAll().Where(a => a.CloudID == ImportFolder.CloudID)) { if (fldr.IsDropDestination == 1) { destFolder = fldr; break; } } if (destFolder == null) { return; } FileSystemResult <IObject> re = f.Resolve(destFolder.ImportFolderLocation); if (!re.IsOk) { return; } // keep the original drop folder for later (take a copy, not a reference) ImportFolder dropFolder = this.ImportFolder; // we can only move the file if it has an anime associated with it List <CrossRef_File_Episode> xrefs = this.VideoLocal.EpisodeCrossRefs; if (xrefs.Count == 0) { return; } CrossRef_File_Episode xref = xrefs[0]; // find the series associated with this episode AnimeSeries series = RepoFactory.AnimeSeries.GetByAnimeID(xref.AnimeID); if (series == null) { return; } // find where the other files are stored for this series // if there are no other files except for this one, it means we need to create a new location bool foundLocation = false; string newFullPath = ""; // sort the episodes by air date, so that we will move the file to the location of the latest episode List <AnimeEpisode> allEps = series.GetAnimeEpisodes().OrderByDescending(a => a.AniDB_EpisodeID).ToList(); IDirectory destination = null; foreach (AnimeEpisode ep in allEps) { // check if this episode belongs to more than one anime // if it does we will ignore it List <CrossRef_File_Episode> fileEpXrefs = RepoFactory.CrossRef_File_Episode.GetByEpisodeID(ep.AniDB_EpisodeID); int? animeID = null; bool crossOver = false; foreach (CrossRef_File_Episode fileEpXref in fileEpXrefs) { if (!animeID.HasValue) { animeID = fileEpXref.AnimeID; } else { if (animeID.Value != fileEpXref.AnimeID) { crossOver = true; } } } if (crossOver) { continue; } foreach (VideoLocal vid in ep.GetVideoLocals().Where(a => a.Places.Any(b => b.ImportFolder.CloudID == destFolder.CloudID && b.ImportFolder.IsDropSource == 0))) { if (vid.VideoLocalID != this.VideoLocalID) { VideoLocal_Place place = vid.Places.FirstOrDefault(a => a.ImportFolder.CloudID == destFolder.CloudID); string thisFileName = place?.FullServerPath; string folderName = Path.GetDirectoryName(thisFileName); FileSystemResult <IObject> dir = f.Resolve(folderName); if (dir.IsOk) { destination = (IDirectory)dir.Result; newFullPath = folderName; foundLocation = true; break; } } } if (foundLocation) { break; } } if (!foundLocation) { // we need to create a new folder string newFolderName = Utils.RemoveInvalidFolderNameCharacters(series.GetAnime().PreferredTitle); newFullPath = Path.Combine(destFolder.ParsedImportFolderLocation, newFolderName); FileSystemResult <IObject> dirn = f.Resolve(newFullPath); if (!dirn.IsOk) { dirn = f.Resolve(destFolder.ImportFolderLocation); if (dirn.IsOk) { IDirectory d = (IDirectory)dirn.Result; FileSystemResult <IDirectory> d2 = Task.Run(async() => await d.CreateDirectoryAsync(newFolderName, null)).Result; destination = d2.Result; } } else if (dirn.Result is IFile) { logger.Error("Destination folder is a file: {0}", newFolderName); } else { destination = (IDirectory)dirn.Result; } } string newFullServerPath = Path.Combine(newFullPath, Path.GetFileName(this.FullServerPath)); Tuple <ImportFolder, string> tup = VideoLocal_PlaceRepository.GetFromFullPath(newFullServerPath); if (tup == null) { logger.Error($"Unable to LOCATE file {newFullServerPath} inside the import folders"); return; } logger.Info("Moving file from {0} to {1}", this.FullServerPath, newFullServerPath); FileSystemResult <IObject> dst = f.Resolve(newFullServerPath); if (dst.IsOk) { logger.Trace("Not moving file as it already exists at the new location, deleting source file instead: {0} --- {1}", this.FullServerPath, newFullServerPath); // if the file already exists, we can just delete the source file instead // this is safer than deleting and moving FileSystemResult fr = new FileSystemResult(); try { fr = source_file.Delete(false); if (!fr.IsOk) { logger.Warn("Unable to DELETE file: {0} error {1}", this.FullServerPath, fr?.Error ?? String.Empty); } this.ImportFolderID = tup.Item1.ImportFolderID; this.FilePath = tup.Item2; RepoFactory.VideoLocalPlace.Save(this); // check for any empty folders in drop folder // only for the drop folder if (dropFolder.IsDropSource == 1) { FileSystemResult <IObject> dd = f.Resolve(dropFolder.ImportFolderLocation); if (dd != null && dd.IsOk && dd.Result is IDirectory) { RecursiveDeleteEmptyDirectories((IDirectory)dd.Result, true); } } } catch { logger.Error("Unable to DELETE file: {0} error {1}", this.FullServerPath, fr?.Error ?? String.Empty); } } else { FileSystemResult fr = source_file.Move(destination); if (!fr.IsOk) { logger.Error("Unable to MOVE file: {0} to {1} error {2)", this.FullServerPath, newFullServerPath, fr?.Error ?? String.Empty); return; } string originalFileName = this.FullServerPath; this.ImportFolderID = tup.Item1.ImportFolderID; this.FilePath = tup.Item2; RepoFactory.VideoLocalPlace.Save(this); try { // move any subtitle files foreach (string subtitleFile in Utils.GetPossibleSubtitleFiles(originalFileName)) { FileSystemResult <IObject> src = f.Resolve(subtitleFile); if (src.IsOk && src.Result is IFile) { string newSubPath = Path.Combine(Path.GetDirectoryName(newFullServerPath), ((IFile)src.Result).Name); dst = f.Resolve(newSubPath); if (dst.IsOk && dst.Result is IFile) { FileSystemResult fr2 = src.Result.Delete(true); if (!fr2.IsOk) { logger.Warn("Unable to DELETE file: {0} error {1}", subtitleFile, fr2?.Error ?? String.Empty); } } else { FileSystemResult fr2 = ((IFile)src.Result).Move(destination); if (!fr2.IsOk) { logger.Error("Unable to MOVE file: {0} to {1} error {2)", subtitleFile, newSubPath, fr2?.Error ?? String.Empty); } } } } } catch (Exception ex) { logger.Error(ex, ex.ToString()); } // check for any empty folders in drop folder // only for the drop folder if (dropFolder.IsDropSource == 1) { FileSystemResult <IObject> dd = f.Resolve(dropFolder.ImportFolderLocation); if (dd != null && dd.IsOk && dd.Result is IDirectory) { RecursiveDeleteEmptyDirectories((IDirectory)dd.Result, true); } } } } catch (Exception ex) { string msg = $"Could not MOVE file: {this.FullServerPath} -- {ex.ToString()}"; logger.Error(ex, msg); } }
public Contract_ImportFolder_SaveResponse SaveImportFolder(Contract_ImportFolder contract) { Contract_ImportFolder_SaveResponse response = new Contract_ImportFolder_SaveResponse(); response.ErrorMessage = ""; response.ImportFolder = null; try { ImportFolder ns = null; if (contract.ImportFolderID.HasValue && contract.ImportFolderID != 0) { // update ns = RepoFactory.ImportFolder.GetByID(contract.ImportFolderID.Value); if (ns == null) { response.ErrorMessage = "Could not find Import Folder ID: " + contract.ImportFolderID.Value.ToString(); return response; } } else { // create ns = new ImportFolder(); } if (string.IsNullOrEmpty(contract.ImportFolderName)) { response.ErrorMessage = "Must specify an Import Folder name"; return response; } if (string.IsNullOrEmpty(contract.ImportFolderLocation)) { response.ErrorMessage = "Must specify an Import Folder location"; return response; } if (contract.CloudID==null && !Directory.Exists(contract.ImportFolderLocation)) { response.ErrorMessage = "Cannot find Import Folder location"; return response; } if (!contract.ImportFolderID.HasValue) { ImportFolder nsTemp = RepoFactory.ImportFolder.GetByImportLocation(contract.ImportFolderLocation); if (nsTemp != null) { response.ErrorMessage = "An entry already exists for the specified Import Folder location"; return response; } } if (contract.IsDropDestination == 1 && contract.IsDropSource == 1) { response.ErrorMessage = "A folder cannot be a drop source and a drop destination at the same time"; return response; } // check to make sure we don't have multiple drop folders IReadOnlyList<ImportFolder> allFolders = RepoFactory.ImportFolder.GetAll(); if (contract.IsDropDestination == 1) { foreach (ImportFolder imf in allFolders) { if (contract.CloudID==imf.CloudID && imf.IsDropDestination == 1 && (!contract.ImportFolderID.HasValue || (contract.ImportFolderID.Value != imf.ImportFolderID))) { imf.IsDropDestination = 0; RepoFactory.ImportFolder.Save(imf); } } } ns.ImportFolderName = contract.ImportFolderName; ns.ImportFolderLocation = contract.ImportFolderLocation; ns.IsDropDestination = contract.IsDropDestination; ns.IsDropSource = contract.IsDropSource; ns.IsWatched = contract.IsWatched; ns.ImportFolderType = contract.ImportFolderType; ns.CloudID = contract.CloudID.HasValue && contract.CloudID == 0 ? null : contract.CloudID; ; RepoFactory.ImportFolder.Save(ns); response.ImportFolder = ns.ToContract(); System.Windows.Application.Current.Dispatcher.Invoke(() => { ServerInfo.Instance.RefreshImportFolders(); }); MainWindow.StopWatchingFiles(); MainWindow.StartWatchingFiles(); return response; } catch (Exception ex) { logger.Error(ex, ex.ToString()); response.ErrorMessage = ex.Message; return response; } }
public static void RunImport_ImportFolderNewFiles(ImportFolder fldr) { List<string> fileList = new List<string>(); int filesFound = 0, videosFound = 0; int i = 0; List<VideoLocal_Place> filesAll = RepoFactory.VideoLocalPlace.GetByImportFolder(fldr.ImportFolderID); Utils.GetFilesForImportFolder(fldr.BaseDirectory, ref fileList); HashSet<string> fs = new HashSet<string>(fileList); foreach (VideoLocal_Place v in filesAll) { if (fs.Contains(v.FullServerPath)) fileList.Remove(v.FullServerPath); } // get a list of all files in the share foreach (string fileName in fileList) { i++; filesFound++; logger.Info("Processing File {0}/{1} --- {2}", i, fileList.Count, fileName); if (!FileHashHelper.IsVideo(fileName)) continue; videosFound++; CommandRequest_HashFile cr_hashfile = new CommandRequest_HashFile(fileName, false); cr_hashfile.Save(); } logger.Debug("Found {0} files", filesFound); logger.Debug("Found {0} videos", videosFound); }
public List <Contract_VideoDetailed> GetVideoDetailedContracts(int userID) { VideoLocalRepository repVids = new VideoLocalRepository(); List <Contract_VideoDetailed> contracts = new List <Contract_VideoDetailed>(); // get all the cross refs foreach (CrossRef_File_Episode xref in FileCrossRefs) { Contract_VideoDetailed contract = new Contract_VideoDetailed(); contract.Percentage = xref.Percentage; contract.EpisodeOrder = xref.EpisodeOrder; contract.CrossRefSource = xref.CrossRefSource; contract.AnimeEpisodeID = this.AnimeEpisodeID; // get the video file // we will assume that it is unique by hash/episodeid VideoLocal vid = repVids.GetByHash(xref.Hash); if (vid != null) { contract.VideoLocal_FilePath = vid.FilePath; contract.VideoLocal_Hash = vid.Hash; contract.VideoLocal_FileSize = vid.FileSize; contract.VideoLocalID = vid.VideoLocalID; contract.VideoLocal_MD5 = vid.MD5; contract.VideoLocal_SHA1 = vid.SHA1; contract.VideoLocal_CRC32 = vid.CRC32; contract.VideoLocal_HashSource = vid.HashSource; VideoLocal_User vidUser = vid.GetUserRecord(userID); //AnimeEpisode_User userRecord = this.GetUserRecord(userID); if (vidUser == null) { contract.VideoLocal_IsWatched = 0; contract.VideoLocal_WatchedDate = null; } else { contract.VideoLocal_IsWatched = 1; contract.VideoLocal_WatchedDate = vidUser.WatchedDate; } contract.VideoLocal_IsIgnored = vid.IsIgnored; contract.VideoLocal_IsVariation = vid.IsVariation; // Import Folder ImportFolder ns = vid.ImportFolder; // to prevent multiple db calls contract.ImportFolderID = ns.ImportFolderID; contract.ImportFolderLocation = ns.ImportFolderLocation; contract.ImportFolderName = ns.ImportFolderName; // video info VideoInfo vi = vid.VideoInfo; // to prevent multiple db calls contract.VideoInfo_AudioBitrate = vi.AudioBitrate; contract.VideoInfo_AudioCodec = vi.AudioCodec; contract.VideoInfo_Duration = vi.Duration; contract.VideoInfo_VideoBitrate = vi.VideoBitrate; contract.VideoInfo_VideoBitDepth = vi.VideoBitDepth; contract.VideoInfo_VideoCodec = vi.VideoCodec; contract.VideoInfo_VideoFrameRate = vi.VideoFrameRate; contract.VideoInfo_VideoResolution = vi.VideoResolution; contract.VideoInfo_VideoInfoID = vi.VideoInfoID; // AniDB File AniDB_File anifile = vid.GetAniDBFile(); // to prevent multiple db calls if (anifile != null) { contract.AniDB_Anime_GroupName = anifile.Anime_GroupName; contract.AniDB_Anime_GroupNameShort = anifile.Anime_GroupNameShort; contract.AniDB_AnimeID = anifile.AnimeID; contract.AniDB_CRC = anifile.CRC; contract.AniDB_Episode_Rating = anifile.Episode_Rating; contract.AniDB_Episode_Votes = anifile.Episode_Votes; contract.AniDB_File_AudioCodec = anifile.File_AudioCodec; contract.AniDB_File_Description = anifile.File_Description; contract.AniDB_File_FileExtension = anifile.File_FileExtension; contract.AniDB_File_LengthSeconds = anifile.File_LengthSeconds; contract.AniDB_File_ReleaseDate = anifile.File_ReleaseDate; contract.AniDB_File_Source = anifile.File_Source; contract.AniDB_File_VideoCodec = anifile.File_VideoCodec; contract.AniDB_File_VideoResolution = anifile.File_VideoResolution; contract.AniDB_FileID = anifile.FileID; contract.AniDB_GroupID = anifile.GroupID; contract.AniDB_MD5 = anifile.MD5; contract.AniDB_SHA1 = anifile.SHA1; contract.AniDB_File_FileVersion = anifile.FileVersion; contract.AniDB_File_IsCensored = anifile.IsCensored; contract.AniDB_File_IsDeprecated = anifile.IsDeprecated; contract.AniDB_File_InternalVersion = anifile.InternalVersion; // languages contract.LanguagesAudio = anifile.LanguagesRAW; contract.LanguagesSubtitle = anifile.SubtitlesRAW; } else { contract.AniDB_Anime_GroupName = ""; contract.AniDB_Anime_GroupNameShort = ""; contract.AniDB_CRC = ""; contract.AniDB_File_AudioCodec = ""; contract.AniDB_File_Description = ""; contract.AniDB_File_FileExtension = ""; contract.AniDB_File_Source = ""; contract.AniDB_File_VideoCodec = ""; contract.AniDB_File_VideoResolution = ""; contract.AniDB_MD5 = ""; contract.AniDB_SHA1 = ""; contract.AniDB_File_FileVersion = 1; // languages contract.LanguagesAudio = ""; contract.LanguagesSubtitle = ""; } AniDB_ReleaseGroup relGroup = vid.ReleaseGroup; // to prevent multiple db calls if (relGroup != null) { contract.ReleaseGroup = relGroup.ToContract(); } else { contract.ReleaseGroup = null; } contracts.Add(contract); } } return(contracts); }
void btnSave_Click(object sender, RoutedEventArgs e) { try { // An import folder cannot be both the drop source and the drop destination if (chkDropDestination.IsChecked.HasValue && chkDropSource.IsChecked.HasValue && chkDropDestination.IsChecked.Value && chkDropSource.IsChecked.Value) { MessageBox.Show(JMMServer.Properties.Resources.ImportFolders_SameFolder, JMMServer.Properties.Resources.Error, MessageBoxButton.OK, MessageBoxImage.Error); return; } // The import folder location cannot be blank. Enter a valid path on OMM Server if (string.IsNullOrEmpty(txtImportFolderLocation.Text)) { MessageBox.Show(JMMServer.Properties.Resources.ImportFolders_BlankImport, JMMServer.Properties.Resources.Error, MessageBoxButton.OK, MessageBoxImage.Error); txtImportFolderLocation.Focus(); return; } Contract_ImportFolder contract = new Contract_ImportFolder(); if (importFldr.ImportFolderID == 0) contract.ImportFolderID = null; else contract.ImportFolderID = importFldr.ImportFolderID; contract.ImportFolderType = (int)(importFldr.CloudID.HasValue ? ImportFolderType.Cloud : ImportFolderType.HDD); contract.ImportFolderName = "NA"; contract.ImportFolderLocation = txtImportFolderLocation.Text.Trim(); contract.IsDropDestination = chkDropDestination.IsChecked.Value ? 1 : 0; contract.IsDropSource = chkDropSource.IsChecked.Value ? 1 : 0; contract.IsWatched = chkIsWatched.IsChecked.Value ? 1 : 0; if (comboProvider.SelectedIndex == 0) contract.CloudID = null; else contract.CloudID = ((CloudAccount) comboProvider.SelectedItem).CloudID; JMMServiceImplementation imp = new JMMServiceImplementation(); Contract_ImportFolder_SaveResponse response = imp.SaveImportFolder(contract); if (!string.IsNullOrEmpty(response.ErrorMessage)) MessageBox.Show(response.ErrorMessage, JMMServer.Properties.Resources.Error, MessageBoxButton.OK, MessageBoxImage.Error); importFldr = null; ServerInfo.Instance.RefreshImportFolders(); } catch (Exception ex) { Utils.ShowErrorMessage(ex); } this.DialogResult = true; this.Close(); }
public void MoveFileIfRequired() { try { logger.Trace("Attempting to move file: {0}", this.FullServerPath); // check if this file is in the drop folder // otherwise we don't need to move it if (this.ImportFolder.IsDropSource == 0) { logger.Trace("Not moving file as it is NOT in the drop folder: {0}", this.FullServerPath); return; } if (!File.Exists(this.FullServerPath)) { logger.Error("Could not find the file to move: {0}", this.FullServerPath); return; } // find the default destination ImportFolder destFolder = null; ImportFolderRepository repFolders = new ImportFolderRepository(); foreach (ImportFolder fldr in repFolders.GetAll()) { if (fldr.IsDropDestination == 1) { destFolder = fldr; break; } } if (destFolder == null) { return; } if (!System.IO.Directory.Exists(destFolder.ImportFolderLocation)) { return; } // keep the original drop folder for later (take a copy, not a reference) ImportFolder dropFolder = this.ImportFolder; // we can only move the file if it has an anime associated with it List <CrossRef_File_Episode> xrefs = this.EpisodeCrossRefs; if (xrefs.Count == 0) { return; } CrossRef_File_Episode xref = xrefs[0]; // find the series associated with this episode AnimeSeriesRepository repSeries = new AnimeSeriesRepository(); AnimeSeries series = repSeries.GetByAnimeID(xref.AnimeID); if (series == null) { return; } // find where the other files are stored for this series // if there are no other files except for this one, it means we need to create a new location bool foundLocation = false; string newFullPath = ""; // sort the episodes by air date, so that we will move the file to the location of the latest episode List <AnimeEpisode> allEps = series.GetAnimeEpisodes().OrderByDescending(a => a.AniDB_EpisodeID).ToList(); AniDB_AnimeRepository repAnime = new AniDB_AnimeRepository(); CrossRef_File_EpisodeRepository repFileEpXref = new CrossRef_File_EpisodeRepository(); foreach (AnimeEpisode ep in allEps) { // check if this episode belongs to more than one anime // if it does we will ignore it List <CrossRef_File_Episode> fileEpXrefs = repFileEpXref.GetByEpisodeID(ep.AniDB_EpisodeID); int? animeID = null; bool crossOver = false; foreach (CrossRef_File_Episode fileEpXref in fileEpXrefs) { if (!animeID.HasValue) { animeID = fileEpXref.AnimeID; } else { if (animeID.Value != fileEpXref.AnimeID) { crossOver = true; } } } if (crossOver) { continue; } foreach (VideoLocal vid in ep.GetVideoLocals()) { if (vid.VideoLocalID != this.VideoLocalID) { // make sure this folder is not the drop source if (vid.ImportFolder.IsDropSource == 1) { continue; } string thisFileName = vid.FullServerPath; string folderName = Path.GetDirectoryName(thisFileName); if (Directory.Exists(folderName)) { newFullPath = folderName; foundLocation = true; break; } } } if (foundLocation) { break; } } if (!foundLocation) { // we need to create a new folder string newFolderName = Utils.RemoveInvalidFolderNameCharacters(series.GetAnime().PreferredTitle); newFullPath = Path.Combine(destFolder.ImportFolderLocation, newFolderName); if (!Directory.Exists(newFullPath)) { Directory.CreateDirectory(newFullPath); } } int newFolderID = 0; string newPartialPath = ""; string newFullServerPath = Path.Combine(newFullPath, Path.GetFileName(this.FullServerPath)); DataAccessHelper.GetShareAndPath(newFullServerPath, repFolders.GetAll(), ref newFolderID, ref newPartialPath); logger.Info("Moving file from {0} to {1}", this.FullServerPath, newFullServerPath); if (File.Exists(newFullServerPath)) { logger.Trace( "Not moving file as it already exists at the new location, deleting source file instead: {0} --- {1}", this.FullServerPath, newFullServerPath); // if the file already exists, we can just delete the source file instead // this is safer than deleting and moving File.Delete(this.FullServerPath); this.ImportFolderID = newFolderID; this.FilePath = newPartialPath; VideoLocalRepository repVids = new VideoLocalRepository(); repVids.Save(this, true); } else { string originalFileName = this.FullServerPath; FileInfo fi = new FileInfo(originalFileName); // now move the file File.Move(this.FullServerPath, newFullServerPath); this.ImportFolderID = newFolderID; this.FilePath = newPartialPath; VideoLocalRepository repVids = new VideoLocalRepository(); repVids.Save(this, true); try { // move any subtitle files foreach (string subtitleFile in Utils.GetPossibleSubtitleFiles(originalFileName)) { if (File.Exists(subtitleFile)) { FileInfo fiSub = new FileInfo(subtitleFile); string newSubPath = Path.Combine(Path.GetDirectoryName(newFullServerPath), fiSub.Name); if (File.Exists(newSubPath)) { // if the file already exists, we can just delete the source file instead // this is safer than deleting and moving File.Delete(newSubPath); } else { File.Move(subtitleFile, newSubPath); } } } } catch (Exception ex) { logger.ErrorException(ex.ToString(), ex); } // check for any empty folders in drop folder // only for the drop folder if (dropFolder.IsDropSource == 1) { foreach ( string folderName in Directory.GetDirectories(dropFolder.ImportFolderLocation, "*", SearchOption.AllDirectories)) { if (Directory.Exists(folderName)) { if (Directory.GetFiles(folderName, "*", SearchOption.AllDirectories).Length == 0) { try { Directory.Delete(folderName, true); } catch (Exception ex) { logger.ErrorException(ex.ToString(), ex); } } } } } } } catch (Exception ex) { string msg = string.Format("Could not move file: {0} -- {1}", this.FullServerPath, ex.ToString()); logger.ErrorException(msg, ex); } }