public static void InitCache()
        {
            string t = "VideoLocal";

            ServerState.Instance.CurrentSetupStatus = string.Format(JMMServer.Properties.Resources.Database_Cache, t, string.Empty);
            VideoLocalRepository repo = new VideoLocalRepository();

            Cache         = new PocoCache <int, VideoLocal>(repo.InternalGetAll(), a => a.VideoLocalID);
            Hashes        = new PocoIndex <int, VideoLocal, string>(Cache, a => a.Hash);
            Paths         = new PocoIndex <int, VideoLocal, string>(Cache, a => a.FilePath);
            Ignored       = new PocoIndex <int, VideoLocal, int>(Cache, a => a.IsIgnored);
            ImportFolders = new PocoIndex <int, VideoLocal, int>(Cache, a => a.ImportFolderID);
            int cnt = 0;
            List <VideoLocal> grps = Cache.Values.Where(a => a.MediaVersion < VideoLocal.MEDIA_VERSION || a.MediaBlob == null).ToList();
            int max = grps.Count;

            foreach (VideoLocal g in grps)
            {
                try
                {
                    if (File.Exists(g.FullServerPath))
                    {
                        repo.Save(g, false);
                    }
                }
                catch (Exception)
                {
                    // ignored
                }
                cnt++;
                if (cnt % 10 == 0)
                {
                    ServerState.Instance.CurrentSetupStatus = string.Format(JMMServer.Properties.Resources.Database_Cache, t,
                                                                            " DbRegen - " + cnt + "/" + max);
                }
            }
            ServerState.Instance.CurrentSetupStatus = string.Format(JMMServer.Properties.Resources.Database_Cache, t,
                                                                    " DbRegen - " + max + "/" + max);
        }
Exemple #2
0
        public string SetVariationStatusOnFile(int videoLocalID, bool isVariation)
        {
            try
            {
                VideoLocalRepository repVids = new VideoLocalRepository();
                VideoLocal vid = repVids.GetByID(videoLocalID);
                if (vid == null)
                    return "Could not find video record";

                vid.IsVariation = isVariation ? 1 : 0;
                repVids.Save(vid);

                return "";

            }
            catch (Exception ex)
            {
                logger.ErrorException(ex.ToString(), ex);
                return ex.Message;
            }
        }
