예제 #1
0
파일: FileTests.cs 프로젝트: tmct/LongPath
        public void TestReplaceIgnoreMergeWithInvalidBackupPath()
        {
            Assert.Throws <DirectoryNotFoundException>(() =>
            {
                var tempLongPathFilename       = new StringBuilder(longPathDirectory).Append(@"\").Append("filename.ext").ToString();
                var tempBackupLongPathFilename = new StringBuilder(longPathDirectory).Append(@"\gibberish\").Append("backup").ToString();
                using (var fileStream = File.Create(tempLongPathFilename))
                {
                    fileStream.WriteByte(42);
                }
                var tempLongPathFilename2 = new StringBuilder(longPathDirectory).Append(@"\").Append("filename2.ext").ToString();

                using (var fileStream = File.Create(tempLongPathFilename2))
                {
                    fileStream.WriteByte(52);
                }
                try
                {
                    const bool ignoreMetadataErrors = true;
                    File.Replace(tempLongPathFilename, tempLongPathFilename2, tempBackupLongPathFilename, ignoreMetadataErrors);
                    using (var fileStream = File.OpenRead(tempLongPathFilename2))
                    {
                        Assert.AreEqual(42, fileStream.ReadByte());
                    }
                    Assert.IsFalse(File.Exists(tempLongPathFilename));
                    Assert.IsTrue(File.Exists(tempBackupLongPathFilename));
                }
                finally
                {
                    if (File.Exists(tempLongPathFilename))
                    {
                        File.Delete(tempLongPathFilename);
                    }
                    File.Delete(tempLongPathFilename2);
                    File.Delete(tempBackupLongPathFilename);
                }
            });
        }
예제 #2
0
        public static string DumpFile_Mono(string file)
        {
            if (!File.Exists(avdumpDestination) && !GetAndExtractAVDump())
            {
                return("Could not find  or download AvDump2 CLI");
            }
            if (string.IsNullOrEmpty(file))
            {
                return("File path cannot be null");
            }
            if (!File.Exists(file))
            {
                return("Could not find Video File: " + file);
            }

            //Create process
            Process pProcess = new Process();

            pProcess.StartInfo.FileName = $"mono";

            //strCommandParameters are parameters to pass to program
            string fileName = (char)34 + file + (char)34;

            pProcess.StartInfo.Arguments =
                $@"{avdumpDestination} --Auth={ServerSettings.Instance.AniDb.Username}:{ServerSettings.Instance.AniDb.AVDumpKey} --LPort={ServerSettings.Instance.AniDb.AVDumpClientPort} --PrintEd2kLink -t {fileName}";

            pProcess.StartInfo.UseShellExecute        = false;
            pProcess.StartInfo.WindowStyle            = ProcessWindowStyle.Hidden;
            pProcess.StartInfo.RedirectStandardOutput = true;
            pProcess.StartInfo.CreateNoWindow         = true;
            pProcess.Start();
            string strOutput = pProcess.StandardOutput.ReadToEnd();

            //Wait for process to finish
            pProcess.WaitForExit();

            return(strOutput);
        }
예제 #3
0
파일: UnitTest1.cs 프로젝트: 24/source_04
        public void TestProblemWithSystemIoExists()
        {
            var filename = new StringBuilder(longPathDirectory).Append(@"\").Append("file4.ext").ToString();

            using (var writer = File.CreateText(filename))
            {
                writer.WriteLine("test");
            }
            Assert.IsTrue(File.Exists(filename));

            try
            {
                using (var fileStream = new System.IO.FileStream(filename, FileMode.Append, FileAccess.Write, FileShare.None))
                    using (var bw = new BinaryWriter(fileStream))
                    {
                        bw.Write(10u);
                    }
            }
            finally
            {
                File.Delete(filename);
            }
        }
예제 #4
0
 /// <summary>
 /// Looks for the given list of exes in the sys path.
 /// </summary>
 /// <param name="files">An array of exes</param>
 /// <returns>The full path to the first occurance of an exe in the path.</returns>
 public static string CheckSysPath(string[] files)
 {
     try
     {
         string sysPath    = Environment.GetEnvironmentVariable("PATH");
         string playerPath = null;
         foreach (var path in sysPath.Split(';'))
         {
             foreach (string file in files)
             {
                 playerPath = Path.Combine(path, file);
                 if (File.Exists(playerPath))
                 {
                     return(playerPath);
                 }
             }
         }
     } catch (Exception ex)
     {
         logger.Log(LogLevel.Error, ex, "Invalid PATH variable.");
     }
     return(null);
 }
        /// <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))
                {
                    File.Delete(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 Log {
                    Text = $"Transcript of {Path.GetFileName(clipPath)} generated.", TextColor = Color.Purple, NewLine = false
                });
            }
        }
예제 #6
0
 public void SetUp()
 {
     directory = Path.Combine(TestContext.CurrentContext.TestDirectory, "subdir");
     System.IO.Directory.CreateDirectory(directory);
     try
     {
         uncDirectory = UncHelper.GetUncFromPath(directory);
         filePath     = new StringBuilder(directory).Append(@"\").Append(Filename).ToString();
         uncFilePath  = UncHelper.GetUncFromPath(filePath);
         using (var writer = System.IO.File.CreateText(filePath))
         {
             writer.WriteLine("test");
         }
         Debug.Assert(File.Exists(uncFilePath));
     }
     catch (Exception)
     {
         if (System.IO.Directory.Exists(directory))
         {
             System.IO.Directory.Delete(directory, true);
         }
         throw;
     }
 }
예제 #7
0
파일: FileTests.cs 프로젝트: tmct/LongPath
        public void TestCreateText()
        {
            var          filename = new StringBuilder(longPathDirectory).Append(@"\").Append("file3.ext").ToString();
            const string fileText = "test";

            using (var writer = File.CreateText(filename))
            {
                writer.WriteLine(fileText);
            }
            try
            {
                Assert.IsTrue(File.Exists(filename));

                using (var reader = File.OpenText(filename))
                {
                    var text = reader.ReadLine();
                    Assert.AreEqual(fileText, text);
                }
            }
            finally
            {
                File.Delete(filename);
            }
        }
예제 #8
0
        /// <summary>
        /// Internal function that return valid image file path on server that exist
        /// </summary>
        /// <param name="id">image id</param>
        /// <param name="type">image type</param>
        /// <param name="thumb">thumb mode</param>
        /// <returns>string</returns>
        internal string GetImagePath(int type, int id, bool thumb)
        {
            ImageEntityType imageType = (ImageEntityType)type;
            string          path;

            switch (imageType)
            {
            // 1
            case ImageEntityType.AniDB_Cover:
                SVR_AniDB_Anime anime = RepoFactory.AniDB_Anime.GetByAnimeID(id);
                if (anime == null)
                {
                    return(null);
                }
                path = anime.PosterPath;
                if (File.Exists(path))
                {
                    return(path);
                }
                else
                {
                    path = string.Empty;
                    logger.Trace("Could not find AniDB_Cover image: {0}", anime.PosterPath);
                }
                break;

            // 2
            case ImageEntityType.AniDB_Character:
                AniDB_Character chr = RepoFactory.AniDB_Character.GetByCharID(id);
                if (chr == null)
                {
                    return(null);
                }
                path = chr.GetPosterPath();
                if (File.Exists(path))
                {
                    return(path);
                }
                else
                {
                    path = string.Empty;
                    logger.Trace("Could not find AniDB_Character image: {0}", chr.GetPosterPath());
                }
                break;

            // 3
            case ImageEntityType.AniDB_Creator:
                AniDB_Seiyuu creator = RepoFactory.AniDB_Seiyuu.GetBySeiyuuID(id);
                if (creator == null)
                {
                    return(null);
                }
                path = creator.GetPosterPath();
                if (File.Exists(path))
                {
                    return(path);
                }
                else
                {
                    path = string.Empty;
                    logger.Trace("Could not find AniDB_Creator image: {0}", creator.GetPosterPath());
                }
                break;

            // 4
            case ImageEntityType.TvDB_Banner:
                TvDB_ImageWideBanner wideBanner = RepoFactory.TvDB_ImageWideBanner.GetByID(id);
                if (wideBanner == null)
                {
                    return(null);
                }
                path = wideBanner.GetFullImagePath();
                if (File.Exists(path))
                {
                    return(path);
                }
                else
                {
                    path = string.Empty;
                    logger.Trace("Could not find TvDB_Banner image: {0}", wideBanner.GetFullImagePath());
                }
                break;

            // 5
            case ImageEntityType.TvDB_Cover:
                TvDB_ImagePoster poster = RepoFactory.TvDB_ImagePoster.GetByID(id);
                if (poster == null)
                {
                    return(null);
                }
                path = poster.GetFullImagePath();
                if (File.Exists(path))
                {
                    return(path);
                }
                else
                {
                    path = string.Empty;
                    logger.Trace("Could not find TvDB_Cover image: {0}", poster.GetFullImagePath());
                }
                break;

            // 6
            case ImageEntityType.TvDB_Episode:
                TvDB_Episode ep = RepoFactory.TvDB_Episode.GetByID(id);
                if (ep == null)
                {
                    return(null);
                }
                path = ep.GetFullImagePath();
                if (File.Exists(path))
                {
                    return(path);
                }
                else
                {
                    path = string.Empty;
                    logger.Trace("Could not find TvDB_Episode image: {0}", ep.GetFullImagePath());
                }
                break;

            // 7
            case ImageEntityType.TvDB_FanArt:
                TvDB_ImageFanart fanart = RepoFactory.TvDB_ImageFanart.GetByID(id);
                if (fanart == null)
                {
                    return(null);
                }
                if (thumb)
                {
                    //ratio
                    path = fanart.GetFullThumbnailPath();
                    if (File.Exists(path))
                    {
                        return(path);
                    }
                    path = string.Empty;
                    logger.Trace("Could not find TvDB_FanArt image: {0}", fanart.GetFullThumbnailPath());
                }
                else
                {
                    path = fanart.GetFullImagePath();
                    if (File.Exists(path))
                    {
                        return(path);
                    }
                    path = string.Empty;
                    logger.Trace("Could not find TvDB_FanArt image: {0}", fanart.GetFullImagePath());
                }
                break;

            // 8
            case ImageEntityType.MovieDB_FanArt:
                MovieDB_Fanart mFanart = RepoFactory.MovieDB_Fanart.GetByID(id);
                if (mFanart == null)
                {
                    return(null);
                }
                mFanart = RepoFactory.MovieDB_Fanart.GetByOnlineID(mFanart.URL);
                if (mFanart == null)
                {
                    return(null);
                }
                path = mFanart.GetFullImagePath();
                if (File.Exists(path))
                {
                    return(path);
                }
                else
                {
                    path = string.Empty;
                    logger.Trace("Could not find MovieDB_FanArt image: {0}", mFanart.GetFullImagePath());
                }
                break;

            // 9
            case ImageEntityType.MovieDB_Poster:
                MovieDB_Poster mPoster = RepoFactory.MovieDB_Poster.GetByID(id);
                if (mPoster == null)
                {
                    return(null);
                }
                mPoster = RepoFactory.MovieDB_Poster.GetByOnlineID(mPoster.URL);
                if (mPoster == null)
                {
                    return(null);
                }
                path = mPoster.GetFullImagePath();
                if (File.Exists(path))
                {
                    return(path);
                }
                else
                {
                    path = string.Empty;
                    logger.Trace("Could not find MovieDB_Poster image: {0}", mPoster.GetFullImagePath());
                }
                break;

            case ImageEntityType.Character:
                AnimeCharacter character = RepoFactory.AnimeCharacter.GetByID(id);
                if (character == null)
                {
                    return(null);
                }
                path = character.ImagePath;
                if (File.Exists(path))
                {
                    return(path);
                }
                else
                {
                    path = string.Empty;
                    logger.Trace("Could not find Character image: {0}", character.ImagePath);
                }
                break;

            case ImageEntityType.Staff:
                var staff = RepoFactory.AnimeStaff.GetByID(id);
                if (staff == null)
                {
                    return(null);
                }
                path = staff.ImagePath;
                if (File.Exists(path))
                {
                    return(path);
                }
                else
                {
                    path = string.Empty;
                    logger.Trace("Could not find Staff image: {0}", staff.ImagePath);
                }
                break;

            default:
                path = string.Empty;
                break;
            }

            return(path);
        }
        public void DecryptCourse(List <ListViewItem> list)
        {
            if (string.IsNullOrWhiteSpace(txtCoursePath.Text))
            {
                MessageBox.Show("Please select course path", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else if (string.IsNullOrWhiteSpace(txtDBPath.Text))
            {
                MessageBox.Show("Please select database path", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else if (string.IsNullOrWhiteSpace(txtOutputPath.Text))
            {
                MessageBox.Show("Please select output path", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }


            foreach (ListViewItem item in list)
            {
                CourseItem courseItem = listCourse.Where(r => r.Course.Name == item.Name).Select(r => r).FirstOrDefault();

                if (chkDecrypt.Checked)
                {
                    bgwDecrypt.ReportProgress(1, new Log {
                        Text = $"Start to decrypt course \"{courseItem.Course.Title}\"", TextColor = Color.Magenta, NewLine = true, IsError = true
                    });

                    //Create new course path with the output path
                    var newCoursePath = Path.Combine(txtOutputPath.Text, this.CleanName(courseItem.Course.Title));

                    DirectoryInfo courseInfo = Directory.Exists(newCoursePath)
                        ? new DirectoryInfo(newCoursePath)
                        : Directory.CreateDirectory(newCoursePath);

                    if (chkCopyImage.Checked && File.Exists($"{courseItem.CoursePath}\\image.jpg"))
                    {
                        File.Copy($"{courseItem.CoursePath}\\image.jpg", $"{newCoursePath}\\image.jpg", true);
                    }


                    //Get list all modules in current course
                    List <Module> listModules = courseItem.Course.Modules;

                    if (listModules.Count > 0)
                    {
                        // integer to add 1 if index should start at 1
                        int startAt1 = Convert.ToInt16(chkStartModuleIndexAt1.Checked);
                        //Get each module
                        foreach (Module module in listModules)
                        {
                            //Generate module hash name
                            string moduleHash = this.ModuleHash(module.Name, module.AuthorHandle);
                            //Generate module path
                            string moduleHashPath = Path.Combine(courseItem.CoursePath, moduleHash);
                            //Create new module path with decryption name
                            string newModulePath = Path.Combine(courseInfo.FullName, $"{(startAt1 + module.Index):00}. {module.Title}");

                            if (Directory.Exists(moduleHashPath))
                            {
                                DirectoryInfo moduleInfo = Directory.Exists(newModulePath)
                                    ? new DirectoryInfo(newModulePath)
                                    : Directory.CreateDirectory(newModulePath);
                                //Decrypt all videos in current module folder
                                this.DecryptAllVideos(moduleHashPath, module, moduleInfo.FullName);
                            }
                            else
                            {
                                bgwDecrypt.ReportProgress(1, new Log {
                                    Text = $"Folder {moduleHash} not found in the current course path", TextColor = Color.Red, NewLine = true, IsError = true
                                });
                            }
                        }
                    }
                    bgwDecrypt.ReportProgress(1, new Log {
                        Text = $"Decrypt \"{courseItem.Course.Title}\" complete!", TextColor = Color.Magenta, NewLine = true, IsError = true
                    });
                }

                if (chkDelete.Checked)
                {
                    try
                    {
                        Directory.Delete(courseItem.CoursePath, true);
                    }
                    catch (Exception ex)
                    {
                        bgwDecrypt.ReportProgress(1, new Log {
                            Text = $"Delete folder course {courseItem.Course.Title} fail\n{ex.Message}", TextColor = Color.Gray, NewLine = true, IsError = true
                        });
                    }

                    try
                    {
                        RemoveCourseInDb(courseItem.CoursePath);
                    }
                    catch (Exception ex)
                    {
                        bgwDecrypt.ReportProgress(1, new Log {
                            Text = $"Delete course {courseItem.Course.Title} from db fail\n{ex.Message}", TextColor = Color.Gray, NewLine = true, IsError = true
                        });
                    }


                    bgwDecrypt.ReportProgress(1, new Log {
                        Text = $"Delete course {courseItem.Course.Title} success!", TextColor = Color.Magenta, NewLine = true
                    });
                }
            }

            bgwDecrypt.ReportProgress(100);
        }
예제 #10
0
        private void RecursivelyRetryDownload(string downloadURL, ref string tempFilePath, int count, int maxretry)
        {
            try
            {
                // download image
                if (downloadURL.Length <= 0)
                {
                    return;
                }
                using (WebClient client = new WebClient())
                {
                    client.Headers.Add("user-agent", "JMM");
                    //OnImageDownloadEvent(new ImageDownloadEventArgs("", req, ImageDownloadEventType.Started));
                    //BaseConfig.MyAnimeLog.Write("ProcessImages: Download: {0}  *** to ***  {1}", req.URL, fullName);

                    byte[] bytes = client.DownloadData(downloadURL);
                    if (bytes.Length < 4)
                    {
                        throw new WebException(
                                  "The image download stream returned less than 4 bytes (a valid image has 2-4 bytes in the header)");
                    }

                    ImageFormatEnum imageFormat = Misc.GetImageFormat(bytes);
                    string          extension;
                    switch (imageFormat)
                    {
                    case ImageFormatEnum.bmp:
                        extension = ".bmp";
                        break;

                    case ImageFormatEnum.gif:
                        extension = ".gif";
                        break;

                    case ImageFormatEnum.jpeg:
                        extension = ".jpeg";
                        break;

                    case ImageFormatEnum.png:
                        extension = ".png";
                        break;

                    case ImageFormatEnum.tiff:
                        extension = ".tiff";
                        break;

                    default: throw new WebException("The image download stream returned an invalid image");
                    }

                    if (extension.Length <= 0)
                    {
                        return;
                    }
                    string newFile = Path.ChangeExtension(tempFilePath, extension);
                    if (newFile == null)
                    {
                        return;
                    }

                    if (File.Exists(newFile))
                    {
                        File.Delete(newFile);
                    }
                    using (var fs = new FileStream(newFile, FileMode.Create, FileAccess.Write))
                    {
                        fs.Write(bytes, 0, bytes.Length);
                    }
                    tempFilePath = newFile;
                }
            }
            catch (WebException)
            {
                if (count + 1 >= maxretry)
                {
                    throw;
                }
                Thread.Sleep(500);
                RecursivelyRetryDownload(downloadURL, ref tempFilePath, count + 1, maxretry);
            }
        }
예제 #11
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);
            }
        }
예제 #12
0
        static void Main(string[] args)
        {
            int totalProcessed = 0;
            int totalHashed    = 0;
            int totalFailed    = 0;


            IEnumerable <FilesDueForHash_Result> filesToHash = GetNextFiles();

            do
            {
                Parallel.ForEach(filesToHash, fileToHash =>
                {
                    Console.WriteLine("Processing: " + fileToHash.FullPath);

                    try
                    {
                        if (File.Exists(fileToHash.FullPath) &&
                            fileToHash.Length > 0)
                        {
                            Stream fileStream = File.OpenRead(fileToHash.FullPath);

                            IHashMaker hasher = new HashMaker();

                            string preHash = hasher.GetPreHash(fileToHash.FullPath);
                            string md5     = hasher.GetMD5(fileStream);
                            string sha1    = hasher.GetSHA1(fileStream);

                            using (DbModelContainer db = new DbModelContainer())
                            {
                                db.Database.CommandTimeout = 60;

                                TrackedFile fileToUpdate = db.TrackedFiles.Find(fileToHash.Id);
                                fileToUpdate.PreHash     = preHash;
                                fileToUpdate.MD5         = md5;
                                fileToUpdate.SHA1        = sha1;

                                db.SaveChanges();
                            }

                            Interlocked.Increment(ref totalHashed);
                        }
                    }
                    catch (Exception)
                    {
                        //mark it as failed
                        Interlocked.Increment(ref totalFailed);
                    }
                    finally
                    {
                        Interlocked.Increment(ref totalProcessed);

                        using (DbModelContainer db = new DbModelContainer())
                        {
                            db.Database.CommandTimeout = 60;
                            TrackedFile file           = db.TrackedFiles.Find(fileToHash.Id);
                            file.HashAttempted         = DateTime.Now;
                            db.SaveChanges();
                        }
                    }
                });

                filesToHash = GetNextFiles();
            } while (filesToHash.Count() > 0);
        }
예제 #13
0
파일: zFile.cs 프로젝트: 24/source_04
 public static bool Exists(string path)
 {
     return(File.Exists(path));
 }
예제 #14
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);
            }
        }
예제 #15
0
        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);
        }
예제 #16
0
        private VideoLocal ProcessFile_LocalInfo()
        {
            // hash and read media info for file
            int    nshareID = -1;
            string filePath = "";

            ImportFolderRepository repNS  = new ImportFolderRepository();
            List <ImportFolder>    shares = repNS.GetAll();

            DataAccessHelper.GetShareAndPath(FileName, shares, ref nshareID, ref filePath);

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

            int  numAttempts = 0;
            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);
        }
예제 #17
0
        public static Media Convert(string filename)
        {
            int       ex = 0;
            MediaInfo mi = new MediaInfo();

            if (mi == null)
            {
                return(null);
            }
            if (!File.Exists(filename))
            {
                return(null);
            }
            try
            {
                mi.Open(filename);
                ex = 1;
                Media  m           = new Media();
                Part   p           = new Part();
                Stream VideoStream = null;
                int    video_count = mi.GetInt(StreamKind.General, 0, "VideoCount");
                int    audio_count = mi.GetInt(StreamKind.General, 0, "AudioCount");
                int    text_count  = mi.GetInt(StreamKind.General, 0, "TextCount");
                m.Duration  = p.Duration = mi.Get(StreamKind.General, 0, "Duration");
                m.Container = p.Container = TranslateContainer(mi.Get(StreamKind.General, 0, "Format"));
                string codid = mi.Get(StreamKind.General, 0, "CodecID");
                if (!string.IsNullOrEmpty(codid) && (codid.Trim().ToLower() == "qt"))
                {
                    m.Container = p.Container = "mov";
                }

                int brate = mi.GetInt(StreamKind.General, 0, "BitRate");
                if (brate != 0)
                {
                    m.Bitrate = Math.Round(brate / 1000F).ToString(CultureInfo.InvariantCulture);
                }
                p.Size = mi.Get(StreamKind.General, 0, "FileSize");
                //m.Id = p.Id = mi.Get(StreamKind.General, 0, "UniqueID");

                ex = 2;
                List <Stream> streams = new List <Stream>();
                int           iidx    = 0;
                if (video_count > 0)
                {
                    for (int x = 0; x < video_count; x++)
                    {
                        Stream s = TranslateVideoStream(mi, x);
                        if (x == 0)
                        {
                            VideoStream = s;
                            m.Width     = s.Width;
                            m.Height    = s.Height;
                            if (!string.IsNullOrEmpty(m.Height))
                            {
                                if (!string.IsNullOrEmpty(m.Width))
                                {
                                    m.VideoResolution = GetResolution(float.Parse(m.Width), float.Parse(m.Height));
                                    m.AspectRatio     = GetAspectRatio(float.Parse(m.Width), float.Parse(m.Height), s.PA);
                                }
                            }
                            if (!string.IsNullOrEmpty(s.FrameRate))
                            {
                                float fr = System.Convert.ToSingle(s.FrameRate);
                                m.VideoFrameRate = ((int)Math.Round(fr)).ToString(CultureInfo.InvariantCulture);
                                if (!string.IsNullOrEmpty(s.ScanType))
                                {
                                    if (s.ScanType.ToLower().Contains("int"))
                                    {
                                        m.VideoFrameRate += "i";
                                    }
                                    else
                                    {
                                        m.VideoFrameRate += "p";
                                    }
                                }
                                else
                                {
                                    m.VideoFrameRate += "p";
                                }
                                if ((m.VideoFrameRate == "25p") || (m.VideoFrameRate == "25i"))
                                {
                                    m.VideoFrameRate = "PAL";
                                }
                                else if ((m.VideoFrameRate == "30p") || (m.VideoFrameRate == "30i"))
                                {
                                    m.VideoFrameRate = "NTSC";
                                }
                            }
                            m.VideoCodec = s.Codec;
                            if (!string.IsNullOrEmpty(m.Duration) && !string.IsNullOrEmpty(s.Duration))
                            {
                                double sdur = 0;
                                double mdur = 0;
                                double.TryParse(s.Duration, NumberStyles.Any, CultureInfo.InvariantCulture, out sdur);
                                double.TryParse(m.Duration, NumberStyles.Any, CultureInfo.InvariantCulture, out mdur);
                                if (sdur > mdur)
                                {
                                    m.Duration = p.Duration = ((int)sdur).ToString();
                                }
                            }
                            if (video_count == 1)
                            {
                                s.Default = null;
                                s.Forced  = null;
                            }
                        }

                        if (m.Container != "mkv")
                        {
                            s.Index = iidx.ToString(CultureInfo.InvariantCulture);
                            iidx++;
                        }
                        streams.Add(s);
                    }
                }
                ex = 3;
                int totalsoundrate = 0;
                if (audio_count > 0)
                {
                    for (int x = 0; x < audio_count; x++)
                    {
                        Stream s = TranslateAudioStream(mi, x);
                        if ((s.Codec == "adpcm") && (p.Container == "flv"))
                        {
                            s.Codec = "adpcm_swf";
                        }
                        if (x == 0)
                        {
                            m.AudioCodec    = s.Codec;
                            m.AudioChannels = s.Channels;
                            if (!string.IsNullOrEmpty(m.Duration) && !string.IsNullOrEmpty(s.Duration))
                            {
                                double sdur = 0;
                                double mdur = 0;
                                double.TryParse(s.Duration, NumberStyles.Any, CultureInfo.InvariantCulture, out sdur);
                                double.TryParse(m.Duration, NumberStyles.Any, CultureInfo.InvariantCulture, out mdur);
                                if (sdur > mdur)
                                {
                                    m.Duration = p.Duration = ((int)sdur).ToString();
                                }
                            }
                            if (audio_count == 1)
                            {
                                s.Default = null;
                                s.Forced  = null;
                            }
                        }
                        if (!string.IsNullOrEmpty(s.Bitrate))
                        {
                            double birate = 0;
                            double.TryParse(s.Bitrate, NumberStyles.Any, CultureInfo.InvariantCulture, out birate);
                            totalsoundrate += (int)brate;
                        }
                        if (m.Container != "mkv")
                        {
                            s.Index = iidx.ToString(CultureInfo.InvariantCulture);
                            iidx++;
                        }
                        streams.Add(s);
                    }
                }
                if ((VideoStream != null) && string.IsNullOrEmpty(VideoStream.Bitrate) &&
                    !string.IsNullOrEmpty(m.Bitrate))
                {
                    double mrate = 0;
                    double.TryParse(m.Bitrate, NumberStyles.Any, CultureInfo.InvariantCulture, out mrate);
                    VideoStream.Bitrate = (((int)mrate) - totalsoundrate).ToString(CultureInfo.InvariantCulture);
                }


                ex = 4;
                if (text_count > 0)
                {
                    for (int x = 0; x < audio_count; x++)
                    {
                        Stream s = TranslateTextStream(mi, x);
                        streams.Add(s);
                        if (text_count == 1)
                        {
                            s.Default = null;
                            s.Forced  = null;
                        }
                        if (m.Container != "mkv")
                        {
                            s.Index = iidx.ToString(CultureInfo.InvariantCulture);
                            iidx++;
                        }
                    }
                }

                ex      = 5;
                m.Parts = new List <Part>();
                m.Parts.Add(p);
                bool over = false;
                if (m.Container == "mkv")
                {
                    int val = int.MaxValue;
                    foreach (Stream s in streams)
                    {
                        if (string.IsNullOrEmpty(s.Index))
                        {
                            over = true;
                            break;
                        }
                        s.idx = int.Parse(s.Index);
                        if (s.idx < val)
                        {
                            val = s.idx;
                        }
                    }
                    if ((val != 0) && !over)
                    {
                        foreach (Stream s in streams)
                        {
                            s.idx   = s.idx - val;
                            s.Index = s.idx.ToString(CultureInfo.InvariantCulture);
                        }
                    }
                    else if (over)
                    {
                        int xx = 0;
                        foreach (Stream s in streams)
                        {
                            s.idx   = xx++;
                            s.Index = s.idx.ToString(CultureInfo.InvariantCulture);
                        }
                    }
                    streams = streams.OrderBy(a => a.idx).ToList();
                }
                ex        = 6;
                p.Streams = streams;
                if ((p.Container == "mp4") || (p.Container == "mov"))
                {
                    p.Has64bitOffsets       = "0";
                    p.OptimizedForStreaming = "0";
                    m.OptimizedForStreaming = "0";
                    byte[]     buffer = new byte[8];
                    FileStream fs     = File.OpenRead(filename);
                    fs.Read(buffer, 0, 4);
                    int siz = buffer[0] << 24 | buffer[1] << 16 | buffer[2] << 8 | buffer[3];
                    fs.Seek(siz, SeekOrigin.Begin);
                    fs.Read(buffer, 0, 8);
                    if ((buffer[4] == 'f') && (buffer[5] == 'r') && (buffer[6] == 'e') && (buffer[7] == 'e'))
                    {
                        siz = buffer[0] << 24 | buffer[1] << 16 | buffer[2] << 8 | buffer[3] - 8;
                        fs.Seek(siz, SeekOrigin.Current);
                        fs.Read(buffer, 0, 8);
                    }
                    if ((buffer[4] == 'm') && (buffer[5] == 'o') && (buffer[6] == 'o') && (buffer[7] == 'v'))
                    {
                        p.OptimizedForStreaming = "1";
                        m.OptimizedForStreaming = "1";
                        siz = (buffer[0] << 24 | buffer[1] << 16 | buffer[2] << 8 | buffer[3]) - 8;

                        buffer = new byte[siz];
                        fs.Read(buffer, 0, siz);
                        int opos;
                        int oposmax;
                        if (FindInBuffer("trak", 0, siz, buffer, out opos, out oposmax))
                        {
                            if (FindInBuffer("mdia", opos, oposmax, buffer, out opos, out oposmax))
                            {
                                if (FindInBuffer("minf", opos, oposmax, buffer, out opos, out oposmax))
                                {
                                    if (FindInBuffer("stbl", opos, oposmax, buffer, out opos, out oposmax))
                                    {
                                        if (FindInBuffer("co64", opos, oposmax, buffer, out opos, out oposmax))
                                        {
                                            p.Has64bitOffsets = "1";
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                ex = 7;
                return(m);
            }
            catch (Exception e)
            {
                throw new Exception(ex + ":" + e.Message, e);
            }
            finally
            {
                mi.Close();
                GC.Collect();
            }
        }
예제 #18
0
파일: FileTests.cs 프로젝트: tmct/LongPath
 public void TestExists()
 {
     Assert.IsTrue(File.Exists(longPathFilename));
 }
        public void ReadCourse(string coursePath, string dbPath)
        {
            try
            {
                if (Directory.Exists(coursePath) && this.InitDb(dbPath))
                {
                    bgwGetCourse.ReportProgress(1, new Log()
                    {
                        Text = "Getting course data . . .", TextColor = Color.Green, NewLine = true
                    });

                    List <string> folderList = Directory.GetDirectories(coursePath, "*", SearchOption.TopDirectoryOnly).ToList();
                    logger.Debug($"FolderList1: {folderList.Count}");
                    folderList = folderList.Where(r => Directory.GetDirectories(r, "*", SearchOption.TopDirectoryOnly).Length > 0).ToList();
                    logger.Debug($"FolderList2: {folderList.Count}");
                    listCourse = folderList.Select(r => new CourseItem()
                    {
                        CoursePath = r, Course = this.GetCourseFromDb(r)
                    }).Where(r => r.Course != null).OrderBy(r => r.Course.Title).ToList();
                    logger.Debug($"listCourse1: {listCourse.Count}");
                    listCourse = listCourse.Where(c => c.Course.IsDownloaded).ToList();
                    logger.Debug($"listCourse2: {listCourse.Count}");

                    foreach (CourseItem item in listCourse)
                    {
                        Image img, thumb;
                        try
                        {
                            img   = File.Exists(item.CoursePath + @"\image.jpg") ? Image.FromFile(item.CoursePath + @"\image.jpg", true) : new Bitmap(100, 100);
                            thumb = img.GetThumbnailImage(160, 90, () => false, IntPtr.Zero);
                            img.Dispose();
                        }
                        catch
                        {
                            thumb = new Bitmap(160, 90);
                            Graphics g = Graphics.FromImage(thumb);
                            g.Clear(Color.Black);
                        }

                        ListViewItem listItem = new ListViewItem()
                        {
                            ImageKey = item.Course.Name, Name = item.Course.Name, Text = item.Course.Title
                        };

                        bgwGetCourse.ReportProgress(1, new { Item = listItem, Image = thumb });
                    }

                    bgwGetCourse.ReportProgress(1, new Log()
                    {
                        Text = $"Complete! Total: {listCourse.Count} courses", TextColor = Color.Green, NewLine = true
                    });

                    bgwGetCourse.ReportProgress(100);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                logger.Error(ex);
            }
        }
예제 #20
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();
        }
예제 #21
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();
        }
예제 #22
0
        /*
         * public static async Task<IObject> ObjectFromPath(this IDirectory dir, string fullname)
         * {
         *  while (!dir.IsRoot)
         *  {
         *      dir = dir.Parent;
         *  }
         *  string[] parts = fullname.Split('\\');
         *  int start = 0;
         *  bool repeat;
         *  do
         *  {
         *      repeat = false;
         *      if (!dir.IsPopulated)
         *          await dir.PopulateAsync();
         *      foreach (IDirectory d in dir.Directories)
         *      {
         *          if (d.Name.Equals(parts[start], StringComparison.InvariantCultureIgnoreCase))
         *          {
         *              if (start == parts.Length - 1)
         *                  return d;
         *              repeat = true;
         *              start++;
         *              dir = d;
         *              break;
         *          }
         *      }
         *      if ((!repeat) && (start == parts.Length-1))
         *      {
         *          foreach (IFile d in dir.Files)
         *          {
         *              if (d.Name.Equals(parts[start], StringComparison.InvariantCultureIgnoreCase))
         *              {
         *                  return d;
         *              }
         *          }
         *      }
         *  } while (repeat);
         *  return null;
         * }
         */
        public static string HashFromExtendedFile(string file, string type = "md5")
        {
            if (File.Exists(file + "." + type))
            {
                string[] lines = File.ReadAllLines(file + "." + type);
                foreach (string s in lines)
                {
                    if ((s.Length >= 32) && (s[0] != '#'))
                    {
                        return(s.Substring(0, 32));
                    }
                }
            }
            FileInfo f   = new FileInfo(file);
            string   dir = string.Empty;

            if (f.Directory != null)
            {
                dir = f.Directory.Name;
            }
            string bas = Path.Combine(Path.GetDirectoryName(file) ?? string.Empty, dir);

            if (File.Exists(bas + ".md5"))
            {
                string[] lines = File.ReadAllLines(bas + "." + type);
                foreach (string s in lines)
                {
                    if ((s.Length >= 35) && (s[0] != '#'))
                    {
                        string hash  = s.Substring(0, 32);
                        string fname = s.Substring(32).Replace("*", string.Empty).Trim();
                        if (string.Equals(f.Name, fname, StringComparison.InvariantCultureIgnoreCase))
                        {
                            return(hash);
                        }
                    }
                }
            }
            if (File.Exists(bas + ""))
            {
                string[] lines = File.ReadAllLines(bas + "");
                bool     hash  = false;
                for (int x = 0; x < lines.Length; x++)
                {
                    string s = lines[x];
                    if ((s.Length > 5) && (s.StartsWith("#" + type + "#")))
                    {
                        hash = true;
                    }
                    else if ((s.Length >= 35) && (s[0] != '#') && hash)
                    {
                        string md    = s.Substring(0, 32);
                        string fname = s.Substring(32).Replace("*", string.Empty).Trim();
                        if (string.Equals(f.Name, fname, StringComparison.InvariantCultureIgnoreCase))
                        {
                            return(md);
                        }
                        hash = false;
                    }
                    else
                    {
                        hash = false;
                    }
                }
            }
            return(string.Empty);
        }
예제 #23
0
        private string GetRandomImagePath(int type)
        {
            ImageEntityType imageType = (ImageEntityType)type;
            string          path;

            switch (imageType)
            {
            // 1
            case ImageEntityType.AniDB_Cover:
                SVR_AniDB_Anime anime = RepoFactory.AniDB_Anime.GetAll()
                                        .Where(a => a?.PosterPath != null && !a.GetAllTags().Contains("18 restricted"))
                                        .GetRandomElement();
                if (anime == null)
                {
                    return(null);
                }
                path = anime.PosterPath;
                if (File.Exists(path))
                {
                    return(path);
                }
                else
                {
                    path = string.Empty;
                    logger.Trace("Could not find AniDB_Cover image: {0}", anime.PosterPath);
                }
                break;

            // 2
            case ImageEntityType.AniDB_Character:
                var chr = RepoFactory.AniDB_Anime.GetAll()
                          .Where(a => a != null && !a.GetAllTags().Contains("18 restricted"))
                          .SelectMany(a => a.GetAnimeCharacters()).Select(a => a.GetCharacter()).Where(a => a != null)
                          .GetRandomElement();
                if (chr == null)
                {
                    return(null);
                }
                path = chr.GetPosterPath();
                if (File.Exists(path))
                {
                    return(path);
                }
                else
                {
                    path = string.Empty;
                    logger.Trace("Could not find AniDB_Character image: {0}", chr.GetPosterPath());
                }
                break;

            // 3 -- this will likely be slow
            case ImageEntityType.AniDB_Creator:
                var creator = RepoFactory.AniDB_Anime.GetAll()
                              .Where(a => a != null && !a.GetAllTags().Contains("18 restricted"))
                              .SelectMany(a => a.GetAnimeCharacters())
                              .SelectMany(a => RepoFactory.AniDB_Character_Seiyuu.GetByCharID(a.CharID))
                              .Select(a => RepoFactory.AniDB_Seiyuu.GetBySeiyuuID(a.SeiyuuID)).Where(a => a != null)
                              .GetRandomElement();
                if (creator == null)
                {
                    return(null);
                }
                path = creator.GetPosterPath();
                if (File.Exists(path))
                {
                    return(path);
                }
                else
                {
                    path = string.Empty;
                    logger.Trace("Could not find AniDB_Creator image: {0}", creator.GetPosterPath());
                }
                break;

            // 4
            case ImageEntityType.TvDB_Banner:
                // TvDB doesn't allow H content, so we get to skip the check!
                TvDB_ImageWideBanner wideBanner = RepoFactory.TvDB_ImageWideBanner.GetAll().GetRandomElement();
                if (wideBanner == null)
                {
                    return(null);
                }
                path = wideBanner.GetFullImagePath();
                if (File.Exists(path))
                {
                    return(path);
                }
                else
                {
                    path = string.Empty;
                    logger.Trace("Could not find TvDB_Banner image: {0}", wideBanner.GetFullImagePath());
                }
                break;

            // 5
            case ImageEntityType.TvDB_Cover:
                // TvDB doesn't allow H content, so we get to skip the check!
                TvDB_ImagePoster poster = RepoFactory.TvDB_ImagePoster.GetAll().GetRandomElement();
                if (poster == null)
                {
                    return(null);
                }
                path = poster.GetFullImagePath();
                if (File.Exists(path))
                {
                    return(path);
                }
                else
                {
                    path = string.Empty;
                    logger.Trace("Could not find TvDB_Cover image: {0}", poster.GetFullImagePath());
                }
                break;

            // 6
            case ImageEntityType.TvDB_Episode:
                // TvDB doesn't allow H content, so we get to skip the check!
                TvDB_Episode ep = RepoFactory.TvDB_Episode.GetAll().GetRandomElement();
                if (ep == null)
                {
                    return(null);
                }
                path = ep.GetFullImagePath();
                if (File.Exists(path))
                {
                    return(path);
                }
                else
                {
                    path = string.Empty;
                    logger.Trace("Could not find TvDB_Episode image: {0}", ep.GetFullImagePath());
                }
                break;

            // 7
            case ImageEntityType.TvDB_FanArt:
                // TvDB doesn't allow H content, so we get to skip the check!
                TvDB_ImageFanart fanart = RepoFactory.TvDB_ImageFanart.GetAll().GetRandomElement();
                if (fanart == null)
                {
                    return(null);
                }
                path = fanart.GetFullImagePath();
                if (File.Exists(path))
                {
                    return(path);
                }
                path = string.Empty;
                logger.Trace("Could not find TvDB_FanArt image: {0}", fanart.GetFullImagePath());
                break;

            // 8
            case ImageEntityType.MovieDB_FanArt:
                MovieDB_Fanart mFanart = RepoFactory.MovieDB_Fanart.GetAll().GetRandomElement();
                if (mFanart == null)
                {
                    return(null);
                }
                path = mFanart.GetFullImagePath();
                if (File.Exists(path))
                {
                    return(path);
                }
                else
                {
                    path = string.Empty;
                    logger.Trace("Could not find MovieDB_FanArt image: {0}", mFanart.GetFullImagePath());
                }
                break;

            // 9
            case ImageEntityType.MovieDB_Poster:
                MovieDB_Poster mPoster = RepoFactory.MovieDB_Poster.GetAll().GetRandomElement();
                if (mPoster == null)
                {
                    return(null);
                }
                path = mPoster.GetFullImagePath();
                if (File.Exists(path))
                {
                    return(path);
                }
                else
                {
                    path = string.Empty;
                    logger.Trace("Could not find MovieDB_Poster image: {0}", mPoster.GetFullImagePath());
                }
                break;

            case ImageEntityType.Character:
                var character = RepoFactory.AniDB_Anime.GetAll()
                                .Where(a => a != null && !a.GetAllTags().Contains("18 restricted"))
                                .SelectMany(a => RepoFactory.CrossRef_Anime_Staff.GetByAnimeID(a.AnimeID))
                                .Where(a => a.RoleType == (int)StaffRoleType.Seiyuu && a.RoleID.HasValue)
                                .Select(a => RepoFactory.AnimeCharacter.GetByID(a.RoleID.Value)).GetRandomElement();
                if (character == null)
                {
                    return(null);
                }
                path = ImageUtils.GetBaseAniDBCharacterImagesPath() + Path.DirectorySeparatorChar + character.ImagePath;
                if (File.Exists(path))
                {
                    return(path);
                }
                else
                {
                    path = string.Empty;
                    logger.Trace("Could not find Character image: {0}",
                                 ImageUtils.GetBaseAniDBCharacterImagesPath() + Path.DirectorySeparatorChar + character.ImagePath);
                }
                break;

            case ImageEntityType.Staff:
                var staff = RepoFactory.AniDB_Anime.GetAll()
                            .Where(a => a != null && !a.GetAllTags().Contains("18 restricted"))
                            .SelectMany(a => RepoFactory.CrossRef_Anime_Staff.GetByAnimeID(a.AnimeID))
                            .Select(a => RepoFactory.AnimeStaff.GetByID(a.StaffID)).GetRandomElement();
                if (staff == null)
                {
                    return(null);
                }
                path = ImageUtils.GetBaseAniDBCreatorImagesPath() + Path.DirectorySeparatorChar + staff.ImagePath;
                if (File.Exists(path))
                {
                    return(path);
                }
                else
                {
                    path = string.Empty;
                    logger.Trace("Could not find Staff image: {0}",
                                 ImageUtils.GetBaseAniDBCreatorImagesPath() + Path.DirectorySeparatorChar + staff.ImagePath);
                }
                break;

            default:
                path = string.Empty;
                break;
            }

            return(path);
        }