Refactor PDF conversion
This commit is contained in:
parent
1a821179db
commit
a4fef8e462
|
@ -20,6 +20,7 @@ import eu.eudat.logic.security.claims.ClaimedAuthorities;
|
||||||
import eu.eudat.logic.services.ApiContext;
|
import eu.eudat.logic.services.ApiContext;
|
||||||
import eu.eudat.logic.services.operations.DatabaseRepository;
|
import eu.eudat.logic.services.operations.DatabaseRepository;
|
||||||
import eu.eudat.logic.utilities.documents.helpers.FileEnvelope;
|
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.datasetprofile.DatasetProfileListingModel;
|
||||||
import eu.eudat.models.data.datasetwizard.DatasetsToBeFinalized;
|
import eu.eudat.models.data.datasetwizard.DatasetsToBeFinalized;
|
||||||
import eu.eudat.models.data.dmp.DataManagementPlan;
|
import eu.eudat.models.data.dmp.DataManagementPlan;
|
||||||
|
@ -69,17 +70,15 @@ public class DMPs extends BaseController {
|
||||||
private DynamicGrantConfiguration dynamicGrantConfiguration;
|
private DynamicGrantConfiguration dynamicGrantConfiguration;
|
||||||
private Environment environment;
|
private Environment environment;
|
||||||
private DataManagementPlanManager dataManagementPlanManager;
|
private DataManagementPlanManager dataManagementPlanManager;
|
||||||
private DatasetManager datasetManager;
|
|
||||||
private ConfigLoader configLoader;
|
private ConfigLoader configLoader;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public DMPs(ApiContext apiContext, DynamicGrantConfiguration dynamicGrantConfiguration, Environment environment,
|
public DMPs(ApiContext apiContext, DynamicGrantConfiguration dynamicGrantConfiguration, Environment environment,
|
||||||
DataManagementPlanManager dataManagementPlanManager, DatasetManager datasetManager, ConfigLoader configLoader) {
|
DataManagementPlanManager dataManagementPlanManager, ConfigLoader configLoader) {
|
||||||
super(apiContext);
|
super(apiContext);
|
||||||
this.dynamicGrantConfiguration = dynamicGrantConfiguration;
|
this.dynamicGrantConfiguration = dynamicGrantConfiguration;
|
||||||
this.environment = environment;
|
this.environment = environment;
|
||||||
this.dataManagementPlanManager = dataManagementPlanManager;
|
this.dataManagementPlanManager = dataManagementPlanManager;
|
||||||
this.datasetManager = datasetManager;
|
|
||||||
this.configLoader = configLoader;
|
this.configLoader = configLoader;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -175,7 +174,7 @@ public class DMPs extends BaseController {
|
||||||
public @ResponseBody
|
public @ResponseBody
|
||||||
ResponseEntity getRDAJsonDocument(@PathVariable String id, @ClaimedAuthorities(claims = {Authorities.ADMIN, Authorities.MANAGER, Authorities.USER, Authorities.ANONYMOUS}) Principal principal) {
|
ResponseEntity getRDAJsonDocument(@PathVariable String id, @ClaimedAuthorities(claims = {Authorities.ADMIN, Authorities.MANAGER, Authorities.USER, Authorities.ANONYMOUS}) Principal principal) {
|
||||||
try {
|
try {
|
||||||
return this.dataManagementPlanManager.getRDAJsonDocument(id, datasetManager, principal);
|
return this.dataManagementPlanManager.getRDAJsonDocument(id, principal);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
return ResponseEntity.status(HttpStatus.EXPECTATION_FAILED).body(new ResponseItem<>().message(e.getMessage()).status(ApiMessageCode.ERROR_MESSAGE));
|
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 {
|
@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);
|
FileEnvelope file = this.dataManagementPlanManager.getWordDocument(id, principal, configLoader);
|
||||||
String name = file.getFilename().substring(0, file.getFilename().length() - 5);
|
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);
|
InputStream resource = new FileInputStream(pdffile);
|
||||||
logger.info("Mime Type of " + name + " is " +
|
logger.info("Mime Type of " + name + " is " +
|
||||||
new MimetypesFileTypeMap().getContentType(file.getFile()));
|
new MimetypesFileTypeMap().getContentType(file.getFile()));
|
||||||
|
|
|
@ -16,6 +16,7 @@ import eu.eudat.logic.security.claims.ClaimedAuthorities;
|
||||||
import eu.eudat.logic.services.ApiContext;
|
import eu.eudat.logic.services.ApiContext;
|
||||||
import eu.eudat.logic.services.forms.VisibilityRuleService;
|
import eu.eudat.logic.services.forms.VisibilityRuleService;
|
||||||
import eu.eudat.logic.utilities.documents.helpers.FileEnvelope;
|
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.dataset.DatasetOverviewModel;
|
||||||
import eu.eudat.models.data.datasetprofile.DatasetProfileListingModel;
|
import eu.eudat.models.data.datasetprofile.DatasetProfileListingModel;
|
||||||
import eu.eudat.models.data.datasetwizard.DataManagentPlanListingModel;
|
import eu.eudat.models.data.datasetwizard.DataManagentPlanListingModel;
|
||||||
|
@ -221,7 +222,7 @@ public class Datasets extends BaseController {
|
||||||
if (fileName.endsWith(".docx")){
|
if (fileName.endsWith(".docx")){
|
||||||
fileName = fileName.substring(0, fileName.length() - 5);
|
fileName = fileName.substring(0, fileName.length() - 5);
|
||||||
}
|
}
|
||||||
File pdffile = datasetManager.convertToPDF(file, environment);
|
File pdffile = PDFUtils.convertToPDF(file, environment);
|
||||||
InputStream resource = new FileInputStream(pdffile);
|
InputStream resource = new FileInputStream(pdffile);
|
||||||
|
|
||||||
HttpHeaders responseHeaders = new HttpHeaders();
|
HttpHeaders responseHeaders = new HttpHeaders();
|
||||||
|
|
|
@ -37,6 +37,7 @@ import eu.eudat.logic.services.operations.DatabaseRepository;
|
||||||
import eu.eudat.logic.services.utilities.UtilitiesService;
|
import eu.eudat.logic.services.utilities.UtilitiesService;
|
||||||
import eu.eudat.logic.utilities.builders.XmlBuilder;
|
import eu.eudat.logic.utilities.builders.XmlBuilder;
|
||||||
import eu.eudat.logic.utilities.documents.helpers.FileEnvelope;
|
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.types.ParagraphStyle;
|
||||||
import eu.eudat.logic.utilities.documents.word.WordBuilder;
|
import eu.eudat.logic.utilities.documents.word.WordBuilder;
|
||||||
import eu.eudat.logic.utilities.documents.xml.ExportXmlBuilder;
|
import eu.eudat.logic.utilities.documents.xml.ExportXmlBuilder;
|
||||||
|
@ -1464,7 +1465,7 @@ public class DataManagementPlanManager {
|
||||||
return fileEnvelope;
|
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));
|
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()))
|
if (!dmp.isPublic() && dmp.getUsers().stream().noneMatch(userInfo -> userInfo.getUser().getId() == principal.getId()))
|
||||||
throw new UnauthorisedException();
|
throw new UnauthorisedException();
|
||||||
|
@ -1694,7 +1695,7 @@ public class DataManagementPlanManager {
|
||||||
// datasets.add(new DatasetListingModel().fromDataModel(dataset));
|
// datasets.add(new DatasetListingModel().fromDataModel(dataset));
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.info(dm.toString());
|
//logger.info(dm.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
return dataManagementPlans;
|
return dataManagementPlans;
|
||||||
|
@ -2154,7 +2155,7 @@ public class DataManagementPlanManager {
|
||||||
// Second step, add the file to the entry.
|
// Second step, add the file to the entry.
|
||||||
FileEnvelope file = getWordDocument(id.toString(), principal, configLoader);
|
FileEnvelope file = getWordDocument(id.toString(), principal, configLoader);
|
||||||
String name = file.getFilename().substring(0, file.getFilename().length() - 5);
|
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";
|
String fileName = name + ".pdf";
|
||||||
FileSystemResource fileSystemResource = new FileSystemResource(pdfFile);
|
FileSystemResource fileSystemResource = new FileSystemResource(pdfFile);
|
||||||
HttpEntity<FileSystemResource> addFileMapRequest = new HttpEntity<>(fileSystemResource, null);
|
HttpEntity<FileSystemResource> addFileMapRequest = new HttpEntity<>(fileSystemResource, null);
|
||||||
|
@ -2165,7 +2166,7 @@ public class DataManagementPlanManager {
|
||||||
|
|
||||||
ResponseEntity<byte[]> jsonFile;
|
ResponseEntity<byte[]> jsonFile;
|
||||||
try {
|
try {
|
||||||
jsonFile = getRDAJsonDocument(id.toString(), datasetManager, principal);
|
jsonFile = getRDAJsonDocument(id.toString(), principal);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
|
|
|
@ -556,54 +556,6 @@ public class DatasetManager {
|
||||||
return fileEnvelope;
|
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 {
|
public eu.eudat.data.entities.Dataset createOrUpdate(DatasetWizardModel datasetWizardModel, Principal principal) throws Exception {
|
||||||
Boolean sendNotification = false;
|
Boolean sendNotification = false;
|
||||||
Dataset tempDataset = null;
|
Dataset tempDataset = null;
|
||||||
|
@ -1094,15 +1046,17 @@ public class DatasetManager {
|
||||||
if (!tagNodes.isEmpty()) {
|
if (!tagNodes.isEmpty()) {
|
||||||
tagNodes.forEach(node -> {
|
tagNodes.forEach(node -> {
|
||||||
JsonNode value = node.get("value");
|
JsonNode value = node.get("value");
|
||||||
String stringValue = value.toString().replaceAll("=", ":");
|
if (!value.toString().equals("\"\"")) {
|
||||||
JSONArray values = new JSONArray(stringValue);
|
String stringValue = value.toString().replaceAll("=", ":");
|
||||||
if (values != null) {
|
JSONArray values = new JSONArray(stringValue);
|
||||||
values.iterator().forEachRemaining(element -> {
|
if (values != null) {
|
||||||
Map<String, Object> data = ((JSONObject) element).toMap();
|
values.iterator().forEachRemaining(element -> {
|
||||||
this.addTag(tags, wizardModel.getTags(), data.get("id").toString(), data.get("name").toString());
|
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());
|
} else {
|
||||||
|
this.addTag(tags, wizardModel.getTags(), "", value.asText());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue