|
|
|
@ -8,7 +8,6 @@ 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; |
|
|
|
@ -18,7 +17,7 @@ import org.springframework.web.multipart.MultipartFile; |
|
|
|
|
|
|
|
|
|
import java.io.File; |
|
|
|
|
import java.io.IOException; |
|
|
|
|
import java.nio.charset.StandardCharsets; |
|
|
|
|
import java.util.UUID; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 论文文本管理接口 |
|
|
|
@ -42,16 +41,21 @@ public class FileController { |
|
|
|
|
//上传论文文本
|
|
|
|
|
@PostMapping("upload") |
|
|
|
|
public ResponseData upload(FileForm fileForm) { |
|
|
|
|
MultipartFile file = fileForm.getFile(); |
|
|
|
|
if (file != null && MediaType.TEXT_PLAIN_VALUE.equals(file.getContentType())) { |
|
|
|
|
MultipartFile multipartFile = fileForm.getFile(); |
|
|
|
|
if (multipartFile != null && (MediaType.TEXT_PLAIN_VALUE.equals(multipartFile.getContentType()) || MediaType.APPLICATION_PDF_VALUE.equals(multipartFile.getContentType()))) { |
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
File txtFile = fileService.newFile(); |
|
|
|
|
logger.debug("论文保存到" + txtFile.getAbsolutePath()); |
|
|
|
|
file.transferTo(txtFile); |
|
|
|
|
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(txtFile.getAbsolutePath()); |
|
|
|
|
f.setUrlPath("/txt/" + txtFile.getName()); |
|
|
|
|
f.setFilePath(file.getAbsolutePath()); |
|
|
|
|
fileRepository.save(f); |
|
|
|
|
return ResponseDataUtil.buildSuccess(f.getId()); |
|
|
|
|
} catch (IOException e) { |
|
|
|
@ -66,15 +70,21 @@ public class FileController { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@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())); |
|
|
|
|
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(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -95,11 +105,11 @@ public class FileController { |
|
|
|
|
fileRepository.deleteById(id); |
|
|
|
|
return ResponseDataUtil.buildSuccess(); |
|
|
|
|
} else { |
|
|
|
|
logger.error(String.format("删除论文id=%d失败",id)); |
|
|
|
|
logger.error(String.format("删除论文id=%d失败", id)); |
|
|
|
|
return ResponseDataUtil.buildError(); |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
logger.error(String.format("论文id=%d不存在",id)); |
|
|
|
|
logger.error(String.format("论文id=%d不存在", id)); |
|
|
|
|
return ResponseDataUtil.buildError(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|