Skip to content
Snippets Groups Projects
Commit bc3ad7b9 authored by Sonia Zorba's avatar Sonia Zorba
Browse files

Handled duplicate files upload and other edge cases

parent 2f592f62
No related branches found
No related tags found
No related merge requests found
Pipeline #957 passed
...@@ -98,7 +98,9 @@ public class GetFileController { ...@@ -98,7 +98,9 @@ public class GetFileController {
return new ResponseEntity<>("File " + file.getName() + " is not readable", INTERNAL_SERVER_ERROR); 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())); response.setHeader("Content-Length", String.valueOf(file.length()));
byte[] bytes = new byte[1024]; byte[] bytes = new byte[1024];
......
...@@ -89,6 +89,51 @@ public class PutFileController { ...@@ -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()); 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;
} }
} }
...@@ -84,6 +84,8 @@ public class FileDAO { ...@@ -84,6 +84,8 @@ public class FileDAO {
if (asyncLocation) { if (asyncLocation) {
String username = rs.getString("username"); String username = rs.getString("username");
completeOsPath = completeOsPath.resolve(username).resolve("retrieve"); completeOsPath = completeOsPath.resolve(username).resolve("retrieve");
} else {
completeOsPath = completeOsPath.resolve(fi.getOwnerId());
} }
completeOsPath = completeOsPath.resolve(osPath); completeOsPath = completeOsPath.resolve(osPath);
...@@ -96,4 +98,28 @@ public class FileDAO { ...@@ -96,4 +98,28 @@ public class FileDAO {
} }
return Arrays.asList((String[]) array.getArray()); 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;
});
}
} }
...@@ -58,6 +58,7 @@ public class GetFileControllerTest { ...@@ -58,6 +58,7 @@ public class GetFileControllerTest {
FileInfo fileInfo = new FileInfo(); FileInfo fileInfo = new FileInfo();
fileInfo.setOsPath(tempFile.getAbsolutePath()); fileInfo.setOsPath(tempFile.getAbsolutePath());
fileInfo.setVirtualPath("/path/to/myfile");
fileInfo.setIsPublic(true); fileInfo.setIsPublic(true);
when(fileDao.getFileInfo(any())).thenReturn(Optional.of(fileInfo)); when(fileDao.getFileInfo(any())).thenReturn(Optional.of(fileInfo));
...@@ -124,6 +125,7 @@ public class GetFileControllerTest { ...@@ -124,6 +125,7 @@ public class GetFileControllerTest {
FileInfo fileInfo = new FileInfo(); FileInfo fileInfo = new FileInfo();
fileInfo.setOsPath(tempFile.getAbsolutePath()); fileInfo.setOsPath(tempFile.getAbsolutePath());
fileInfo.setVirtualPath("/path/to/myfile");
fileInfo.setGroupRead(Collections.singletonList("group1")); fileInfo.setGroupRead(Collections.singletonList("group1"));
when(fileDao.getFileInfo(any())).thenReturn(Optional.of(fileInfo)); when(fileDao.getFileInfo(any())).thenReturn(Optional.of(fileInfo));
...@@ -138,6 +140,7 @@ public class GetFileControllerTest { ...@@ -138,6 +140,7 @@ public class GetFileControllerTest {
when(tokenParser.getClaims(any())).thenReturn(claims); when(tokenParser.getClaims(any())).thenReturn(claims);
FileInfo fileInfo = new FileInfo(); FileInfo fileInfo = new FileInfo();
fileInfo.setVirtualPath("/path/to/myfile");
fileInfo.setOsPath(tempFile.getAbsolutePath()); fileInfo.setOsPath(tempFile.getAbsolutePath());
fileInfo.setOwnerId("123"); fileInfo.setOwnerId("123");
...@@ -153,6 +156,7 @@ public class GetFileControllerTest { ...@@ -153,6 +156,7 @@ public class GetFileControllerTest {
public void testPrivateFileNullToken() throws Exception { public void testPrivateFileNullToken() throws Exception {
FileInfo fileInfo = new FileInfo(); FileInfo fileInfo = new FileInfo();
fileInfo.setVirtualPath("/path/to/myfile");
when(fileDao.getFileInfo(any())).thenReturn(Optional.of(fileInfo)); when(fileDao.getFileInfo(any())).thenReturn(Optional.of(fileInfo));
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment