Skip to content

Commit

Permalink
ComicConverter project changes
Browse files Browse the repository at this point in the history
* IsValidOutputFormat refactor
* Comic format enum change namespace and naming
* General typos fixed
* Functions name change
* Linq expretions changed
  • Loading branch information
caraplana1 committed Aug 4, 2024
1 parent 3db80fb commit 16db108
Show file tree
Hide file tree
Showing 12 changed files with 110 additions and 118 deletions.
68 changes: 29 additions & 39 deletions ComicConverter/Comic.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.IO;
using ComicConverter.Enums;
using SharpCompress.Archives.Rar;
using SharpCompress.Archives.Zip;
using SharpCompress.Archives.Tar;
Expand All @@ -17,13 +18,13 @@ public class Comic
/// Comic File direction.
/// </summary>
/// <value>String</value>
public string Path { get; }
private string Path { get; }

/// <summary>
/// Comic's format enum.
/// </summary>
/// <value>Enum</value>
public ComicFormat Format { get; }
private ComicFormat Format { get; }

#endregion

Expand Down Expand Up @@ -52,15 +53,15 @@ public void Convert(string outputPath, ComicFormat format)
if (!IsValidOutputFormat(format))
throw new FormatException($"Can't convert comic to {format}");

DirectoryInfo dir = Directory.CreateDirectory(".ConvertedImagesHiddenDir");
var dir = Directory.CreateDirectory(".ConvertedImagesHiddenDir");
dir.Attributes = FileAttributes.Hidden | FileAttributes.Directory;

var ExtractImages = FindExtractorImageAction(Format);
var BuildComic = FindComicBuilderAction(format);
var extractImages = FindExtractorImageAction(Format);
var buildComic = FindComicBuilderAction(format);

ExtractImages(Path, dir.Name);
extractImages(Path, dir.Name);

BuildComic(Directory.GetFiles(dir.Name), outputPath);
buildComic(Directory.GetFiles(dir.Name), outputPath);

dir.Delete(true);
}
Expand All @@ -72,13 +73,13 @@ public void Convert(string outputPath, ComicFormat format)
private ComicFormat FindComicFormat()
{
if (RarArchive.IsRarFile(Path))
return ComicFormat.CBR;
return ComicFormat.Cbr;
if (ZipArchive.IsZipFile(Path))
return ComicFormat.CBZ;
return ComicFormat.Cbz;
if (TarArchive.IsTarFile(Path))
return ComicFormat.CBT;
if (IsValidPDF())
return ComicFormat.PDF;
return ComicFormat.Cbt;
if (IsValidPdf())
return ComicFormat.Pdf;

throw new FormatException("File is not in proper format");
}
Expand All @@ -87,64 +88,53 @@ private ComicFormat FindComicFormat()
/// Verify if the comic file PDF encrypted
/// </summary>
/// <returns>True if it is a valid pdf, false otherwise</returns>
private bool IsValidPDF()
private bool IsValidPdf()
{
StreamReader file = new(Path);
string firstLine = file.ReadLine().Substring(0, 8);
var firstLine = file.ReadLine()?.Substring(0, 8);
file.Close();

if (firstLine == "%PDF-1.4" || firstLine == "%PDF-1.5")
return true;
else
return false;
return firstLine is "%PDF-1.4" or "%PDF-1.5";
}

/// <summary>
/// Verify is the output format to convert is suported.
/// Verify is the output format to convert is supported.
/// </summary>
/// <param name="format">Format enum to compare</param>
/// <returns>True if it's valid false otherwise.</returns>
private bool IsValidOutputFormat(ComicFormat format)
{
if (format == ComicFormat.CBZ)
return true;
if (format == ComicFormat.CBT)
return true;
if (format == ComicFormat.PDF)
return true;

return false;
}
private static bool IsValidOutputFormat(ComicFormat format)
=> format is ComicFormat.Cbz or ComicFormat.Cbt or ComicFormat.Pdf;


/// <summary>
/// Find the correct method to extract images from the supported file given an format.
/// Find the correct method to extract images from the supported file given a format.
/// </summary>
/// <param name="format">The format of the file to extract images.</param>
/// <returns>The correct method to extract image to the correct format file.</returns>
private Action<string, string> FindExtractorImageAction(ComicFormat format)
{
return format switch
{
ComicFormat.CBR => ImageExporter.UnRar,
ComicFormat.CBZ => ImageExporter.UnZip,
ComicFormat.CBT => ImageExporter.UnTar,
ComicFormat.PDF => ImageExporter.ExportPdfImages,
ComicFormat.Cbr => ImageExporter.UnRar,
ComicFormat.Cbz => ImageExporter.UnZip,
ComicFormat.Cbt => ImageExporter.UnTar,
ComicFormat.Pdf => ImageExporter.ExportPdfImages,
_ => throw new FormatException(),
};
}

