Skip to content

Commit

Permalink
BE: [Fix] 부정행위 영상 저장 후 DB에 링크 저장 #6, #26
Browse files Browse the repository at this point in the history
  • Loading branch information
JongbeomLee623 committed Dec 2, 2024
1 parent c4a5a8c commit 30697d0
Showing 1 changed file with 22 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.fortune.eyesee.entity.VideoRecording;
import com.fortune.eyesee.repository.VideoRecordingRepository;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
Expand All @@ -17,9 +19,11 @@ public class VideoService {
private String bucketName;

private final AmazonS3 amazonS3;
private final VideoRecordingRepository videoRecordingRepository;

public VideoService(AmazonS3 amazonS3) {
public VideoService(AmazonS3 amazonS3, VideoRecordingRepository videoRecordingRepository) {
this.amazonS3 = amazonS3;
this.videoRecordingRepository = videoRecordingRepository;
}

public String saveVideo(Integer userId, LocalDateTime startOffset, LocalDateTime endOffset, MultipartFile videoFile) throws IOException {
Expand All @@ -34,14 +38,29 @@ public String saveVideo(Integer userId, LocalDateTime startOffset, LocalDateTime

amazonS3.putObject(bucketName, key, videoFile.getInputStream(), metadata);

// S3 URL 반환
return amazonS3.getUrl(bucketName, key).toString();
String filePath = amazonS3.getUrl(bucketName, key).toString();

// 비디오 정보를 DB에 저장
saveVideoRecording(userId, startOffset, endOffset, filePath);

return filePath;

} catch (Exception e) {
throw new IOException("Failed to upload video to S3", e);
}
}

private void saveVideoRecording(Integer userId, LocalDateTime startTime, LocalDateTime endTime, String filePath) {
VideoRecording videoRecording = new VideoRecording();
videoRecording.setUserId(userId);
videoRecording.setStartTime(startTime);
videoRecording.setEndTime(endTime);
videoRecording.setFilePath(filePath);

videoRecordingRepository.save(videoRecording);
}


private String generateFileName(Integer userId) {
return userId + "_" + UUID.randomUUID() + ".webm";
}
Expand Down

0 comments on commit 30697d0

Please sign in to comment.