Search in sources :

Example 1 with FileDeleteException

use of com.ncedu.fooddelivery.api.v1.errors.badrequest.FileDeleteException in project 2021-msk-food-delivery by netcracker-edu.

the class FileServiceImpl method delete.

@Override
public void delete(File file, User authedUser) {
    boolean isAdminOrOwner = checkAdminOrOwner(file, authedUser);
    if (!isAdminOrOwner) {
        log.error(authedUser.getEmail() + " not Admin and not Owner of the file " + file.getId().toString());
        throw new CustomAccessDeniedException();
    }
    try {
        fileRepo.delete(file);
        Path fullFilePath = createFullPathToFile(file.getId());
        Files.deleteIfExists(fullFilePath);
        Path fileParentDirPath = fullFilePath.getParent();
        log.debug("PARENT DIR PATH: " + fileParentDirPath + "\n");
        boolean isParentDirEmpty = checkParentDirEmpty(fileParentDirPath);
        if (isParentDirEmpty) {
            Files.delete(fileParentDirPath);
        }
    } catch (IOException e) {
        throw new FileDeleteException();
    }
}
Also used : Path(java.nio.file.Path) CustomAccessDeniedException(com.ncedu.fooddelivery.api.v1.errors.security.CustomAccessDeniedException) IOException(java.io.IOException) FileDeleteException(com.ncedu.fooddelivery.api.v1.errors.badrequest.FileDeleteException)

Aggregations

FileDeleteException (com.ncedu.fooddelivery.api.v1.errors.badrequest.FileDeleteException)1 CustomAccessDeniedException (com.ncedu.fooddelivery.api.v1.errors.security.CustomAccessDeniedException)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1