From a8ba7084778e782bb101f817a85cf5aabbdac49d Mon Sep 17 00:00:00 2001 From: rahairston Date: Mon, 21 Jul 2025 10:25:22 -0500 Subject: [PATCH] Adding minor code to Fix file permissions on Upload (#752) Adding permission changes to temp file to allow proper upload --- CONTRIBUTING.md | 2 +- .../service/upload/FileUploadService.java | 29 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index b4e021504..58ef1ae6e 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -147,7 +147,7 @@ git checkout -b fix/my-fix - Follow code conventions, keep PRs focused and scoped - Link the relevant issue in your PR - Test your changes -- Target the `master` branch when opening PRs +- Target the `develop` branch when opening PRs --- diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/service/upload/FileUploadService.java b/booklore-api/src/main/java/com/adityachandel/booklore/service/upload/FileUploadService.java index a8b649fc1..2b956c65a 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/service/upload/FileUploadService.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/service/upload/FileUploadService.java @@ -22,14 +22,22 @@ import com.adityachandel.booklore.util.FileUtils; import com.adityachandel.booklore.util.PathPatternResolver; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; + +import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import java.io.File; import java.io.IOException; +import java.nio.file.FileSystems; import java.nio.file.Files; +import java.nio.file.LinkOption; import java.nio.file.Path; import java.nio.file.Paths; +import java.nio.file.attribute.GroupPrincipal; +import java.nio.file.attribute.PosixFileAttributeView; +import java.nio.file.attribute.UserPrincipal; +import java.nio.file.attribute.UserPrincipalLookupService; import java.util.Objects; @RequiredArgsConstructor @@ -47,6 +55,12 @@ public class FileUploadService { private final EpubMetadataExtractor epubMetadataExtractor; private final MonitoringService monitoringService; + @Value("${PUID:0}") + private String userId; + + @Value("${GUID:0}") + private String groupId; + public Book uploadFile(MultipartFile file, long libraryId, long pathId) throws IOException { validateFile(file); @@ -70,6 +84,8 @@ public class FileUploadService { try { file.transferTo(tempPath); + + setTemporaryFileOwnership(tempPath); BookFileExtension fileExt = BookFileExtension.fromFileName(file.getOriginalFilename()).orElseThrow(() -> ApiError.INVALID_FILE_FORMAT.createException("Unsupported file extension")); @@ -138,6 +154,19 @@ public class FileUploadService { } } + private void setTemporaryFileOwnership(Path tempPath) throws IOException { + UserPrincipalLookupService lookupService = FileSystems.getDefault() + .getUserPrincipalLookupService(); + if (!userId.equals("0")) { + UserPrincipal user = lookupService.lookupPrincipalByName(userId); + Files.getFileAttributeView(tempPath, PosixFileAttributeView.class, LinkOption.NOFOLLOW_LINKS).setOwner(user); + } + if (!groupId.equals("0")) { + GroupPrincipal group = lookupService.lookupPrincipalByGroupName(groupId); + Files.getFileAttributeView(tempPath, PosixFileAttributeView.class, LinkOption.NOFOLLOW_LINKS).setGroup(group); + } + } + private Book processFile(String fileName, LibraryEntity libraryEntity, LibraryPathEntity libraryPathEntity, File storageFile, BookFileType fileType) { String subPath = FileUtils.getRelativeSubPath(libraryPathEntity.getPath(), storageFile.toPath());