Return pdf file

This commit is contained in:
2025-01-31 14:15:14 +01:00
parent c13320ab57
commit b6905e5732
3 changed files with 55 additions and 0 deletions

View File

@ -1,6 +1,7 @@
package ch.dlmw.swisssignchallenge.controllers; package ch.dlmw.swisssignchallenge.controllers;
import ch.dlmw.swisssignchallenge.entities.SigningRequestDocument; import ch.dlmw.swisssignchallenge.entities.SigningRequestDocument;
import ch.dlmw.swisssignchallenge.services.PdfService;
import ch.dlmw.swisssignchallenge.services.SigningRequestDocumentService; import ch.dlmw.swisssignchallenge.services.SigningRequestDocumentService;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
@ -9,6 +10,7 @@ import com.github.fge.jsonpatch.JsonPatchException;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import org.openapitools.api.SigningRequestDocumentApi; import org.openapitools.api.SigningRequestDocumentApi;
import org.openapitools.model.PatchOperation; import org.openapitools.model.PatchOperation;
import org.springframework.core.io.Resource;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
@ -19,6 +21,7 @@ import java.util.List;
@AllArgsConstructor @AllArgsConstructor
public class SigningRequestDocumentController implements SigningRequestDocumentApi { public class SigningRequestDocumentController implements SigningRequestDocumentApi {
private final SigningRequestDocumentService signingRequestDocumentService; private final SigningRequestDocumentService signingRequestDocumentService;
private final PdfService pdfService;
private final ObjectMapper objectMapper; private final ObjectMapper objectMapper;
@Override @Override
@ -38,6 +41,13 @@ public class SigningRequestDocumentController implements SigningRequestDocumentA
} }
} }
@Override
public ResponseEntity<Resource> getSigningRequestDocumentData(String id) {
var document = signingRequestDocumentService.getSigningRequestDocument(id);
return ResponseEntity.ok(pdfService.getPdf(document.getName()));
}
private <T> T applyPatch(List<PatchOperation> patchOperations, T object, Class<T> entityType) throws JsonPatchException, IOException { private <T> T applyPatch(List<PatchOperation> patchOperations, T object, Class<T> entityType) throws JsonPatchException, IOException {
JsonNode patchNode = objectMapper.valueToTree(patchOperations); JsonNode patchNode = objectMapper.valueToTree(patchOperations);
JsonPatch jsonPatch = JsonPatch.fromJson(patchNode); JsonPatch jsonPatch = JsonPatch.fromJson(patchNode);

View File

@ -0,0 +1,7 @@
package ch.dlmw.swisssignchallenge.services;
import org.springframework.core.io.Resource;
public interface PdfService {
Resource getPdf(String name);
}

View File

@ -0,0 +1,38 @@
package ch.dlmw.swisssignchallenge.services.impl;
import ch.dlmw.swisssignchallenge.services.PdfService;
import lombok.AllArgsConstructor;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.stereotype.Service;
import java.net.MalformedURLException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.NoSuchElementException;
@Service
@AllArgsConstructor
public class PdfServiceImpl implements PdfService {
private static final String FILE_DIRECTORY = "/srv/pdfs/";
@Override
public Resource getPdf(String name) {
if (name.contains("..")) { // this is to avoid traversal attacks
throw new IllegalArgumentException("Invalid file name: " + name);
}
Path filePath = Paths.get(FILE_DIRECTORY).resolve(name).normalize();
Resource resource;
try {
resource = new UrlResource(filePath.toUri());
} catch (MalformedURLException e) {
throw new IllegalArgumentException("Invalid file name: " + name);
}
if (!resource.exists() || !resource.isReadable()) {
throw new NoSuchElementException("Couldn't find file: " + name);
}
return resource;
}
}