Skip to content

Commit 973bec2

Browse files
authored
fix/OPS-403 : 공유 스페이스 썸네일 삭제 문제 (#185)
1 parent deab085 commit 973bec2

File tree

2 files changed

+24
-16
lines changed

2 files changed

+24
-16
lines changed

src/main/java/org/tuna/zoopzoop/backend/domain/datasource/service/DataSourceService.java

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ public void moveMany(List<Integer> ids, int targetFolderId) {
181181
public void hardDeleteOne(int dataSourceId) {
182182
DataSource ds = dataSourceRepository.findById(dataSourceId)
183183
.orElseThrow(() -> new NoResultException("존재하지 않는 자료입니다."));
184-
deleteOwnedImageIfAny(ds);
184+
// deleteOwnedImageIfAny(ds);
185185
dataSourceRepository.delete(ds);
186186
}
187187

@@ -190,7 +190,7 @@ public void hardDeleteMany(List<Integer> ids) {
190190
if (ids == null || ids.isEmpty()) return;
191191
List<DataSource> list = dataSourceRepository.findAllById(ids);
192192
if (list.size() != ids.size()) throw new NoResultException("존재하지 않는 자료 포함");
193-
for (DataSource ds : list) deleteOwnedImageIfAny(ds);
193+
// for (DataSource ds : list) deleteOwnedImageIfAny(ds);
194194
dataSourceRepository.deleteAll(list);
195195
}
196196

@@ -321,19 +321,15 @@ public String uploadThumbnailAndReturnFinalUrl(MultipartFile image, String key)
321321

322322
// ===== S3 삭제 관련 유틸 =====
323323
// 소유한 이미지가 있으면 S3에서 삭제
324-
private void deleteOwnedImageIfAny(DataSource ds) {
325-
String url = ds.getImageUrl();
326-
if (url == null || url.isBlank()) return;
327-
if (!isOurS3Url(url)) return;
328-
329-
String key = extractKeyFromUrl(url);
330-
if (key == null || key.isBlank()) return;
331-
332-
try {
333-
s3Service.delete(key);
334-
} catch (Exception ignore) {
335-
// 파일 삭제 실패로 전체 삭제를 롤백하지 않음
336-
// 필요하면 warn 로그 추가
324+
public void deleteIfOwnedByExactKey(String imageUrl, String expectedKey) {
325+
if (imageUrl == null || imageUrl.isBlank() || expectedKey == null || expectedKey.isBlank()) return;
326+
String key = extractKeyFromUrl(imageUrl);
327+
if (expectedKey.equals(key)) {
328+
try {
329+
s3Service.delete(key);
330+
} catch (Exception ignore) {
331+
// S3 삭제 실패가 전체 트랜잭션을 막지 않도록 무시
332+
}
337333
}
338334
}
339335

src/main/java/org/tuna/zoopzoop/backend/domain/datasource/service/PersonalDataSourceService.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.tuna.zoopzoop.backend.domain.datasource.dto.DataSourceSearchCondition;
1717
import org.tuna.zoopzoop.backend.domain.datasource.dto.DataSourceSearchItem;
1818
import org.tuna.zoopzoop.backend.domain.datasource.dto.UpdateOutcome;
19+
import org.tuna.zoopzoop.backend.domain.datasource.entity.DataSource;
1920
import org.tuna.zoopzoop.backend.domain.datasource.entity.Tag;
2021
import org.tuna.zoopzoop.backend.domain.datasource.repository.DataSourceRepository;
2122

@@ -79,9 +80,13 @@ public int create(int memberId, String sourceUrl, Integer folderIdOrZero, DataSo
7980
// hard delete
8081
@Transactional
8182
public int deleteOne(int memberId, int dataSourceId) {
82-
dataSourceRepository.findByIdAndMemberId(dataSourceId, memberId)
83+
DataSource ds = dataSourceRepository.findByIdAndMemberId(dataSourceId, memberId)
8384
.orElseThrow(() -> new NoResultException("존재하지 않는 자료입니다."));
8485

86+
// S3 삭제
87+
String expectedKey = domain.thumbnailKeyForPersonal(memberId, dataSourceId);
88+
domain.deleteIfOwnedByExactKey(ds.getImageUrl(), expectedKey);
89+
8590
domain.hardDeleteOne(dataSourceId);
8691
return dataSourceId;
8792
}
@@ -98,6 +103,13 @@ public void deleteMany(int memberId, List<Integer> ids) {
98103
missing.removeAll(new HashSet<>(existing));
99104
throw new NoResultException("존재하지 않거나 소유자가 다른 자료 ID 포함: " + missing);
100105
}
106+
// S3 삭제
107+
List<DataSource> list = dataSourceRepository.findAllById(ids);
108+
for (DataSource ds : list) {
109+
String expectedKey = domain.thumbnailKeyForPersonal(memberId, ds.getId());
110+
domain.deleteIfOwnedByExactKey(ds.getImageUrl(), expectedKey);
111+
}
112+
101113
domain.hardDeleteMany(ids);
102114
}
103115

0 commit comments

Comments
 (0)