package com.bupt.note.Controller; import com.bupt.note.Model.PaperNote; import com.bupt.note.Repository.FileRepository; import com.bupt.note.Repository.PaperNoteRepository; import com.bupt.note.ResponseData.ResponseData; import com.bupt.note.ResponseData.ResponseDataUtil; import com.bupt.note.dto.FileForm; import com.bupt.note.dto.QueryContent; import com.bupt.note.service.FileService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import java.io.File; import java.io.IOException; import java.util.UUID; /** * 论文文本管理接口 */ @RestController @RequestMapping(value = "/v1/api/file") public class FileController { private static Logger logger = LoggerFactory.getLogger(FileController.class); @Autowired private FileRepository fileRepository; @Autowired private PaperNoteRepository paperNoteRepository; @Autowired private FileService fileService; //上传论文文本 @PostMapping("upload") public ResponseData upload(FileForm fileForm) { MultipartFile multipartFile = fileForm.getFile(); if (multipartFile != null && (MediaType.TEXT_PLAIN_VALUE.equals(multipartFile.getContentType()) || MediaType.APPLICATION_PDF_VALUE.equals(multipartFile.getContentType()))) { try { String name = multipartFile.getOriginalFilename(); File file; if (name != null && name.contains(".")) { file = fileService.newFile(name.replace(multipartFile.getOriginalFilename().split("\\.")[0], UUID.randomUUID().toString())); } else { file = fileService.newFile(); } logger.debug("论文保存到" + file.getAbsolutePath()); multipartFile.transferTo(file); com.bupt.note.Model.File f = new com.bupt.note.Model.File(); f.setFilePath(file.getAbsolutePath()); fileRepository.save(f); return ResponseDataUtil.buildSuccess(f.getId()); } catch (IOException e) { logger.error(String.valueOf(e)); e.printStackTrace(); return ResponseDataUtil.buildError(); } } else { logger.error("上传论文表单校验失败"); return ResponseDataUtil.buildError(); } } @GetMapping("find") public ResponseData get(QueryContent queryContent, @CookieValue("user") String username){ try { if (paperNoteRepository.existsByPaperIdAndUserName(queryContent.getPaperId(), username)) { PaperNote paperNote = paperNoteRepository.findByPaperIdAndUserName(queryContent.getPaperId(), username); return ResponseDataUtil.buildSuccess(fileService.getContent(paperNote.getFilePath())); } else if (fileRepository.existsById(queryContent.getFileId())) { com.bupt.note.Model.File file = fileRepository.getOne(queryContent.getFileId()); return ResponseDataUtil.buildSuccess(fileService.getContent(file.getFilePath())); } else { logger.error(String.format("论文文件id=%d不存在", queryContent.getFileId())); return ResponseDataUtil.buildError(); } } catch (IOException e) { e.printStackTrace(); logger.error(String.format("解析文件失败,原因是%s", e)); return ResponseDataUtil.buildError(); } } /** * 删除论文文本文件 * * @param id 文件id * @return */ @DeleteMapping("remove/{id}") public ResponseData remove(@PathVariable Long id) { if (fileRepository.existsById(id)) { com.bupt.note.Model.File txtFile = fileRepository.getOne(id); File f = new File(txtFile.getFilePath()); if (f.exists() && f.delete()) { fileRepository.deleteById(id); return ResponseDataUtil.buildSuccess(); } else { logger.error(String.format("删除论文id=%d失败", id)); return ResponseDataUtil.buildError(); } } else { logger.error(String.format("论文id=%d不存在", id)); return ResponseDataUtil.buildError(); } } }