Beispiel #1
0
        //static void Unmount()
        //{
        //	Console.WriteLine("Unmounting G:");
        //	StartProcess(@"C:\Program Files\PowerISO\piso.exe", "unmount G:");
        //}

        public static void CategorizeLibraryFolders()
        {
            var folders = Directory.EnumerateDirectories(Paths.OutputPath, "*", SearchOption.TopDirectoryOnly);

            foreach (var folder in folders)
            {
                if (Path.GetFileName(folder) == "_Kontakt" || Path.GetFileName(folder) == "_Loops")
                {
                    continue;
                }
                var files = Directory.EnumerateFiles(folder, "*.nki", SearchOption.AllDirectories).ToArray();
                if (files.Length > 0)
                {
                    Directory.Move(folder, Paths.OutputPath + @"\_Kontakt\" + Path.GetFileName(folder));
                }
                else
                {
                    files = Directory.EnumerateFiles(folder, "*.iso", SearchOption.AllDirectories).ToArray();
                    files = files.Concat(Directory.EnumerateFiles(folder, "*.bin", SearchOption.AllDirectories)).ToArray();
                    if (files.Length > 0)
                    {
                        Directory.Move(folder, Paths.OutputPath + @"\_Iso\" + Path.GetFileName(folder));
                    }
                    else
                    {
                        Directory.Move(folder, Paths.OutputPath + @"\_Loops\" + Path.GetFileName(folder));
                    }
                }
            }
        }
Beispiel #2
0
        public void FileInfoReturnsCorrectDirectoryForLongPathFile()
        {
            Assert.IsTrue(Directory.Exists(longPathDirectory));
            string tempLongPathFilename;

            do
            {
                tempLongPathFilename = Path.Combine(longPathDirectory, Path.GetRandomFileName());
            } while (File.Exists(tempLongPathFilename));
            Assert.IsFalse(File.Exists(tempLongPathFilename));

            using (var writer = File.CreateText(tempLongPathFilename))
            {
                writer.WriteLine("test");
            }
            try
            {
                Assert.IsTrue(File.Exists(tempLongPathFilename));
                var fileInfo = new FileInfo(tempLongPathFilename);
                Assert.AreEqual(longPathDirectory, fileInfo.Directory.FullName);
                Assert.AreEqual(Path.GetFileName(longPathDirectory), fileInfo.Directory.Name);
            }
            finally
            {
                File.Delete(tempLongPathFilename);
            }
        }
        private void OnRenamed(object source, RenamedEventArgs e)
        {
            this.PathChanged(e.FullPath, fileExists: true);
            // Irritatingly, e.OldFullPath will throw an exception if the path is longer than the windows max
            // (but e.FullPath is fine).
            // So, construct it from e.FullPath and e.OldName
            // Note that we're using Pri.LongPath to get a Path.GetDirectoryName implementation that can handle
            // long paths

            // Apparently Path.GetDirectoryName(e.FullPath) or Path.GetFileName(e.OldName) can return null, see #112
            // Not sure why this might be, but let's work around it...
            var oldFullPathDirectory = Path.GetDirectoryName(e.FullPath);
            var oldFileName          = Path.GetFileName(e.OldName);

            if (oldFullPathDirectory == null)
            {
                logger.Warn("OldFullPathDirectory is null. Not sure why... e.FullPath: {0}", e.FullPath);
                return;
            }

            if (oldFileName == null)
            {
                logger.Warn("OldFileName is null. Not sure why... e.OldName: {0}", e.OldName);
                return;
            }

            var oldFullPath = Path.Combine(oldFullPathDirectory, oldFileName);

            this.PathChanged(oldFullPath, fileExists: false);
        }
 private void btnCoursePath_Click(object sender, EventArgs e)
 {
     try
     {
         VistaFolderBrowserDialog folderBrowserDialog = new VistaFolderBrowserDialog {
             SelectedPath = txtCoursePath.Text
         };
         if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
         {
             string coursePath = folderBrowserDialog.SelectedPath;
             if (Path.GetFileName(folderBrowserDialog.SelectedPath) != "courses")
             {
                 coursePath += @"\courses";
             }
             txtCoursePath.Text    = coursePath;
             appSetting.CoursePath = coursePath;
         }
     }
     catch (PathTooLongException)
     {
         VistaFolderBrowserDialog folderBrowserDialog = new VistaFolderBrowserDialog();
         if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
         {
             string coursePath = folderBrowserDialog.SelectedPath;
             if (Path.GetFileName(folderBrowserDialog.SelectedPath) != "courses")
             {
                 coursePath += @"\courses";
             }
             txtCoursePath.Text    = coursePath;
             appSetting.CoursePath = coursePath;
         }
     }
 }
        /// <summary>
        /// Write transcript for the clip if it available.
        /// </summary>
        /// <param name="clipId">Clip Id</param>
        /// <param name="clipPath">Path of current clip</param>
        public void WriteTranscriptFile(Clip clipId, string clipPath)
        {
            // Get all transcript to list
            List <ClipTranscript> clipTranscripts = clipId.Subtitle;

            if (clipTranscripts.Count > 0)
            {
                // Create transcript path with the same name of the clip
                string transcriptPath = Path.Combine(Path.GetDirectoryName(clipPath),
                                                     Path.GetFileNameWithoutExtension(clipPath) + ".srt");
                if (!File.Exists(transcriptPath))
                {
                    using (FileStream transcriptStream = File.OpenWrite(transcriptPath))
                    {
                        using (StreamWriter writer = new StreamWriter(transcriptStream))
                        {
                            // Write it to file with stream writer
                            int i = 1;
                            foreach (var clipTranscript in clipTranscripts)
                            {
                                var start = TimeSpan.FromMilliseconds(clipTranscript.StartTime).ToString(@"hh\:mm\:ss\,fff");
                                var end   = TimeSpan.FromMilliseconds(clipTranscript.EndTime).ToString(@"hh\:mm\:ss\,fff");
                                writer.WriteLine(i++);
                                writer.WriteLine(start + " --> " + end);
                                writer.WriteLine(clipTranscript.Text);
                                writer.WriteLine();
                            }
                        }
                    }
                    bgwDecrypt.ReportProgress(1, new { Text = $"Transcript of {Path.GetFileName(clipPath)} generated.", Color = Color.Purple, newLine = false });
                }
            }
        }
Beispiel #6
0
        public void TestInUseMove()
        {
            const bool recursive = true;

#if SHORT_SOURCE
            var tempPathFilename1 = System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), System.IO.Path.GetRandomFileName());
            System.IO.Directory.CreateDirectory(tempPathFilename1);
            Assert.IsTrue(System.IO.Directory.Exists(Path.GetFullPath(tempPathFilename1)));
            var tempPathFilename2 = System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), System.IO.Path.GetRandomFileName());
            System.IO.Directory.CreateDirectory(tempPathFilename2);
            Assert.IsTrue(System.IO.Directory.Exists(System.IO.Path.GetFullPath(tempPathFilename2)));
            try
            {
                using (
                    var writer = System.IO.File.CreateText(System.IO.Path.Combine(tempPathFilename2, "TestInUseMove")))
                {
                    string destinationPath =
                        System.IO.Path.GetFullPath(System.IO.Path.Combine(tempPathFilename1, System.IO.Path.GetFileName(tempPathFilename2)));
                    System.IO.Directory.Move(tempPathFilename2, destinationPath);
                    Assert.IsTrue(System.IO.Directory.Exists(System.IO.Path.GetFullPath(tempPathFilename1)));
                    Assert.IsFalse(System.IO.Directory.Exists(System.IO.Path.GetFullPath(tempPathFilename2)));
                    Assert.IsTrue(System.IO.Directory.Exists(destinationPath));
                }
            }
            catch (Exception e)
            {
                throw;
            }
            finally
            {
                Directory.Delete(tempPathFilename1, recursive);
                Directory.Delete(tempPathFilename2, recursive);
            }
