コード例 #1
0
        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);
            }
        }
コード例 #2
0
ファイル: VideoLocal.cs プロジェクト: alimbada/jmmserver
        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);
                                }
                            }
                        }
                    }
                }
            }
        }