use of com.qiwenshare.ufop.operation.copy.domain.CopyFile in project qiwen-file by qiwenshare.
the class AsyncTaskComp method saveUnzipFile.
public Future<String> saveUnzipFile(UserFile userFile, FileBean fileBean, int unzipMode, String entryName, String filePath) {
String unzipUrl = UFOPUtils.getTempFile(fileBean.getFileUrl()).getAbsolutePath().replace("." + userFile.getExtendName(), "");
String totalFileUrl = unzipUrl + entryName;
File currentFile = new File(totalFileUrl);
UserFile saveUserFile = new UserFile();
saveUserFile.setUploadTime(DateUtil.getCurrentTime());
saveUserFile.setUserId(userFile.getUserId());
saveUserFile.setFilePath(UFOPUtils.pathSplitFormat(userFile.getFilePath() + entryName.replace(currentFile.getName(), "")).replace("\\", "/"));
if (currentFile.isDirectory()) {
saveUserFile.setIsDir(1);
saveUserFile.setFileName(currentFile.getName());
} else {
FileInputStream fis = null;
String md5Str = UUID.randomUUID().toString();
try {
fis = new FileInputStream(currentFile);
md5Str = DigestUtils.md5Hex(fis);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(fis);
}
FileInputStream fileInputStream = null;
try {
Map<String, Object> param = new HashMap<>();
param.put("identifier", md5Str);
List<FileBean> list = fileMapper.selectByMap(param);
if (list != null && !list.isEmpty()) {
// 文件已存在
saveUserFile.setFileId(list.get(0).getFileId());
} else {
// 文件不存在
fileInputStream = new FileInputStream(currentFile);
CopyFile createFile = new CopyFile();
createFile.setExtendName(UFOPUtils.getFileExtendName(totalFileUrl));
String saveFileUrl = ufopFactory.getCopier().copy(fileInputStream, createFile);
FileBean tempFileBean = new FileBean(saveFileUrl, currentFile.length(), storageType, md5Str, userFile.getUserId());
;
fileMapper.insert(tempFileBean);
saveUserFile.setFileId(tempFileBean.getFileId());
}
} catch (IOException e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(fileInputStream);
System.gc();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
currentFile.delete();
}
saveUserFile.setIsDir(0);
saveUserFile.setExtendName(UFOPUtils.getFileExtendName(totalFileUrl));
saveUserFile.setFileName(UFOPUtils.getFileNameNotExtend(currentFile.getName()));
}
saveUserFile.setDeleteFlag(0);
if (unzipMode == 1) {
saveUserFile.setFilePath(UFOPUtils.pathSplitFormat(userFile.getFilePath() + userFile.getFileName() + "/" + entryName.replace(currentFile.getName(), "")).replace("\\", "/"));
} else if (unzipMode == 2) {
saveUserFile.setFilePath(UFOPUtils.pathSplitFormat(filePath + entryName.replace(currentFile.getName(), "")).replace("\\", "/"));
}
String fileName = fileDealComp.getRepeatFileName(saveUserFile, saveUserFile.getFilePath());
if (saveUserFile.getIsDir() == 1 && !fileName.equals(saveUserFile.getFileName())) {
// 如果是目录,而且重复,什么也不做
} else {
saveUserFile.setFileName(fileName);
userFileMapper.insert(saveUserFile);
}
return new AsyncResult<String>("saveUnzipFile");
}
use of com.qiwenshare.ufop.operation.copy.domain.CopyFile in project qiwen-file by qiwenshare.
the class OfficeController method createOfficeFile.
@Operation(summary = "创建office文件", description = "创建office文件", tags = { "office" })
@ResponseBody
@RequestMapping(value = "/createofficefile", method = RequestMethod.POST)
public RestResult<Object> createOfficeFile(@RequestBody CreateOfficeFileDTO createOfficeFileDTO) {
RestResult<Object> result = new RestResult<>();
try {
JwtUser loginUser = SessionUtil.getSession();
String fileName = createOfficeFileDTO.getFileName();
String filePath = createOfficeFileDTO.getFilePath();
String extendName = createOfficeFileDTO.getExtendName();
List<UserFile> userFiles = userFileService.selectSameUserFile(fileName, filePath, extendName, loginUser.getUserId());
if (userFiles != null && !userFiles.isEmpty()) {
return RestResult.fail().message("同名文件已存在");
}
String uuid = UUID.randomUUID().toString().replaceAll("-", "");
String templateFilePath = "";
if ("docx".equals(extendName)) {
templateFilePath = "template/Word.docx";
} else if ("xlsx".equals(extendName)) {
templateFilePath = "template/Excel.xlsx";
} else if ("pptx".equals(extendName)) {
templateFilePath = "template/PowerPoint.pptx";
}
String url2 = ClassUtils.getDefaultClassLoader().getResource("static/" + templateFilePath).getPath();
FileInputStream fileInputStream = new FileInputStream(url2);
Copier copier = ufopFactory.getCopier();
CopyFile copyFile = new CopyFile();
copyFile.setExtendName(extendName);
String fileUrl = copier.copy(fileInputStream, copyFile);
FileBean fileBean = new FileBean();
fileBean.setFileSize(0L);
fileBean.setFileUrl(fileUrl);
fileBean.setStorageType(storageType);
fileBean.setIdentifier(uuid);
fileBean.setCreateTime(DateUtil.getCurrentTime());
fileBean.setCreateUserId(loginUser.getUserId());
fileBean.setFileStatus(1);
boolean saveFlag = fileService.save(fileBean);
UserFile userFile = new UserFile();
if (saveFlag) {
userFile.setUserId(loginUser.getUserId());
userFile.setFileName(fileName);
userFile.setFilePath(filePath);
userFile.setDeleteFlag(0);
userFile.setIsDir(0);
userFile.setExtendName(extendName);
userFile.setUploadTime(DateUtil.getCurrentTime());
userFile.setFileId(fileBean.getFileId());
userFileService.save(userFile);
}
return RestResult.success().message("文件创建成功");
} catch (Exception e) {
log.error(e.getMessage());
return RestResult.fail().message(e.getMessage());
}
}
Aggregations