|
|
@ -8,7 +8,6 @@ import com.bupt.note.ResponseData.ResponseDataUtil; |
|
|
|
import com.bupt.note.dto.FileForm; |
|
|
|
import com.bupt.note.dto.FileForm; |
|
|
|
import com.bupt.note.dto.QueryContent; |
|
|
|
import com.bupt.note.dto.QueryContent; |
|
|
|
import com.bupt.note.service.FileService; |
|
|
|
import com.bupt.note.service.FileService; |
|
|
|
import org.apache.commons.io.FileUtils; |
|
|
|
|
|
|
|
import org.slf4j.Logger; |
|
|
|
import org.slf4j.Logger; |
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
@ -18,7 +17,7 @@ import org.springframework.web.multipart.MultipartFile; |
|
|
|
|
|
|
|
|
|
|
|
import java.io.File; |
|
|
|
import java.io.File; |
|
|
|
import java.io.IOException; |
|
|
|
import java.io.IOException; |
|
|
|
import java.nio.charset.StandardCharsets; |
|
|
|
import java.util.UUID; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 论文文本管理接口 |
|
|
|
* 论文文本管理接口 |
|
|
@ -42,16 +41,21 @@ public class FileController { |
|
|
|
//上传论文文本
|
|
|
|
//上传论文文本
|
|
|
|
@PostMapping("upload") |
|
|
|
@PostMapping("upload") |
|
|
|
public ResponseData upload(FileForm fileForm) { |
|
|
|
public ResponseData upload(FileForm fileForm) { |
|
|
|
MultipartFile file = fileForm.getFile(); |
|
|
|
MultipartFile multipartFile = fileForm.getFile(); |
|
|
|
if (file != null && MediaType.TEXT_PLAIN_VALUE.equals(file.getContentType())) { |
|
|
|
if (multipartFile != null && (MediaType.TEXT_PLAIN_VALUE.equals(multipartFile.getContentType()) || MediaType.APPLICATION_PDF_VALUE.equals(multipartFile.getContentType()))) { |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
try { |
|
|
|
File txtFile = fileService.newFile(); |
|
|
|
String name = multipartFile.getOriginalFilename(); |
|
|
|
logger.debug("论文保存到" + txtFile.getAbsolutePath()); |
|
|
|
File file; |
|
|
|
file.transferTo(txtFile); |
|
|
|
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(); |
|
|
|
com.bupt.note.Model.File f = new com.bupt.note.Model.File(); |
|
|
|
f.setFilePath(txtFile.getAbsolutePath()); |
|
|
|
f.setFilePath(file.getAbsolutePath()); |
|
|
|
f.setUrlPath("/txt/" + txtFile.getName()); |
|
|
|
|
|
|
|
fileRepository.save(f); |
|
|
|
fileRepository.save(f); |
|
|
|
return ResponseDataUtil.buildSuccess(f.getId()); |
|
|
|
return ResponseDataUtil.buildSuccess(f.getId()); |
|
|
|
} catch (IOException e) { |
|
|
|
} catch (IOException e) { |
|
|
@ -66,15 +70,21 @@ public class FileController { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@GetMapping("find") |
|
|
|
@GetMapping("find") |
|
|
|
public ResponseData get(QueryContent queryContent,@CookieValue("user") String username) throws IOException { |
|
|
|
public ResponseData get(QueryContent queryContent, @CookieValue("user") String username){ |
|
|
|
if (paperNoteRepository.existsByPaperIdAndUserName(queryContent.getPaperId(),username)) { |
|
|
|
try { |
|
|
|
PaperNote paperNote=paperNoteRepository.findByPaperIdAndUserName(queryContent.getPaperId(),username); |
|
|
|
if (paperNoteRepository.existsByPaperIdAndUserName(queryContent.getPaperId(), username)) { |
|
|
|
return ResponseDataUtil.buildSuccess(FileUtils.readFileToString(new File(paperNote.getFilePath()), StandardCharsets.UTF_8)); |
|
|
|
PaperNote paperNote = paperNoteRepository.findByPaperIdAndUserName(queryContent.getPaperId(), username); |
|
|
|
}else if(fileRepository.existsById(queryContent.getFileId())){ |
|
|
|
return ResponseDataUtil.buildSuccess(fileService.getContent(paperNote.getFilePath())); |
|
|
|
com.bupt.note.Model.File file=fileRepository.getOne(queryContent.getFileId()); |
|
|
|
} else if (fileRepository.existsById(queryContent.getFileId())) { |
|
|
|
return ResponseDataUtil.buildSuccess(FileUtils.readFileToString(new File(file.getFilePath()), StandardCharsets.UTF_8)); |
|
|
|
com.bupt.note.Model.File file = fileRepository.getOne(queryContent.getFileId()); |
|
|
|
} else{ |
|
|
|
return ResponseDataUtil.buildSuccess(fileService.getContent(file.getFilePath())); |
|
|
|
logger.error(String.format("论文文件id=%d不存在", queryContent.getFileId())); |
|
|
|
} 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(); |
|
|
|
return ResponseDataUtil.buildError(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -95,11 +105,11 @@ public class FileController { |
|
|
|
fileRepository.deleteById(id); |
|
|
|
fileRepository.deleteById(id); |
|
|
|
return ResponseDataUtil.buildSuccess(); |
|
|
|
return ResponseDataUtil.buildSuccess(); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
logger.error(String.format("删除论文id=%d失败",id)); |
|
|
|
logger.error(String.format("删除论文id=%d失败", id)); |
|
|
|
return ResponseDataUtil.buildError(); |
|
|
|
return ResponseDataUtil.buildError(); |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
logger.error(String.format("论文id=%d不存在",id)); |
|
|
|
logger.error(String.format("论文id=%d不存在", id)); |
|
|
|
return ResponseDataUtil.buildError(); |
|
|
|
return ResponseDataUtil.buildError(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|