Skip to content

Commit

Permalink
Merging general improvements to main
Browse files Browse the repository at this point in the history
General improvements
  • Loading branch information
caraplana1 authored May 24, 2024
2 parents b24bd40 + bbda3e4 commit b94eb4a
Show file tree
Hide file tree
Showing 36 changed files with 79 additions and 29 deletions.
45 changes: 41 additions & 4 deletions ComicConverter/Comic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,28 @@ public Comic(string comicPath)
Format = FindComicFormat();
}

/// <summary>
/// Constructor
/// </summary>
/// <param name="comicPath">Direction of the comic file.</param>
/// <param name="format">Specify the format</param>
/// <exception cref="FileNotFoundException"></exception>
public Comic(string comicPath, ComicFormat format)
{
if (!File.Exists(comicPath))
throw new FileNotFoundException();
else
Path = comicPath;

Format = format;
}

/// <summary>
/// Convert the file to the given format.
/// </summary>
/// <param name="outputPath">File name to be converted.(without extension)</param>
/// <param name="format">Format to be converted.</param>
/// <exception cref="FormatException"></exception>
public void Convert(string outputPath, ComicFormat format)
{
if (!IsValidOutputFormat(format))
Expand All @@ -50,10 +72,10 @@ public void Convert(string outputPath, ComicFormat format)
DirectoryInfo dir = Directory.CreateDirectory(".ConvertedImagesHiddenDir");
dir.Attributes = FileAttributes.Hidden | FileAttributes.Directory;

var ExtractImages = FindExtractorImageAction(this.Format);
var ExtractImages = FindExtractorImageAction(Format);
var BuildComic = FindComicBuilderAction(format);

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

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

Expand All @@ -74,10 +96,25 @@ private ComicFormat FindComicFormat()
return ComicFormat.CBT;
if (SevenZipArchive.IsSevenZipFile(Path))
return ComicFormat.CB7;
if (Path.EndsWith(".pdf"))
if (IsValidPDF())
return ComicFormat.PDF;

throw new FormatException("The file cannot be used beacuse is not in a propper format.");
throw new FormatException("File is not in proper format");
}

/// <summary>
/// Verify if the comic file PDF encrypted
/// </summary>
/// <returns>True if it is a valid pdf, false otherwise</returns>
private bool IsValidPDF()
{
StreamReader file = new(Path);
string firstLine = file.ReadLine().Substring(0, 7);

if (firstLine == "%PDF-1.")
return true;
else
return false;
}

/// <summary>
Expand Down
1 change: 1 addition & 0 deletions ComicConverter/ComicConverter.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@
<ItemGroup>
<PackageReference Include="PdfSharpCore" Version="1.3.63" />
<PackageReference Include="SharpCompress" Version="0.37.2" />
<PackageReference Include="SixLabors.ImageSharp" Version="2.1.8" />
</ItemGroup>
</Project>
19 changes: 17 additions & 2 deletions ComicConverter/ImageExtractors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,12 @@ public static void UnSevenZip(string filePath, string outputDir = ".")
/// </summary>
/// <param name="filePath">Pdf file</param>
/// <param name="outputDir">Output directory</param>
public static void ExtractPdfImages(string filePath, string outputDir = ".")
public static void ExtractPdfImages(string filePath, string outputDir = ".")
{
if (!File.Exists(filePath))
throw new FileNotFoundException();

if (!filePath.EndsWith(".pdf") || outputDir?.Length == 0)
if (!IsValidPDF(filePath) || outputDir?.Length == 0)
throw new FormatException();

DirectoryInfo dir = Directory.CreateDirectory(outputDir);
Expand Down Expand Up @@ -196,5 +196,20 @@ private static void ExtractJpegFromPdf(PdfDictionary image, string name)
bw.Write(stream);
bw.Close();
}

/// <summary>
/// Verify if the comic file PDF encrypted
/// </summary>
/// <returns>True if it is a valid pdf, false otherwise</returns>
static private bool IsValidPDF(string path)
{
StreamReader file = new(path);
string firstLine = file.ReadLine().Substring(0, 7);

if (firstLine == "%PDF-1.")
return true;
else
return false;
}
}
}
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2022 Cristian Landaez
Copyright (c) 2024 Cristian Landaez

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ A multi-plataform .NET library written in Net standard 2.0 for comic file format

## Suported Formats

These are the format that we current support.
These are the format that we currently support.

| Format | Extract Images | Create Comic |
| :----: | :------------: | :--------------: |
Expand All @@ -14,6 +14,8 @@ These are the format that we current support.
| CB7 | ✔️ ||
| PDF | :heavy_check_mark:(only jpeg) | :heavy_check_mark: |

We can't make cbr files due to RAR compression algorithm lincense.

## Samples

For start using the library just compy and paste `using ComicConverter;` and follow the next examples depending of your case.
Expand Down
6 changes: 3 additions & 3 deletions Tests/ComicCreators/CbzFiles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ public void CreateCbz()
File.Delete($"{cbzPath}.cbz");
}

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

Assert.Throws<IOException>(() => ComicBuilder.CreateCBZ(files, cbzPath));
Expand Down
8 changes: 4 additions & 4 deletions Tests/ComicCreators/PdfFiles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ public void CreatePdf()
File.Delete("pdftest.pdf");
}

