mirror of
https://github.com/adityachandelgit/BookLore.git
synced 2026-02-11 08:49:55 -06:00
Adding minor code to Fix file permissions on Upload (#752)
Adding permission changes to temp file to allow proper upload
This commit is contained in:
@@ -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
|
||||
|
||||
---
|
||||
|
||||
|
||||
@@ -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());
|
||||
|
||||
|
||||
Reference in New Issue
Block a user