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:
+1
-1
@@ -147,7 +147,7 @@ git checkout -b fix/my-fix
|
|||||||
- Follow code conventions, keep PRs focused and scoped
|
- Follow code conventions, keep PRs focused and scoped
|
||||||
- Link the relevant issue in your PR
|
- Link the relevant issue in your PR
|
||||||
- Test your changes
|
- Test your changes
|
||||||
- Target the `master` branch when opening PRs
|
- Target the `develop` branch when opening PRs
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|||||||
+29
@@ -22,14 +22,22 @@ import com.adityachandel.booklore.util.FileUtils;
|
|||||||
import com.adityachandel.booklore.util.PathPatternResolver;
|
import com.adityachandel.booklore.util.PathPatternResolver;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.nio.file.FileSystems;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.LinkOption;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.Paths;
|
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;
|
import java.util.Objects;
|
||||||
|
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
@@ -47,6 +55,12 @@ public class FileUploadService {
|
|||||||
private final EpubMetadataExtractor epubMetadataExtractor;
|
private final EpubMetadataExtractor epubMetadataExtractor;
|
||||||
private final MonitoringService monitoringService;
|
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 {
|
public Book uploadFile(MultipartFile file, long libraryId, long pathId) throws IOException {
|
||||||
validateFile(file);
|
validateFile(file);
|
||||||
|
|
||||||
@@ -71,6 +85,8 @@ public class FileUploadService {
|
|||||||
try {
|
try {
|
||||||
file.transferTo(tempPath);
|
file.transferTo(tempPath);
|
||||||
|
|
||||||
|
setTemporaryFileOwnership(tempPath);
|
||||||
|
|
||||||
BookFileExtension fileExt = BookFileExtension.fromFileName(file.getOriginalFilename()).orElseThrow(() -> ApiError.INVALID_FILE_FORMAT.createException("Unsupported file extension"));
|
BookFileExtension fileExt = BookFileExtension.fromFileName(file.getOriginalFilename()).orElseThrow(() -> ApiError.INVALID_FILE_FORMAT.createException("Unsupported file extension"));
|
||||||
|
|
||||||
BookMetadata metadata = extractMetadata(fileExt, tempPath.toFile());
|
BookMetadata metadata = extractMetadata(fileExt, tempPath.toFile());
|
||||||
@@ -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) {
|
private Book processFile(String fileName, LibraryEntity libraryEntity, LibraryPathEntity libraryPathEntity, File storageFile, BookFileType fileType) {
|
||||||
String subPath = FileUtils.getRelativeSubPath(libraryPathEntity.getPath(), storageFile.toPath());
|
String subPath = FileUtils.getRelativeSubPath(libraryPathEntity.getPath(), storageFile.toPath());
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user