Commit 193d6ce2 authored by Nicola Fulvio Calabria's avatar Nicola Fulvio Calabria
Browse files

#3824 File upload improvements. First part of implementation. Need to secure...

#3824 File upload improvements. First part of implementation. Need to secure it due to local machine reset.
parent 7fd1ed2f
Loading
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -32,3 +32,4 @@ nbactions.xml

### VS Code ###
.vscode/
/nb-configuration.xml
 No newline at end of file
+7 −1
Original line number Diff line number Diff line
@@ -67,6 +67,12 @@
            <version>0.13.3</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>it.inaf.oats</groupId>
            <artifactId>vospace-rest</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

    <profiles>
+1 −0
Original line number Diff line number Diff line
@@ -9,6 +9,7 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
public class FileServiceApplication {
+36 −0
Original line number Diff line number Diff line
@@ -15,11 +15,47 @@ public class FileInfo {
    private boolean asyncTrans;
    private List<String> acceptViews;
    private List<String> provideViews;
    private String contentType;
    private String contentEncoding;
    private Long contentLength;
    private String contentMd5;

    public int getNodeId() {
        return nodeId;
    }

    public String getContentType() {
        return contentType;
    }

    public void setContentType(String contentType) {
        this.contentType = contentType;
    }

    public String getContentEncoding() {
        return contentEncoding;
    }

    public void setContentEncoding(String contentEncoding) {
        this.contentEncoding = contentEncoding;
    }

    public Long getContentLength() {
        return contentLength;
    }

    public void setContentLength(Long contentLength) {
        this.contentLength = contentLength;
    }

    public String getContentMd5() {
        return contentMd5;
    }

    public void setContentMd5(String contentMd5) {
        this.contentMd5 = contentMd5;
    }

    public void setNodeId(int nodeId) {
        this.nodeId = nodeId;
    }
+73 −12
Original line number Diff line number Diff line
@@ -2,15 +2,21 @@ package it.inaf.ia2.transfer.controller;

import it.inaf.ia2.transfer.persistence.FileDAO;
import it.inaf.ia2.transfer.persistence.ListOfFilesDAO;
//import it.inaf.oats.vospace.persistence.JobDAO;
import net.ivoa.xml.uws.v1.JobSummary;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import javax.xml.bind.DatatypeConverter;
import net.ivoa.xml.uws.v1.ExecutionPhase;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
@@ -20,6 +26,8 @@ import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.http.HttpHeaders;

@RestController
public class PutFileController extends FileController {
@@ -29,20 +37,47 @@ public class PutFileController extends FileController {
    @Autowired
    private FileDAO fileDAO;

    //@Autowired
    //private JobDAO jobDAO;
    @Autowired
    private ListOfFilesDAO listOfFilesDAO;

    @PutMapping("/**")
    public ResponseEntity<?> putFile(@RequestParam("file") MultipartFile file) throws IOException {
    public ResponseEntity<?> putFile(@RequestHeader(value = HttpHeaders.CONTENT_ENCODING, required = false) String contentEncoding,
            @RequestParam("file") MultipartFile file,
            @RequestParam(value = "jobid", required = false) String jobId) throws IOException, NoSuchAlgorithmException {

        String path = getPath();
        JobSummary job = null;

        if (jobId == null) {
            LOG.debug("putFile called for path {}", path);
        } else {
            LOG.debug("putFile called for path {} with jobId {}", path, jobId);
            /*
            Optional<JobSummary> resultJob = jobDAO.getJob(jobId);
            if(resultJob.isPresent())
            {
                job = resultJob.get();
            } else {
                return new ResponseEntity<>("Job "+jobId+ " not found", NOT_FOUND);
            }*/
        }

        Optional<FileInfo> optFileInfo = fileDAO.getFileInfo(path);

        if (optFileInfo.isPresent()) {
            try (InputStream in = file.getInputStream()) {
                storeFile(optFileInfo.get(), in);
                FileInfo fileInfo = optFileInfo.get();

                if (fileInfo.getAcceptViews() != null && fileInfo.getAcceptViews().contains("urn:list-of-files")) {
                    storeListOfFiles(fileInfo, in);
                } else {
                    fileInfo.setContentType(file.getContentType());
                    fileInfo.setContentEncoding(contentEncoding);
                    storeGenericFile(fileInfo, in, job);
                }

            }
            return ResponseEntity.ok().build();
        } else {
@@ -50,15 +85,15 @@ public class PutFileController extends FileController {
        }
    }

    private void storeFile(FileInfo fileInfo, InputStream is) throws IOException {
    /*
    private void storeFile(FileInfo fileInfo, InputStream is, String jobId) throws IOException {

        if (fileInfo.getAcceptViews() != null && fileInfo.getAcceptViews().contains("urn:list-of-files")) {
            storeListOfFiles(fileInfo, is);
        } else {
            storeGenericFile(fileInfo, is);
            storeGenericFile(fileInfo, is, jobId);
        }
    }

    }*/
    private void storeListOfFiles(FileInfo fileInfo, InputStream is) throws IOException {
        List<String> filePaths = parseListOfFiles(is);
        listOfFilesDAO.createList(fileInfo.getVirtualPath(), filePaths);
@@ -75,7 +110,7 @@ public class PutFileController extends FileController {
        return filePaths;
    }

    private void storeGenericFile(FileInfo fileInfo, InputStream is) throws IOException {
    private void storeGenericFile(FileInfo fileInfo, InputStream is, JobSummary job) throws IOException, NoSuchAlgorithmException {

        File file = new File(fileInfo.getOsPath());

@@ -100,7 +135,24 @@ public class PutFileController extends FileController {
        try {
            fileDAO.setBusy(fileInfo.getNodeId(), true);
            Files.copy(is, file.toPath());
        } catch (IOException ex) {
            Long fileSize = Files.size(file.toPath());
            String md5Checksum = makeMD5Checksum(file);

            fileDAO.updateFileAttributes(fileInfo.getNodeId(),
                    fileInfo.getContentType(),
                    fileInfo.getContentEncoding(),
                    fileSize,
                    md5Checksum);
            if (job != null) {
                //job.setPhase(ExecutionPhase.COMPLETED);
                //jobDAO.updateJob(job);
            }

        } catch (IOException | NoSuchAlgorithmException ex) {
            if (job != null) {
                //job.setPhase(ExecutionPhase.ERROR);
                //jobDAO.updateJob(job);
            }
            throw ex;
        } finally {
            fileDAO.setBusy(fileInfo.getNodeId(), false);
@@ -138,4 +190,13 @@ public class PutFileController extends FileController {
        }
        return file;
    }

    private String makeMD5Checksum(File file) throws NoSuchAlgorithmException, IOException {
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(Files.readAllBytes(file.toPath()));
        byte[] digest = md.digest();
        String checksum = DatatypeConverter.printHexBinary(digest);
        return checksum;
    }

}
Loading