added unrar command line fallback for comic books

This commit is contained in:
John Andrews
2022-08-09 15:28:11 +12:00
parent f143cec1f2
commit 915705fcd1
2 changed files with 96 additions and 6 deletions

View File

@@ -19,8 +19,17 @@ internal class GenericExtractor
if (args?.PartPercentageUpdate != null)
args?.PartPercentageUpdate(halfProgress ? 50 : 0);
ArchiveFactory.WriteToDirectory(workingFile, destinationPath);
PageNameHelper.FixPageNames(destinationPath);
bool isRar = workingFile.ToLowerInvariant().EndsWith(".cbr");
try
{
ArchiveFactory.WriteToDirectory(workingFile, destinationPath);
PageNameHelper.FixPageNames(destinationPath);
}
catch (Exception ex) when (isRar && ex.Message.Contains("Unknown Rar Header"))
{
UnrarCommandLine.Extract(args, workingFile, destinationPath, halfProgress: halfProgress);
}
if (args?.PartPercentageUpdate != null)
args?.PartPercentageUpdate(halfProgress ? 50 : 100);
@@ -28,9 +37,17 @@ internal class GenericExtractor
internal static int GetImageCount(string workingFile)
{
var rgxImages = new Regex(@"\.(jpeg|jpg|jpe|png|bmp|tiff|webp|gif)$", RegexOptions.IgnoreCase);
using var archive = ArchiveFactory.Open(workingFile);
var files = archive.Entries.Where(entry => !entry.IsDirectory).ToArray();
return files.Where(x => rgxImages.IsMatch(x.Key)).Count();
bool isRar = workingFile.ToLowerInvariant().EndsWith(".cbr");
try
{
var rgxImages = new Regex(@"\.(jpeg|jpg|jpe|png|bmp|tiff|webp|gif)$", RegexOptions.IgnoreCase);
using var archive = ArchiveFactory.Open(workingFile);
var files = archive.Entries.Where(entry => !entry.IsDirectory).ToArray();
return files.Where(x => rgxImages.IsMatch(x.Key)).Count();
}
catch(Exception ex) when (isRar && ex.Message.Contains("Unknown Rar Header"))
{
return UnrarCommandLine.GetImageCount(workingFile);
}
}
}

View File

@@ -0,0 +1,73 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace FileFlows.ComicNodes.Helpers;
internal class UnrarCommandLine
{
/// <summary>
/// Uncompresses a folder
/// </summary>
/// <param name="args">the node paratemers</param>
/// <param name="workingFile">the file to extract</param>
/// <param name="destinationPath">the location to extract to</param>
/// <param name="halfProgress">if the NodeParameter.PartPercentageUpdate should end at 50%</param>
internal static void Extract(NodeParameters args, string workingFile, string destinationPath, bool halfProgress = true)
{
if (args?.PartPercentageUpdate != null)
args?.PartPercentageUpdate(halfProgress ? 50 : 0);
var process = new Process();
process.StartInfo.FileName = "unrar";
process.StartInfo.ArgumentList.Add("x");
process.StartInfo.ArgumentList.Add(workingFile);
process.StartInfo.ArgumentList.Add(destinationPath);
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true;
process.Start();
string output = process.StandardError.ReadToEnd();
string error = process.StandardError.ReadToEnd();
process.WaitForExit();
args.Logger?.ILog("Unrar output:\n" + output);
if(string.IsNullOrWhiteSpace(error) == false)
args.Logger?.ELog("Unrar error:\n" + error);
if (process.ExitCode != 0)
PageNameHelper.FixPageNames(destinationPath);
if (args?.PartPercentageUpdate != null)
args?.PartPercentageUpdate(halfProgress ? 50 : 100);
}
internal static int GetImageCount(string workingFile)
{
var rgxImages = new Regex(@"\.(jpeg|jpg|jpe|png|bmp|tiff|webp|gif)$", RegexOptions.IgnoreCase);
var process = new Process();
process.StartInfo.FileName = "unrar";
process.StartInfo.ArgumentList.Add("list");
process.StartInfo.ArgumentList.Add(workingFile);
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true;
process.Start();
string output = process.StandardError.ReadToEnd();
string error = process.StandardError.ReadToEnd();
process.WaitForExit();
if (process.ExitCode != 0)
return 0;
var lines = output.Split('\n', StringSplitOptions.RemoveEmptyEntries);
return lines.Where(x => rgxImages.IsMatch(x.Trim())).Count();
}
}