Stirling-PDF/src/main/java/stirling/software/SPDF/controller/web/GeneralWebController.java

250 lines
7.4 KiB
Java
Raw Normal View History

package stirling.software.SPDF.controller.web;
2023-07-09 20:36:41 +02:00
import java.io.IOException;
2023-07-12 01:17:44 +02:00
import java.nio.charset.StandardCharsets;
2023-07-09 20:36:41 +02:00
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
2023-07-26 23:08:19 +02:00
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
2023-07-09 20:36:41 +02:00
import java.util.List;
2023-07-12 01:17:44 +02:00
import java.util.Map;
2023-09-06 22:58:28 +02:00
import java.util.Objects;
2023-07-09 20:36:41 +02:00
import java.util.stream.Collectors;
2023-07-26 23:08:19 +02:00
import java.util.stream.Stream;
2023-07-09 20:36:41 +02:00
2023-07-26 23:08:19 +02:00
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.ResourcePatternUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
2023-07-12 01:17:44 +02:00
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.v3.oas.annotations.Hidden;
2023-06-25 10:16:32 +02:00
import io.swagger.v3.oas.annotations.tags.Tag;
2023-08-27 12:59:08 +02:00
@Controller
2023-06-25 10:16:32 +02:00
@Tag(name = "General", description = "General APIs")
public class GeneralWebController {
2023-07-12 01:17:44 +02:00
2023-08-12 03:29:10 +02:00
2023-08-13 02:12:29 +02:00
2023-07-12 01:17:44 +02:00
@GetMapping("/pipeline")
@Hidden
public String pipelineForm(Model model) {
model.addAttribute("currentPage", "pipeline");
List<String> pipelineConfigs = new ArrayList<>();
try (Stream<Path> paths = Files.walk(Paths.get("./pipeline/defaultWebUIConfigs/"))) {
List<Path> jsonFiles = paths
.filter(Files::isRegularFile)
.filter(p -> p.toString().endsWith(".json"))
.collect(Collectors.toList());
for (Path jsonFile : jsonFiles) {
String content = Files.readString(jsonFile, StandardCharsets.UTF_8);
pipelineConfigs.add(content);
}
List<Map<String, String>> pipelineConfigsWithNames = new ArrayList<>();
for (String config : pipelineConfigs) {
Map<String, Object> jsonContent = new ObjectMapper().readValue(config, Map.class);
String name = (String) jsonContent.get("name");
Map<String, String> configWithName = new HashMap<>();
configWithName.put("json", config);
configWithName.put("name", name);
pipelineConfigsWithNames.add(configWithName);
}
model.addAttribute("pipelineConfigsWithNames", pipelineConfigsWithNames);
} catch (IOException e) {
e.printStackTrace();
2023-06-13 01:32:15 +02:00
}
2023-07-12 01:17:44 +02:00
model.addAttribute("pipelineConfigs", pipelineConfigs);
return "pipeline";
}
2023-06-13 01:32:15 +02:00
@GetMapping("/merge-pdfs")
@Hidden
public String mergePdfForm(Model model) {
model.addAttribute("currentPage", "merge-pdfs");
return "merge-pdfs";
}
2023-06-25 10:16:32 +02:00
2023-04-29 23:26:16 +02:00
@GetMapping("/multi-tool")
@Hidden
public String multiToolForm(Model model) {
model.addAttribute("currentPage", "multi-tool");
return "multi-tool";
}
2023-06-25 10:16:32 +02:00
@GetMapping("/remove-pages")
@Hidden
public String pageDeleter(Model model) {
model.addAttribute("currentPage", "remove-pages");
return "remove-pages";
}
@GetMapping("/pdf-organizer")
@Hidden
public String pageOrganizer(Model model) {
model.addAttribute("currentPage", "pdf-organizer");
return "pdf-organizer";
}
2023-08-01 01:03:13 +02:00
@GetMapping("/extract-page")
@Hidden
public String extractPages(Model model) {
model.addAttribute("currentPage", "extract-page");
return "extract-page";
}
@GetMapping("/pdf-to-single-page")
@Hidden
public String pdfToSinglePage(Model model) {
model.addAttribute("currentPage", "pdf-to-single-page");
return "pdf-to-single-page";
}
@GetMapping("/rotate-pdf")
@Hidden
public String rotatePdfForm(Model model) {
model.addAttribute("currentPage", "rotate-pdf");
return "rotate-pdf";
}
@GetMapping("/split-pdfs")
@Hidden
public String splitPdfForm(Model model) {
model.addAttribute("currentPage", "split-pdfs");
return "split-pdfs";
}
2023-05-02 23:59:16 +02:00
@GetMapping("/sign")
@Hidden
public String signForm(Model model) {
model.addAttribute("currentPage", "sign");
2023-07-09 20:36:41 +02:00
model.addAttribute("fonts", getFontNames());
2023-05-02 23:59:16 +02:00
return "sign";
}
2023-07-26 23:08:19 +02:00
@Autowired
private ResourceLoader resourceLoader;
2023-09-06 22:58:28 +02:00
private List<FontResource> getFontNames() {
List<FontResource> fontNames = new ArrayList<>();
// Extract font names from classpath
fontNames.addAll(getFontNamesFromLocation("classpath:static/fonts/*.woff2"));
// Extract font names from external directory
fontNames.addAll(getFontNamesFromLocation("file:customFiles/static/fonts/*"));
return fontNames;
}
private List<FontResource> getFontNamesFromLocation(String locationPattern) {
2023-07-09 20:36:41 +02:00
try {
2023-07-26 23:08:19 +02:00
Resource[] resources = ResourcePatternUtils.getResourcePatternResolver(resourceLoader)
2023-09-06 22:58:28 +02:00
.getResources(locationPattern);
2023-07-26 23:08:19 +02:00
return Arrays.stream(resources)
.map(resource -> {
try {
String filename = resource.getFilename();
2023-09-06 22:58:28 +02:00
if (filename != null) {
int lastDotIndex = filename.lastIndexOf('.');
if (lastDotIndex != -1) {
String name = filename.substring(0, lastDotIndex);
String extension = filename.substring(lastDotIndex + 1);
return new FontResource(name, extension);
}
}
return null;
2023-07-26 23:08:19 +02:00
} catch (Exception e) {
throw new RuntimeException("Error processing filename", e);
}
})
2023-09-06 22:58:28 +02:00
.filter(Objects::nonNull)
2023-07-26 23:08:19 +02:00
.collect(Collectors.toList());
} catch (Exception e) {
2023-09-06 22:58:28 +02:00
throw new RuntimeException("Failed to read font directory from " + locationPattern, e);
}
}
public String getFormatFromExtension(String extension) {
switch (extension) {
case "ttf": return "truetype";
case "woff": return "woff";
case "woff2": return "woff2";
case "eot": return "embedded-opentype";
case "svg": return "svg";
default: return ""; // or throw an exception if an unexpected extension is encountered
2023-07-09 20:36:41 +02:00
}
}
2023-07-26 14:00:06 +02:00
2023-07-09 20:36:41 +02:00
2023-09-06 22:58:28 +02:00
public class FontResource {
private String name;
private String extension;
private String type;
public FontResource(String name, String extension) {
this.name = name;
this.extension = extension;
this.type = getFormatFromExtension(extension);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getExtension() {
return extension;
}
public void setExtension(String extension) {
this.extension = extension;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
@GetMapping("/crop")
@Hidden
public String cropForm(Model model) {
model.addAttribute("currentPage", "crop");
return "crop";
}
2023-07-15 12:39:10 +02:00
@GetMapping("/auto-split-pdf")
@Hidden
public String autoSPlitPDFForm(Model model) {
model.addAttribute("currentPage", "auto-split-pdf");
return "auto-split-pdf";
}
}