Skip to content

Commit

Permalink
feat: delete files on delete project
Browse files Browse the repository at this point in the history
  • Loading branch information
Quentin-Guillemin committed Mar 7, 2024
1 parent 9878a97 commit 0668c7d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
1 change: 1 addition & 0 deletions NOTICE
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ This project includes:
AntLR Parser Generator under BSD License
Apache Commons Collections under Apache License, Version 2.0
Apache Commons Compress under Apache License, Version 2.0
Apache Commons IO under Apache-2.0
Apache Commons Lang under Apache License, Version 2.0
Apache Log4j API under Apache License, Version 2.0
Apache Log4j to SLF4J Adapter under Apache License, Version 2.0
Expand Down
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@
<groupId>com.querydsl</groupId>
<artifactId>querydsl-jpa</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.15.1</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import fr.recia.collabsoft.configuration.CollabsoftProperties;
import fr.recia.collabsoft.configuration.bean.StorageProperties;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.springframework.core.io.PathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;
Expand All @@ -39,8 +40,12 @@ public ResourceService(CollabsoftProperties collabsoftProperties) {
this.storageProperties = collabsoftProperties.getStorage();
}

private String getFolderPath(Long fileId) {
return storageProperties.getLocation() + File.separator + fileId;
}

private String getFilePath(Long fileId, String resourceName) {
return storageProperties.getLocation() + File.separator + fileId + File.separator + resourceName;
return getFolderPath(fileId) + File.separator + resourceName;
}

public Resource getResource(Long fileId, String resourceName) {
Expand Down Expand Up @@ -83,4 +88,21 @@ public boolean deleteResource(Long fileId, String resourceName) {
return false;
}

public boolean deleteResources(Long fileId) {
final String path = getFolderPath(fileId);
File file = new File(path);

if (file.exists()) {
try {
FileUtils.deleteDirectory(file);
return true;
} catch (IOException e) {
log.error("Tried to delete the folder {} failed, track errors!", path);
return false;
}
}
log.error("Tried to delete the folder {} but it doesn't exist, track errors!", path);
return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@ public ResponseEntity<Object> putFile(@PathVariable Long id, @NonNull @RequestBo
@DeleteMapping(value = "/{id}")
public ResponseEntity<Object> deleteFile(@PathVariable Long id) {
final boolean isFile = fileService.deleteFile(id);
if (!isFile) return new ResponseEntity<>(HttpStatus.NOT_FOUND);
if (isFile) resourceService.deleteResources(id);
else return new ResponseEntity<>(HttpStatus.NOT_FOUND);

return new ResponseEntity<>(HttpStatus.OK);
}
Expand Down

0 comments on commit 0668c7d

Please sign in to comment.