Exemple #3
0
        public Contract_VideoLocalRenamed RenameFile(int videoLocalID, string renameRules)
        {
            Contract_VideoLocalRenamed ret = new Contract_VideoLocalRenamed();
            ret.VideoLocalID = videoLocalID;
            ret.Success = true;
            try
            {
                VideoLocalRepository repVids = new VideoLocalRepository();
                VideoLocal vid = repVids.GetByID(videoLocalID);
                if (vid == null)
                {
                    ret.VideoLocal = null;
                    ret.NewFileName = string.Format("ERROR: Could not find file record");
                    ret.Success = false;
                }
                else
                {
                    ret.VideoLocal = null;
                    ret.NewFileName = RenameFileHelper.GetNewFileName(vid, renameRules);

                    if (!string.IsNullOrEmpty(ret.NewFileName))
                    {
                        // check if the file exists
                        string fullFileName = vid.FullServerPath;
                        if (!File.Exists(fullFileName))
                        {
                            ret.NewFileName = "Error could not find the original file";
                            ret.Success = false;
                            return ret;
                        }

                        // actually rename the file
                        string path = Path.GetDirectoryName(fullFileName);
                        string newFullName = Path.Combine(path, ret.NewFileName);

                        try
                        {
                            logger.Info(string.Format("Renaming file From ({0}) to ({1})....", fullFileName, newFullName));

                            if (fullFileName.Equals(newFullName, StringComparison.InvariantCultureIgnoreCase))
                            {
                                logger.Info(string.Format("Renaming file SKIPPED, no change From ({0}) to ({1})", fullFileName, newFullName));
                                ret.NewFileName = newFullName;
                            }
                            else
                            {
                                File.Move(fullFileName, newFullName);
                                logger.Info(string.Format("Renaming file SUCCESS From ({0}) to ({1})", fullFileName, newFullName));
                                ret.NewFileName = newFullName;

                                string newPartialPath = "";
                                int folderID = vid.ImportFolderID;
                                ImportFolderRepository repFolders = new ImportFolderRepository();

                                DataAccessHelper.GetShareAndPath(newFullName, repFolders.GetAll(), ref folderID, ref newPartialPath);

                                vid.FilePath = newPartialPath;
                                repVids.Save(vid);
                            }
                        }
                        catch (Exception ex)
                        {
                            logger.Info(string.Format("Renaming file FAIL From ({0}) to ({1}) - {2}", fullFileName, newFullName, ex.Message));
                            logger.ErrorException(ex.ToString(), ex);
                            ret.Success = false;
                            ret.NewFileName = ex.Message;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                logger.ErrorException(ex.ToString(), ex);
                ret.VideoLocal = null;
                ret.NewFileName = string.Format("ERROR: {0}", ex.Message);
                ret.Success = false;
            }
            return ret;
        }
Exemple #4
0
        private VideoLocal ProcessFile_LocalInfo()
        {
            // hash and read media info for file
            int nshareID = -1;
            string filePath = "";

            ImportFolderRepository repNS = new ImportFolderRepository();
            List<ImportFolder> shares = repNS.GetAll();
            DataAccessHelper.GetShareAndPath(FileName, shares, ref nshareID, ref filePath);

            if (!File.Exists(FileName))
            {
                logger.Error("File does not exist: {0}", FileName);
                return null;
            }

            int numAttempts = 0;
            // Wait 3 minutes seconds before giving up on trying to access the file
            while ((!CanAccessFile(FileName)) && (numAttempts < 180))
            {
                numAttempts++;
                Thread.Sleep(1000);
                Console.WriteLine("Attempt # " + numAttempts.ToString());
            }

            // if we failed to access the file, get ouuta here
            if (numAttempts == 180)
            {
                logger.Error("Could not access file: " + FileName);
                return null;
            }

            // check if we have already processed this file
            VideoLocal vlocal = null;
            VideoLocalRepository repVidLocal = new VideoLocalRepository();
            FileNameHashRepository repFNHash = new FileNameHashRepository();

            List<VideoLocal> vidLocals = repVidLocal.GetByFilePathAndShareID(filePath, nshareID);
            FileInfo fi = new FileInfo(FileName);

            if (vidLocals.Count > 0)
            {
                vlocal = vidLocals[0];
                logger.Trace("VideoLocal record found in database: {0}", vlocal.VideoLocalID);

                if (ForceHash)
                {
                    vlocal.FileSize = fi.Length;
                    vlocal.DateTimeUpdated = DateTime.Now;
                }
            }
            else
            {
                logger.Trace("VideoLocal, creating new record");
                vlocal = new VideoLocal();
                vlocal.DateTimeUpdated = DateTime.Now;
                vlocal.DateTimeCreated = vlocal.DateTimeUpdated;
                vlocal.FilePath = filePath;
                vlocal.FileSize = fi.Length;
                vlocal.ImportFolderID = nshareID;
                vlocal.Hash = "";
                vlocal.CRC32 = "";
                vlocal.MD5 = "";
                vlocal.SHA1 = "";
                vlocal.IsIgnored = 0;
                vlocal.IsVariation = 0;
            }

            // check if we need to get a hash this file
            Hashes hashes = null;
            if (string.IsNullOrEmpty(vlocal.Hash) || ForceHash)
            {
                // try getting the hash from the CrossRef
                if (!ForceHash)
                {
                    CrossRef_File_EpisodeRepository repCrossRefs = new CrossRef_File_EpisodeRepository();
                    List<CrossRef_File_Episode> crossRefs = repCrossRefs.GetByFileNameAndSize(Path.GetFileName(vlocal.FilePath), vlocal.FileSize);
                    if (crossRefs.Count == 1)
                    {
                        vlocal.Hash = crossRefs[0].Hash;
                        vlocal.HashSource = (int)HashSource.DirectHash;
                    }
                }

                // try getting the hash from the LOCAL cache
                if (!ForceHash && string.IsNullOrEmpty(vlocal.Hash))
                {
                    List<FileNameHash> fnhashes = repFNHash.GetByFileNameAndSize(Path.GetFileName(vlocal.FilePath), vlocal.FileSize);
                    if (fnhashes != null && fnhashes.Count > 1)
                    {
                        // if we have more than one record it probably means there is some sort of corruption
                        // lets delete the local records
                        foreach (FileNameHash fnh in fnhashes)
                        {
                            repFNHash.Delete(fnh.FileNameHashID);
                        }
                    }

                    if (fnhashes != null && fnhashes.Count == 1)
                    {
                        logger.Trace("Got hash from LOCAL cache: {0} ({1})", FileName, fnhashes[0].Hash);
                        vlocal.Hash = fnhashes[0].Hash;
                        vlocal.HashSource = (int)HashSource.WebCacheFileName;
                    }
                }

                // hash the file
                if (string.IsNullOrEmpty(vlocal.Hash) || ForceHash)
                {
                    DateTime start = DateTime.Now;
                    logger.Trace("Calculating hashes for: {0}", FileName);
                    // update the VideoLocal record with the Hash
                    hashes = FileHashHelper.GetHashInfo(FileName, true, MainWindow.OnHashProgress, ServerSettings.Hash_CRC32, ServerSettings.Hash_MD5, ServerSettings.Hash_SHA1);
                    TimeSpan ts = DateTime.Now - start;
                    logger.Trace("Hashed file in {0} seconds --- {1} ({2})", ts.TotalSeconds.ToString("#0.0"), FileName, Utils.FormatByteSize(vlocal.FileSize));

                    vlocal.Hash = hashes.ed2k;
                    vlocal.CRC32 = hashes.crc32;
                    vlocal.MD5 = hashes.md5;
                    vlocal.SHA1 = hashes.sha1;
                    vlocal.HashSource = (int)HashSource.DirectHash;
                }

                // We should have a hash by now
                // before we save it, lets make sure there is not any other record with this hash (possible duplicate file)
                VideoLocal vidTemp = repVidLocal.GetByHash(vlocal.Hash);
                if (vidTemp != null)
                {
                    // don't delete it, if it is actually the same record
                    if (vidTemp.VideoLocalID != vlocal.VideoLocalID)
                    {
                        // delete the VideoLocal record
                        logger.Warn("Deleting duplicate video file record");
                        logger.Warn("---------------------------------------------");
                        logger.Warn("Keeping record for: {0}", vlocal.FullServerPath);
                        logger.Warn("Deleting record for: {0}", vidTemp.FullServerPath);
                        logger.Warn("---------------------------------------------");

                        // check if we have a record of this in the database, if not create one
                        DuplicateFileRepository repDups = new DuplicateFileRepository();
                        List<DuplicateFile> dupFiles = repDups.GetByFilePathsAndImportFolder(vlocal.FilePath, vidTemp.FilePath, vlocal.ImportFolderID, vidTemp.ImportFolderID);
                        if (dupFiles.Count == 0)
                            dupFiles = repDups.GetByFilePathsAndImportFolder(vidTemp.FilePath, vlocal.FilePath, vidTemp.ImportFolderID, vlocal.ImportFolderID);

                        if (dupFiles.Count == 0)
                        {
                            DuplicateFile dup = new DuplicateFile();
                            dup.DateTimeUpdated = DateTime.Now;
                            dup.FilePathFile1 = vlocal.FilePath;
                            dup.FilePathFile2 = vidTemp.FilePath;
                            dup.ImportFolderIDFile1 = vlocal.ImportFolderID;
                            dup.ImportFolderIDFile2 = vidTemp.ImportFolderID;
                            dup.Hash = vlocal.Hash;
                            repDups.Save(dup);
                        }

                        repVidLocal.Delete(vidTemp.VideoLocalID);
                    }
                }

                repVidLocal.Save(vlocal);

                // also save the filename to hash record
                // replace the existing records just in case it was corrupt
                FileNameHash fnhash = null;
                List<FileNameHash> fnhashes2 = repFNHash.GetByFileNameAndSize(Path.GetFileName(vlocal.FilePath), vlocal.FileSize);
                if (fnhashes2 != null && fnhashes2.Count > 1)
                {
                    // if we have more than one record it probably means there is some sort of corruption
                    // lets delete the local records
                    foreach (FileNameHash fnh in fnhashes2)
                    {
                        repFNHash.Delete(fnh.FileNameHashID);
                    }
                }

                if (fnhashes2 != null && fnhashes2.Count == 1)
                    fnhash = fnhashes2[0];
                else
                    fnhash = new FileNameHash();

                fnhash.FileName = Path.GetFileName(vlocal.FilePath);
                fnhash.FileSize = vlocal.FileSize;
                fnhash.Hash = vlocal.Hash;
                fnhash.DateTimeUpdated = DateTime.Now;
                repFNHash.Save(fnhash);
            }

            // now check if we have stored a VideoInfo record
            bool refreshMediaInfo = false;

            VideoInfoRepository repVidInfo = new VideoInfoRepository();
            VideoInfo vinfo = repVidInfo.GetByHash(vlocal.Hash);

            if (vinfo == null)
            {
                refreshMediaInfo = true;

                vinfo = new VideoInfo();
                vinfo.Hash = vlocal.Hash;

                vinfo.Duration = 0;
                vinfo.FileSize = fi.Length;
                vinfo.DateTimeUpdated = DateTime.Now;
                vinfo.FileName = filePath;

                vinfo.AudioBitrate = "";
                vinfo.AudioCodec = "";
                vinfo.VideoBitrate = "";
                vinfo.VideoBitDepth = "";
                vinfo.VideoCodec = "";
                vinfo.VideoFrameRate = "";
                vinfo.VideoResolution = "";

                repVidInfo.Save(vinfo);
            }
            else
            {
                // check if we need to update the media info
                if (vinfo.VideoCodec.Trim().Length == 0) refreshMediaInfo = true;
                else refreshMediaInfo = false;

            }

            if (refreshMediaInfo)
            {
                logger.Trace("Getting media info for: {0}", FileName);
                MediaInfoResult mInfo = FileHashHelper.GetMediaInfo(FileName, true);

                vinfo.AudioBitrate = string.IsNullOrEmpty(mInfo.AudioBitrate) ? "" : mInfo.AudioBitrate;
                vinfo.AudioCodec = string.IsNullOrEmpty(mInfo.AudioCodec) ? "" : mInfo.AudioCodec;

                vinfo.DateTimeUpdated = vlocal.DateTimeUpdated;
                vinfo.Duration = mInfo.Duration;
                vinfo.FileName = filePath;
                vinfo.FileSize = fi.Length;

                vinfo.VideoBitrate = string.IsNullOrEmpty(mInfo.VideoBitrate) ? "" : mInfo.VideoBitrate;
                vinfo.VideoBitDepth = string.IsNullOrEmpty(mInfo.VideoBitDepth) ? "" : mInfo.VideoBitDepth;
                vinfo.VideoCodec = string.IsNullOrEmpty(mInfo.VideoCodec) ? "" : mInfo.VideoCodec;
                vinfo.VideoFrameRate = string.IsNullOrEmpty(mInfo.VideoFrameRate) ? "" : mInfo.VideoFrameRate;
                vinfo.VideoResolution = string.IsNullOrEmpty(mInfo.VideoResolution) ? "" : mInfo.VideoResolution;
                vinfo.FullInfo = string.IsNullOrEmpty(mInfo.FullInfo) ? "" : mInfo.FullInfo;
                repVidInfo.Save(vinfo);
            }

            // now add a command to process the file
            CommandRequest_ProcessFile cr_procfile = new CommandRequest_ProcessFile(vlocal.VideoLocalID, false);
            cr_procfile.Save();

            return vlocal;
        }
Exemple #5
0
        public static void FixHashes()
        {
            try
            {
                VideoLocalRepository repVids = new VideoLocalRepository();

                foreach (VideoLocal vid in repVids.GetAll())
                {
                    bool fixedHash = false;
                    if (vid.CRC32.Equals("00000000"))
                    {
                        vid.CRC32 = null;
                        fixedHash = true;
                    }
                    if (vid.MD5.Equals("00000000000000000000000000000000"))
                    {
                        vid.MD5 = null;
                        fixedHash = true;
                    }
                    if (vid.SHA1.Equals("0000000000000000000000000000000000000000"))
                    {
                        vid.SHA1 = null;
                        fixedHash = true;
                    }
                    if (fixedHash)
                    {
                        repVids.Save(vid);
                        logger.Info("Fixed hashes on file: {0}", vid.FullServerPath);
                    }
                }
            }
            catch (Exception ex)
            {
                logger.ErrorException(ex.ToString(), ex);
            }
        }
Exemple #6
0
		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);
								}
							}
						}
					}
				}

			}

		}