#endif
            var tempLongPathFilename1 = Path.Combine(uncDirectory, Path.GetRandomFileName());
            Directory.CreateDirectory(tempLongPathFilename1);
            Assert.IsTrue(Directory.Exists(Path.GetFullPath(tempLongPathFilename1)));
            var tempLongPathFilename2 = Path.Combine(uncDirectory, Path.GetRandomFileName());
            Directory.CreateDirectory(tempLongPathFilename2);
            Assert.IsTrue(Directory.Exists(Path.GetFullPath(tempLongPathFilename2)));
            try
            {
                using (
                    var writer = File.CreateText(Path.Combine(tempLongPathFilename2, "TestInUseMove")))
                {
                    string destinationPath =
                        Path.GetFullPath(Path.Combine(tempLongPathFilename1, Path.GetFileName(tempLongPathFilename2)));
                    Directory.Move(tempLongPathFilename2, destinationPath);
                    Assert.IsTrue(Directory.Exists(Path.GetFullPath(tempLongPathFilename1)));
                    Assert.IsFalse(Directory.Exists(Path.GetFullPath(tempLongPathFilename2)));
                    Assert.IsTrue(Directory.Exists(destinationPath));
                }
            }
            finally
            {
                Directory.Delete(tempLongPathFilename1, recursive);
                Directory.Delete(tempLongPathFilename2, recursive);
            }
        }
