This repository has been archived by the owner on Nov 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
57 additions
and
0 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
src/main/java/com/example/daemawiki/domain/info/service/DocumentImageUpload.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.example.daemawiki.domain.info.service; | ||
|
||
import com.example.daemawiki.domain.common.UserFilter; | ||
import com.example.daemawiki.domain.document.component.facade.DocumentFacade; | ||
import com.example.daemawiki.domain.document.model.DefaultDocument; | ||
import com.example.daemawiki.domain.file.model.File; | ||
import com.example.daemawiki.domain.file.model.type.FileType; | ||
import com.example.daemawiki.domain.revision.component.RevisionComponent; | ||
import com.example.daemawiki.domain.revision.dto.request.SaveRevisionHistoryRequest; | ||
import com.example.daemawiki.domain.revision.model.type.RevisionType; | ||
import com.example.daemawiki.domain.user.service.facade.UserFacade; | ||
import com.example.daemawiki.infra.s3.service.S3UploadObject; | ||
import org.springframework.http.codec.multipart.FilePart; | ||
import org.springframework.stereotype.Service; | ||
import reactor.core.publisher.Mono; | ||
|
||
@Service | ||
public class DocumentImageUpload { | ||
private final S3UploadObject s3UploadObject; | ||
private final UserFacade userFacade; | ||
private final DocumentFacade documentFacade; | ||
private final UserFilter userFilter; | ||
private final RevisionComponent revisionComponent; | ||
|
||
public DocumentImageUpload(S3UploadObject s3UploadObject, UserFacade userFacade, DocumentFacade documentFacade, UserFilter userFilter, RevisionComponent revisionComponent) { | ||
this.s3UploadObject = s3UploadObject; | ||
this.userFacade = userFacade; | ||
this.documentFacade = documentFacade; | ||
this.userFilter = userFilter; | ||
this.revisionComponent = revisionComponent; | ||
} | ||
|
||
public Mono<Void> execute(FilePart filePart, String documentId) { | ||
return userFacade.currentUser() | ||
.zipWith(documentFacade.findDocumentById(documentId)) | ||
.map(tuple -> { | ||
userFilter.userPermissionCheck(tuple.getT2(), tuple.getT1().getId()); | ||
return tuple.getT2(); | ||
}) | ||
.zipWith(s3UploadObject.uploadObject(filePart, FileType.DOCUMENT.toString())) | ||
.flatMap(tuple -> { | ||
DefaultDocument document = tuple.getT1(); | ||
File file = tuple.getT2(); | ||
|
||
document.getInfo().setDocumentImage(file); | ||
|
||
return documentFacade.saveDocument(document) | ||
.then(createRevision(document)); | ||
}); | ||
} | ||
|
||
private Mono<Void> createRevision(DefaultDocument document) { | ||
return revisionComponent.saveHistory(SaveRevisionHistoryRequest | ||
.create(RevisionType.UPDATE, document.getId(), document.getTitle())); | ||
} | ||
|
||
} |