Skip to content

Commit

Permalink
refactor: 리눅스 환경에서 안전하게 권한 설정 가능하도록 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
limehee committed Jul 19, 2024
1 parent e54ff69 commit 7f984ba
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -180,23 +180,32 @@ private void setFilePermissions(File file, String savePath, String extension) th
ImageCompressionUtil.compressImage(savePath, imageQuality);
}
if (os.contains("win")) {
boolean isReadOnly = file.setReadOnly();
if (!isReadOnly) {
boolean readOnly = file.setReadOnly();
if (!readOnly) {
log.error("Failed to set file read-only: {}", savePath);
}
} else {
Runtime.getRuntime().exec("chmod 400 " + savePath);
setFilePermissionsUnix(savePath);
}
} catch (IOException e) {
throw new FileUploadFailException("파일 저장 실패", e);
} catch (Exception e) {
throw new FileUploadFailException("Failed to upload file: " + savePath, e);
}
}

private void setFilePermissionsUnix(String filePath) throws IOException, InterruptedException {
ProcessBuilder processBuilder = new ProcessBuilder("chmod", "400", filePath);
Process process = processBuilder.start();
int exitCode = process.waitFor();
if (exitCode != 0) {
log.error("Failed to set file permissions for: {}", filePath);
}
}

public void deleteFile(String savedPath) {
File fileToDelete = new File(savedPath);
boolean deleted = fileToDelete.delete();
if (!deleted) {
log.info("Failed to delete file: {}", savedPath);
log.error("Failed to delete file: {}", savedPath);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ public class UploadedFileService {
private final UploadFileRepository uploadFileRepository;

public UploadedFile saveUploadedFile(UploadedFile uploadedFile) {
log.info("UploadedFileService.saveUploadedFile - uploadedFile: {}", uploadedFile.getUploader());
return uploadFileRepository.save(uploadedFile);
}

Expand Down

0 comments on commit 7f984ba

Please sign in to comment.