Beispiel #7
0
        public void DecryptAllVideos(string folderPath, Module module, string outputPath, bool hasTranscript)
        {
            try
            {
                // Get all clips of this module from database
                List <Clip> listClips = module.Clips;

                if (listClips.Count > 0)
                {
                    // integer to add 1 if index should start at 1
                    int startAt1 = Convert.ToInt16(chkStartClipIndexAt1.Checked);
                    foreach (Clip clip in listClips)
                    {
                        // Get current path of the encrypted video
                        string currentPath = Path.Combine(folderPath, $"{clip.Name}.psv");
                        if (File.Exists(currentPath))
                        {
                            // Create new path with output folder
                            string newPath = Path.Combine(outputPath, $"{(startAt1 + clip.Index):00}. {clip.Title}.mp4");

                            // Init video and get it from iStream
                            var playingFileStream = new VirtualFileStream(currentPath);
                            playingFileStream.Clone(out IStream iStream);

                            string fileName = Path.GetFileName(currentPath);

                            bgwDecrypt.ReportProgress(1, new Log {
                                Text = $"Start to Decrypt File \"{fileName}\"", TextColor = Color.Yellow, NewLine = false
                            });

                            this.DecryptVideo(iStream, newPath);
                            if (chkCreateSub.Checked && hasTranscript)
                            {
                                // Generate transcript file if user ask
                                this.WriteTranscriptFile(clip, newPath);
                            }

                            bgwDecrypt.ReportProgress(1, new Log {
                                Text = $"Decrypt File \"{Path.GetFileName(newPath)}\" success!", TextColor = Color.Green, NewLine = true
                            });
                            playingFileStream.Dispose();
                        }
                        else
                        {
                            bgwDecrypt.ReportProgress(1, new Log {
                                Text = $"File \"{Path.GetFileName(currentPath)}\"  cannot be found", TextColor = Color.Gray, NewLine = true, IsError = true
                            });
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
            }
        }
Beispiel #8
0
        public Media GetMediaFromUser(int userID)
        {
            Media n = null;

            if (Media == null)
            {
                VideoLocal_Place pl = GetBestVideoLocalPlace();
                if (pl != null)
                {
                    IFileSystem f = pl.ImportFolder.FileSystem;
                    FileSystemResult <IObject> src = f.Resolve(pl.FullServerPath);
                    if (src != null && src.IsOk && src.Result is IFile)
                    {
                        if (pl.RefreshMediaInfo())
                        {
                            RepoFactory.VideoLocal.Save(pl.VideoLocal, true);
                        }
                    }
                }
            }
            if (Media != null)
            {
                n = Media.DeepClone();
                if (n?.Parts != null)
                {
                    foreach (Part p in n?.Parts)
                    {
                        string name = UrlSafe.Replace(Path.GetFileName(FileName), " ").Replace("  ", " ").Replace("  ", " ").Trim();
                        name = UrlSafe2.Replace(name, string.Empty).Trim().Replace("..", ".").Replace("..", ".").Replace("__", "_").Replace("__", "_").Replace(" ", "_").Replace("_.", ".");
                        while (name.StartsWith("_"))
                        {
                            name = name.Substring(1);
                        }
                        while (name.StartsWith("."))
                        {
                            name = name.Substring(1);
                        }
                        p.Key = PlexAndKodi.Helper.ReplaceSchemeHost(PlexAndKodi.Helper.ConstructVideoLocalStream(userID, VideoLocalID.ToString(), name, false));
                        if (p.Streams != null)
                        {
                            foreach (Stream s in p.Streams.Where(a => a.File != null && a.StreamType == "3"))
                            {
                                s.Key = PlexAndKodi.Helper.ReplaceSchemeHost(PlexAndKodi.Helper.ConstructFileStream(userID, s.File, false));
                            }
                        }
                    }
                }
            }
            return(n);
        }
Beispiel #9
0
        public static void FixLibraryFolders()
        {
            var folders = Directory.EnumerateDirectories(Paths.OutputPath, "*", SearchOption.TopDirectoryOnly);

            foreach (var folder in folders)
            {
                var name    = Path.GetFileName(folder);
                var oldName = name;
                name = Regex.Replace(name, @"\b(HYBRID|DYNAMiCS|PROPER|VSTi|RTAS|CHAOS|AMPLiFY|AU|MATRiX|DVDR|WAV|AiR|ArCADE|VR|CDDA|PAD|MiDi|CoBaLT|DiSCOVER)\b", "");
                name = Regex.Replace(name, @"\b(WareZ Audio info|Kontakt|Audiostrike|SYNTHiC4TE|AUDIOXiMiK|MAGNETRiXX|TZ7iSO|KLI|DVDriSO|DVD9|KRock|ACiD|REX|RMX|SynthX|AiFF|Apple Loops|AiRISO|MULTiFORMAT|AudioP2P|GHOSTiSO|REX2|DXi|HYBRiD|AKAI|ALFiSO)\b", "", RegexOptions.IgnoreCase);
                name = Regex.Replace(name, @"  +", " ");
                if (name != oldName && !Directory.Exists(Path.GetDirectoryName(folder) + @"\" + name))
                {
                    File.Move(folder, Path.GetDirectoryName(folder) + @"\" + name);
                }
            }
        }
Beispiel #10
0
        public Media GetMediaFromUser(int userID)
        {
            Media n = null;

            if (Media == null)
            {
                if (File.Exists(FullServerPath))
                {
                    VideoLocalRepository repo = new VideoLocalRepository();
                    repo.Save(this, false);
                }
            }
            if (Media != null)
            {
                n = (Media)Media.DeepCopy();
                if (n?.Parts != null)
                {
                    foreach (Part p in n?.Parts)
                    {
                        string name = UrlSafe.Replace(Path.GetFileName(FilePath), " ").Replace("  ", " ").Replace("  ", " ").Trim();
                        name = UrlSafe2.Replace(name, string.Empty).Trim().Replace("..", ".").Replace("..", ".").Replace("__", "_").Replace("__", "_").Replace(" ", "_").Replace("_.", ".");
                        while (name.StartsWith("_"))
                        {
                            name = name.Substring(1);
                        }
                        while (name.StartsWith("."))
                        {
                            name = name.Substring(1);
                        }
                        p.Key = PlexAndKodi.Helper.ReplaceSchemeHost(PlexAndKodi.Helper.ConstructVideoLocalStream(userID, VideoLocalID.ToString(), name, false));
                        if (p.Streams != null)
                        {
                            foreach (Stream s in p.Streams.Where(a => a.File != null && a.StreamType == "3"))
                            {
                                s.Key = PlexAndKodi.Helper.ReplaceSchemeHost(PlexAndKodi.Helper.ConstructFileStream(userID, s.File, false));
                            }
                        }
                    }
                }
            }
            return(n);
        }
Beispiel #11
0
        public static void InstallLibs()
        {
            // Unmount();
            var files    = Directory.EnumerateFiles(Paths.ToProcessPath, "*").ToArray();
            var archives = GetArchiveFiles(files);

            foreach (var archive in archives)
            {
                CleanupTempFolders();
                try
                {
                    Console.Write("Processing {0}\r\n", archive);
                    ProcessArchive(Paths.ToProcessPath, Paths.ToProcessPath, archive, files, Paths.OutputPath);
                    var associatedArchives = GetAssociatedArchives(archive, files);
                    foreach (var file in associatedArchives)
                    {
                        Directory.Move(file, file.Replace(Paths.ToProcessPath, Paths.ProcessedPath));
                    }
                    Console.WriteLine("Successfully processed {0}", archive);
                }
                catch (Exception e)
                {
                    foreach (var key in e.Data.Keys)
                    {
                        Console.WriteLine(e.Data[key] + ": " + key);
                    }
                    Console.Write(e.Message);
                    Console.WriteLine("Errors while processing {0}", archive);
                    foreach (var file in GetAssociatedArchives(archive, files))
                    {
                        Directory.Move(file, Paths.ToProcessPath + @"\_Broken\" + Path.GetFileName(file));
                    }
                }
                finally
                {
                    Console.WriteLine();
                    CleanupTempFolders();
                }
            }
        }
Beispiel #12
0
        private void OnRenamed(object source, RenamedEventArgs e)
        {
            // Apparently e.FullPath or e.OldName can be null, see #112
            // Not sure why this might be, but let's work around it...
            if (e.FullPath == null)
            {
                logger.Warn("OnRenamed: e.FullPath is null. Ignoring...");
                return;
            }

            this.RecordPathChange(e.FullPath, pathExists: true);
            // Irritatingly, e.OldFullPath will throw an exception if the path is longer than the windows max
            // (but e.FullPath is fine).
            // So, construct it from e.FullPath and e.OldName.
            // Note that we're using Pri.LongPath to get a Path.GetDirectoryName implementation that can handle
            // long paths

            if (e.OldName == null)
            {
                logger.Warn("OnRenamed: e.OldName is null. Not sure why");
                return;
            }

            // Note that e.FullPath could be a file or a directory. If it's a directory, it could be a drive
            // root. If it's a drive root, Path.GetDirectoryName will return null. I'm not sure *how* it could
            // be a drive root though... Record a change to the drive root if so.
            var oldFullPathDirectory = Path.GetDirectoryName(e.FullPath);

            if (oldFullPathDirectory == null)
            {
                this.RecordPathChange(e.FullPath, pathExists: true);
            }
            else
            {
                var oldFileName = Path.GetFileName(e.OldName);
                var oldFullPath = Path.Combine(oldFullPathDirectory, oldFileName);

                this.RecordPathChange(oldFullPath, pathExists: false);
            }
        }
Beispiel #13
0
        public void TestGetRecursiveFilesWithSubsetSearch()
        {
            var tempLongPathFilename = Path.Combine(uncDirectory, Path.GetRandomFileName());

            Directory.CreateDirectory(tempLongPathFilename);
            try
            {
                Assert.IsTrue(Directory.Exists(tempLongPathFilename));
                var randomFileName = Util.CreateNewEmptyFile(tempLongPathFilename);

                var searchPattern = Path.GetFileName(randomFileName).Substring(0, 3) + "*" + Path.GetExtension(randomFileName);

                var files = Directory.GetFiles(uncDirectory, searchPattern, SearchOption.AllDirectories).ToArray();
                Assert.AreEqual(1, files.Length);
                Assert.IsFalse(files.Contains(uncFilePath));
                Assert.IsTrue(files.Contains(randomFileName));
            }
            finally
            {
                const bool recursive = true;
                Directory.Delete(tempLongPathFilename, recursive);
            }
        }
Beispiel #14
0
        static void ProcessArchive(string rootPath, string folder, string file, IEnumerable <string> files, string destFolder)
        {
            if (Regex.IsMatch(file, @"(?i)\.(rar|r00|zip)$"))
            {
                var zipSize         = GetAssociatedArchives(file, files).Sum(archiveFile => new FileInfo(archiveFile).Length);
                var tempPath        = GetTempPath(zipSize * 2);
                var zipContentsPath = tempPath + folder.Substring(rootPath.Length) + Path.GetFileName(file);
                Console.WriteLine("Extracting {0} => {1}", Path.GetFileName(file), zipContentsPath);
                if (StartProcess(@"C:\Program Files\WinRAR\WinRar.exe", $"x -o+ -ibck -inul -y \"{file}\" \"{zipContentsPath}\"\\") < 2 && Directory.Exists(zipContentsPath))
                {
                    MarkFolderWritable(zipContentsPath);
                    ProcessFolder(tempPath, zipContentsPath, destFolder, true, false);
                }
                else
                {
                    Console.WriteLine("Error while extracting {0}", file);
                    throw new UnzipError();
                }
                Directory.Delete(zipContentsPath, true);
            }

            /*
             * else if (Path.GetExtension(file) == ".iso")
             * {
             *      Console.WriteLine("Mounting {0} => G:", file);
             *      StartProcess(@"C:\Program Files\PowerISO\piso.exe", string.Format("mount \"{0}\" G:", file));
             *      var isoSize = new FileInfo(file).Length;
             *      var tempPath = GetTempPath(isoSize);
             *      var isoContentsPath = tempPath + folder.Substring(rootPath.Length);
             *      CopyFiles("G:", isoContentsPath, false);
             *      MarkFolderWritable(isoContentsPath);
             *      ProcessFolder(tempPath, isoContentsPath, destFolder, true, true, string.Format(@"\{0}", Path.GetFileName(file)));
             *      Unmount();
             *      Directory.Delete(isoContentsPath, true);
             * }
             * */
        }
Beispiel #15
0
        public void TestMove()
        {
            var tempLongPathFilename1 = Path.Combine(uncDirectory, Path.GetRandomFileName());

            Directory.CreateDirectory(tempLongPathFilename1);
            Assert.IsTrue(Directory.Exists(Path.GetFullPath(tempLongPathFilename1)));
            var tempLongPathFilename2 = Path.Combine(uncDirectory, Path.GetRandomFileName());

            Directory.CreateDirectory(tempLongPathFilename2);
            Assert.IsTrue(Directory.Exists(Path.GetFullPath(tempLongPathFilename2)));

            string destinationPath = Path.GetFullPath(Path.Combine(tempLongPathFilename1, Path.GetFileName(tempLongPathFilename2)));

            Directory.Move(tempLongPathFilename2, destinationPath);
            Assert.IsTrue(Directory.Exists(Path.GetFullPath(tempLongPathFilename1)));
            Assert.IsFalse(Directory.Exists(Path.GetFullPath(tempLongPathFilename2)));
            Assert.IsTrue(Directory.Exists(destinationPath));

            const bool recursive = true;

            Directory.Delete(tempLongPathFilename1, recursive);
            Directory.Delete(tempLongPathFilename2, recursive);
        }
Beispiel #16
0
        public override void ProcessCommand()
        {
            logger.Info("Processing CommandRequest_DownloadImage: {0}", EntityID);
            string downloadURL = string.Empty;

            try
            {
                ImageDownloadRequest req = null;
                switch (EntityTypeEnum)
                {
                case ImageEntityType.TvDB_Episode:
                    TvDB_Episode ep = RepoFactory.TvDB_Episode.GetByID(EntityID);
                    if (string.IsNullOrEmpty(ep?.Filename))
                    {
                        logger.Warn($"TvDB Episode image failed to download: Can't get episode with ID: {EntityID}");
                        return;
                    }
                    req = new ImageDownloadRequest(EntityTypeEnum, ep, ForceDownload);
                    break;

                case ImageEntityType.TvDB_FanArt:
                    TvDB_ImageFanart fanart = RepoFactory.TvDB_ImageFanart.GetByID(EntityID);
                    if (string.IsNullOrEmpty(fanart?.BannerPath))
                    {
                        logger.Warn($"TvDB Fanart image failed to download: Can't find valid fanart with ID: {EntityID}");
                        RemoveImageRecord();
                        return;
                    }
                    req = new ImageDownloadRequest(EntityTypeEnum, fanart, ForceDownload);
                    break;

                case ImageEntityType.TvDB_Cover:
                    TvDB_ImagePoster poster = RepoFactory.TvDB_ImagePoster.GetByID(EntityID);
                    if (string.IsNullOrEmpty(poster?.BannerPath))
                    {
                        logger.Warn($"TvDB Poster image failed to download: Can't find valid poster with ID: {EntityID}");
                        RemoveImageRecord();
                        return;
                    }
                    req = new ImageDownloadRequest(EntityTypeEnum, poster, ForceDownload);
                    break;

                case ImageEntityType.TvDB_Banner:
                    TvDB_ImageWideBanner wideBanner = RepoFactory.TvDB_ImageWideBanner.GetByID(EntityID);
                    if (string.IsNullOrEmpty(wideBanner?.BannerPath))
                    {
                        logger.Warn($"TvDB Banner image failed to download: Can't find valid banner with ID: {EntityID}");
                        RemoveImageRecord();
                        return;
                    }
                    req = new ImageDownloadRequest(EntityTypeEnum, wideBanner, ForceDownload);
                    break;

                case ImageEntityType.MovieDB_Poster:
                    MovieDB_Poster moviePoster = RepoFactory.MovieDB_Poster.GetByID(EntityID);
                    if (string.IsNullOrEmpty(moviePoster?.URL))
                    {
                        logger.Warn($"MovieDB Poster image failed to download: Can't find valid poster with ID: {EntityID}");
                        RemoveImageRecord();
                        return;
                    }
                    req = new ImageDownloadRequest(EntityTypeEnum, moviePoster, ForceDownload);
                    break;

                case ImageEntityType.MovieDB_FanArt:
                    MovieDB_Fanart movieFanart = RepoFactory.MovieDB_Fanart.GetByID(EntityID);
                    if (string.IsNullOrEmpty(movieFanart?.URL))
                    {
                        logger.Warn($"MovieDB Fanart image failed to download: Can't find valid fanart with ID: {EntityID}");
                        return;
                    }
                    req = new ImageDownloadRequest(EntityTypeEnum, movieFanart, ForceDownload);
                    break;

                case ImageEntityType.AniDB_Cover:
                    SVR_AniDB_Anime anime = RepoFactory.AniDB_Anime.GetByAnimeID(EntityID);
                    if (anime == null)
                    {
                        logger.Warn($"AniDB poster image failed to download: Can't find AniDB_Anime with ID: {EntityID}");
                        return;
                    }
                    AniDbImageRateLimiter.Instance.EnsureRate();
                    req = new ImageDownloadRequest(EntityTypeEnum, anime, ForceDownload);
                    break;

                case ImageEntityType.AniDB_Character:
                    AniDB_Character chr = RepoFactory.AniDB_Character.GetByCharID(EntityID);
                    if (chr == null)
                    {
                        logger.Warn($"AniDB Character image failed to download: Can't find AniDB Character with ID: {EntityID}");
                        return;
                    }
                    AniDbImageRateLimiter.Instance.EnsureRate();
                    req = new ImageDownloadRequest(EntityTypeEnum, chr, ForceDownload);
                    break;

                case ImageEntityType.AniDB_Creator:
                    AniDB_Seiyuu creator = RepoFactory.AniDB_Seiyuu.GetBySeiyuuID(EntityID);
                    if (creator == null)
                    {
                        logger.Warn($"AniDB Seiyuu image failed to download: Can't find Seiyuu with ID: {EntityID}");
                        return;
                    }
                    AniDbImageRateLimiter.Instance.EnsureRate();
                    req = new ImageDownloadRequest(EntityTypeEnum, creator, ForceDownload);
                    break;
                }

                if (req == null)
                {
                    logger.Warn($"Image failed to download: No implementation found for {EntityTypeEnum}");
                    return;
                }

                List <string> fileNames    = new List <string>();
                List <string> downloadURLs = new List <string>();

                string fileNameTemp    = GetFileName(req, false);
                string downloadURLTemp = GetFileURL(req, false);

                fileNames.Add(fileNameTemp);
                downloadURLs.Add(downloadURLTemp);

                if (req.ImageType == ImageEntityType.TvDB_FanArt)
                {
                    fileNameTemp    = GetFileName(req, true);
                    downloadURLTemp = GetFileURL(req, true);

                    fileNames.Add(fileNameTemp);
                    downloadURLs.Add(downloadURLTemp);
                }

                for (int i = 0; i < fileNames.Count; i++)
                {
                    try
                    {
                        string fileName = fileNames[i];
                        downloadURL = downloadURLs[i];

                        bool downloadImage = true;
                        bool fileExists    = File.Exists(fileName);
                        bool imageValid    = fileExists && Misc.IsImageValid(fileName);

                        if (imageValid && !req.ForceDownload)
                        {
                            downloadImage = false;
                        }

                        if (!downloadImage)
                        {
                            continue;
                        }

                        string tempName = Path.Combine(ImageUtils.GetImagesTempFolder(), Path.GetFileName(fileName));

                        try
                        {
                            if (fileExists)
                            {
                                File.Delete(fileName);
                            }
                        }
                        catch (Exception ex)
                        {
                            Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(ServerSettings.Culture);

                            logger.Warn(Resources.Command_DeleteError, fileName, ex.Message);
                            return;
                        }

                        // If this has any issues, it will throw an exception, so the catch below will handle it
                        RecursivelyRetryDownload(downloadURL, ref tempName, 0, 5);

                        // move the file to it's final location
                        // check that the final folder exists
                        string fullPath = Path.GetDirectoryName(fileName);
                        if (!Directory.Exists(fullPath))
                        {
                            Directory.CreateDirectory(fullPath);
                        }

                        File.Move(tempName, fileName);
                        logger.Info($"Image downloaded: {fileName} from {downloadURL}");
                    }
                    catch (WebException e)
                    {
                        logger.Warn("Error processing CommandRequest_DownloadImage: {0} ({1}) - {2}", downloadURL,
                                    EntityID,
                                    e.Message);
                        // Remove the record if the image doesn't exist or can't download
                        RemoveImageRecord();
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Warn("Error processing CommandRequest_DownloadImage: {0} ({1}) - {2}", downloadURL, EntityID,
                            ex.Message);
            }
        }
Beispiel #17
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);
            }
        }
Beispiel #18
0
 static void CopyFiles(string source, string dest, bool move, bool includeArchives = true)
 {
     if (move)
     {
         MarkFolderWritable(source);
     }
     MarkFolderWritable(dest);
     Console.WriteLine("{0} {1} => {2}", move ? "Moving" : "Copying", source, dest);
     source = source.TrimEnd('\\');
     dest   = dest.TrimEnd('\\');
     if (!Directory.Exists(dest))
     {
         Directory.CreateDirectory(dest);
     }
     Directory.EnumerateDirectories(source, "*", SearchOption.AllDirectories).Select(d => d.Replace(source, dest)).ForEach(path => Directory.CreateDirectory(path));
     foreach (var file in Directory.EnumerateFiles(source, "*", SearchOption.AllDirectories).Where(f => Path.GetExtension(f) != ".nfo" && !Regex.IsMatch(Path.GetFileName(f), "All.Collection.Upload|WareZ-Audio", RegexOptions.IgnoreCase)).ToArray())
     {
         if (Path.GetExtension(file) == ".sfv")
         {
             continue;
         }
         //if (!includeArchives && Regex.IsMatch(Path.GetExtension(file), @"\.(rar|r\d+|zip|iso)"))
         if (!includeArchives && Path.GetDirectoryName(file) == source && Regex.IsMatch(Path.GetExtension(file), @"\.(rar|r\d+|zip)"))
         {
             continue;
         }
         var newFile = file.Replace(source, dest);
         if (move)
         {
             if (File.Exists(newFile))
             {
                 File.Delete(newFile);
             }
             File.Move(file, newFile);
         }
         else
         {
             File.Copy(file, newFile, true);
         }
     }
 }
Beispiel #19
0
 public void GetFileNameReturnsNullWithNullParameter()
 {
     Assert.IsNull(Path.GetFileName(null));
 }
Beispiel #20
0
 public static string GetFileName(string path)
 {
     return(Path.GetFileName(path));
 }
        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;
            long filesize    = 0;

            // Wait 3 minutes seconds before giving up on trying to access the file
            while ((filesize = CanAccessFile(FileName)) == 0 && (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        = filesize;
                    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        = filesize;
                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, true);

                // 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);
            }
            //Resave videolocal, since it do not have media populated (videinfo was not created).
            repVidLocal.Save(vlocal, true);
            // now add a command to process the file
            CommandRequest_ProcessFile cr_procfile = new CommandRequest_ProcessFile(vlocal.VideoLocalID, false);

            cr_procfile.Save();

            return(vlocal);
        }
Beispiel #22
0
        public override void ProcessCommand()
        {
            logger.Info("Processing CommandRequest_DownloadAniDBImages: {0}", AnimeID);

            AniDbRateLimiter.Instance.EnsureRate();
            try
            {
                List <ImageEntityType> types = new List <ImageEntityType>
                {
                    ImageEntityType.AniDB_Cover,
                    ImageEntityType.AniDB_Character,
                    ImageEntityType.AniDB_Creator
                };
                foreach (var EntityTypeEnum in types)
                {
                    List <string> downloadURLs = new List <string>();
                    List <string> fileNames    = new List <string>();
                    switch (EntityTypeEnum)
                    {
                    case ImageEntityType.AniDB_Cover:
                        SVR_AniDB_Anime anime = RepoFactory.AniDB_Anime.GetByAnimeID(AnimeID);
                        if (anime == null)
                        {
                            logger.Warn(
                                $"AniDB poster image failed to download: Can't find AniDB_Anime with ID: {AnimeID}");
                            return;
                        }

                        downloadURLs.Add(string.Format(Constants.URLS.AniDB_Images, anime.Picname));
                        fileNames.Add(anime.PosterPath);
                        break;

                    case ImageEntityType.AniDB_Character:
                        if (!ServerSettings.AniDB_DownloadCharacters)
                        {
                            continue;
                        }
                        var chrs = (from xref1 in RepoFactory.AniDB_Anime_Character.GetByAnimeID(AnimeID)
                                    select RepoFactory.AniDB_Character.GetByCharID(xref1.CharID))
                                   .Where(a => !string.IsNullOrEmpty(a?.PicName))
                                   .DistinctBy(a => a.CharID)
                                   .ToList();
                        if (chrs == null || chrs.Count == 0)
                        {
                            logger.Warn(
                                $"AniDB Character image failed to download: Can't find Character for anime: {AnimeID}");
                            return;
                        }

                        foreach (var chr in chrs)
                        {
                            downloadURLs.Add(string.Format(Constants.URLS.AniDB_Images, chr.PicName));
                            fileNames.Add(chr.GetPosterPath());
                        }

                        ShokoService.CmdProcessorGeneral.QueueState = PrettyDescriptionCharacters;
                        break;

                    case ImageEntityType.AniDB_Creator:
                        if (!ServerSettings.AniDB_DownloadCreators)
                        {
                            continue;
                        }

                        var creators = (from xref1 in RepoFactory.AniDB_Anime_Character.GetByAnimeID(AnimeID)
                                        from xref2 in RepoFactory.AniDB_Character_Seiyuu.GetByCharID(xref1.CharID)
                                        select RepoFactory.AniDB_Seiyuu.GetBySeiyuuID(xref2.SeiyuuID))
                                       .Where(a => !string.IsNullOrEmpty(a?.PicName))
                                       .DistinctBy(a => a.SeiyuuID)
                                       .ToList();
                        if (creators == null || creators.Count == 0)
                        {
                            logger.Warn(
                                $"AniDB Seiyuu image failed to download: Can't find Seiyuus for anime: {AnimeID}");
                            return;
                        }

                        foreach (var creator in creators)
                        {
                            downloadURLs.Add(string.Format(Constants.URLS.AniDB_Images, creator.PicName));
                            fileNames.Add(creator.GetPosterPath());
                        }

                        ShokoService.CmdProcessorGeneral.QueueState = PrettyDescriptionCreators;
                        break;
                    }

                    if (downloadURLs.Count == 0 || fileNames.All(a => string.IsNullOrEmpty(a)))
                    {
                        logger.Warn("Image failed to download: No URLs were generated. This should never happen");
                        return;
                    }


                    for (int i = 0; i < downloadURLs.Count; i++)
                    {
                        try
                        {
                            if (string.IsNullOrEmpty(fileNames[i]))
                            {
                                continue;
                            }
                            bool downloadImage = true;
                            bool fileExists    = File.Exists(fileNames[i]);
                            bool imageValid    = fileExists && Misc.IsImageValid(fileNames[i]);

                            if (imageValid && !ForceDownload)
                            {
                                downloadImage = false;
                            }

                            if (!downloadImage)
                            {
                                continue;
                            }

                            string tempName = Path.Combine(ImageUtils.GetImagesTempFolder(),
                                                           Path.GetFileName(fileNames[i]));

                            try
                            {
                                if (fileExists)
                                {
                                    File.Delete(fileNames[i]);
                                }
                            }
                            catch (Exception ex)
                            {
                                Thread.CurrentThread.CurrentUICulture =
                                    CultureInfo.GetCultureInfo(ServerSettings.Culture);

                                logger.Warn(Resources.Command_DeleteError, fileNames, ex.Message);
                                return;
                            }

                            // If this has any issues, it will throw an exception, so the catch below will handle it
                            RecursivelyRetryDownload(downloadURLs[i], ref tempName, 0, 5);

                            // move the file to it's final location
                            // check that the final folder exists
                            string fullPath = Path.GetDirectoryName(fileNames[i]);
                            if (!Directory.Exists(fullPath))
                            {
                                Directory.CreateDirectory(fullPath);
                            }

                            File.Move(tempName, fileNames[i]);
                            logger.Info($"Image downloaded: {fileNames[i]} from {downloadURLs[i]}");
                        }
                        catch (WebException e)
                        {
                            logger.Warn("Error processing CommandRequest_DownloadAniDBImages: {0} ({1}) - {2}",
                                        downloadURLs[i],
                                        AnimeID,
                                        e.Message);
                        }catch (Exception e)
                        {
                            logger.Error("Error processing CommandRequest_DownloadAniDBImages: {0} ({1}) - {2}",
                                         downloadURLs[i],
                                         AnimeID,
                                         e);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error("Error processing CommandRequest_DownloadAniDBImages: {0} - {1}", AnimeID, ex);
            }
            AniDbRateLimiter.Instance.ResetRate();
        }
        private VideoLocal_Place ProcessFile_LocalInfo()
        {
            // hash and read media info for file
            int    nshareID = -1;
            string filePath = "";


            Tuple <ImportFolder, string> tup = VideoLocal_PlaceRepository.GetFromFullPath(FileName);

            if (tup == null)
            {
                logger.Error($"Unable to locate file {FileName} inside the import folders");
                return(null);
            }
            ImportFolder folder = tup.Item1;

            filePath = tup.Item2;
            IFileSystem f = tup.Item1.FileSystem;

            if (f == null)
            {
                logger.Error("Unable to open filesystem for: {0}", FileName);
                return(null);
            }
            long filesize = 0;

            if (folder.CloudID == null) // Local Access
            {
                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 ((filesize = CanAccessFile(FileName)) == 0 && (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);
                }
            }


            FileSystemResult <IObject> source = f.Resolve(FileName);

            if (source == null || !source.IsOk || (!(source.Result is IFile)))
            {
                logger.Error("Could not access file: " + FileName);
                return(null);
            }
            IFile source_file = (IFile)source.Result;

            if (folder.CloudID.HasValue)
            {
                filesize = source_file.Size;
            }
            nshareID = folder.ImportFolderID;
            // check if we have already processed this file


            VideoLocal_Place vlocalplace = RepoFactory.VideoLocalPlace.GetByFilePathAndShareID(filePath, nshareID);
            VideoLocal       vlocal;

            if (vlocalplace != null)
            {
                vlocal = vlocalplace.VideoLocal;
                logger.Trace("VideoLocal record found in database: {0}", vlocal.VideoLocalID);

                if (ForceHash)
                {
                    vlocal.FileSize        = filesize;
                    vlocal.DateTimeUpdated = DateTime.Now;
                }
            }
            else
            {
                logger.Trace("VideoLocal, creating temporary record");
                vlocal = new VideoLocal();
                vlocal.DateTimeUpdated       = DateTime.Now;
                vlocal.DateTimeCreated       = vlocal.DateTimeUpdated;
                vlocal.FileName              = Path.GetFileName(filePath);
                vlocal.FileSize              = filesize;
                vlocal.Hash                  = string.Empty;
                vlocal.CRC32                 = string.Empty;
                vlocal.MD5                   = source_file.MD5.ToUpperInvariant() ?? string.Empty;
                vlocal.SHA1                  = source_file.SHA1.ToUpperInvariant() ?? string.Empty;
                vlocal.IsIgnored             = 0;
                vlocal.IsVariation           = 0;
                vlocalplace                  = new VideoLocal_Place();
                vlocalplace.FilePath         = filePath;
                vlocalplace.ImportFolderID   = nshareID;
                vlocalplace.ImportFolderType = folder.ImportFolderType;
            }

            // 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)
                {
                    List <CrossRef_File_Episode> crossRefs = RepoFactory.CrossRef_File_Episode.GetByFileNameAndSize(vlocal.FileName, 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 = RepoFactory.FileNameHash.GetByFileNameAndSize(vlocal.FileName, 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)
                        {
                            RepoFactory.FileNameHash.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;
                    }
                }
                if (string.IsNullOrEmpty(vlocal.Hash))
                {
                    FillVideoHashes(vlocal);
                }
                if (string.IsNullOrEmpty(vlocal.Hash) && folder.CloudID.HasValue)
                {
                    //Cloud and no hash, Nothing to do, except maybe Get the mediainfo....
                    logger.Trace("No Hash found for cloud " + vlocal.FileName + " putting in videolocal table with empty ED2K");
                    RepoFactory.VideoLocal.Save(vlocal, false);
                    vlocalplace.VideoLocalID = vlocal.VideoLocalID;
                    RepoFactory.VideoLocalPlace.Save(vlocalplace);
                    if (vlocalplace.RefreshMediaInfo())
                    {
                        RepoFactory.VideoLocal.Save(vlocalplace.VideoLocal, true);
                    }
                    return(vlocalplace);
                }
                // hash the file
                if (string.IsNullOrEmpty(vlocal.Hash) || ForceHash)
                {
                    JMMService.CmdProcessorHasher.QueueState = PrettyDescriptionHashing;
                    DateTime start = DateTime.Now;
                    logger.Trace("Calculating ED2K hashes for: {0}", FileName);
                    // update the VideoLocal record with the Hash, since cloud support we calculate everything
                    hashes = FileHashHelper.GetHashInfo(FileName.Replace("/", "\\"), true, MainWindow.OnHashProgress,
                                                        true, true, true);
                    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?.ToUpperInvariant();
                    vlocal.CRC32      = hashes.crc32?.ToUpperInvariant();
                    vlocal.MD5        = hashes.md5?.ToUpperInvariant();
                    vlocal.SHA1       = hashes.sha1?.ToUpperInvariant();
                    vlocal.HashSource = (int)HashSource.DirectHash;
                }
                FillMissingHashes(vlocal);
                // 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 tlocal = RepoFactory.VideoLocal.GetByHash(vlocal.Hash);

                bool             intercloudfolder = false;
                VideoLocal_Place prep             = tlocal?.Places.FirstOrDefault(a => a.ImportFolder.CloudID == folder.CloudID && a.ImportFolderID == folder.ImportFolderID && vlocalplace.VideoLocal_Place_ID != a.VideoLocal_Place_ID);
                if (prep != null)
                {
                    // delete the VideoLocal record
                    logger.Warn("Deleting duplicate video file record");
                    logger.Warn("---------------------------------------------");
                    logger.Warn($"Keeping record for: {vlocalplace.FullServerPath}");
                    logger.Warn($"Deleting record for: {prep.FullServerPath}");
                    logger.Warn("---------------------------------------------");

                    // check if we have a record of this in the database, if not create one
                    List <DuplicateFile> dupFiles = RepoFactory.DuplicateFile.GetByFilePathsAndImportFolder(vlocalplace.FilePath,
                                                                                                            prep.FilePath,
                                                                                                            vlocalplace.ImportFolderID, prep.ImportFolderID);
                    if (dupFiles.Count == 0)
                    {
                        dupFiles = RepoFactory.DuplicateFile.GetByFilePathsAndImportFolder(prep.FilePath, vlocalplace.FilePath, prep.ImportFolderID, vlocalplace.ImportFolderID);
                    }

                    if (dupFiles.Count == 0)
                    {
                        DuplicateFile dup = new DuplicateFile();
                        dup.DateTimeUpdated     = DateTime.Now;
                        dup.FilePathFile1       = vlocalplace.FilePath;
                        dup.FilePathFile2       = prep.FilePath;
                        dup.ImportFolderIDFile1 = vlocalplace.ImportFolderID;
                        dup.ImportFolderIDFile2 = prep.ImportFolderID;
                        dup.Hash = vlocal.Hash;
                        RepoFactory.DuplicateFile.Save(dup);
                    }
                    //Notify duplicate, don't delete
                }
                else if (tlocal != null)
                {
                    vlocal           = tlocal;
                    intercloudfolder = true;
                }


                if (!intercloudfolder)
                {
                    RepoFactory.VideoLocal.Save(vlocal, true);
                }

                vlocalplace.VideoLocalID = vlocal.VideoLocalID;
                RepoFactory.VideoLocalPlace.Save(vlocalplace);

                if (intercloudfolder)
                {
                    CommandRequest_ProcessFile cr_procfile3 = new CommandRequest_ProcessFile(vlocal.VideoLocalID, false);
                    cr_procfile3.Save();
                    return(vlocalplace);
                }

                // also save the filename to hash record
                // replace the existing records just in case it was corrupt
                FileNameHash        fnhash    = null;
                List <FileNameHash> fnhashes2 = RepoFactory.FileNameHash.GetByFileNameAndSize(vlocal.FileName, 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)
                    {
                        RepoFactory.FileNameHash.Delete(fnh.FileNameHashID);
                    }
                }

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

                fnhash.FileName        = vlocal.FileName;
                fnhash.FileSize        = vlocal.FileSize;
                fnhash.Hash            = vlocal.Hash;
                fnhash.DateTimeUpdated = DateTime.Now;
                RepoFactory.FileNameHash.Save(fnhash);
            }
            else
            {
                FillMissingHashes(vlocal);
            }


            if ((vlocal.Media == null) || vlocal.MediaVersion < VideoLocal.MEDIA_VERSION || vlocal.Duration == 0)
            {
                if (vlocalplace.RefreshMediaInfo())
                {
                    RepoFactory.VideoLocal.Save(vlocalplace.VideoLocal, true);
                }
            }
            // now add a command to process the file
            CommandRequest_ProcessFile cr_procfile = new CommandRequest_ProcessFile(vlocal.VideoLocalID, false);

            cr_procfile.Save();

            return(vlocalplace);
        }
Beispiel #24
0
        private void ProcessFile_LocalInfo()
        {
            // hash and read media info for file
            int nshareID = -1;


            Tuple <SVR_ImportFolder, string> tup = VideoLocal_PlaceRepository.GetFromFullPath(FileName);

            if (tup == null)
            {
                logger.Error($"Unable to locate Import Folder for {FileName}");
                return;
            }
            SVR_ImportFolder folder   = tup.Item1;
            string           filePath = tup.Item2;
            IFileSystem      f        = tup.Item1.FileSystem;

            if (f == null)
            {
                logger.Error("Unable to open filesystem for: {0}", FileName);
                return;
            }
            long filesize = 0;

            if (folder.CloudID == null) // Local Access
            {
                if (!File.Exists(FileName))
                {
                    logger.Error("File does not exist: {0}", FileName);
                    return;
                }

                int numAttempts = 0;

                // Wait 1 minute before giving up on trying to access the file
                while ((filesize = CanAccessFile(FileName)) == 0 && (numAttempts < 60))
                {
                    numAttempts++;
                    Thread.Sleep(1000);
                    logger.Error($@"Failed to access, (or filesize is 0) Attempt # {numAttempts}, {FileName}");
                }

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

                //For systems with no locking
                while (FileModified(FileName, 3))
                {
                    Thread.Sleep(1000);
                    logger.Error($@"An external process is modifying the file, {FileName}");
                }
            }


            FileSystemResult <IObject> source = f.Resolve(FileName);

            if (source == null || !source.IsOk || !(source.Result is IFile))
            {
                logger.Error("Could not access file: " + FileName);
                return;
            }
            IFile source_file = (IFile)source.Result;

            if (folder.CloudID.HasValue)
            {
                filesize = source_file.Size;
            }
            nshareID = folder.ImportFolderID;


            // check if we have already processed this file
            SVR_VideoLocal_Place vlocalplace = RepoFactory.VideoLocalPlace.GetByFilePathAndImportFolderID(filePath, nshareID);
            SVR_VideoLocal       vlocal      = null;
            var filename = Path.GetFileName(filePath);

            if (vlocalplace != null)
            {
                vlocal = vlocalplace.VideoLocal;
                if (vlocal != null)
                {
                    logger.Trace("VideoLocal record found in database: {0}", FileName);

                    // This will only happen with DB corruption, so just clean up the mess.
                    if (vlocalplace.FullServerPath == null)
                    {
                        if (vlocal.Places.Count == 1)
                        {
                            RepoFactory.VideoLocal.Delete(vlocal);
                            vlocal = null;
                        }

                        RepoFactory.VideoLocalPlace.Delete(vlocalplace);
                        vlocalplace = null;
                    }

                    if (vlocal != null && ForceHash)
                    {
                        vlocal.FileSize        = filesize;
                        vlocal.DateTimeUpdated = DateTime.Now;
                    }
                }
            }

            if (vlocal == null)
            {
                logger.Trace("No existing VideoLocal, creating temporary record");
                vlocal = new SVR_VideoLocal
                {
                    DateTimeUpdated = DateTime.Now,
                    DateTimeCreated = DateTimeUpdated,
                    FileName        = filename,
                    FileSize        = filesize,
                    Hash            = string.Empty,
                    CRC32           = string.Empty,
                    MD5             = source_file?.MD5?.ToUpperInvariant() ?? string.Empty,
                    SHA1            = source_file?.SHA1?.ToUpperInvariant() ?? string.Empty,
                    IsIgnored       = 0,
                    IsVariation     = 0
                };
            }

            if (vlocalplace == null)
            {
                logger.Trace("No existing VideoLocal_Place, creating a new record");
                vlocalplace = new SVR_VideoLocal_Place
                {
                    FilePath         = filePath,
                    ImportFolderID   = nshareID,
                    ImportFolderType = folder.ImportFolderType
                };
                // Make sure we have an ID
                RepoFactory.VideoLocalPlace.Save(vlocalplace);
            }

            // check if we need to get a hash this file
            if (string.IsNullOrEmpty(vlocal.Hash) || ForceHash)
            {
                logger.Trace("No existing hash in VideoLocal, checking XRefs");
                if (!ForceHash)
                {
                    // try getting the hash from the CrossRef
                    List <CrossRef_File_Episode> crossRefs =
                        RepoFactory.CrossRef_File_Episode.GetByFileNameAndSize(filename, vlocal.FileSize);
                    if (crossRefs.Any())
                    {
                        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 =
                        RepoFactory.FileNameHash.GetByFileNameAndSize(filename, 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)
                        {
                            RepoFactory.FileNameHash.Delete(fnh.FileNameHashID);
                        }
                    }
                    // reinit this to check if we erased them
                    fnhashes = RepoFactory.FileNameHash.GetByFileNameAndSize(filename, vlocal.FileSize);

                    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;
                    }
                }

                if (string.IsNullOrEmpty(vlocal.Hash))
                {
                    FillVideoHashes(vlocal);
                }

                //Cloud and no hash, Nothing to do, except maybe Get the mediainfo....
                if (string.IsNullOrEmpty(vlocal.Hash) && folder.CloudID.HasValue)
                {
                    logger.Trace("No Hash found for cloud " + filename +
                                 " putting in videolocal table with empty ED2K");
                    RepoFactory.VideoLocal.Save(vlocal, false);
                    vlocalplace.VideoLocalID = vlocal.VideoLocalID;
                    RepoFactory.VideoLocalPlace.Save(vlocalplace);
                    if (vlocalplace.RefreshMediaInfo())
                    {
                        RepoFactory.VideoLocal.Save(vlocalplace.VideoLocal, true);
                    }
                    return;
                }

                // hash the file
                if (string.IsNullOrEmpty(vlocal.Hash) || ForceHash)
                {
                    logger.Info("Hashing File: {0}", FileName);
                    ShokoService.CmdProcessorHasher.QueueState = PrettyDescriptionHashing;
                    DateTime start = DateTime.Now;
                    // update the VideoLocal record with the Hash, since cloud support we calculate everything
                    var hashes = FileHashHelper.GetHashInfo(FileName.Replace("/", $"{System.IO.Path.DirectorySeparatorChar}"), true, ShokoServer.OnHashProgress,
                                                            true, true, true);
                    TimeSpan ts = DateTime.Now - start;
                    logger.Trace("Hashed file in {0:#0.0} seconds --- {1} ({2})", ts.TotalSeconds, FileName,
                                 Utils.FormatByteSize(vlocal.FileSize));
                    vlocal.Hash       = hashes.ED2K?.ToUpperInvariant();
                    vlocal.CRC32      = hashes.CRC32?.ToUpperInvariant();
                    vlocal.MD5        = hashes.MD5?.ToUpperInvariant();
                    vlocal.SHA1       = hashes.SHA1?.ToUpperInvariant();
                    vlocal.HashSource = (int)HashSource.DirectHash;
                }
                FillMissingHashes(vlocal);
                // 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)

                SVR_VideoLocal tlocal    = RepoFactory.VideoLocal.GetByHash(vlocal.Hash);
                bool           duplicate = false;
                bool           changed   = false;

                if (tlocal != null)
                {
                    logger.Trace("Found existing VideoLocal with hash, merging info from it");
                    // Aid with hashing cloud. Merge hashes and save, regardless of duplicate file
                    changed = tlocal.MergeInfoFrom(vlocal);
                    vlocal  = tlocal;

                    List <SVR_VideoLocal_Place> preps = vlocal.Places.Where(
                        a => a.ImportFolder.CloudID == folder.CloudID &&
                        !vlocalplace.FullServerPath.Equals(a.FullServerPath)).ToList();
                    foreach (var prep in preps)
                    {
                        if (prep == null)
                        {
                            continue;
                        }
                        // clean up, if there is a 'duplicate file' that is invalid, remove it.
                        if (prep.FullServerPath == null)
                        {
                            RepoFactory.VideoLocalPlace.Delete(prep);
                        }
                        else
                        {
                            FileSystemResult dupFileSystemResult =
                                prep.ImportFolder?.FileSystem?.Resolve(prep.FullServerPath);
                            if (dupFileSystemResult == null || !dupFileSystemResult.IsOk)
                            {
                                RepoFactory.VideoLocalPlace.Delete(prep);
                            }
                        }
                    }

                    var dupPlace = vlocal.Places.FirstOrDefault(
                        a => a.ImportFolder.CloudID == folder.CloudID &&
                        !vlocalplace.FullServerPath.Equals(a.FullServerPath));

                    if (dupPlace != null)
                    {
                        logger.Warn("Found Duplicate File");
                        logger.Warn("---------------------------------------------");
                        logger.Warn($"New File: {vlocalplace.FullServerPath}");
                        logger.Warn($"Existing File: {dupPlace.FullServerPath}");
                        logger.Warn("---------------------------------------------");

                        // check if we have a record of this in the database, if not create one
                        List <DuplicateFile> dupFiles = RepoFactory.DuplicateFile.GetByFilePathsAndImportFolder(
                            vlocalplace.FilePath,
                            dupPlace.FilePath,
                            vlocalplace.ImportFolderID, dupPlace.ImportFolderID);
                        if (dupFiles.Count == 0)
                        {
                            dupFiles = RepoFactory.DuplicateFile.GetByFilePathsAndImportFolder(dupPlace.FilePath,
                                                                                               vlocalplace.FilePath, dupPlace.ImportFolderID, vlocalplace.ImportFolderID);
                        }

                        if (dupFiles.Count == 0)
                        {
                            DuplicateFile dup = new DuplicateFile
                            {
                                DateTimeUpdated     = DateTime.Now,
                                FilePathFile1       = vlocalplace.FilePath,
                                FilePathFile2       = dupPlace.FilePath,
                                ImportFolderIDFile1 = vlocalplace.ImportFolderID,
                                ImportFolderIDFile2 = dupPlace.ImportFolderID,
                                Hash = vlocal.Hash
                            };
                            RepoFactory.DuplicateFile.Save(dup);
                        }
                        //Notify duplicate, don't delete
                        duplicate = true;
                    }
                }

                if (!duplicate || changed)
                {
                    RepoFactory.VideoLocal.Save(vlocal, true);
                }

                vlocalplace.VideoLocalID = vlocal.VideoLocalID;
                RepoFactory.VideoLocalPlace.Save(vlocalplace);

                if (duplicate)
                {
                    CommandRequest_ProcessFile cr_procfile3 =
                        new CommandRequest_ProcessFile(vlocal.VideoLocalID, false);
                    cr_procfile3.Save();
                    return;
                }

                // also save the filename to hash record
                // replace the existing records just in case it was corrupt
                FileNameHash        fnhash;
                List <FileNameHash> fnhashes2 =
                    RepoFactory.FileNameHash.GetByFileNameAndSize(filename, 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)
                    {
                        RepoFactory.FileNameHash.Delete(fnh.FileNameHashID);
                    }
                }

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

                fnhash.FileName        = filename;
                fnhash.FileSize        = vlocal.FileSize;
                fnhash.Hash            = vlocal.Hash;
                fnhash.DateTimeUpdated = DateTime.Now;
                RepoFactory.FileNameHash.Save(fnhash);
            }
            else
            {
                FillMissingHashes(vlocal);
            }


            if ((vlocal.Media == null) || vlocal.MediaVersion < SVR_VideoLocal.MEDIA_VERSION || vlocal.Duration == 0)
            {
                if (vlocalplace.RefreshMediaInfo())
                {
                    RepoFactory.VideoLocal.Save(vlocalplace.VideoLocal, true);
                }
            }
            // now add a command to process the file
            CommandRequest_ProcessFile cr_procfile = new CommandRequest_ProcessFile(vlocal.VideoLocalID, false);

            cr_procfile.Save();
        }