[Fact]
[Fact]
public void FilesAreNotImages()
{
var images = Directory.GetFiles(Samples.IMAGESDIR);
images = [.. images, Samples.FAKEIMAGE];
{
var images = Directory.GetFiles(Samples.IMAGESDIR);
images = [.. images, Samples.FAKEIMAGE];

Assert.ThrowsAny<Exception>(() => ComicBuilder.CreatePdf(images, "pdftest"));
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/ImageCollectors/CbtFiles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class CbtFiles
public void ExtractImages()
{
ImageExtractors.UnTar(Samples.CBTPATH, "CbtTest1");
Assert.Equal(14, Directory.GetFiles("CbtTest1").Length);
Assert.Equal(3, Directory.GetFiles("CbtTest1").Length);
Directory.Delete("CbtTest1", true);
}

Expand Down
6 changes: 3 additions & 3 deletions Tests/ImageCollectors/PdfFiles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ namespace Test.ImageCollector
public class PdfFiles
{
[Fact]
public void ExtractImagesPdf()
public void ExtractImagesPdfJpg()
{
ImageExtractors.ExtractPdfImages(Samples.PDFPATH, "PdfExtractedImages");

var files = Directory.GetFiles("PdfExtractedImages");

Assert.Equal(49, files.Length);
Assert.Equal(3, files.Length);

Directory.Delete("PdfExtractedImages", true);
}
Expand All @@ -38,7 +38,7 @@ public void EmptyAttrubuteDirectory()

var files = Directory.GetFiles("PdfExtracted");

Assert.Equal(49, files.Length);
Assert.Equal(3, files.Length);

Directory.Delete("PdfExtracted", true);
}
Expand Down
Binary file removed Tests/Samples/CROSSED Wish you were here.cbr
Binary file not shown.
Binary file removed Tests/Samples/CROSSED Wish you were here.cbt
Binary file not shown.
Binary file removed Tests/Samples/Crossed 3D.cbz
Binary file not shown.
Binary file removed Tests/Samples/Crossed 3D.pdf
Binary file not shown.
6 changes: 0 additions & 6 deletions Tests/Samples/Images/Facebook de Crossed en español.txt

This file was deleted.

Binary file added Tests/Samples/Images/Numbers.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Tests/Samples/Images/Numbers1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Tests/Samples/Images/Numbers2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed Tests/Samples/Images/vol-4-chapter-1-0-title.jpg
Binary file not shown.
Binary file removed Tests/Samples/Images/vol-4-chapter-1-1-a.jpg
Binary file not shown.
Binary file removed Tests/Samples/Images/vol-4-chapter-1-1-b.jpg
Binary file not shown.
Binary file removed Tests/Samples/Images/vol-4-chapter-1-2-a.jpg
Binary file not shown.
Binary file removed Tests/Samples/Images/vol-4-chapter-1-2-b.jpg
Binary file not shown.
Binary file removed Tests/Samples/Images/vol-4-chapter-1-3-a.jpg
Binary file not shown.
Binary file removed Tests/Samples/Images/vol-4-chapter-1-3-b.jpg
Binary file not shown.
Binary file removed Tests/Samples/Images/vol-4-chapter-1-4-a.jpg
Binary file not shown.
Binary file removed Tests/Samples/Images/vol-4-chapter-1-4-b.jpg
Binary file not shown.
Binary file removed Tests/Samples/Images/vol-4-chapter-1-5-a.jpg
Binary file not shown.
Binary file removed Tests/Samples/Images/vol-4-chapter-1-5-b.jpg
Binary file not shown.
Binary file removed Tests/Samples/Images/vol-4-chapter-1-6-a.jpg
Binary file not shown.
Binary file removed Tests/Samples/Images/vol-4-chapter-1-6-b.jpg
Binary file not shown.
9 changes: 5 additions & 4 deletions Tests/Samples/Samples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ namespace Test
public static class Samples
{
public const string IMAGESDIR = "../../../Samples/Images";
public const string CBZPATH = "../../../Samples/Crossed 3D.cbz";
public const string CBRPATH = "../../../Samples/CROSSED Wish you were here.cbr";
public const string CBTPATH = "../../../Samples/CROSSED Wish you were here.cbt";
public const string CBZPATH = "../../../Samples/TestComic.cbz";
public const string CBRPATH = "../../../Samples/TestComic.cbr";
public const string CBTPATH = "../../../Samples/TestComic.cbt";
public const string TESTPATH = "../../../Samples/File.test";
public const string FAKEFILE = "Do not exist.txt";
public const string FAKEIMAGE = "../../../Samples/FakeImage.png";
public const string PDFPATH = "../../../Samples/Crossed 3D.pdf";
public const string PDFPATH = "../../../Samples/TestComic.pdf";
public const string PDFPNGPATH = "../../../Samples/TestPNGComic.pdf";
}
}
Binary file added Tests/Samples/TestComic.cbr
Binary file not shown.
Binary file added Tests/Samples/TestComic.cbt
Binary file not shown.
Binary file added Tests/Samples/TestComic.cbz
Binary file not shown.
Binary file added Tests/Samples/TestComic.pdf
Binary file not shown.
Binary file added Tests/Samples/TestPNGComic.pdf
Binary file not shown.

0 comments on commit b94eb4a

Please sign in to comment.