Skip to content

Commit

Permalink
Proto Done
Browse files Browse the repository at this point in the history
  • Loading branch information
JANGYANG committed Apr 27, 2020
1 parent 5aa30e4 commit 67f70a0
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 31 deletions.
Binary file added .DS_Store
Binary file not shown.
20 changes: 14 additions & 6 deletions 90s/src/main/java/com/yapp/ios2/controller/PhotoController.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.yapp.ios2.controller;

import com.yapp.ios2.dto.PhotoDto;
import com.yapp.ios2.dto.ResponseDto;
import com.yapp.ios2.service.PhotoService;
import com.yapp.ios2.service.UserService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
Expand All @@ -11,7 +13,10 @@

import java.io.IOException;


@Api(tags = {"3. Photo"})
@RestController
@RequestMapping("/photo/*")
public class PhotoController {

@Autowired
Expand All @@ -20,7 +25,7 @@ public class PhotoController {
@Autowired
private UserService userService;

@GetMapping("/photo")
@GetMapping("/")
@ResponseBody
public String home(@AuthenticationPrincipal UserDetails user){

Expand All @@ -30,17 +35,20 @@ public String home(@AuthenticationPrincipal UserDetails user){
}


@PostMapping(value = "/photo/upload")
@PostMapping(value = "/upload")
@ResponseBody
public String upload(@RequestParam(value="image") MultipartFile image, @RequestParam("albumUid") Long albumUid, @RequestParam("photoOrder") Integer photoOrder, @AuthenticationPrincipal UserDetails user) throws IOException {
public ResponseDto.UrlDto upload(@RequestParam(value="image") MultipartFile image, @RequestParam("albumUid") Long albumUid, @RequestParam("photoOrder") Integer photoOrder, @AuthenticationPrincipal UserDetails user) throws IOException {
String url = photoService.upload(image, albumUid, photoOrder, userService.getUserByEmail(user.getUsername()).getUid());

return url;
ResponseDto.UrlDto urlDto = new ResponseDto.UrlDto();
urlDto.setUrl(url);

return urlDto;
}

@PostMapping(value = "/photo/download")
@PostMapping(value = "/download")
@ResponseBody
public byte[] download(@RequestBody PhotoDto photoInfo) throws IOException {
public byte[] download(@RequestBody PhotoDto.PhotoInfoDto photoInfo) throws IOException {

byte[] file = photoService.download(photoInfo.getAlbumUid(), photoInfo.getPhotoUid());

Expand Down
9 changes: 7 additions & 2 deletions 90s/src/main/java/com/yapp/ios2/dto/PhotoDto.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package com.yapp.ios2.dto;

import lombok.Data;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class PhotoDto {
private Long albumUid;
private Long photoUid;
@Data
public static class PhotoInfoDto{
private Long photoUid;
private Long albumUid;
}

}
5 changes: 5 additions & 0 deletions 90s/src/main/java/com/yapp/ios2/dto/ResponseDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,9 @@ public static class BooleanDto{
private Boolean result;
}

@Data
public static class UrlDto{
private String url;
}

}
4 changes: 3 additions & 1 deletion 90s/src/main/java/com/yapp/ios2/service/PhotoService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.yapp.ios2.service;

import com.yapp.ios2.dto.ResponseDto;
import com.yapp.ios2.repository.PhotoRepository;
import com.yapp.ios2.vo.Photo;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -19,7 +20,7 @@ public class PhotoService{

public String upload(MultipartFile photo, Long albumUid, Integer photoOrder, Long uploader) throws IOException{

String fileName = albumUid.toString() + "/" + photoOrder;
String fileName = albumUid.toString() + "/" + photoOrder + ".jpeg";
String url = s3Service.upload(photo, fileName);

Photo newPhoto = Photo.builder()
Expand All @@ -31,6 +32,7 @@ public String upload(MultipartFile photo, Long albumUid, Integer photoOrder, Lon


photoRepository.save(newPhoto);

return url;
}

Expand Down
23 changes: 1 addition & 22 deletions 90s/src/main/java/com/yapp/ios2/service/S3Service.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,20 @@
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.*;
import com.amazonaws.services.sns.AmazonSNS;
import com.amazonaws.services.sns.AmazonSNSClient;
import com.amazonaws.services.sns.AmazonSNSClientBuilder;
import com.amazonaws.services.sns.model.MessageAttributeValue;
import com.amazonaws.services.sns.model.PublishRequest;
import com.amazonaws.services.sns.model.PublishResult;
import com.amazonaws.util.IOUtils;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import sun.nio.ch.IOUtil;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Service
@NoArgsConstructor
public class S3Service{

private AmazonS3 s3Client;
private AmazonSNS snsClient;
@Value("${cloud.aws.credentials.accessKey}")
private String accessKey;

Expand All @@ -47,8 +36,6 @@ public class S3Service{
@Value("${cloud.aws.region.static}")
private String region;

private final Map<String, MessageAttributeValue> smsAttributes = new HashMap<String, MessageAttributeValue>();

@PostConstruct
public void setS3Client() {
AWSCredentials credentials = new BasicAWSCredentials(this.accessKey, this.secretKey);
Expand All @@ -68,7 +55,7 @@ public String upload(MultipartFile file, String fileName) throws IOException {

public byte[] download(Long albumUid, String fileName) throws IOException {
S3Object file = s3Client.getObject(new GetObjectRequest(bucket +
"/" + albumUid.toString(), fileName));
"/" + albumUid.toString(), fileName + ".jpeg"));

S3ObjectInputStream s3OIS = file.getObjectContent();
byte[] bytes = IOUtils.toByteArray(s3OIS);
Expand All @@ -78,13 +65,5 @@ public byte[] download(Long albumUid, String fileName) throws IOException {
// return resource;
}

public String sendSMS(String message, String phoneNumber){
PublishResult result = snsClient.publish(new PublishRequest()
.withMessage(message)
.withPhoneNumber(phoneNumber)
.withMessageAttributes(smsAttributes));
System.out.println(result);
return result.toString();
}

}

0 comments on commit 67f70a0

Please sign in to comment.