-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# 구현 기능 - 파일 공유 링크 생성 / 다운로드 - 링크 일괄 삭제 ## 파일 공유 링크 생성 - UUID.randomUUID 기반 링크 생성 - 링크 중복 검사 ## 파일 공유 다운로드 - 링크 유효 기간은 3시간 - 유효 기간 내 요청만 다운로드 가능 - 파일 공유 이후 원본 파일 삭제 발생 시 다운 불가능 - 스케줄러를 통해 유효하지 않은 링크 삭제 ( 5분 단위)
- Loading branch information
1 parent
0415c16
commit 9919fac
Showing
19 changed files
with
549 additions
and
132 deletions.
There are no files selected for viewing
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
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
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
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
28 changes: 28 additions & 0 deletions
28
src/main/java/com/c4cometrue/mystorage/fileshare/DeleteLinkScheduler.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,28 @@ | ||
package com.c4cometrue.mystorage.fileshare; | ||
|
||
import java.time.ZoneOffset; | ||
import java.time.ZonedDateTime; | ||
import java.util.List; | ||
|
||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.stereotype.Component; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class DeleteLinkScheduler { | ||
private static final int DELETE_SIZE = 100; | ||
|
||
private final FileShareRepository fileShareRepository; | ||
|
||
@Scheduled(cron = "*/5 * * * * ?") | ||
public void deleteExpiredLinks() { | ||
ZonedDateTime expirationTime = ZonedDateTime.now(ZoneOffset.UTC).minusHours(3); | ||
List<Long> expirationLinkIds; | ||
|
||
do { | ||
expirationLinkIds = fileShareRepository.deleteByExpirations(expirationTime, DELETE_SIZE); | ||
} while (expirationLinkIds.size() == DELETE_SIZE); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/main/java/com/c4cometrue/mystorage/fileshare/FileShare.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,46 @@ | ||
package com.c4cometrue.mystorage.fileshare; | ||
|
||
import java.time.ZoneOffset; | ||
import java.time.ZonedDateTime; | ||
|
||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.Index; | ||
import jakarta.persistence.PrePersist; | ||
import jakarta.persistence.Table; | ||
import lombok.AccessLevel; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Entity | ||
@Getter | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@Table(name = "file_share", indexes = { | ||
@Index(name = "idx_share_link", columnList = "shareLink"), | ||
@Index(name = "idx_share_created_at", columnList = "createAt") | ||
}) | ||
public class FileShare { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
private Long fileId; | ||
@Column(nullable = false, unique = true) | ||
private String shareLink; | ||
@Column(updatable = false) | ||
private ZonedDateTime createdAt; | ||
|
||
@Builder | ||
public FileShare(Long fileId, String shareLink) { | ||
this.fileId = fileId; | ||
this.shareLink = shareLink; | ||
} | ||
|
||
@PrePersist | ||
public void prePersist() { | ||
createdAt = ZonedDateTime.now(ZoneOffset.UTC); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/main/java/com/c4cometrue/mystorage/fileshare/FileShareController.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,36 @@ | ||
package com.c4cometrue.mystorage.fileshare; | ||
|
||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.ModelAttribute; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import com.c4cometrue.mystorage.fileshare.dto.CreateShareFileReq; | ||
import com.c4cometrue.mystorage.fileshare.dto.CreateShareFileRes; | ||
import com.c4cometrue.mystorage.fileshare.dto.DownloadShareFileReq; | ||
import com.c4cometrue.mystorage.fileshare.dto.DownloadShareFileRes; | ||
|
||
import jakarta.validation.Valid; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@RestController | ||
@RequestMapping("/files") | ||
@RequiredArgsConstructor | ||
public class FileShareController { | ||
private final FileShareService fileShareService; | ||
|
||
@PostMapping("/share") | ||
public ResponseEntity<CreateShareFileRes> createShareFile(@Valid @RequestBody CreateShareFileReq req) { | ||
CreateShareFileRes res = fileShareService.createShareFileLink(req.fileId(), req.userId()); | ||
return ResponseEntity.ok(res); | ||
} | ||
|
||
@GetMapping("/share/download") | ||
public ResponseEntity<DownloadShareFileRes> downloadShareFile(@Valid @ModelAttribute DownloadShareFileReq req) { | ||
DownloadShareFileRes res = fileShareService.downloadShareFile(req.shareLink(), req.userPath()); | ||
return ResponseEntity.ok(res); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/com/c4cometrue/mystorage/fileshare/FileShareRepository.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,21 @@ | ||
package com.c4cometrue.mystorage.fileshare; | ||
|
||
import java.time.ZonedDateTime; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Query; | ||
import org.springframework.data.repository.query.Param; | ||
|
||
public interface FileShareRepository extends JpaRepository<FileShare, Long> { | ||
boolean existsByShareLink(String shareLink); | ||
|
||
Optional<FileShare> findByShareLink(String shareLink); | ||
|
||
@Query(value = "DELETE FROM FileShare fs WHERE fs.createdAt < :expirationTime LIMIT :limit", nativeQuery = true) | ||
List<Long> deleteByExpirations(@Param("expirationTime") ZonedDateTime expirationTime, @Param("limit") int limit); | ||
|
||
@Query("DELETE FROM FileShare fs WHERE fs.id IN :ids") | ||
void deleteByIds(@Param("ids") List<Long> ids); | ||
} |
Oops, something went wrong.