-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #682 from Stirling-Tools/ebook
Ebook
- Loading branch information
Showing
23 changed files
with
652 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
src/main/java/stirling/software/SPDF/config/PostStartupProcesses.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package stirling.software.SPDF.config; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import org.springframework.stereotype.Component; | ||
|
||
import jakarta.annotation.PostConstruct; | ||
import stirling.software.SPDF.model.ApplicationProperties; | ||
import stirling.software.SPDF.utils.ProcessExecutor; | ||
import stirling.software.SPDF.utils.ProcessExecutor.ProcessExecutorResult; | ||
|
||
@Component | ||
public class PostStartupProcesses { | ||
|
||
@Autowired ApplicationProperties applicationProperties; | ||
|
||
@Autowired | ||
@Qualifier("RunningInDocker") | ||
private boolean runningInDocker; | ||
|
||
@Autowired | ||
@Qualifier("bookFormatsInstalled") | ||
private boolean bookFormatsInstalled; | ||
|
||
@Autowired | ||
@Qualifier("htmlFormatsInstalled") | ||
private boolean htmlFormatsInstalled; | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(PostStartupProcesses.class); | ||
|
||
@PostConstruct | ||
public void runInstallCommandBasedOnEnvironment() throws IOException, InterruptedException { | ||
List<List<String>> commands = new ArrayList<>(); | ||
// Checking for DOCKER_INSTALL_BOOK_FORMATS environment variable | ||
if (bookFormatsInstalled) { | ||
List<String> tmpList = new ArrayList<>(); | ||
// Set up the timezone configuration commands | ||
tmpList.addAll( | ||
Arrays.asList( | ||
"sh", | ||
"-c", | ||
"echo 'tzdata tzdata/Areas select Europe' | debconf-set-selections; " | ||
+ "echo 'tzdata tzdata/Zones/Europe select Berlin' | debconf-set-selections")); | ||
commands.add(tmpList); | ||
|
||
// Install calibre with DEBIAN_FRONTEND set to noninteractive | ||
tmpList = new ArrayList<>(); | ||
tmpList.addAll( | ||
Arrays.asList( | ||
"sh", | ||
"-c", | ||
"DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends calibre")); | ||
commands.add(tmpList); | ||
} | ||
|
||
// Checking for DOCKER_INSTALL_HTML_FORMATS environment variable | ||
if (htmlFormatsInstalled) { | ||
List<String> tmpList = new ArrayList<>(); | ||
// Add -y flag for automatic yes to prompts and --no-install-recommends to reduce size | ||
tmpList.addAll( | ||
Arrays.asList( | ||
"apt-get", "install", "wkhtmltopdf", "-y", "--no-install-recommends")); | ||
commands.add(tmpList); | ||
} | ||
|
||
if (!commands.isEmpty()) { | ||
// Run the command | ||
if (runningInDocker) { | ||
List<String> tmpList = new ArrayList<>(); | ||
tmpList.addAll(Arrays.asList("apt-get", "update")); | ||
commands.add(0, tmpList); | ||
|
||
for (List<String> list : commands) { | ||
ProcessExecutorResult returnCode = | ||
ProcessExecutor.getInstance(ProcessExecutor.Processes.INSTALL_APP, true) | ||
.runCommandWithOutputHandling(list); | ||
logger.info("RC for app installs {}", returnCode.getRc()); | ||
} | ||
} else { | ||
|
||
logger.info( | ||
"Not running inside Docker so skipping automated install process with command."); | ||
} | ||
|
||
} else { | ||
if (runningInDocker) { | ||
logger.info("No custom apps to install."); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -115,4 +115,4 @@ protected boolean shouldNotFilter(HttpServletRequest request) throws ServletExce | |
|
||
return false; | ||
} | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
...ain/java/stirling/software/SPDF/controller/api/converters/ConvertBookToPDFController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package stirling.software.SPDF.controller.api.converters; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.ModelAttribute; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
|
||
import stirling.software.SPDF.model.api.GeneralFile; | ||
import stirling.software.SPDF.utils.FileToPdf; | ||
import stirling.software.SPDF.utils.WebResponseUtils; | ||
|
||
@RestController | ||
@Tag(name = "Convert", description = "Convert APIs") | ||
@RequestMapping("/api/v1/convert") | ||
public class ConvertBookToPDFController { | ||
|
||
@Autowired | ||
@Qualifier("bookFormatsInstalled") | ||
private boolean bookFormatsInstalled; | ||
|
||
@PostMapping(consumes = "multipart/form-data", value = "/book/pdf") | ||
@Operation( | ||
summary = | ||
"Convert a BOOK/comic (*.epub | *.mobi | *.azw3 | *.fb2 | *.txt | *.docx) to PDF", | ||
description = | ||
"(Requires bookFormatsInstalled flag and Calibre installed) This endpoint takes an BOOK/comic (*.epub | *.mobi | *.azw3 | *.fb2 | *.txt | *.docx) input and converts it to PDF format.") | ||
public ResponseEntity<byte[]> HtmlToPdf(@ModelAttribute GeneralFile request) throws Exception { | ||
MultipartFile fileInput = request.getFileInput(); | ||
|
||
if (!bookFormatsInstalled) { | ||
throw new IllegalArgumentException( | ||
"bookFormatsInstalled flag is False, this functionality is not avaiable"); | ||
} | ||
|
||
if (fileInput == null) { | ||
throw new IllegalArgumentException("Please provide a file for conversion."); | ||
} | ||
|
||
String originalFilename = fileInput.getOriginalFilename(); | ||
|
||
if (originalFilename != null) { | ||
String originalFilenameLower = originalFilename.toLowerCase(); | ||
if (!originalFilenameLower.endsWith(".epub") | ||
&& !originalFilenameLower.endsWith(".mobi") | ||
&& !originalFilenameLower.endsWith(".azw3") | ||
&& !originalFilenameLower.endsWith(".fb2") | ||
&& !originalFilenameLower.endsWith(".txt") | ||
&& !originalFilenameLower.endsWith(".docx")) { | ||
throw new IllegalArgumentException( | ||
"File must be in .epub, .mobi, .azw3, .fb2, .txt, or .docx format."); | ||
} | ||
} | ||
byte[] pdfBytes = FileToPdf.convertBookTypeToPdf(fileInput.getBytes(), originalFilename); | ||
|
||
String outputFilename = | ||
originalFilename.replaceFirst("[.][^.]+$", "") | ||
+ ".pdf"; // Remove file extension and append .pdf | ||
|
||
return WebResponseUtils.bytesToWebResponse(pdfBytes, outputFilename); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.