Refactor PDF conversion

This commit is contained in:
George Kalampokis 2021-10-12 18:17:16 +03:00
parent 1a821179db
commit a4fef8e462
5 changed files with 67 additions and 67 deletions

View File

@ -20,6 +20,7 @@ import eu.eudat.logic.security.claims.ClaimedAuthorities;
import eu.eudat.logic.services.ApiContext;
import eu.eudat.logic.services.operations.DatabaseRepository;
import eu.eudat.logic.utilities.documents.helpers.FileEnvelope;
import eu.eudat.logic.utilities.documents.pdf.PDFUtils;
import eu.eudat.models.data.datasetprofile.DatasetProfileListingModel;
import eu.eudat.models.data.datasetwizard.DatasetsToBeFinalized;
import eu.eudat.models.data.dmp.DataManagementPlan;
@ -69,17 +70,15 @@ public class DMPs extends BaseController {
private DynamicGrantConfiguration dynamicGrantConfiguration;
private Environment environment;
private DataManagementPlanManager dataManagementPlanManager;
private DatasetManager datasetManager;
private ConfigLoader configLoader;
@Autowired
public DMPs(ApiContext apiContext, DynamicGrantConfiguration dynamicGrantConfiguration, Environment environment,
DataManagementPlanManager dataManagementPlanManager, DatasetManager datasetManager, ConfigLoader configLoader) {
DataManagementPlanManager dataManagementPlanManager, ConfigLoader configLoader) {
super(apiContext);
this.dynamicGrantConfiguration = dynamicGrantConfiguration;
this.environment = environment;
this.dataManagementPlanManager = dataManagementPlanManager;
this.datasetManager = datasetManager;
this.configLoader = configLoader;
}
@ -175,7 +174,7 @@ public class DMPs extends BaseController {
public @ResponseBody
ResponseEntity getRDAJsonDocument(@PathVariable String id, @ClaimedAuthorities(claims = {Authorities.ADMIN, Authorities.MANAGER, Authorities.USER, Authorities.ANONYMOUS}) Principal principal) {
try {
return this.dataManagementPlanManager.getRDAJsonDocument(id, datasetManager, principal);
return this.dataManagementPlanManager.getRDAJsonDocument(id, principal);
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.EXPECTATION_FAILED).body(new ResponseItem<>().message(e.getMessage()).status(ApiMessageCode.ERROR_MESSAGE));
}
@ -187,7 +186,7 @@ public class DMPs extends BaseController {
@ClaimedAuthorities(claims = {Authorities.ADMIN, Authorities.MANAGER, Authorities.USER, Authorities.ANONYMOUS}) Principal principal) throws IllegalAccessException, IOException, InstantiationException, InterruptedException {
FileEnvelope file = this.dataManagementPlanManager.getWordDocument(id, principal, configLoader);
String name = file.getFilename().substring(0, file.getFilename().length() - 5);
File pdffile = datasetManager.convertToPDF(file, environment);
File pdffile = PDFUtils.convertToPDF(file, environment);
InputStream resource = new FileInputStream(pdffile);
logger.info("Mime Type of " + name + " is " +
new MimetypesFileTypeMap().getContentType(file.getFile()));

View File

@ -16,6 +16,7 @@ import eu.eudat.logic.security.claims.ClaimedAuthorities;
import eu.eudat.logic.services.ApiContext;
import eu.eudat.logic.services.forms.VisibilityRuleService;
import eu.eudat.logic.utilities.documents.helpers.FileEnvelope;
import eu.eudat.logic.utilities.documents.pdf.PDFUtils;
import eu.eudat.models.data.dataset.DatasetOverviewModel;
import eu.eudat.models.data.datasetprofile.DatasetProfileListingModel;
import eu.eudat.models.data.datasetwizard.DataManagentPlanListingModel;
@ -221,7 +222,7 @@ public class Datasets extends BaseController {
if (fileName.endsWith(".docx")){
fileName = fileName.substring(0, fileName.length() - 5);
}
File pdffile = datasetManager.convertToPDF(file, environment);
File pdffile = PDFUtils.convertToPDF(file, environment);
InputStream resource = new FileInputStream(pdffile);
HttpHeaders responseHeaders = new HttpHeaders();

View File

@ -37,6 +37,7 @@ import eu.eudat.logic.services.operations.DatabaseRepository;
import eu.eudat.logic.services.utilities.UtilitiesService;
import eu.eudat.logic.utilities.builders.XmlBuilder;
import eu.eudat.logic.utilities.documents.helpers.FileEnvelope;
import eu.eudat.logic.utilities.documents.pdf.PDFUtils;
import eu.eudat.logic.utilities.documents.types.ParagraphStyle;
import eu.eudat.logic.utilities.documents.word.WordBuilder;
import eu.eudat.logic.utilities.documents.xml.ExportXmlBuilder;
@ -1464,7 +1465,7 @@ public class DataManagementPlanManager {
return fileEnvelope;
}
public ResponseEntity<byte[]> getRDAJsonDocument(String id, DatasetManager datasetManager, Principal principal) throws Exception {
public ResponseEntity<byte[]> getRDAJsonDocument(String id, Principal principal) throws Exception {
eu.eudat.data.entities.DMP dmp = databaseRepository.getDmpDao().find(UUID.fromString(id));
if (!dmp.isPublic() && dmp.getUsers().stream().noneMatch(userInfo -> userInfo.getUser().getId() == principal.getId()))
throw new UnauthorisedException();
@ -1694,7 +1695,7 @@ public class DataManagementPlanManager {
// datasets.add(new DatasetListingModel().fromDataModel(dataset));
}
logger.info(dm.toString());
//logger.info(dm.toString());
}
return dataManagementPlans;
@ -2154,7 +2155,7 @@ public class DataManagementPlanManager {
// Second step, add the file to the entry.
FileEnvelope file = getWordDocument(id.toString(), principal, configLoader);
String name = file.getFilename().substring(0, file.getFilename().length() - 5);
File pdfFile = datasetManager.convertToPDF(file, environment);
File pdfFile = PDFUtils.convertToPDF(file, environment);
String fileName = name + ".pdf";
FileSystemResource fileSystemResource = new FileSystemResource(pdfFile);
HttpEntity<FileSystemResource> addFileMapRequest = new HttpEntity<>(fileSystemResource, null);
@ -2165,7 +2166,7 @@ public class DataManagementPlanManager {
ResponseEntity<byte[]> jsonFile;
try {
jsonFile = getRDAJsonDocument(id.toString(), datasetManager, principal);
jsonFile = getRDAJsonDocument(id.toString(), principal);
} catch (Exception e) {
throw e;
}

View File

@ -556,54 +556,6 @@ public class DatasetManager {
return fileEnvelope;
}
public File convertToPDF(FileEnvelope file, Environment environment) throws IOException, InterruptedException {
LinkedMultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
String uuid = UUID.randomUUID().toString();
map.add("files", new FileSystemResource(file.getFile()));
map.add("filename", uuid + ".pdf");
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
headers.add("Content-disposition", "attachment; filename=" + uuid + ".pdf");
headers.add("Content-type", "application/pdf");
HttpEntity<LinkedMultiValueMap<String, Object>> requestEntity = new HttpEntity<LinkedMultiValueMap<String, Object>>(
map, headers);
byte[] queueResult = new RestTemplate().postForObject(environment.getProperty("pdf.converter.url") + "convert/office"
, requestEntity, byte[].class);
File resultPdf = new File(environment.getProperty("temp.temp") + uuid + ".pdf");
FileOutputStream output = new FileOutputStream(resultPdf);
IOUtils.write(queueResult, output);
output.close();
Files.deleteIfExists(file.getFile().toPath());
return resultPdf;
}
private File extractFromZip(File file, String filename) throws IOException {
byte[] buffer = new byte[1024];
File newFile = new File(filename);
ZipInputStream zis = new ZipInputStream(new FileInputStream(file));
ZipEntry zipEntry = zis.getNextEntry();
while (zipEntry != null) {
String zippedFileName = zipEntry.getName();
if (zippedFileName.equals("pdf")) {
FileOutputStream fos = new FileOutputStream(newFile);
int len;
while ((len = zis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
fos.close();
zipEntry = zis.getNextEntry();
}
}
zis.closeEntry();
zis.close();
return newFile;
}
public eu.eudat.data.entities.Dataset createOrUpdate(DatasetWizardModel datasetWizardModel, Principal principal) throws Exception {
Boolean sendNotification = false;
Dataset tempDataset = null;
@ -1094,15 +1046,17 @@ public class DatasetManager {
if (!tagNodes.isEmpty()) {
tagNodes.forEach(node -> {
JsonNode value = node.get("value");
String stringValue = value.toString().replaceAll("=", ":");
JSONArray values = new JSONArray(stringValue);
if (values != null) {
values.iterator().forEachRemaining(element -> {
Map<String, Object> data = ((JSONObject) element).toMap();
this.addTag(tags, wizardModel.getTags(), data.get("id").toString(), data.get("name").toString());
});
} else {
this.addTag(tags, wizardModel.getTags(), "", value.asText());
if (!value.toString().equals("\"\"")) {
String stringValue = value.toString().replaceAll("=", ":");
JSONArray values = new JSONArray(stringValue);
if (values != null) {
values.iterator().forEachRemaining(element -> {
Map<String, Object> data = ((JSONObject) element).toMap();
this.addTag(tags, wizardModel.getTags(), data.get("id").toString(), data.get("name").toString());
});
} else {
this.addTag(tags, wizardModel.getTags(), "", value.asText());
}
}
});
}

View File

@ -0,0 +1,45 @@
package eu.eudat.logic.utilities.documents.pdf;
import eu.eudat.logic.utilities.documents.helpers.FileEnvelope;
import org.apache.commons.io.IOUtils;
import org.springframework.core.env.Environment;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.client.RestTemplate;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.util.UUID;
public class PDFUtils {
public static File convertToPDF(FileEnvelope file, Environment environment) throws IOException {
LinkedMultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
String uuid = UUID.randomUUID().toString();
map.add("files", new FileSystemResource(file.getFile()));
map.add("filename", uuid + ".pdf");
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
headers.add("Content-disposition", "attachment; filename=" + uuid + ".pdf");
headers.add("Content-type", "application/pdf");
HttpEntity<LinkedMultiValueMap<String, Object>> requestEntity = new HttpEntity<LinkedMultiValueMap<String, Object>>(
map, headers);
byte[] queueResult = new RestTemplate().postForObject(environment.getProperty("pdf.converter.url") + "forms/libreoffice/convert"
, requestEntity, byte[].class);
File resultPdf = new File(environment.getProperty("temp.temp") + uuid + ".pdf");
FileOutputStream output = new FileOutputStream(resultPdf);
IOUtils.write(queueResult, output);
output.close();
Files.deleteIfExists(file.getFile().toPath());
return resultPdf;
}
}