Skip to content

Commit 4b97df3

Browse files
authored
Merge pull request #85 from TU-NEBULA/fix/SCRUM-214
SCRUM 214 : html_file S3 버킷 공유 문제 해결
2 parents 5a936ef + baf344d commit 4b97df3

3 files changed

Lines changed: 35 additions & 6 deletions

File tree

src/main/java/com/team_nebula/nebula/domain/star/service/StarCommandServiceImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public CreateStarResponseDTO createFirstStar(Long userId, MultipartFile htmlFile
5555
.orElseThrow(() -> new GeneralException(ErrorStatus._USER_NOT_FOUND));
5656

5757
String sanitizedTitle = s3Service.sanitizeTitleForS3(title);
58-
String htmlFileKey = s3Service.saveHtmlFile(htmlFile, sanitizedTitle);
58+
String htmlFileKey = s3Service.saveHtmlFile(htmlFile, sanitizedTitle, userNode.getUserId());
5959

6060
Star star = Star.builder()
6161
.title(title)
@@ -215,7 +215,7 @@ public DeleteStarResponseDTO cancelStar(UUID starId){
215215

216216
@Override
217217
public AddBookMarkResponseDTO addBookMark(Long userId, MultipartFile htmlFile, String title, String siteUrl){
218-
String htmlFileKey = s3Service.saveHtmlFile(htmlFile, title);
218+
String htmlFileKey = s3Service.saveHtmlFile(htmlFile, title, userId);
219219

220220
Favicon favicon = faviconService.getOrCreateFavicon(siteUrl);
221221

src/main/java/com/team_nebula/nebula/global/image/S3Service.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.amazonaws.services.s3.model.PutObjectRequest;
88
import com.team_nebula.nebula.global.apipayload.code.status.ErrorStatus;
99
import com.team_nebula.nebula.global.apipayload.exception.GeneralException;
10+
import com.team_nebula.nebula.global.util.HashUtil;
1011
import lombok.RequiredArgsConstructor;
1112
import lombok.extern.slf4j.Slf4j;
1213
import org.springframework.beans.factory.annotation.Value;
@@ -40,20 +41,21 @@ public String saveThumbnail(MultipartFile thumbnailImage, String dataInfo) {
4041
return uploadThumbnailToS3(thumbnailImage, THUMBNAIL_DIR, dataInfo);
4142
}
4243

43-
public String saveHtmlFile(MultipartFile htmlFile, String dataInfo) {
44-
return uploadHtmlToS3(htmlFile, HTML_FILE_DIR, dataInfo);
44+
public String saveHtmlFile(MultipartFile htmlFile, String dataInfo, Long userId) {
45+
return uploadHtmlToS3(htmlFile, HTML_FILE_DIR, dataInfo, userId);
4546
}
4647

4748
public String saveFavicon(File faviconFile, String domain) {
4849
String fileName = FAVICON_FILE_DIR + domain + ".png";
4950
return uploadFileToS3(faviconFile, fileName);
5051
}
5152

52-
private String uploadHtmlToS3(MultipartFile file, String dirName, String dataInfo) {
53+
private String uploadHtmlToS3(MultipartFile file, String dirName, String dataInfo, Long userId) {
5354
File uploadFile = convert(file)
5455
.orElseThrow(() -> new GeneralException(ErrorStatus._MULTIPARTFILE_CONVERT_FAIL));
56+
String hashUserId = HashUtil.hashUserId(userId);
5557

56-
String fileName = dirName + dataInfo + "/" + UUID.randomUUID() + "_" + file.getOriginalFilename();
58+
String fileName = dirName + hashUserId + "/" + dataInfo + "/" + UUID.randomUUID() + "_" + file.getOriginalFilename();
5759

5860
putHtmlS3(uploadFile, fileName);
5961
removeNewFile(uploadFile);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.team_nebula.nebula.global.util;
2+
3+
4+
import java.security.MessageDigest;
5+
import java.security.NoSuchAlgorithmException;
6+
7+
public class HashUtil {
8+
public static String hashUserId(Long userId){
9+
try {
10+
MessageDigest digest = MessageDigest.getInstance("SHA-256");
11+
byte[] encodedHash = digest.digest(userId.toString().getBytes());
12+
return bytesToHex(encodedHash).substring(0, 16);
13+
} catch (NoSuchAlgorithmException e) {
14+
throw new RuntimeException("UserId Hash Algorithm failed to calculate hash", e);
15+
}
16+
}
17+
18+
private static String bytesToHex(byte[] hash) {
19+
StringBuilder hexString = new StringBuilder(2 * hash.length);
20+
for (byte b : hash) {
21+
String hex = Integer.toHexString(0xff & b);
22+
if (hex.length() == 1) hexString.append('0');
23+
hexString.append(hex);
24+
}
25+
return hexString.toString();
26+
}
27+
}

0 commit comments

Comments
 (0)