Search in sources :

Example 1 with UseCaseUserUploadsJobData

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);
            }
        }
    }
}
Also used : Path(java.nio.file.Path) StringInputStream(com.amazonaws.util.StringInputStream) IOException(java.io.IOException) JobStorage(com.mercedesbenz.sechub.storage.core.JobStorage) UseCaseUserUploadsJobData(com.mercedesbenz.sechub.pds.usecase.UseCaseUserUploadsJobData)

Aggregations

StringInputStream (com.amazonaws.util.StringInputStream)1 UseCaseUserUploadsJobData (com.mercedesbenz.sechub.pds.usecase.UseCaseUserUploadsJobData)1 JobStorage (com.mercedesbenz.sechub.storage.core.JobStorage)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1