Skip to content

Commit

Permalink
MetaData 추출 Util, 모델 추가
Browse files Browse the repository at this point in the history
- Exif 모델 만들고 One to One Mapping
- Exif 추출 metadata-extractor 추가
- Test 코드 추가
- 파일 업로드시 최대 사이즈 지정
  • Loading branch information
ventulus95 committed Oct 28, 2020
1 parent b667310 commit add2b06
Show file tree
Hide file tree
Showing 13 changed files with 156 additions and 11 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ dependencies {
compile 'org.springframework.boot:spring-boot-starter-validation'
implementation group: 'org.springframework.cloud', name: 'spring-cloud-starter-aws', version: '2.2.1.RELEASE'
compile("org.mariadb.jdbc:mariadb-java-client")
compile group: 'com.drewnoakes', name: 'metadata-extractor', version: '2.11.0'
}

test {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.ventulus95.ouathjwt.controller;

import com.drew.imaging.ImageProcessingException;
import com.ventulus95.ouathjwt.dto.artwork.ArtworkListResponseDto;
import com.ventulus95.ouathjwt.dto.artwork.ArtworkResponseDto;
import com.ventulus95.ouathjwt.dto.artwork.ArtworkSaveRequestDto;
Expand Down Expand Up @@ -41,7 +42,7 @@ public List<ArtworkListResponseDto> ListGeneration(){

@PreAuthorize("hasRole('ADMIN')")
@PostMapping("/")
public Long save(ArtworkSaveRequestDto dto, @CurrentUser UserPrincipal user) throws IOException {
public Long save(ArtworkSaveRequestDto dto, @CurrentUser UserPrincipal user) throws IOException, ImageProcessingException {
String imgpath = s3Service.upload(dto.getFilePath(), dto.getFile());
dto.setFilePath(imgpath);
return artworkService.save(dto, user);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.ventulus95.ouathjwt.dto.artwork;

import com.ventulus95.ouathjwt.model.exif.Exif;
import com.ventulus95.ouathjwt.model.user.User;
import com.ventulus95.ouathjwt.model.artwork.Artwork;
import lombok.Builder;
Expand All @@ -20,10 +21,11 @@ public class ArtworkSaveRequestDto {
private String format;
private MultipartFile file;
private String filePath;
private Exif exif;


@Builder
public ArtworkSaveRequestDto(String title, String content, User user, MultipartFile file, String filePath, String artist, int generation, String format){
public ArtworkSaveRequestDto(String title, String content, User user, MultipartFile file, String filePath, String artist, int generation, String format,Exif exif){
this.title = title;
this.content = content;
this.user = user;
Expand All @@ -32,6 +34,7 @@ public ArtworkSaveRequestDto(String title, String content, User user, MultipartF
this.artist = artist;
this.generation = generation;
this.format = format;
this.exif = exif;
}

public Artwork toEntity(){
Expand All @@ -43,6 +46,7 @@ public Artwork toEntity(){
.artist(artist)
.generation(generation)
.format(format)
.exif(exif)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.ventulus95.ouathjwt.dto.artwork;

import com.ventulus95.ouathjwt.model.exif.Exif;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand All @@ -18,15 +19,17 @@ public class ArtworkUpadteRequestDto {
private String format;
private MultipartFile file;
private String filePath;
private Exif exif;

@Builder
public ArtworkUpadteRequestDto(String title, String content, MultipartFile file, String filePath, String artist, int generation, String format){
public ArtworkUpadteRequestDto(String title, String content, MultipartFile file, String filePath, String artist, int generation, String format, Exif exif){
this.title = title;
this.content = content;
this.file = file;
this.filePath = filePath;
this.artist = artist;
this.generation = generation;
this.format = format;
this.exif = exif;
}
}
14 changes: 11 additions & 3 deletions src/main/java/com/ventulus95/ouathjwt/model/artwork/Artwork.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.ventulus95.ouathjwt.model.artwork;

import com.drew.metadata.Directory;
import com.ventulus95.ouathjwt.model.BaseTimeEntity;
import com.ventulus95.ouathjwt.model.exif.Exif;
import com.ventulus95.ouathjwt.model.user.User;
import lombok.Builder;
import lombok.Getter;
Expand All @@ -14,7 +16,7 @@
public class Artwork extends BaseTimeEntity {

@Id
@GeneratedValue
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "artwork_id")
private Long Id;

Expand All @@ -41,23 +43,29 @@ public class Artwork extends BaseTimeEntity {
@Column(columnDefinition = "TEXT")
private String filePath;

@OneToOne
@JoinColumn(name = "exif_id")
private Exif exif;

@Builder
public Artwork(String title, String content, User user, String filePath, String artist, int generation, String format){
public Artwork(String title, String content, User user, String filePath, String artist, int generation, String format, Exif exif){
this.title = title;
this.content = content;
this.user = user;
this.filePath = filePath;
this.artist = artist;
this.generation = generation;
this.format = format;
this.exif = exif;
}

public void update(String title, String content, String filePath, String artist, int generation, String format){
public void update(String title, String content, String filePath, String artist, int generation, String format, Exif exif){
this.title = title;
this.content = content;
this.filePath = filePath;
this.artist = artist;
this.generation = generation;
this.format = format;
this.exif = exif;
}
}
45 changes: 45 additions & 0 deletions src/main/java/com/ventulus95/ouathjwt/model/exif/Exif.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.ventulus95.ouathjwt.model.exif;

import lombok.Builder;
import lombok.Data;

import javax.persistence.*;

@Entity
@Data
public class Exif {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "exif_id")
private Long id;

@Column
private String aperture;

@Column
private String focusLength;

@Column
private String iso;

@Column
private String exposureTime;

@Column
private String Model;

@Column
private String Maker;

@Builder
public Exif(String aperture, String focusLength, String iso, String exposureTime, String model, String maker){
this.aperture = aperture;
this.focusLength = focusLength;
this.iso = iso;
this.exposureTime = exposureTime;
this.Maker = maker;
this.Model = model;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.ventulus95.ouathjwt.model.exif;

import org.springframework.data.jpa.repository.JpaRepository;

public interface ExifRepository extends JpaRepository<Exif, Long> {
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
package com.ventulus95.ouathjwt.service.artwork;

import com.drew.imaging.ImageProcessingException;
import com.ventulus95.ouathjwt.dto.artwork.ArtworkListResponseDto;
import com.ventulus95.ouathjwt.dto.artwork.ArtworkResponseDto;
import com.ventulus95.ouathjwt.dto.artwork.ArtworkSaveRequestDto;
import com.ventulus95.ouathjwt.dto.artwork.ArtworkUpadteRequestDto;
import com.ventulus95.ouathjwt.model.exif.ExifRepository;
import com.ventulus95.ouathjwt.model.user.User;
import com.ventulus95.ouathjwt.model.artwork.Artwork;
import com.ventulus95.ouathjwt.model.artwork.ArtworkRepository;
import com.ventulus95.ouathjwt.repository.UserRepository;
import com.ventulus95.ouathjwt.security.CurrentUser;
import com.ventulus95.ouathjwt.security.UserPrincipal;
import com.ventulus95.ouathjwt.util.ExifUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;
Expand All @@ -26,17 +30,19 @@ public class ArtworkService {

private final ArtworkRepository artworkRepository;
private final UserRepository userRepository;
private final ExifRepository exifRepository;

public Long save(ArtworkSaveRequestDto dto, @CurrentUser UserPrincipal user) {
public Long save(ArtworkSaveRequestDto dto, @CurrentUser UserPrincipal user) throws IOException, ImageProcessingException {
Optional<User> user1 = userRepository.findByEmail(user.getEmail());
dto.setUser(user1.get());
dto.setExif(exifRepository.save(ExifUtil.imgtoExif(dto.getFile())));
return artworkRepository.save(dto.toEntity()).getId();
}

@Transactional
public Long update(Long id, ArtworkUpadteRequestDto dto) {
Artwork artwork = artworkRepository.findById(id).orElseThrow(()-> new IllegalArgumentException("해당 게시글이 없습니다. id ="+id));
artwork.update(dto.getTitle(), dto.getContent(), dto.getFilePath(), dto.getArtist(), dto.getGeneration(), dto.getFormat());
artwork.update(dto.getTitle(), dto.getContent(), dto.getFilePath(), dto.getArtist(), dto.getGeneration(), dto.getFormat(), dto.getExif());
return id;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import javax.annotation.PostConstruct;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;

@Service
Expand Down Expand Up @@ -56,7 +57,9 @@ public String upload(String currentFilePath, MultipartFile file) throws IOExcept
s3.deleteObject(bucket, currentFilePath);
}
}
s3.putObject(new PutObjectRequest(bucket, fileName, file.getInputStream(), null)
LocalDate date1 = LocalDate.now();
String folderString = date1.getYear()+"/"+date1.getMonthValue()+"/"+date1.getDayOfMonth()+"/";
s3.putObject(new PutObjectRequest(bucket, folderString+fileName, file.getInputStream(), null)
.withCannedAcl(CannedAccessControlList.PublicRead));
return s3.getUrl(bucket, fileName).toString();
}
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/com/ventulus95/ouathjwt/util/ExifUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.ventulus95.ouathjwt.util;

import com.drew.imaging.ImageMetadataReader;
import com.drew.imaging.ImageProcessingException;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.exif.ExifIFD0Directory;
import com.drew.metadata.exif.ExifSubIFDDirectory;
import com.ventulus95.ouathjwt.model.exif.Exif;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;

public class ExifUtil {

public static Exif imgtoExif(MultipartFile file) throws IOException, ImageProcessingException {
InputStream inputStream = new BufferedInputStream(file.getInputStream());
Metadata metadata = ImageMetadataReader.readMetadata(file.getInputStream());
Directory SubDirectory = metadata.getFirstDirectoryOfType(ExifSubIFDDirectory.class);
Directory IFD0Directory = metadata.getFirstDirectoryOfType(ExifIFD0Directory.class);
String fnum = SubDirectory.getString(ExifSubIFDDirectory.TAG_FNUMBER);
String fl = SubDirectory.getString(ExifSubIFDDirectory.TAG_FOCAL_LENGTH);
String iso = SubDirectory.getString(ExifSubIFDDirectory.TAG_ISO_EQUIVALENT);
String exp = SubDirectory.getString(ExifSubIFDDirectory.TAG_EXPOSURE_TIME);
String model = IFD0Directory.getString(ExifIFD0Directory.TAG_MODEL);
String maker = IFD0Directory.getString(ExifIFD0Directory.TAG_MAKE);
return Exif.builder().aperture(fnum).focusLength(fl).iso(iso).exposureTime(exp).model(model).maker(maker).build();
}
}
6 changes: 5 additions & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@ spring:
active: local
jackson:
serialization:
fail-on-empty-beans: false
fail-on-empty-beans: false
servlet:
multipart:
max-file-size: 500MB

Binary file added src/main/resources/testPhoto/NikonE990.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,19 +1,34 @@
package com.ventulus95.ouathjwt;

import com.drew.imaging.ImageMetadataReader;
import com.drew.imaging.ImageProcessingException;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.Tag;
import com.drew.metadata.exif.ExifSubIFDDirectory;
import com.ventulus95.ouathjwt.dto.artwork.ArtworkSaveRequestDto;
import com.ventulus95.ouathjwt.service.upload.S3Service;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.io.ClassPathResource;
import org.springframework.mock.web.MockMultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import static org.junit.jupiter.api.Assertions.*;

@SpringBootTest
class OuathJwtApplicationTests {

@Autowired
S3Service s3Service;

MockMultipartFile mockMultipartFile;

@Test
void kkk() throws UnsupportedEncodingException {
String filePath = "https://bomnae-static.s3.ap-northeast-2.amazonaws.com/myplot.png ";
Expand All @@ -24,7 +39,25 @@ void kkk() throws UnsupportedEncodingException {
}

@Test
void contextLoads() {
public void S3_폴더_업로드() throws IOException {
ArtworkSaveRequestDto a = new ArtworkSaveRequestDto();
MockMultipartFile firstFile = new MockMultipartFile("testPhoto", "filename.txt", "text/plain", "some xml".getBytes());
a.setFile(firstFile);
s3Service.upload(a.getFilePath(), a.getFile());
}

@Test
public void 메타데이터_확인() throws ImageProcessingException, IOException {
File file = new ClassPathResource("/testPhoto/NikonE990.jpg").getFile();
Metadata metadata = ImageMetadataReader.readMetadata(file);
Directory directory1 = metadata.getFirstDirectoryOfType(ExifSubIFDDirectory.class);
assertEquals("3.5", directory1.getString(ExifSubIFDDirectory.TAG_MAX_APERTURE));
for (Directory directory : metadata.getDirectories()) {
for (Tag tag : directory.getTags()) {
System.out.println(tag);
}
}

}

}

0 comments on commit add2b06

Please sign in to comment.