-
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.
Merge pull request #140 from HGU-WALAB/HISTUDY-139
Feat: 스터디 보고서 이미지 업로드 (#139)
- Loading branch information
Showing
7 changed files
with
250 additions
and
21 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
7 changes: 7 additions & 0 deletions
7
src/main/java/edu/handong/csee/histudy/exception/FileTransferException.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,7 @@ | ||
package edu.handong.csee.histudy.exception; | ||
|
||
public class FileTransferException extends RuntimeException { | ||
public FileTransferException() { | ||
super("이미지를 저장하는데 실패했습니다."); | ||
} | ||
} |
142 changes: 142 additions & 0 deletions
142
src/main/java/edu/handong/csee/histudy/service/ImageService.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,142 @@ | ||
package edu.handong.csee.histudy.service; | ||
|
||
import edu.handong.csee.histudy.domain.Image; | ||
import edu.handong.csee.histudy.exception.FileTransferException; | ||
import edu.handong.csee.histudy.exception.ReportNotFoundException; | ||
import edu.handong.csee.histudy.repository.GroupReportRepository; | ||
import edu.handong.csee.histudy.util.Utils; | ||
import org.apache.commons.io.IOUtils; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.core.io.Resource; | ||
import org.springframework.core.io.UrlResource; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
|
||
import static org.springframework.util.ResourceUtils.isUrl; | ||
|
||
@Service | ||
@Transactional | ||
public class ImageService { | ||
|
||
@Value("${custom.resource.location}") | ||
private String imageBaseLocation; | ||
private final GroupReportRepository groupReportRepository; | ||
|
||
public ImageService(GroupReportRepository groupReportRepository) { | ||
this.groupReportRepository = groupReportRepository; | ||
} | ||
|
||
public String getImagePaths(MultipartFile imageAsFormData, Integer tag, Optional<Long> reportIdOr) { | ||
if (reportIdOr.isPresent()) { | ||
Long id = reportIdOr.get(); | ||
Optional<String> sameResource = getSameContent(imageAsFormData, id); | ||
|
||
if (sameResource.isPresent()) { | ||
return sameResource.get(); | ||
} | ||
} | ||
int year = Utils.getCurrentYear(); | ||
int semester = Utils.getCurrentSemester(); | ||
String formattedDateTime = Utils.getCurrentFormattedDateTime("yyyyMMdd_HHmmss"); | ||
|
||
String originalName = Objects.requireNonNullElse(imageAsFormData.getOriginalFilename(), ".jpg"); | ||
String extension = originalName.substring(originalName.lastIndexOf(".")); | ||
|
||
// yyyy-{1|2}-group{%02d}-report_{yyyyMMdd}_{HHmmss}.{extension} | ||
// e.g. 2023-2-group1-report_20230923_123456.jpg | ||
String pathname = String.format("%d-%d-group%02d-report_%s%s", | ||
year, | ||
semester, | ||
tag, | ||
formattedDateTime, | ||
extension); | ||
return saveImage(imageAsFormData, pathname); | ||
} | ||
|
||
private String saveImage( | ||
MultipartFile image, | ||
String pathname) { | ||
try { | ||
File file = new File(imageBaseLocation + File.separator + pathname); | ||
File dir = file.getParentFile(); | ||
|
||
if (!dir.exists()) { | ||
dir.mkdirs(); | ||
} | ||
image.transferTo(file); | ||
return pathname; | ||
} catch (IOException e) { | ||
throw new FileTransferException(); | ||
} | ||
} | ||
|
||
private Optional<String> getSameContent(MultipartFile src, Long reportId) { | ||
List<String> targetPaths = groupReportRepository.findById(reportId) | ||
.orElseThrow(ReportNotFoundException::new) | ||
.getImages() | ||
.stream() | ||
.map(Image::getPath) | ||
.toList(); | ||
|
||
return targetPaths.stream() | ||
.filter(path -> { | ||
try { | ||
return (isUrl(path)) | ||
? contentMatches(src, new URL(path)) | ||
: contentMatches(src, Path.of(path)); | ||
} catch (MalformedURLException e) { | ||
throw new RuntimeException(e); | ||
} | ||
}).findAny(); | ||
} | ||
|
||
private boolean contentMatches(MultipartFile src, Path targetPath) { | ||
try { | ||
byte[] targetContent = Files.readAllBytes(targetPath); | ||
return contentMatches(src.getBytes(), targetContent); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
private boolean contentMatches(MultipartFile src, URL targetPath) { | ||
try (InputStream in = targetPath.openStream()) { | ||
byte[] targetContent = IOUtils.toByteArray(in); | ||
return contentMatches(src.getBytes(), targetContent); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
private boolean contentMatches(byte[] sourceContent, byte[] targetContent) { | ||
return Arrays.equals(sourceContent, targetContent); | ||
} | ||
|
||
public Resource fetchImage(String imageName) { | ||
try { | ||
Path path = Paths.get(imageBaseLocation + imageName); | ||
Resource resource = new UrlResource(path.toUri()); | ||
|
||
if (resource.exists() && resource.isReadable()) { | ||
return resource; | ||
} | ||
throw new RuntimeException(); | ||
} catch (MalformedURLException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package edu.handong.csee.histudy.util; | ||
|
||
import java.time.LocalDate; | ||
import java.time.LocalDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
|
||
public class Utils { | ||
|
||
public static int getCurrentSemester() { | ||
int month = LocalDate.now().getMonthValue(); | ||
return (month >= 3 && month <= 8) ? 1 : 2; | ||
} | ||
|
||
public static int getCurrentYear() { | ||
return LocalDate.now().getYear(); | ||
} | ||
|
||
public static String getCurrentFormattedDateTime(String pattern) { | ||
return LocalDateTime.now() | ||
.format(DateTimeFormatter.ofPattern(pattern)); | ||
} | ||
} |