use of com.qiwenshare.ufop.operation.write.Writer in project qiwen-file by qiwenshare.
the class FileController method updateFile.
@Operation(summary = "修改文件", description = "支持普通文本类文件的修改", tags = { "file" })
@RequestMapping(value = "/update", method = RequestMethod.POST)
@ResponseBody
public RestResult<String> updateFile(@RequestBody UpdateFileDTO updateFileDTO) {
JwtUser sessionUserBean = SessionUtil.getSession();
UserFile userFile = userFileService.getById(updateFileDTO.getUserFileId());
FileBean fileBean = fileService.getById(userFile.getFileId());
Long pointCount = fileService.getFilePointCount(userFile.getFileId());
if (pointCount > 1) {
return RestResult.fail().message("暂不支持修改");
}
String content = updateFileDTO.getFileContent();
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(content.getBytes());
try {
Writer writer1 = ufopFactory.getWriter(fileBean.getStorageType());
WriteFile writeFile = new WriteFile();
writeFile.setFileUrl(fileBean.getFileUrl());
int fileSize = byteArrayInputStream.available();
writeFile.setFileSize(fileSize);
writer1.write(byteArrayInputStream, writeFile);
DownloadFile downloadFile = new DownloadFile();
downloadFile.setFileUrl(fileBean.getFileUrl());
InputStream inputStream = ufopFactory.getDownloader(fileBean.getStorageType()).getInputStream(downloadFile);
String md5Str = DigestUtils.md5Hex(inputStream);
fileBean.setIdentifier(md5Str);
fileBean.setModifyTime(DateUtil.getCurrentTime());
fileBean.setModifyUserId(sessionUserBean.getUserId());
fileBean.setFileSize((long) fileSize);
fileService.updateById(fileBean);
} catch (Exception e) {
throw new QiwenException(999999, "修改文件异常");
} finally {
try {
byteArrayInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return RestResult.success().message("修改文件成功");
}
use of com.qiwenshare.ufop.operation.write.Writer in project qiwen-file by qiwenshare.
the class OfficeController method IndexServlet.
@RequestMapping(value = "/IndexServlet", method = RequestMethod.POST)
@ResponseBody
public void IndexServlet(HttpServletResponse response, HttpServletRequest request) throws IOException {
String token = request.getParameter("token");
Long userId = userService.getUserIdByToken(token);
if (userId == null) {
throw new NotLoginException();
}
PrintWriter writer = response.getWriter();
Scanner scanner = new Scanner(request.getInputStream()).useDelimiter("\\A");
String body = scanner.hasNext() ? scanner.next() : "";
JSONObject jsonObj = JSON.parseObject(body);
log.info("===saveeditedfile:" + jsonObj.get("status"));
;
String status = jsonObj != null ? jsonObj.get("status").toString() : "";
if ("2".equals(status) || "6".equals(status)) {
String type = request.getParameter("type");
String downloadUri = (String) jsonObj.get("url");
if ("edit".equals(type)) {
// 修改报告
String userFileId = request.getParameter("userFileId");
UserFile userFile = userFileService.getById(userFileId);
FileBean fileBean = fileService.getById(userFile.getFileId());
Long pointCount = fileService.getFilePointCount(userFile.getFileId());
if (pointCount > 1) {
// 该场景,暂不支持编辑修改
writer.write("{\"error\":1}");
return;
}
URL url = new URL(downloadUri);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
int fileLength = 0;
try {
InputStream stream = connection.getInputStream();
Writer writer1 = ufopFactory.getWriter(fileBean.getStorageType());
WriteFile writeFile = new WriteFile();
writeFile.setFileUrl(fileBean.getFileUrl());
writeFile.setFileSize(connection.getContentLength());
writer1.write(stream, writeFile);
} catch (Exception e) {
log.error(e.getMessage());
} finally {
if ("2".equals(status)) {
LambdaUpdateWrapper<UserFile> userFileUpdateWrapper = new LambdaUpdateWrapper<>();
userFileUpdateWrapper.set(UserFile::getUploadTime, DateUtil.getCurrentTime()).eq(UserFile::getUserFileId, userFileId);
userFileService.update(userFileUpdateWrapper);
}
LambdaUpdateWrapper<FileBean> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
fileLength = connection.getContentLength();
log.info("当前修改文件大小为:" + Long.valueOf(fileLength));
DownloadFile downloadFile = new DownloadFile();
downloadFile.setFileUrl(fileBean.getFileUrl());
InputStream inputStream = ufopFactory.getDownloader(fileBean.getStorageType()).getInputStream(downloadFile);
String md5Str = DigestUtils.md5Hex(inputStream);
lambdaUpdateWrapper.set(FileBean::getIdentifier, md5Str).set(FileBean::getFileSize, Long.valueOf(fileLength)).set(FileBean::getModifyTime, DateUtil.getCurrentTime()).set(FileBean::getModifyUserId, userId).eq(FileBean::getFileId, fileBean.getFileId());
fileService.update(lambdaUpdateWrapper);
connection.disconnect();
}
}
}
if ("3".equals(status) || "7".equals(status)) {
// 不强制手动保存时为6,"6".equals(status)
log.debug("====保存失败:");
writer.write("{\"error\":1}");
} else {
log.debug("状态为:0");
writer.write("{\"error\":" + "0" + "}");
}
}
Aggregations