Search in sources :

Example 1 with UserFile

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");
}
Also used : UserFile(com.qiwenshare.file.domain.UserFile) FileBean(com.qiwenshare.file.domain.FileBean) AsyncResult(org.springframework.scheduling.annotation.AsyncResult) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) LambdaQueryWrapper(com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper)

Example 2 with UserFile

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");
}
Also used : HashMap(java.util.HashMap) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) UserFile(com.qiwenshare.file.domain.UserFile) FileBean(com.qiwenshare.file.domain.FileBean) CopyFile(com.qiwenshare.ufop.operation.copy.domain.CopyFile) CopyFile(com.qiwenshare.ufop.operation.copy.domain.CopyFile) UserFile(com.qiwenshare.file.domain.UserFile) File(java.io.File) AsyncResult(org.springframework.scheduling.annotation.AsyncResult)

Example 3 with UserFile

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);
}
Also used : CommonFile(com.qiwenshare.file.domain.CommonFile) UserFile(com.qiwenshare.file.domain.UserFile) CommonFileListVo(com.qiwenshare.file.vo.commonfile.CommonFileListVo) FileListVo(com.qiwenshare.file.vo.file.FileListVo) Operation(io.swagger.v3.oas.annotations.Operation)

Example 4 with UserFile

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("修改文件成功");
}
Also used : WriteFile(com.qiwenshare.ufop.operation.write.domain.WriteFile) DownloadFile(com.qiwenshare.ufop.operation.download.domain.DownloadFile) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) QiwenException(com.qiwenshare.common.exception.QiwenException) IOException(java.io.IOException) QiwenException(com.qiwenshare.common.exception.QiwenException) ByteArrayInputStream(java.io.ByteArrayInputStream) UserFile(com.qiwenshare.file.domain.UserFile) FileBean(com.qiwenshare.file.domain.FileBean) JwtUser(com.qiwenshare.common.util.security.JwtUser) Writer(com.qiwenshare.ufop.operation.write.Writer) Operation(io.swagger.v3.oas.annotations.Operation)

Example 5 with UserFile

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;
}
Also used : RestResult(com.qiwenshare.common.result.RestResult) TreeNode(com.qiwenshare.file.util.TreeNode) UserFile(com.qiwenshare.file.domain.UserFile) JwtUser(com.qiwenshare.common.util.security.JwtUser) Operation(io.swagger.v3.oas.annotations.Operation)

Aggregations

UserFile (com.qiwenshare.file.domain.UserFile)26 Operation (io.swagger.v3.oas.annotations.Operation)15 JwtUser (com.qiwenshare.common.util.security.JwtUser)14 MyLog (com.qiwenshare.common.anno.MyLog)8 IOException (java.io.IOException)8 FileBean (com.qiwenshare.file.domain.FileBean)7 LambdaUpdateWrapper (com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper)5 RestResult (com.qiwenshare.common.result.RestResult)5 JSONObject (com.alibaba.fastjson.JSONObject)4 NotLoginException (com.qiwenshare.common.exception.NotLoginException)4 FileListVo (com.qiwenshare.file.vo.file.FileListVo)4 IPage (com.baomidou.mybatisplus.core.metadata.IPage)3 Page (com.baomidou.mybatisplus.extension.plugins.pagination.Page)3 QiwenException (com.qiwenshare.common.exception.QiwenException)3 DownloadFile (com.qiwenshare.ufop.operation.download.domain.DownloadFile)3 FileInputStream (java.io.FileInputStream)3 LambdaQueryWrapper (com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper)2 RecoveryFile (com.qiwenshare.file.domain.RecoveryFile)2 ShareFile (com.qiwenshare.file.domain.ShareFile)2 FileModel (com.qiwenshare.file.util.FileModel)2