Skip to content

Commit

Permalink
Merge pull request #98 from Team-INSERT/refactor/#96
Browse files Browse the repository at this point in the history
refactor(file): 파일 도메인 리팩토링
  • Loading branch information
NameIsUser06 authored May 2, 2024
2 parents 7e65bf5 + f6859e0 commit a6de4c0
Show file tree
Hide file tree
Showing 20 changed files with 164 additions and 259 deletions.
Original file line number Diff line number Diff line change
@@ -1,42 +1,45 @@
package com.project.bumawiki.global.s3.implement;
package com.project.bumawiki.domain.file.implementation;

import java.io.IOException;
import java.util.UUID;

import org.springframework.stereotype.Service;
import com.project.bumawiki.global.annotation.Implementation;
import com.project.bumawiki.global.error.exception.BumawikiException;

import com.project.bumawiki.global.error.exception.ErrorCode;

import org.springframework.web.multipart.MultipartFile;

import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.project.bumawiki.global.config.s3.S3Bucket;
import com.project.bumawiki.global.s3.exception.S3SaveException;
import com.project.bumawiki.global.config.file.r2.R2Bucket;

import lombok.RequiredArgsConstructor;

@Service
@Implementation
@RequiredArgsConstructor
public class ImageCreator {
private final S3Bucket s3Bucket;
public class FileCreator {
private final R2Bucket r2Bucket;
private final AmazonS3 amazonS3;

public String create(MultipartFile multipartFile) {
String fileName = createFileName(multipartFile);

try {
PutObjectRequest request = new PutObjectRequest(
s3Bucket.getS3Bucket(),
r2Bucket.getS3Bucket(),
fileName,
multipartFile.getInputStream(),
getMetadata(multipartFile)
);

amazonS3.putObject(request);
} catch (IOException e) {
throw new S3SaveException();
throw new BumawikiException(ErrorCode.S3_SAVE_EXCEPTION);
}

return s3Bucket.getReadUrl() + fileName;
return r2Bucket.getReadUrl() + fileName;
}

private String createFileName(MultipartFile multipartFile) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.project.bumawiki.domain.file.presentation;

import com.project.bumawiki.domain.file.presentation.dto.FileResponseDto;
import com.project.bumawiki.domain.file.presentation.dto.R2FileResponseDto;
import com.project.bumawiki.domain.file.service.CommandFileService;

import org.springframework.core.io.Resource;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;

import org.springframework.web.multipart.MultipartFile;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/file")
public class FileController {
private final CommandFileService commandFileService;

@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public R2FileResponseDto upload(@RequestPart("file") MultipartFile file) {
return new R2FileResponseDto(
commandFileService.uploadFile(file)
);
}

@GetMapping("/display/{docsName}/{fileName}")
public ResponseEntity<Resource> displayImage(
@PathVariable String fileName,
@PathVariable String docsName,
HttpServletRequest request
) {
FileResponseDto fileResponseDto = commandFileService.getFile(docsName, fileName, request);
return ResponseEntity.ok()
.contentType(MediaType.parseMediaType(fileResponseDto.contentType()))
.header("Content-Type", fileResponseDto.contentType())
.body(fileResponseDto.resource());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.project.bumawiki.domain.file.presentation.dto;

import org.springframework.core.io.Resource;

public record FileResponseDto(
Resource resource,
String contentType
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.project.bumawiki.domain.file.presentation.dto;


public record R2FileResponseDto(
String url
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.project.bumawiki.domain.file.service;

import com.project.bumawiki.domain.file.implementation.FileCreator;
import com.project.bumawiki.domain.file.presentation.dto.FileResponseDto;
import com.project.bumawiki.global.config.file.local.FileProperties;
import com.project.bumawiki.global.error.exception.BumawikiException;

import com.project.bumawiki.global.error.exception.ErrorCode;

import jakarta.servlet.http.HttpServletRequest;

import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import lombok.RequiredArgsConstructor;

import java.io.IOException;
import java.net.MalformedURLException;
import java.nio.file.Path;
import java.nio.file.Paths;

@Service
@RequiredArgsConstructor
public class CommandFileService {
private final FileCreator fileCreator;
private final FileProperties fileProperties;

public String uploadFile(MultipartFile file) {
return fileCreator.create(file);
}

public FileResponseDto getFile(String docsName, String fileName, HttpServletRequest request) {
Resource resource = loadFileAsResource(docsName, fileName);
String contentType = getContentType(request, resource);
return new FileResponseDto(resource, contentType);
}

private Resource loadFileAsResource(String docsName, String fileName) {
Path uploadPath = Paths.get(fileProperties.path(), docsName);
try {
Path filePath = uploadPath.resolve(fileName).normalize();
Resource resource = new UrlResource(filePath.toUri());
if (resource.exists()) {
return resource;
} else {
throw new BumawikiException(ErrorCode.IMAGE_NOT_FOUND_EXCEPTION);
}
} catch (MalformedURLException ex) {
throw new BumawikiException(ErrorCode.MALFORMED_URL);
}
}

private String getContentType(HttpServletRequest request, Resource resource) {
String contentType = "application/octet-stream";
try {
String mimeType = request.getServletContext().getMimeType(resource.getFile().getAbsolutePath());
if (mimeType != null) {
contentType = mimeType;
}
}
catch (IOException ignored) {
}
return contentType;
}

}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.project.bumawiki.global.config.file.local;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "file")
public record FileProperties(
String path
) {
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.project.bumawiki.global.config.s3;
package com.project.bumawiki.global.config.file.r2;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
Expand All @@ -7,7 +7,7 @@

@Getter
@Configuration
public class S3Bucket {
public class R2Bucket {
@Value("${aws.s3.bucket}")
private String s3Bucket;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.project.bumawiki.global.config.s3;
package com.project.bumawiki.global.config.file.r2;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
Expand All @@ -10,7 +10,7 @@
import com.amazonaws.services.s3.AmazonS3ClientBuilder;

@Configuration
class S3Config {
class R2Config {

@Value("${aws.s3.access-key}")
private String accessKey;
Expand Down
Loading

0 comments on commit a6de4c0

Please sign in to comment.