You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
cloudnote_server/src/main/java/com/bupt/note/Controller/FileController.java

106 lines
3.9 KiB

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.apache.commons.io.FileUtils;
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.nio.charset.StandardCharsets;
/**
* 论文文本管理接口
*/
@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 file = fileForm.getFile();
if (file != null && MediaType.TEXT_PLAIN_VALUE.equals(file.getContentType())) {
try {
File txtFile = fileService.newFile();
logger.debug("论文保存到" + txtFile.getAbsolutePath());
file.transferTo(txtFile);
com.bupt.note.Model.File f = new com.bupt.note.Model.File();
f.setFilePath(txtFile.getAbsolutePath());
f.setUrlPath("/txt/" + txtFile.getName());
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) throws IOException {
if (paperNoteRepository.existsByPaperIdAndUserName(queryContent.getPaperId(),username)) {
PaperNote paperNote=paperNoteRepository.findByPaperIdAndUserName(queryContent.getPaperId(),username);
return ResponseDataUtil.buildSuccess(FileUtils.readFileToString(new File(paperNote.getFilePath()), StandardCharsets.UTF_8));
}else if(fileRepository.existsById(queryContent.getFileId())){
com.bupt.note.Model.File file=fileRepository.getOne(queryContent.getFileId());
return ResponseDataUtil.buildSuccess(FileUtils.readFileToString(new File(file.getFilePath()), StandardCharsets.UTF_8));
} else{
logger.error(String.format("论文文件id=%d不存在", queryContent.getFileId()));
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();
}
}
}