/// <summary>
/// Find the correct Method to created a comic in the given supproted format.
/// Find the correct Method to create a comic in the given supported format.
/// </summary>
/// <param name="format">The end format of the file to create</param>
/// <returns>The Method to transform the images to a supported comic file.</returns>
private Action<string[], string> FindComicBuilderAction(ComicFormat format)
{
return format switch
{
ComicFormat.CBZ => ComicBuilder.CreateCBZ,
ComicFormat.CBT => ComicBuilder.CreateCBT,
ComicFormat.PDF => ComicBuilder.CreatePdf,
ComicFormat.Cbz => ComicBuilder.CreateCbz,
ComicFormat.Cbt => ComicBuilder.CreateCbt,
ComicFormat.Pdf => ComicBuilder.CreatePdf,
_ => throw new FormatException(),
};
}
Expand Down
21 changes: 11 additions & 10 deletions ComicConverter/ComicBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ public static class ComicBuilder
/// <summary>
/// Creates zip file but with cbz extension
/// </summary>
/// <param name="imagesPath">Arrays of paths to the images to include in the file</param>
/// <param name="fileName">Name of the final document. Dont need to add the extension name</param>
public static void CreateCBZ(string[] imagesPaths, string fileName)
/// <param name="imagesPaths">Arrays of paths to the images to include in the file</param>
/// <param name="fileName">Name of the final document. Don't need to add the extension name</param>
public static void CreateCbz(string[] imagesPaths, string fileName)
{
DirectoryInfo dir = CreateHiddenDir(imagesPaths, fileName.Replace('\\', '/').Split('/').Last() + "CBZ");

Expand All @@ -34,8 +34,8 @@ public static void CreateCBZ(string[] imagesPaths, string fileName)
/// Creates tar file but with cbt extension
/// </summary>
/// <param name="imagesPaths">Arrays of paths to the images to include in the file</param>
/// <param name="fileName">Name of the final document. Dont need to add the extension name</param>
public static void CreateCBT(string[] imagesPaths, string fileName)
/// <param name="fileName">Name of the final document. Don't need to add the extension name</param>
public static void CreateCbt(string[] imagesPaths, string fileName)
{
DirectoryInfo dir = CreateHiddenDir(imagesPaths, fileName.Replace('\\', '/').Split('/').Last() + "CBT");

Expand All @@ -49,13 +49,13 @@ public static void CreateCBT(string[] imagesPaths, string fileName)
}

/// <summary>
/// Creates Pdf Files with a image per page.
/// Creates Pdf Files with an image per page.
/// </summary>
/// <param name="imagesPaths">Arrays of paths to the images to include in the file</param>
/// <param name="fileName">Name of the final document. Dont need to add the extension name</param>
/// <param name="fileName">Name of the final document. Don't need to add the extension name</param>
public static void CreatePdf(string[] imagesPaths, string fileName)
{
imagesPaths = imagesPaths.Where(f => File.Exists(f)).ToArray();
imagesPaths = imagesPaths.Where(File.Exists).ToArray();
imagesPaths = imagesPaths.Where(f => f.EndsWith(".PNG", StringComparison.OrdinalIgnoreCase)
|| f.EndsWith(".JPEG", StringComparison.OrdinalIgnoreCase)
|| f.EndsWith(".JPG", StringComparison.OrdinalIgnoreCase)).ToArray();
Expand All @@ -78,9 +78,10 @@ public static void CreatePdf(string[] imagesPaths, string fileName)
}

/// <summary>
/// Create a hidden directory and copy all files specificataed.
/// Create a hidden directory and copy all files specified.
/// </summary>
/// <param name="filesPaths">Files wanted to copy on the directory</param>
/// <param name="dirName">Name of the directory to create.</param>
/// <returns>Directory info class</returns>
private static DirectoryInfo CreateHiddenDir(string[] filesPaths, string dirName)
{
Expand All @@ -90,7 +91,7 @@ private static DirectoryInfo CreateHiddenDir(string[] filesPaths, string dirName
if (filesPaths.All(f => !File.Exists(f)))
throw new IOException("There is no file to add");

filesPaths = filesPaths.Where(f => File.Exists(f)).ToArray();
filesPaths = filesPaths.Where(File.Exists).ToArray();

foreach (var image in filesPaths)
{
Expand Down
10 changes: 5 additions & 5 deletions ComicConverter/Enums/ComicFormat.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
namespace ComicConverter
namespace ComicConverter.Enums
{
/// <summary>
/// Enum listing comic formats
/// </summary>
public enum ComicFormat
{
CBR,
CBZ,
CBT,
PDF
Cbr,
Cbz,
Cbt,
Pdf
}
}
23 changes: 12 additions & 11 deletions Tests/ComicConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System;
using System.IO;
using ComicConverter;
using ComicConverter.Enums;

namespace Test
{
Expand All @@ -12,9 +13,9 @@ public class ComicConverter
[Fact]
public void ConvertCbr2Cbz()
{
Comic comic = new(Samples.CBRPATH);
Comic comic = new(Samples.Cbrpath);

comic.Convert(ComicName, ComicFormat.CBZ);
comic.Convert(ComicName, ComicFormat.Cbz);

Assert.True(File.Exists($"{ComicName}.cbz"));

Expand All @@ -24,9 +25,9 @@ public void ConvertCbr2Cbz()
[Fact]
public void ConvertCbz2Cbt()
{
Comic comic = new(Samples.CBZPATH);
Comic comic = new(Samples.Cbzpath);

comic.Convert(ComicName, ComicFormat.CBT);
comic.Convert(ComicName, ComicFormat.Cbt);

Assert.True(File.Exists($"{ComicName}.cbt"));

Expand All @@ -36,9 +37,9 @@ public void ConvertCbz2Cbt()
[Fact]
public void ConvertCbr2Pdf()
{
Comic comic = new(Samples.CBRPATH);
Comic comic = new(Samples.Cbrpath);

comic.Convert("Pdf", ComicFormat.PDF);
comic.Convert("Pdf", ComicFormat.Pdf);

Assert.True(File.Exists("Pdf.pdf"));

Expand All @@ -48,9 +49,9 @@ public void ConvertCbr2Pdf()
[Fact]
public void ConvertPdf2Cbz()
{
Comic comic = new(Samples.PDFPATH);
Comic comic = new(Samples.Pdfpath);

comic.Convert("pdf2cbz", ComicFormat.CBZ);
comic.Convert("pdf2cbz", ComicFormat.Cbz);

Assert.True(File.Exists("pdf2cbz.cbz"));

Expand All @@ -60,17 +61,17 @@ public void ConvertPdf2Cbz()
[Fact]
public void OutInvalidFormat()
{
Comic comic = new(Samples.CBZPATH);
Comic comic = new(Samples.Cbzpath);

Assert.Throws<FormatException>(() => comic.Convert(ComicName, ComicFormat.CBR));
Assert.Throws<FormatException>(() => comic.Convert(ComicName, ComicFormat.Cbr));
}

[Fact]
public void InInvalidFormat()
{
Comic comic;

Assert.Throws<FormatException>(() => comic = new(Samples.TESTPATH));
Assert.Throws<FormatException>(() => comic = new(Samples.Testpath));
}
}
}
12 changes: 6 additions & 6 deletions Tests/ComicCreators/CbtFiles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,26 @@ namespace Test.ComicCreators
{
public class CbtFiles
{
private const string cbtPath = "CreatedCbtFileTest";
private const string CbtPath = "CreatedCbtFileTest";

[Fact]
public void CreateCbt()
{
string[] files = Directory.GetFiles(Samples.IMAGESDIR);
string[] files = Directory.GetFiles(Samples.Imagesdir);

ComicBuilder.CreateCBT(files, cbtPath);
ComicBuilder.CreateCbt(files, CbtPath);

Assert.True(File.Exists($"{cbtPath}.cbt"));
Assert.True(File.Exists($"{CbtPath}.cbt"));

File.Delete($"{cbtPath}.cbt");
File.Delete($"{CbtPath}.cbt");
}

[Fact]
public void ImagesNotFound()
{
string[] fakeFiles = ["fakefile1.jpg", "fakefile2.jpg", "fakefile3.jpg"];

Assert.Throws<IOException>(() => ComicBuilder.CreateCBT(fakeFiles, cbtPath));
Assert.Throws<IOException>(() => ComicBuilder.CreateCbt(fakeFiles, CbtPath));
}
}
}
12 changes: 6 additions & 6 deletions Tests/ComicCreators/CbzFiles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,26 @@ namespace Test.ComicCreators
{
public class CbzFiles
{
private const string cbzPath = "CreatedCbzFileTest";
private const string CbzPath = "CreatedCbzFileTest";

[Fact]
public void CreateCbz()
{
string[] images = Directory.GetFiles(Samples.IMAGESDIR);
string[] images = Directory.GetFiles(Samples.Imagesdir);

ComicBuilder.CreateCBZ(images, cbzPath);
ComicBuilder.CreateCbz(images, CbzPath);

Assert.True(File.Exists($"{cbzPath}.cbz"));
Assert.True(File.Exists($"{CbzPath}.cbz"));

File.Delete($"{cbzPath}.cbz");
File.Delete($"{CbzPath}.cbz");
}

[Fact]
public void ImagesNotFound()
{
string[] files = ["Fakename", "fakename2", "fakename3"];

Assert.Throws<IOException>(() => ComicBuilder.CreateCBZ(files, cbzPath));
Assert.Throws<IOException>(() => ComicBuilder.CreateCbz(files, CbzPath));
}
}
}
Loading

0 comments on commit 16db108

Please sign in to comment.