add file uploading

pull/128/head
Anton Romanov 6 years ago
parent 4eaa7b3f59
commit d8fa601103

@ -0,0 +1,48 @@
package ru.ulstu.file;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import ru.ulstu.configuration.Constants;
import ru.ulstu.core.model.response.Response;
import ru.ulstu.file.model.File;
import ru.ulstu.file.service.FileService;
import java.io.IOException;
import static ru.ulstu.paper.controller.PaperController.URL;
@RestController
@RequestMapping(URL)
public class FileController {
public static final String URL = Constants.API_1_0 + "files";
private final FileService fileService;
public FileController(FileService fileService) {
this.fileService = fileService;
}
@GetMapping("/download-tmp/{tmp-file-name}")
public ResponseEntity<byte[]> getFile(@PathVariable("tmp-file-name") String tmpFileName) throws IOException {
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Disposition", "attachment; filename="
+ fileService.getTmpFileName(tmpFileName));
return new ResponseEntity<>(fileService.getTmpFile(tmpFileName), headers, HttpStatus.OK);
}
@GetMapping("/download/{file-id}")
public ResponseEntity<byte[]> getFile(@PathVariable("file-id") Integer fileId) {
HttpHeaders headers = new HttpHeaders();
File file = fileService.getFile(fileId);
headers.add("Content-Disposition", "attachment; filename=" + file.getName());
return new ResponseEntity<>(file.getData(), headers, HttpStatus.OK);
}
@PostMapping("/uploadTmpFile")
public Response<String> upload(@RequestBody MultipartFile multipartFile) throws IOException {
return new Response(fileService.uploadToTmpDir(multipartFile));
}
}

