Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BE: [Fix] 부정행위 영상 저장 후 DB에 링크 저장 #6, #37 #87

Merged
merged 1 commit into from
Dec 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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