use of com.qiwenshare.file.domain.UserFile in project qiwen-file by qiwenshare.
the class AsyncTaskComp method deleteUserFile.
public Future<String> deleteUserFile(Long userFileId) {
long begin = System.currentTimeMillis();
UserFile userFile = userFileService.getById(userFileId);
if (userFile.getIsDir() == 1) {
LambdaQueryWrapper<UserFile> userFileLambdaQueryWrapper = new LambdaQueryWrapper<>();
userFileLambdaQueryWrapper.eq(UserFile::getDeleteBatchNum, userFile.getDeleteBatchNum());
List<UserFile> list = userFileService.list(userFileLambdaQueryWrapper);
recoveryFileService.deleteUserFileByDeleteBatchNum(userFile.getDeleteBatchNum());
for (UserFile userFileItem : list) {
Long filePointCount = getFilePointCount(userFileItem.getFileId());
if (filePointCount != null && filePointCount == 0 && userFileItem.getIsDir() == 0) {
FileBean fileBean = fileMapper.selectById(userFileItem.getFileId());
try {
filetransferService.deleteFile(fileBean);
fileMapper.deleteById(fileBean.getFileId());
} catch (Exception e) {
log.error("删除本地文件失败:" + JSON.toJSONString(fileBean));
}
}
}
} else {
recoveryFileService.deleteUserFileByDeleteBatchNum(userFile.getDeleteBatchNum());
Long filePointCount = getFilePointCount(userFile.getFileId());
if (filePointCount != null && filePointCount == 0 && userFile.getIsDir() == 0) {
FileBean fileBean = fileMapper.selectById(userFile.getFileId());
try {
filetransferService.deleteFile(fileBean);
fileMapper.deleteById(fileBean.getFileId());
} catch (Exception e) {
log.error("删除本地文件失败:" + JSON.toJSONString(fileBean));
}
}
}
long end = System.currentTimeMillis();
System.out.println("任务 deleteUserFile 耗时=" + (end - begin));
return new AsyncResult<String>("deleteUserFile");
}
use of com.qiwenshare.file.domain.UserFile 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.file.domain.UserFile in project qiwen-file by qiwenshare.
the class CommonFileController method commonFileList.
@Operation(summary = "获取共享用户文件列表", description = "用来做前台列表展示", tags = { "file" })
@RequestMapping(value = "/commonFileList", method = RequestMethod.GET)
@ResponseBody
public RestResult<FileListVo> commonFileList(@Parameter(description = "用户id", required = true) Long commonFileId, @Parameter(description = "文件路径", required = true) String filePath, @Parameter(description = "当前页", required = true) long currentPage, @Parameter(description = "页面数量", required = true) long pageCount) {
CommonFile commonFile = commonFileService.getById(commonFileId);
UserFile userFile = userFileService.getById(commonFile.getUserFileId());
filePath = userFile.getFilePath() + filePath;
IPage<FileListVo> fileList = userFileService.userFileList(userFile.getUserId(), filePath, currentPage, pageCount);
return RestResult.success().data(fileList);
}
use of com.qiwenshare.file.domain.UserFile 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.file.domain.UserFile in project qiwen-file by qiwenshare.
the class FileController method getFileTree.
@Operation(summary = "获取文件树", description = "文件移动的时候需要用到该接口,用来展示目录树", tags = { "file" })
@RequestMapping(value = "/getfiletree", method = RequestMethod.GET)
@ResponseBody
public RestResult<TreeNode> getFileTree() {
RestResult<TreeNode> result = new RestResult<TreeNode>();
JwtUser sessionUserBean = SessionUtil.getSession();
List<UserFile> userFileList = userFileService.selectFilePathTreeByUserId(sessionUserBean.getUserId());
TreeNode resultTreeNode = new TreeNode();
resultTreeNode.setLabel("/");
resultTreeNode.setId(0L);
long id = 1;
for (int i = 0; i < userFileList.size(); i++) {
UserFile userFile = userFileList.get(i);
String filePath = userFile.getFilePath() + userFile.getFileName() + "/";
Queue<String> queue = new LinkedList<>();
String[] strArr = filePath.split("/");
for (int j = 0; j < strArr.length; j++) {
if (!"".equals(strArr[j]) && strArr[j] != null) {
queue.add(strArr[j]);
}
}
if (queue.size() == 0) {
continue;
}
resultTreeNode = fileDealComp.insertTreeNode(resultTreeNode, id++, "/", queue);
}
List<TreeNode> treeNodeList = resultTreeNode.getChildren();
Collections.sort(treeNodeList, new Comparator<TreeNode>() {
@Override
public int compare(TreeNode o1, TreeNode o2) {
long i = o1.getId() - o2.getId();
return (int) i;
}
});
result.setSuccess(true);
result.setData(resultTreeNode);
return result;
}
Aggregations