Search in sources :

Example 1 with CopyFile

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");
}
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 2 with CopyFile

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());
    }
}
Also used : RestResult(com.qiwenshare.common.result.RestResult) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) NotLoginException(com.qiwenshare.common.exception.NotLoginException) UserFile(com.qiwenshare.file.domain.UserFile) FileBean(com.qiwenshare.file.domain.FileBean) Copier(com.qiwenshare.ufop.operation.copy.Copier) JSONObject(com.alibaba.fastjson.JSONObject) JwtUser(com.qiwenshare.common.util.security.JwtUser) CopyFile(com.qiwenshare.ufop.operation.copy.domain.CopyFile) Operation(io.swagger.v3.oas.annotations.Operation)

Aggregations

FileBean (com.qiwenshare.file.domain.FileBean)2 UserFile (com.qiwenshare.file.domain.UserFile)2 CopyFile (com.qiwenshare.ufop.operation.copy.domain.CopyFile)2 FileInputStream (java.io.FileInputStream)2 IOException (java.io.IOException)2 JSONObject (com.alibaba.fastjson.JSONObject)1 NotLoginException (com.qiwenshare.common.exception.NotLoginException)1 RestResult (com.qiwenshare.common.result.RestResult)1 JwtUser (com.qiwenshare.common.util.security.JwtUser)1 Copier (com.qiwenshare.ufop.operation.copy.Copier)1 Operation (io.swagger.v3.oas.annotations.Operation)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 HashMap (java.util.HashMap)1 AsyncResult (org.springframework.scheduling.annotation.AsyncResult)1