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

266 lines
8.6 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-08-13 02:12:29 +02:00
import java.util.Optional;
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;
2023-08-13 02:12:29 +02:00
import org.springframework.security.access.prepost.PreAuthorize;
2023-08-12 03:29:10 +02:00
import org.springframework.security.core.Authentication;
2023-08-13 02:12:29 +02:00
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
2023-08-13 02:12:29 +02:00
import com.fasterxml.jackson.core.JsonProcessingException;
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-12 03:29:10 +02:00
import jakarta.servlet.http.HttpServletRequest;
2023-08-13 02:12:29 +02:00
import stirling.software.SPDF.config.security.UserService;
import stirling.software.SPDF.model.User;
import stirling.software.SPDF.repository.UserRepository;
@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
@GetMapping("/login")
public String login(HttpServletRequest request, Model model, Authentication authentication) {
if (authentication != null && authentication.isAuthenticated()) {
return "redirect:/";
}
if (request.getParameter("error") != null) {
model.addAttribute("error", request.getParameter("error"));
}
if (request.getParameter("logout") != null) {
model.addAttribute("logoutMessage", "You have been logged out.");
}
return "login";
}
2023-08-13 02:12:29 +02:00
@Autowired
private UserRepository userRepository; // Assuming you have a repository for user operations
@Autowired
private UserService userService; // Assuming you have a repository for user operations
@PreAuthorize("hasRole('ROLE_ADMIN')")
@GetMapping("/addUsers")
public String showAddUserForm(Model model) {
List<User> allUsers = userRepository.findAll();
model.addAttribute("users", allUsers);
return "addUsers";
}
@GetMapping("/account")
public String account(HttpServletRequest request, Model model, Authentication authentication) {
if (authentication == null || !authentication.isAuthenticated()) {
return "redirect:/";
}
if (authentication != null && authentication.isAuthenticated()) {
Object principal = authentication.getPrincipal();
if (principal instanceof UserDetails) {
// Cast the principal object to UserDetails
UserDetails userDetails = (UserDetails) principal;
// Retrieve username and other attributes
String username = userDetails.getUsername();
// Fetch user details from the database
Optional<User> user = userRepository.findByUsername(username); // Assuming findByUsername method exists
if (!user.isPresent()) {
// Handle error appropriately
return "redirect:/error"; // Example redirection in case of error
}
// Convert settings map to JSON string
ObjectMapper objectMapper = new ObjectMapper();
String settingsJson;
try {
settingsJson = objectMapper.writeValueAsString(user.get().getSettings());
} catch (JsonProcessingException e) {
// Handle JSON conversion error
e.printStackTrace();
return "redirect:/error"; // Example redirection in case of error
}
// Add attributes to the model
model.addAttribute("username", username);
model.addAttribute("role", user.get().getRolesAsString());
model.addAttribute("settings", settingsJson);
}
} else {
return "redirect:/";
}
return "account";
}
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-07-09 20:36:41 +02:00
private List<String> getFontNames() {
try {
2023-07-26 23:08:19 +02:00
Resource[] resources = ResourcePatternUtils.getResourcePatternResolver(resourceLoader)
.getResources("classpath:static/fonts/*.woff2");
return Arrays.stream(resources)
.map(resource -> {
try {
String filename = resource.getFilename();
return filename.substring(0, filename.length() - 6); // Remove .woff2 extension
} catch (Exception e) {
throw new RuntimeException("Error processing filename", e);
}
})
.collect(Collectors.toList());
} catch (Exception e) {
2023-07-09 20:36:41 +02:00
throw new RuntimeException("Failed to read font directory", e);
}
}
2023-07-26 14:00:06 +02:00
2023-07-09 20:36:41 +02:00
@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";
}
}