Exemple #7
0
		public void RenameFile(string renameScript)
		{
			string renamed = RenameFileHelper.GetNewFileName(this, renameScript);
			if (string.IsNullOrEmpty(renamed)) return;

			ImportFolderRepository repFolders = new ImportFolderRepository();
			VideoLocalRepository repVids = new VideoLocalRepository();

			// actually rename the file
			string fullFileName = this.FullServerPath;

			// check if the file exists
			if (!File.Exists(fullFileName))
			{
				logger.Error("Error could not find the original file for renaming: " + fullFileName);
				return;
			}

			// actually rename the file
			string path = Path.GetDirectoryName(fullFileName);
			string newFullName = Path.Combine(path, renamed);

			try
			{
				logger.Info(string.Format("Renaming file From ({0}) to ({1})....", fullFileName, newFullName));

				if (fullFileName.Equals(newFullName, StringComparison.InvariantCultureIgnoreCase))
				{
					logger.Info(string.Format("Renaming file SKIPPED, no change From ({0}) to ({1})", fullFileName, newFullName));
				}
				else
				{
					File.Move(fullFileName, newFullName);
					logger.Info(string.Format("Renaming file SUCCESS From ({0}) to ({1})", fullFileName, newFullName));

					string newPartialPath = "";
					int folderID = this.ImportFolderID;

					DataAccessHelper.GetShareAndPath(newFullName, repFolders.GetAll(), ref folderID, ref newPartialPath);

					this.FilePath = newPartialPath;
					repVids.Save(this);
				}
			}
			catch (Exception ex)
			{
				logger.Info(string.Format("Renaming file FAIL From ({0}) to ({1}) - {2}", fullFileName, newFullName, ex.Message));
				logger.ErrorException(ex.ToString(), ex);
			}
		}