Skip to content

Commit

Permalink
refactor(core): re-organize core code
Browse files Browse the repository at this point in the history
  • Loading branch information
cherfia committed Apr 6, 2024
1 parent 1f00000 commit f1a098e
Show file tree
Hide file tree
Showing 7 changed files with 489 additions and 1 deletion.
157 changes: 156 additions & 1 deletion src/main/java/dev/inaka/common/CommonUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,141 @@
* CommonUtils is a utility class that provides various common utility methods for handling files and URLs.
*/
public class CommonUtils {
private static final String[] extensions = {"bib", "doc", "xml", "docx", "fodt", "html", "ltx", "txt", "odt", "ott", "pdb", "pdf", "psw", "rtf", "sdw", "stw", "sxw", "uot", "vor", "wps", "epub", "png", "bmp", "emf", "eps", "fodg", "gif", "jpg", "met", "odd", "otg", "pbm", "pct", "pgm", "ppm", "ras", "std", "svg", "svm", "swf", "sxd", "sxw", "tiff", "xhtml", "xpm", "fodp", "potm", "pot", "pptx", "pps", "ppt", "pwp", "sda", "sdd", "sti", "sxi", "uop", "wmf", "csv", "dbf", "dif", "fods", "ods", "ots", "pxl", "sdc", "slk", "stc", "sxc", "uos", "xls", "xlt", "xlsx", "tif", "jpeg", "odp",};
private static final String[] extensions = {
"123",
"602",
"abw",
"bib",
"bmp",
"cdr",
"cgm",
"cmx",
"csv",
"cwk",
"dbf",
"dif",
"doc",
"docm",
"docx",
"dot",
"dotm",
"dotx",
"dxf",
"emf",
"eps",
"epub",
"fodg",
"fodp",
"fods",
"fodt",
"fopd",
"gif",
"htm",
"html",
"hwp",
"jpeg",
"jpg",
"key",
"ltx",
"lwp",
"mcw",
"met",
"mml",
"mw",
"numbers",
"odd",
"odg",
"odm",
"odp",
"ods",
"odt",
"otg",
"oth",
"otp",
"ots",
"ott",
"pages",
"pbm",
"pcd",
"pct",
"pcx",
"pdb",
"pdf",
"pgm",
"png",
"pot",
"potm",
"potx",
"ppm",
"pps",
"ppt",
"pptm",
"pptx",
"psd",
"psw",
"pub",
"pwp",
"pxl",
"ras",
"rtf",
"sda",
"sdc",
"sdd",
"sdp",
"sdw",
"sgl",
"slk",
"smf",
"stc",
"std",
"sti",
"stw",
"svg",
"svm",
"swf",
"sxc",
"sxd",
"sxg",
"sxi",
"sxm",
"sxw",
"tga",
"tif",
"tiff",
"txt",
"uof",
"uop",
"uos",
"uot",
"vdx",
"vor",
"vsd",
"vsdm",
"vsdx",
"wb2",
"wk1",
"wks",
"wmf",
"wpd",
"wpg",
"wps",
"xbm",
"xhtml",
"xls",
"xlsb",
"xlsm",
"xlsx",
"xlt",
"xltm",
"xltx",
"xlw",
"xml",
"xpm",
"zabw"
};
private static final String INDEX_HTML = "index.html";
private static final String HEADER_HTML = "header.html";
private static final String FOOTER_HTML = "footer.html";

private CommonUtils() {
}
Expand Down Expand Up @@ -45,6 +178,28 @@ public static boolean isIndex(File file) {
return file.isFile() && filename.equals(INDEX_HTML);
}

/**
* Checks if a file is a header HTML file.
*
* @param file The file to check.
* @return `true` if the file is a header HTML file, `false` otherwise.
*/
public static boolean isHeader(File file) {
String filename = FilenameUtils.getName(file.getName());
return file.isFile() && filename.equals(HEADER_HTML);
}

/**
* Checks if a file is a footer HTML file.
*
* @param file The file to check.
* @return `true` if the file is a footer HTML file, `false` otherwise.
*/
public static boolean isFooter(File file) {
String filename = FilenameUtils.getName(file.getName());
return file.isFile() && filename.equals(FOOTER_HTML);
}

/**
* Checks if a file has a supported file extension.
*
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/dev/inaka/common/EmulatedMediaType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package dev.inaka.common;

/**
* PdfFormat is an enumeration of various PDF formats for document conversion.
*/
public enum EmulatedMediaType {
/**
* screen media type
*/
SCREEN("screen"),

/**
* print media type
*/
PRINT("print");

private final String mediaType;

/**
* Constructs a EmulatedMediaType enum with the specified format string.
*
* @param mediaType The emulated media type string.
*/
EmulatedMediaType(String mediaType) {
this.mediaType = mediaType;
}

/**
* Gets the emulated media type string for this EmulatedMediaType.
*
* @return The emulated media type string.
*/
public String mediaType() {
return this.mediaType;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package dev.inaka.common.exceptions;

import java.io.FileNotFoundException;

/**
* FooterFileNotFoundExceptions is an exception class that is thrown when a footer.html file is not found.
*/
public class FooterFileNotFoundExceptions extends FileNotFoundException {
/**
* Constructs a FooterFileNotFoundExceptions with a default error message.
*/
public FooterFileNotFoundExceptions() {
super("No footer.html file found.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package dev.inaka.common.exceptions;

import java.io.FileNotFoundException;

/**
* HeaderFileNotFoundExceptions is an exception class that is thrown when a header.html file is not found.
*/
public class HeaderFileNotFoundExceptions extends FileNotFoundException {
/**
* Constructs a HeaderFileNotFoundExceptions with a default error message.
*/
public HeaderFileNotFoundExceptions() {
super("No header.html file found.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ public IndexFileNotFoundExceptions() {
super("No index.html file found.");
}
}


150 changes: 150 additions & 0 deletions src/main/java/dev/inaka/core/ConversionHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package dev.inaka.core;

import dev.inaka.Jotenberg;
import dev.inaka.chromium.ChromiumOptions;
import dev.inaka.chromium.ChromiumPageProperties;
import dev.inaka.common.AbstractOptions;
import dev.inaka.libreoffice.LibreOfficeOptions;
import dev.inaka.libreoffice.LibreOfficePageProperties;
import dev.inaka.pdfengines.PDFEnginesMergeOptions;
import dev.inaka.pdfengines.PDFEnginesOptions;
import dev.inaka.screenshots.ImageProperties;

import java.io.File;
import java.lang.reflect.Field;

/**
* ConversionHelper is a class that provides helper methods for converting between different formats.
*/
public class ConversionHelper {
private final Jotenberg jotenberg;

public ConversionHelper(Jotenberg jotenberg) {
this.jotenberg = jotenberg;
}

/**
* Builds Chromium page properties using reflection and adds them to the request entity.
*
* @param pageProperties Chromium page properties to add to the request entity.
*/
void buildPageProperties(ChromiumPageProperties pageProperties) {
Field[] fields = ChromiumPageProperties.class.getDeclaredFields();
try {
for (Field field : fields) {
field.setAccessible(true);
Object value = field.get(pageProperties);
if (value != null) {
jotenberg.getBuilder().addTextBody(field.getName(), (String) value);
}
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}

/**
* Builds Chromium options using reflection and adds them to the request entity.
*
* @param options Chromium options to add to the request entity.
*/
void buildChromiumOptions(AbstractOptions options) {
Field[] fields = ChromiumOptions.class.getDeclaredFields();
try {
for (Field field : fields) {
field.setAccessible(true);
Object value = field.get(options);
if (value != null) {
if (field.getDeclaringClass().equals(File.class)) {
jotenberg.getBuilder().addBinaryBody(field.getName(), (File) value);
} else {
jotenberg.getBuilder().addTextBody(field.getName(), (String) value);
}
}
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}

/**
* Builds LibreOffice page properties using reflection and adds them to the request entity.
*
* @param pageProperties LibreOffice page properties to add to the request entity.
*/
void buildPageProperties(LibreOfficePageProperties pageProperties) {
Field[] fields = LibreOfficePageProperties.class.getDeclaredFields();
try {
for (Field field : fields) {
field.setAccessible(true);
Object value = field.get(pageProperties);
if (value != null) {
jotenberg.getBuilder().addTextBody(field.getName(), (String) value);
}
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}

/**
* Builds LibreOffice options using reflection and adds them to the request entity.
*
* @param options LibreOffice options to add to the request entity.
*/
void buildPageOptions(LibreOfficeOptions options) {
Field[] fields = LibreOfficeOptions.class.getDeclaredFields();
try {
for (Field field : fields) {
field.setAccessible(true);
Object value = field.get(options);
if (value != null) {
jotenberg.getBuilder().addTextBody(field.getName(), (String) value);
}
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}

/**
* Builds image properties using reflection and adds them to the request entity.
*
* @param imageProperties image properties to add to the request entity.
*/
void buildImageProperties(ImageProperties imageProperties) {
Field[] fields = ImageProperties.class.getDeclaredFields();
try {
for (Field field : fields) {
field.setAccessible(true);
Object value = field.get(imageProperties);
if (value != null) {
jotenberg.getBuilder().addTextBody(field.getName(), (String) value);
}
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}


/**
* Builds PDF engines options using reflection and adds them to the request entity.
*
* @param options PDF engines options to add to the request entity.
*/
public void buildPdfEngineOptions(PDFEnginesOptions options) {
Field[] fields = PDFEnginesMergeOptions.class.getDeclaredFields();
try {
for (Field field : fields) {
field.setAccessible(true);
Object value = field.get(options);
if (value != null) {
jotenberg.getBuilder().addTextBody(field.getName(), (String) value);
}
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
Loading

0 comments on commit f1a098e

Please sign in to comment.