use of com.mercedesbenz.sechub.pds.usecase.UseCaseUserUploadsJobData in project sechub by mercedes-benz.
the class PDSFileUploadJobService method upload.
@UseCaseUserUploadsJobData(@PDSStep(name = "service call", description = "uploaded file is stored by storage service", number = 2))
public void upload(UUID jobUUID, String fileName, MultipartFile file, String checkSum) {
notNull(jobUUID, "job uuid may not be null");
notNull(file, "file may not be null");
notNull(checkSum, "checkSum may not be null");
validateFileName(fileName);
PDSJob job = assertJobFound(jobUUID, repository);
assertJobIsInState(job, PDSJobStatusState.CREATED);
/*
* fetch job storage without path - storage service decides location
* automatically
*/
JobStorage storage = storageService.getJobStorage(jobUUID);
Path tmpFile = null;
try {
/* prepare a tmp file for validation */
try {
tmpFile = Files.createTempFile("pds_upload_tmp", null);
file.transferTo(tmpFile);
} catch (IOException e) {
LOG.error("Was not able to create temp file of zipped sources!", e);
throw new IllegalStateException("Was not able to create temp file");
}
/* validate */
if (fileName.toLowerCase().endsWith(".zip")) {
// we check for ZIP file correctness, so automated unzipping can be done
// correctly
assertValidZipFile(tmpFile);
}
assertCheckSumCorrect(checkSum, tmpFile);
/* now store */
try {
LOG.info("Upload file {} for job {} to storage", fileName, jobUUID);
storage.store(fileName, file.getInputStream());
// we also store checksum
storage.store(fileName + ".checksum", new StringInputStream(checkSum));
} catch (IOException e) {
LOG.error("Was not able to store {} for job {}, reason:", fileName, jobUUID, e.getMessage());
throw new IllegalArgumentException("Cannot store given file", e);
}
} finally {
if (tmpFile != null && Files.exists(tmpFile)) {
try {
Files.delete(tmpFile);
} catch (IOException e) {
LOG.error("Was not able delete former temp file for zipped sources! {}", jobUUID, e);
}
}
}
}
Aggregations