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

fix: 이미지 해결중 #67

Merged
merged 1 commit into from
Aug 24, 2024
Merged
Show file tree
Hide file tree
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
37 changes: 17 additions & 20 deletions src/main/java/com/wefood/back/farm/controller/FarmController.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
Expand All @@ -45,33 +46,29 @@ public class FarmController {
private final static String DIR_NAME = "farm";

@ResponseStatus(HttpStatus.CREATED)
@PostMapping
@PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public void uploadImages(
@Valid @ModelAttribute UploadImageRequestDto requestDto,
BindingResult result) {
if (result.hasErrors()) {
throw new InvalidRequestException(result);
}

try {
storageService.saveImages(requestDto, DIR_NAME);
} catch (IOException e) {
throw new FileUploadException("An error occurred while uploading files.", e);
}
}

@ResponseStatus(HttpStatus.CREATED)
@PostMapping("/upload-part")
public void uploadImages(
@RequestPart("files") MultipartFile[] files, @RequestParam("id") Long id) {
@RequestParam("id") Long id,
@RequestParam("files") List<MultipartFile> files) {

UploadImageRequestDto uploadImageRequestDto = new UploadImageRequestDto(Arrays.stream(files).toList(), id);
try {
storageService.saveImages(uploadImageRequestDto, DIR_NAME);
storageService.saveImages(id,files, DIR_NAME);
} catch (IOException e) {
throw new FileUploadException("An error occurred while uploading files.", e);
}
}
// @ResponseStatus(HttpStatus.CREATED)
// @PostMapping("/upload-part")
// public void uploadImages(
// @RequestPart("files") MultipartFile[] files, @RequestParam("id") Long id) {
//
// UploadImageRequestDto uploadImageRequestDto = new UploadImageRequestDto(Arrays.stream(files).toList(), id);
// try {
// storageService.saveImages(uploadImageRequestDto, DIR_NAME);
// } catch (IOException e) {
// throw new FileUploadException("An error occurred while uploading files.", e);
// }
// }

@ResponseStatus(HttpStatus.CREATED)
@PostMapping("/thumbnail")
Expand Down
14 changes: 3 additions & 11 deletions src/main/java/com/wefood/back/global/image/service/S3Service.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,11 @@ public class S3Service implements StorageService {
@Value("${cloud.aws.s3.bucketName}")
private String bucket;

/**
*
* @param uploadImageRequestDto
* @param dirName
* @return
* @throws IOException
*/

@Override
public void saveImages(UploadImageRequestDto uploadImageRequestDto, String dirName) throws IOException {
List<MultipartFile> multipartFiles = uploadImageRequestDto.getFiles();
Long id = uploadImageRequestDto.getId();
public void saveImages(Long id, List<MultipartFile> multipartFiles, String dirName) throws IOException {
List<Image> images = new ArrayList<>();
ImageRootType rootType= dbCheckRootType(dirName, uploadImageRequestDto.getId());
ImageRootType rootType= dbCheckRootType(dirName, id);
for (MultipartFile multipartFile : multipartFiles) {
File uploadFile = convert(multipartFile) // 파일 변환할 수 없으면 에러
.orElseThrow(() -> new IllegalArgumentException("error: MultipartFile -> File convert fail"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.wefood.back.global.image.dto.UploadImageRequestDto;
import com.wefood.back.global.image.dto.UploadThumbnailRequestDto;
import java.io.IOException;
import java.util.List;
import org.springframework.web.multipart.MultipartFile;

/**
* class: StorageService.
Expand All @@ -14,7 +16,7 @@
* @version 2024/08/11
*/
public interface StorageService {
void saveImages(UploadImageRequestDto uploadImageRequestDto, String dirName) throws IOException;
void saveImages(Long id, List<MultipartFile> multipartFiles, String dirName) throws IOException;

void saveThumbnail(UploadThumbnailRequestDto uploadThumbnail, String dirName) throws IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/api/product")
Expand Down Expand Up @@ -89,16 +91,13 @@ public ResponseEntity<Message<ProductDetailResponse>> getProduct(@PathVariable(n
}

@ResponseStatus(HttpStatus.CREATED)
@PostMapping
@PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public void uploadImages(
@Valid @ModelAttribute UploadImageRequestDto requestDto,
BindingResult result) {
@RequestParam("id") Long id,
@RequestParam("files") List<MultipartFile> files) {

if (result.hasErrors()) {
throw new InvalidRequestException(result);
}
try {
storageService.saveImages(requestDto, DIR_NAME);
storageService.saveImages(id,files, DIR_NAME);
} catch (IOException e) {
throw new FileUploadException("An error occurred while uploading files.", e);
}
Expand Down
Loading