Commit bc3ad7b9 authored by Sonia Zorba's avatar Sonia Zorba
Browse files

Handled duplicate files upload and other edge cases

parent 2f592f62
Loading
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -98,7 +98,9 @@ public class GetFileController {
            return new ResponseEntity<>("File " + file.getName() + " is not readable", INTERNAL_SERVER_ERROR);
        }

        response.setHeader("Content-Disposition", "attachment; filename=" + file.getName());
        String vosName = fileInfo.getVirtualPath().substring(fileInfo.getVirtualPath().lastIndexOf("/") + 1);
        
        response.setHeader("Content-Disposition", "attachment; filename=" + vosName);
        response.setHeader("Content-Length", String.valueOf(file.length()));

        byte[] bytes = new byte[1024];
+46 −1
Original line number Diff line number Diff line
@@ -89,6 +89,51 @@ public class PutFileController {
            }
        }

        String originalFileName = file.getName();
        file = getEmptyFile(file, 1);
        if (!originalFileName.equals(file.getName())) {
            fileDAO.setOsName(fileInfo.getNodeId(), file.getName());
        }

        try {
            fileDAO.setBusy(fileInfo.getNodeId(), true);
            Files.copy(is, file.toPath());
        } catch (IOException ex) {
            throw ex;
        } finally {
            fileDAO.setBusy(fileInfo.getNodeId(), false);
        }
    }

    /**
     * Handles duplicate file uploads generating a new non existent path. This
     * is necessary in some edge cases, like when a file has been renamed in
     * VOSpace only but the original file on disk still has the old name or if a
     * file has been marked for deletion and a file with the same name is
     * uploaded before the cleanup.
     */
    private File getEmptyFile(File file, int index) {
        if (file.exists()) {

            String fileName = file.getName();

            String nameWithoutExtension;
            String extension = null;
            if (fileName.contains(".")) {
                nameWithoutExtension = fileName.substring(0, fileName.lastIndexOf("."));
                extension = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length());
            } else {
                nameWithoutExtension = fileName;
            }

            String newName = nameWithoutExtension + "-" + index;
            if (extension != null) {
                newName += "." + extension;
            }

            File newFile = file.toPath().getParent().resolve(newName).toFile();
            return getEmptyFile(newFile, index + 1);
        }
        return file;
    }
}
+26 −0
Original line number Diff line number Diff line
@@ -84,6 +84,8 @@ public class FileDAO {
        if (asyncLocation) {
            String username = rs.getString("username");
            completeOsPath = completeOsPath.resolve(username).resolve("retrieve");
        } else {
            completeOsPath = completeOsPath.resolve(fi.getOwnerId());
        }

        completeOsPath = completeOsPath.resolve(osPath);
@@ -96,4 +98,28 @@ public class FileDAO {
        }
        return Arrays.asList((String[]) array.getArray());
    }

    public void setBusy(int nodeId, boolean busy) {

        String sql = "UPDATE node SET busy_state = ? WHERE node_id = ?";

        jdbcTemplate.update(conn -> {
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setBoolean(1, busy);
            ps.setInt(2, nodeId);
            return ps;
        });
    }

    public void setOsName(int nodeId, String newName) {

        String sql = "UPDATE node SET os_name = ? WHERE node_id = ?";

        jdbcTemplate.update(conn -> {
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, newName);
            ps.setInt(2, nodeId);
            return ps;
        });
    }
}
+4 −0
Original line number Diff line number Diff line
@@ -58,6 +58,7 @@ public class GetFileControllerTest {

        FileInfo fileInfo = new FileInfo();
        fileInfo.setOsPath(tempFile.getAbsolutePath());
        fileInfo.setVirtualPath("/path/to/myfile");
        fileInfo.setIsPublic(true);

        when(fileDao.getFileInfo(any())).thenReturn(Optional.of(fileInfo));
@@ -124,6 +125,7 @@ public class GetFileControllerTest {

        FileInfo fileInfo = new FileInfo();
        fileInfo.setOsPath(tempFile.getAbsolutePath());
        fileInfo.setVirtualPath("/path/to/myfile");
        fileInfo.setGroupRead(Collections.singletonList("group1"));

        when(fileDao.getFileInfo(any())).thenReturn(Optional.of(fileInfo));
@@ -138,6 +140,7 @@ public class GetFileControllerTest {
        when(tokenParser.getClaims(any())).thenReturn(claims);

        FileInfo fileInfo = new FileInfo();
        fileInfo.setVirtualPath("/path/to/myfile");
        fileInfo.setOsPath(tempFile.getAbsolutePath());
        fileInfo.setOwnerId("123");

@@ -153,6 +156,7 @@ public class GetFileControllerTest {
    public void testPrivateFileNullToken() throws Exception {

        FileInfo fileInfo = new FileInfo();
        fileInfo.setVirtualPath("/path/to/myfile");

        when(fileDao.getFileInfo(any())).thenReturn(Optional.of(fileInfo));