Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package withoutjjin.withoutjjin.domain.document.entity;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.PrePersist;
import jakarta.persistence.Table;
import jakarta.persistence.UniqueConstraint;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;
import java.time.ZoneId;

// DocumentVersion과 StoreFile을 삭제한 뒤에도 S3 삭제를 재시도할 수 있도록 키를 보관한다.
@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Table(name = "s3_object_deletion_task", uniqueConstraints = @UniqueConstraint(
name = "uk_s3_object_deletion_task_object_key", columnNames = "object_key"
))
public class S3ObjectDeletionTask {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(name = "object_key", nullable = false, updatable = false, length = 512)
private String objectKey;

@Column(name = "created_at", nullable = false, updatable = false)
private LocalDateTime createdAt;

public S3ObjectDeletionTask(String objectKey) {
if (objectKey == null || objectKey.isBlank() || objectKey.length() > 512) {
throw new IllegalArgumentException("S3 삭제 작업의 저장 키는 비어 있지 않고 512자 이하여야 합니다.");
}
this.objectKey = objectKey;
}

@PrePersist
private void onCreate() {
this.createdAt = LocalDateTime.now(ZoneId.of("Asia/Seoul"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ public interface DocumentConversionJobRepository extends JpaRepository<DocumentC

Optional<DocumentConversionJob> findByDocumentVersionId(Long documentVersionId);

@Lock(LockModeType.PESSIMISTIC_WRITE)
@Query("select job from DocumentConversionJob job where job.documentVersion.id = :documentVersionId")
Optional<DocumentConversionJob> findByDocumentVersionIdForUpdate(
@Param("documentVersionId") Long documentVersionId);

Optional<DocumentConversionJob> findByDocumentUploadId(Long documentUploadId);

@Query("""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@

public interface DocumentUploadRepository extends JpaRepository<DocumentUpload, Long> {

@Query("""
select count(upload) > 0 from DocumentUpload upload
where upload.id <> :uploadId
and (upload.originalFile.id = :fileId or upload.reviewPdfFile.id = :fileId)
""")
boolean existsOtherFileReference(@Param("fileId") Long fileId, @Param("uploadId") Long uploadId);

@Lock(LockModeType.PESSIMISTIC_WRITE)
@Query("select upload from DocumentUpload upload where upload.id = :uploadId")
Optional<DocumentUpload> findByIdForUpdate(@Param("uploadId") Long uploadId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public interface DocumentVersionRepository extends JpaRepository<DocumentVersion

Optional<DocumentVersion> findByIdAndDocumentId(Long documentVersionId, Long documentId);

boolean existsByStorageKeyAndIdNot(String storageKey, Long documentVersionId);

@Query("""
select new withoutjjin.withoutjjin.domain.document.dto.DocumentVersionListResponse(
dv.id, dv.document.id, dv.versionNumber, dv.originalFileName, dv.fileSize,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package withoutjjin.withoutjjin.domain.document.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import withoutjjin.withoutjjin.domain.document.entity.S3ObjectDeletionTask;

public interface S3ObjectDeletionTaskRepository extends JpaRepository<S3ObjectDeletionTask, Long> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
package withoutjjin.withoutjjin.domain.document.service;

import jakarta.persistence.EntityManager;
import jakarta.persistence.LockModeType;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.server.ResponseStatusException;
import withoutjjin.withoutjjin.domain.document.entity.Document;
import withoutjjin.withoutjjin.domain.document.entity.DocumentConversionJob;
import withoutjjin.withoutjjin.domain.document.entity.DocumentConversionJobStatus;
import withoutjjin.withoutjjin.domain.document.entity.DocumentUpload;
import withoutjjin.withoutjjin.domain.document.entity.DocumentUploadStatus;
import withoutjjin.withoutjjin.domain.document.entity.DocumentVersion;
import withoutjjin.withoutjjin.domain.document.entity.S3ObjectDeletionTask;
import withoutjjin.withoutjjin.domain.document.entity.StoreFile;
import withoutjjin.withoutjjin.domain.document.repository.DocumentConversionJobRepository;
import withoutjjin.withoutjjin.domain.document.repository.DocumentRepository;
import withoutjjin.withoutjjin.domain.document.repository.DocumentUploadRepository;
import withoutjjin.withoutjjin.domain.document.repository.DocumentVersionRepository;
import withoutjjin.withoutjjin.domain.document.repository.S3ObjectDeletionTaskRepository;
import withoutjjin.withoutjjin.domain.document.repository.StoreFileRepository;
import withoutjjin.withoutjjin.domain.feedback.repository.DocumentFeedbackRepository;
import withoutjjin.withoutjjin.domain.user.entity.AppUser;
import withoutjjin.withoutjjin.domain.user.service.AppUserService;
import withoutjjin.withoutjjin.domain.workspace.service.WorkspaceService;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
@RequiredArgsConstructor
public class DocumentVersionDeletionService {

private static final String DOCX_MEDIA_TYPE =
"application/vnd.openxmlformats-officedocument.wordprocessingml.document";

private final DocumentRepository documents;
private final DocumentVersionRepository versions;
private final DocumentConversionJobRepository conversionJobs;
private final DocumentUploadRepository uploads;
private final DocumentFeedbackRepository feedbacks;
private final StoreFileRepository files;
private final S3ObjectDeletionTaskRepository deletionTasks;
private final AppUserService appUserService;
private final WorkspaceService workspaceService;
private final EntityManager entityManager;

@Transactional(isolation = Isolation.READ_COMMITTED)
public void deleteVersion(Long workspaceId, Long documentId, Long documentVersionId,
String issuer, String subject) {
requirePositiveId(workspaceId, "workspaceId");
requirePositiveId(documentId, "documentId");
requirePositiveId(documentVersionId, "documentVersionId");
AppUser requester = appUserService.getByIssuerAndSubject(issuer, subject);
workspaceService.getWorkSpaceForMember(workspaceId, requester);

// 새 버전 등록과 같은 Document 잠금을 사용한다. revision은 발급한 번호로 유지한다.
Document document = documents.findByIdForUpdate(documentId)
.filter(found -> Objects.equals(found.getWorkspace().getId(), workspaceId))
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND,
"워크스페이스에서 Document를 찾을 수 없습니다."));
DocumentVersion version = versions.findByIdAndDocumentId(documentVersionId, document.getId())
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND,
"Document에서 DocumentVersion을 찾을 수 없습니다."));
if (!Objects.equals(version.getUploadedBy().getId(), requester.getId())) {
throw new ResponseStatusException(HttpStatus.FORBIDDEN,
"DocumentVersion의 업로더만 삭제할 수 있습니다.");
}

// 변환 작업자가 먼저 PROCESSING으로 바꿨다면 삭제를 거부한다.
DocumentConversionJob job = conversionJobs.findByDocumentVersionIdForUpdate(documentVersionId)
.orElseThrow(DocumentVersionDeletionService::inconsistentFiles);
entityManager.refresh(job, LockModeType.PESSIMISTIC_WRITE);
if (job.getStatus() == DocumentConversionJobStatus.PROCESSING) {
throw new ResponseStatusException(HttpStatus.CONFLICT,
"PDF 변환 중인 DocumentVersion은 변환이 끝난 뒤 삭제할 수 있습니다.");
}
DocumentUpload upload = uploads.findByIdForUpdate(job.getDocumentUpload().getId())
.orElseThrow(DocumentVersionDeletionService::inconsistentFiles);
entityManager.refresh(upload, LockModeType.PESSIMISTIC_WRITE);
List<StoreFile> targetFiles = validateFiles(workspaceId, version, upload, job);
List<S3ObjectDeletionTask> tasks = targetFiles.stream()
.map(file -> new S3ObjectDeletionTask(file.getObjectKey()))
.toList();

// FK를 참조하는 행부터 삭제하며 어느 단계에서 실패해도 전체를 롤백한다.
feedbacks.deleteAllForDocumentVersion(documentVersionId);
conversionJobs.delete(job);
conversionJobs.flush();
uploads.delete(upload);
uploads.flush();
versions.delete(version);
versions.flush();
files.deleteAll(targetFiles);
files.flush();

// 외부 S3 요청은 하지 않는다. DB 삭제와 S3 삭제 작업 등록을 함께 커밋한다.
deletionTasks.saveAllAndFlush(tasks);
}

private List<StoreFile> validateFiles(Long workspaceId, DocumentVersion version,
DocumentUpload upload, DocumentConversionJob job) {
StoreFile original = upload.getOriginalFile();
StoreFile pdf = upload.getReviewPdfFile();
if (upload.getStatus() != DocumentUploadStatus.COMPLETED || upload.getDocument() == null
|| !Objects.equals(upload.getDocument().getId(), version.getDocument().getId())
|| !Objects.equals(upload.getWorkspace().getId(), workspaceId)
|| !Objects.equals(upload.getCreatedBy().getId(), version.getUploadedBy().getId())
|| original == null || !Objects.equals(original.getObjectKey(), version.getStorageKey())
|| !DOCX_MEDIA_TYPE.equals(original.getMediaType())
|| (job.getStatus() == DocumentConversionJobStatus.COMPLETED && pdf == null)) {
throw inconsistentFiles();
}

validateFile(workspaceId, version, upload, original, "uploads/");
List<StoreFile> targetFiles = new ArrayList<>();
targetFiles.add(original);
if (pdf != null) {
if (!"application/pdf".equals(pdf.getMediaType())
|| Objects.equals(original.getId(), pdf.getId())) {
throw inconsistentFiles();
}
validateFile(workspaceId, version, upload, pdf, "review-pdfs/");
targetFiles.add(pdf);
}
return targetFiles;
}

private void validateFile(Long workspaceId, DocumentVersion version, DocumentUpload upload,
StoreFile file, String folder) {
if (!Objects.equals(file.getWorkspace().getId(), workspaceId)
|| !file.getObjectKey().startsWith("workspaces/" + workspaceId + "/" + folder)
|| uploads.existsOtherFileReference(file.getId(), upload.getId())
|| versions.existsByStorageKeyAndIdNot(file.getObjectKey(), version.getId())) {
throw inconsistentFiles();
}
}

private static void requirePositiveId(Long id, String name) {
if (id == null || id <= 0) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, name + "는 양수여야 합니다.");
}
}

private static ResponseStatusException inconsistentFiles() {
return new ResponseStatusException(HttpStatus.CONFLICT,
"DocumentVersion의 DocumentUpload 또는 StoreFile 연결을 확인할 수 없습니다.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package withoutjjin.withoutjjin.domain.document.storage;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.DeleteObjectRequest;

@Component
public class S3ObjectDeleter {

private final S3Client s3Client;
private final String bucket;

public S3ObjectDeleter(
S3Client s3Client,
@Value("${aws.s3.bucket}") String bucket
) {
if (bucket == null || bucket.isBlank()) {
throw new IllegalStateException("S3 버킷 이름을 설정해야 합니다.");
}
this.s3Client = s3Client;
this.bucket = bucket;
}

// 권한 검사 후 서버에서 조회한 StoreFile.objectKey를 전달한다.
public void delete(String objectKey) {
if (objectKey == null || objectKey.isBlank()) {
throw new IllegalArgumentException("삭제할 파일의 S3 저장 키가 필요합니다.");
}

DeleteObjectRequest request = DeleteObjectRequest.builder()
.bucket(bucket)
.key(objectKey)
.build();

// S3 오류는 호출한 쪽으로 전달하여 삭제 완료로 처리하지 않는다.
s3Client.deleteObject(request);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package withoutjjin.withoutjjin.domain.feedback.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import withoutjjin.withoutjjin.domain.feedback.entity.DocumentFeedback;
Expand All @@ -9,6 +10,10 @@

public interface DocumentFeedbackRepository extends JpaRepository<DocumentFeedback, Long> {

@Modifying(flushAutomatically = true)
@Query("delete from DocumentFeedback feedback where feedback.documentVersion.id = :documentVersionId")
int deleteAllForDocumentVersion(@Param("documentVersionId") Long documentVersionId);

@Query("""
select feedback from DocumentFeedback feedback
join fetch feedback.createdBy
Expand Down
Loading
Loading