@ -0,0 +1,51 @@
package ru.ulstu.file.model;
import ru.ulstu.core.model.BaseEntity;
import javax.persistence.Column;
import javax.persistence.Entity;
import java.util.Date;
@Entity
public class File extends BaseEntity {
private String name;
private long size;
@Column(name = "create_date")
private Date createDate;
private byte[] data;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getSize() {
return size;
}
public void setSize(long size) {
this.size = size;
}
public Date getCreateDate() {
return createDate;
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
public byte[] getData() {
return data;
}
public void setData(byte[] data) {
this.data = data;
}
}

@ -0,0 +1,7 @@
package ru.ulstu.file.repostory;
import org.springframework.data.jpa.repository.JpaRepository;
import ru.ulstu.file.model.File;
public interface FileRepository extends JpaRepository<File, Integer> {
}

@ -0,0 +1,75 @@
package ru.ulstu.file.service;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import ru.ulstu.file.model.File;
import ru.ulstu.file.repostory.FileRepository;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import static java.nio.charset.StandardCharsets.UTF_8;
@Service
public class FileService {
private final static int META_FILE_NAME_INDEX = 0;
private final static int META_FILE_SIZE_INDEX = 1;
private String tmpDir;
private FileRepository fileRepository;
public FileService(FileRepository fileRepository) {
tmpDir = System.getProperty("java.io.tmpdir");
this.fileRepository = fileRepository;
}
public File createFileFromTmp(String tmpFileName) throws IOException {
File file = new File();
file.setData(getTmpFile(tmpFileName));
file.setName(getTmpFileName(tmpFileName));
return fileRepository.save(file);
}
public String uploadToTmpDir(MultipartFile multipartFile) throws IOException {
String tmpFileName = String.valueOf(System.currentTimeMillis());
Files.write(getTmpFilePath(tmpFileName), multipartFile.getBytes());
String meta = multipartFile.getOriginalFilename() + "\n" + multipartFile.getSize();
Files.write(getTmpFileMetaPath(tmpFileName), meta.getBytes(UTF_8));
return tmpFileName;
}
private String[] getMeta(String tmpFileName) throws IOException {
return new String(Files.readAllBytes(getTmpFileMetaPath(tmpFileName)), UTF_8)
.split("\n");
}
public long getTmpFileSize(String tmpFileName) throws IOException {
return Long.valueOf(getMeta(tmpFileName)[META_FILE_SIZE_INDEX]).longValue();
}
public String getTmpFileName(String tmpFileName) throws IOException {
return getMeta(tmpFileName)[META_FILE_NAME_INDEX];
}
public byte[] getTmpFile(String tmpFileName) throws IOException {
return Files.readAllBytes(getTmpFilePath(tmpFileName));
}
public File getFile(Integer fileId) {
return fileRepository.findOne(fileId);
}
public void deleteTmpFile(String tmpFileName) throws IOException {
Files.delete(getTmpFilePath(tmpFileName));
}
private Path getTmpFilePath(String tmpFileName) {
return Paths.get(tmpDir + tmpFileName);
}
private Path getTmpFileMetaPath(String tmpFileName) {
return Paths.get(getTmpFilePath(tmpFileName) + ".meta");
}
}

@ -1,16 +1,16 @@
package ru.ulstu.paper.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import ru.ulstu.configuration.Constants;
import ru.ulstu.core.model.response.Response;
import ru.ulstu.paper.model.Paper;
import ru.ulstu.paper.model.PaperDto;
import ru.ulstu.paper.service.PaperService;
import java.io.IOException;
import java.util.List;
import static ru.ulstu.user.controller.UserController.URL;
import static ru.ulstu.paper.controller.PaperController.URL;
@RestController
@RequestMapping(URL)
@ -27,4 +27,9 @@ public class PaperController {
public Response<List<Paper>> getPapers() {
return new Response<>(paperService.findAll());
}
@PostMapping
public Response createPaper(@RequestBody PaperDto paperDto) throws IOException {
return new Response(paperService.create(paperDto));
}
}

@ -1,20 +1,93 @@
package ru.ulstu.paper.model;
public class Paper {
private int id;
import org.hibernate.validator.constraints.NotBlank;
import ru.ulstu.core.model.BaseEntity;
import ru.ulstu.file.model.File;
import javax.persistence.*;
import java.util.Date;
@Entity
public class Paper extends BaseEntity {
public enum PaperStatus {
DRAFT("Черновик"), ON_PREPARATION("На подготовке"), COMPLETED("Завершена");
private String name;
PaperStatus(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
@NotBlank
private String title;
public Paper(int id, String title) {
this.id = id;
this.title = title;
@Enumerated(value = EnumType.STRING)
private PaperStatus status;
@Column(name = "create_date")
private Date createDate;
@Column(name = "update_date")
private Date updateDate;
private String comment;
private Boolean locked;
@ManyToOne
private File file;
public PaperStatus getStatus() {
return status;
}
public void setStatus(PaperStatus status) {
this.status = status;
}
public Date getCreateDate() {
return createDate;
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
public Date getUpdateDate() {
return updateDate;
}
public void setUpdateDate(Date updateDate) {
this.updateDate = updateDate;
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
public Boolean getLocked() {
return locked;
}
public void setLocked(Boolean locked) {
this.locked = locked;
}
public int getId() {
return id;
public File getFile() {
return file;
}
public void setId(int id) {
this.id = id;
public void setFile(File file) {
this.file = file;
}
public String getTitle() {

@ -0,0 +1,71 @@
package ru.ulstu.paper.model;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.Date;
public class PaperDto {
private final String title;
private final Paper.PaperStatus status;
private final Date createDate;
private final Date updateDate;
private final String comment;
private final Boolean locked;
private final String tmpFileName;
@JsonCreator
public PaperDto(@JsonProperty("title") String title,
@JsonProperty("status") Paper.PaperStatus status,
@JsonProperty("createDate") Date createDate,
@JsonProperty("updateDate") Date updateDate,
@JsonProperty("comment") String comment,
@JsonProperty("locked") Boolean locked,
@JsonProperty("tmpFileName") String tmpFileName) {
this.title = title;
this.status = status;
this.createDate = createDate;
this.updateDate = updateDate;
this.comment = comment;
this.locked = locked;
this.tmpFileName = tmpFileName;
}
public PaperDto(Paper paper) {
this.title = paper.getTitle();
this.status = paper.getStatus();
this.createDate = paper.getCreateDate();
this.updateDate = paper.getUpdateDate();
this.comment = paper.getComment();
this.locked = paper.getLocked();
this.tmpFileName = null;
}
public String getTitle() {
return title;
}
public Paper.PaperStatus getStatus() {
return status;
}
public Date getCreateDate() {
return createDate;
}
public Date getUpdateDate() {
return updateDate;
}
public String getComment() {
return comment;
}
public Boolean getLocked() {
return locked;
}
public String getTmpFileName() {
return tmpFileName;
}
}

@ -0,0 +1,7 @@
package ru.ulstu.paper.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import ru.ulstu.paper.model.Paper;
public interface PaperRepository extends JpaRepository<Paper, Integer> {
}

@ -1,15 +1,48 @@
package ru.ulstu.paper.service;
import org.springframework.stereotype.Service;
import ru.ulstu.file.model.File;
import ru.ulstu.file.service.FileService;
import ru.ulstu.paper.model.Paper;
import ru.ulstu.paper.model.PaperDto;
import ru.ulstu.paper.repository.PaperRepository;
import javax.transaction.Transactional;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
@Service
public class PaperService {
private final PaperRepository paperRepository;
private final FileService fileService;
public PaperService(PaperRepository paperRepository,
FileService fileService) {
this.paperRepository = paperRepository;
this.fileService = fileService;
}
public List<Paper> findAll() {
return Arrays.asList(new Paper(1, "Название стартьи"));
return paperRepository.findAll();
}
@Transactional
public int create(PaperDto paperDto) throws IOException {
return paperRepository.save(copyFromDto(new Paper(), paperDto)).getId();
}
private Paper copyFromDto(Paper paper, PaperDto paperDto) throws IOException {
paper.setComment(paperDto.getComment());
paper.setCreateDate(paperDto.getCreateDate());
paper.setLocked(paperDto.getLocked());
paper.setStatus(paperDto.getStatus());
paper.setTitle(paperDto.getTitle());
paper.setUpdateDate(paperDto.getUpdateDate());
if (paperDto.getTmpFileName() != null) {
paper.setFile(fileService.createFileFromTmp(paperDto.getTmpFileName()));
}
return paper;
}
}

@ -0,0 +1,37 @@
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">
<changeSet author="orion" id="20180505_000000-0">
<createTable tableName="file">
<column name="id" type="integer">
<constraints nullable="false"/>
</column>
<column name="name" type="varchar(255)"/>
<column name="size" type="bigint"/>
<column name="create_date" type="timestamp"/>
<column name="data" type="bytea"/>
<column name="version" type="integer"/>
</createTable>
<addPrimaryKey columnNames="id" constraintName="pk_file" tableName="file"/>
</changeSet>
<changeSet author="orion" id="20180505_000000-1">
<createTable tableName="paper">
<column name="id" type="integer">
<constraints nullable="false"/>
</column>
<column name="title" type="varchar(255)"/>
<column name="status" type="varchar(255)"/>
<column name="create_date" type="timestamp"/>
<column name="update_date" type="timestamp"/>
<column name="comment" type="varchar(255)"/>
<column name="locked" type="boolean"/>
<column name="file_id" type="integer"/>
<column name="version" type="integer"/>
</createTable>
<addPrimaryKey columnNames="id" constraintName="pk_paper" tableName="paper"/>
<addForeignKeyConstraint baseTableName="paper" baseColumnNames="file_id"
constraintName="fk_paper_file" referencedTableName="file"
referencedColumnNames="id"/>
</changeSet>
</databaseChangeLog>

@ -10,4 +10,5 @@
<include file="db/changelog-20180321_193000-data.xml"/>
<include file="db/changelog-20180405_110000-schema.xml"/>
<include file="db/changelog-20180428_110000-schema.xml"/>
<include file="db/changelog-20180505_000000-schema.xml"/>
</databaseChangeLog>
Loading…
Cancel
Save