Search in sources :

Example 1 with DownloadFile

use of com.qiwenshare.ufop.operation.download.domain.DownloadFile 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 2 with DownloadFile

use of com.qiwenshare.ufop.operation.download.domain.DownloadFile in project qiwen-file by qiwenshare.

the class FiletransferService method downloadFile.

@Override
public void downloadFile(HttpServletResponse httpServletResponse, DownloadFileDTO downloadFileDTO) {
    UserFile userFile = userFileMapper.selectById(downloadFileDTO.getUserFileId());
    if (userFile.getIsDir() == 0) {
        FileBean fileBean = fileMapper.selectById(userFile.getFileId());
        Downloader downloader = ufopFactory.getDownloader(fileBean.getStorageType());
        if (downloader == null) {
            log.error("下载失败,文件存储类型不支持下载,storageType:{}", fileBean.getStorageType());
            throw new DownloadException("下载失败");
        }
        DownloadFile downloadFile = new DownloadFile();
        downloadFile.setFileUrl(fileBean.getFileUrl());
        downloadFile.setFileSize(fileBean.getFileSize());
        httpServletResponse.setContentLengthLong(fileBean.getFileSize());
        downloader.download(httpServletResponse, downloadFile);
    } else {
        LambdaQueryWrapper<UserFile> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.likeRight(UserFile::getFilePath, userFile.getFilePath() + userFile.getFileName() + "/").eq(UserFile::getUserId, userFile.getUserId()).eq(UserFile::getIsDir, 0).eq(UserFile::getDeleteFlag, 0);
        List<UserFile> userFileList = userFileMapper.selectList(lambdaQueryWrapper);
        String staticPath = UFOPUtils.getStaticPath();
        String tempPath = staticPath + "temp" + File.separator;
        File tempDirFile = new File(tempPath);
        if (!tempDirFile.exists()) {
            tempDirFile.mkdirs();
        }
        FileOutputStream f = null;
        try {
            f = new FileOutputStream(tempPath + userFile.getFileName() + ".zip");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        CheckedOutputStream csum = new CheckedOutputStream(f, new Adler32());
        ZipOutputStream zos = new ZipOutputStream(csum);
        BufferedOutputStream out = new BufferedOutputStream(zos);
        try {
            for (UserFile userFile1 : userFileList) {
                FileBean fileBean = fileMapper.selectById(userFile1.getFileId());
                Downloader downloader = ufopFactory.getDownloader(fileBean.getStorageType());
                if (downloader == null) {
                    log.error("下载失败,文件存储类型不支持下载,storageType:{}", fileBean.getStorageType());
                    throw new UploadException("下载失败");
                }
                DownloadFile downloadFile = new DownloadFile();
                downloadFile.setFileUrl(fileBean.getFileUrl());
                downloadFile.setFileSize(fileBean.getFileSize());
                InputStream inputStream = downloader.getInputStream(downloadFile);
                BufferedInputStream bis = new BufferedInputStream(inputStream);
                try {
                    zos.putNextEntry(new ZipEntry(userFile1.getFilePath().replace(userFile.getFilePath(), "/") + userFile1.getFileName() + "." + userFile1.getExtendName()));
                    byte[] buffer = new byte[1024];
                    int i = bis.read(buffer);
                    while (i != -1) {
                        out.write(buffer, 0, i);
                        i = bis.read(buffer);
                    }
                } catch (IOException e) {
                    log.error("" + e);
                    e.printStackTrace();
                } finally {
                    try {
                        bis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    try {
                        out.flush();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        } catch (Exception e) {
            log.error("压缩过程中出现异常:" + e);
        } finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        String zipPath = "";
        try {
            Downloader downloader = ufopFactory.getDownloader(StorageTypeEnum.LOCAL.getCode());
            DownloadFile downloadFile = new DownloadFile();
            downloadFile.setFileUrl("temp" + File.separator + userFile.getFileName() + ".zip");
            File tempFile = new File(UFOPUtils.getStaticPath() + downloadFile.getFileUrl());
            httpServletResponse.setContentLengthLong(tempFile.length());
            downloader.download(httpServletResponse, downloadFile);
            zipPath = UFOPUtils.getStaticPath() + "temp" + File.separator + userFile.getFileName() + ".zip";
        } catch (Exception e) {
            // org.apache.catalina.connector.ClientAbortException: java.io.IOException: Connection reset by peer
            if (e.getMessage().contains("ClientAbortException")) {
            // 该异常忽略不做处理
            } else {
                log.error("下传zip文件出现异常:{}", e.getMessage());
            }
        } finally {
            File file = new File(zipPath);
            if (file.exists()) {
                file.delete();
            }
        }
    }
}
Also used : DownloadFile(com.qiwenshare.ufop.operation.download.domain.DownloadFile) ImageInputStream(javax.imageio.stream.ImageInputStream) UploadException(com.qiwenshare.ufop.exception.operation.UploadException) ZipEntry(java.util.zip.ZipEntry) Downloader(com.qiwenshare.ufop.operation.download.Downloader) Adler32(java.util.zip.Adler32) UploadException(com.qiwenshare.ufop.exception.operation.UploadException) DownloadException(com.qiwenshare.ufop.exception.operation.DownloadException) LambdaQueryWrapper(com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper) ZipOutputStream(java.util.zip.ZipOutputStream) DownloadException(com.qiwenshare.ufop.exception.operation.DownloadException) CheckedOutputStream(java.util.zip.CheckedOutputStream) DeleteFile(com.qiwenshare.ufop.operation.delete.domain.DeleteFile) UploadFile(com.qiwenshare.ufop.operation.upload.domain.UploadFile) DownloadFile(com.qiwenshare.ufop.operation.download.domain.DownloadFile) PreviewFile(com.qiwenshare.ufop.operation.preview.domain.PreviewFile)

Example 3 with DownloadFile

use of com.qiwenshare.ufop.operation.download.domain.DownloadFile 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" + "}");
    }
}
Also used : Scanner(java.util.Scanner) WriteFile(com.qiwenshare.ufop.operation.write.domain.WriteFile) DownloadFile(com.qiwenshare.ufop.operation.download.domain.DownloadFile) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) URL(java.net.URL) IOException(java.io.IOException) NotLoginException(com.qiwenshare.common.exception.NotLoginException) LambdaUpdateWrapper(com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper) HttpURLConnection(java.net.HttpURLConnection) JSONObject(com.alibaba.fastjson.JSONObject) NotLoginException(com.qiwenshare.common.exception.NotLoginException) UserFile(com.qiwenshare.file.domain.UserFile) FileBean(com.qiwenshare.file.domain.FileBean) Writer(com.qiwenshare.ufop.operation.write.Writer) PrintWriter(java.io.PrintWriter) PrintWriter(java.io.PrintWriter)

Example 4 with DownloadFile

use of com.qiwenshare.ufop.operation.download.domain.DownloadFile in project qiwen-file by qiwenshare.

the class FileService method unzipFile.

@Override
public void unzipFile(long userFileId, int unzipMode, String filePath) {
    UserFile userFile = userFileMapper.selectById(userFileId);
    FileBean fileBean = fileMapper.selectById(userFile.getFileId());
    File destFile = new File(UFOPUtils.getStaticPath() + "temp" + File.separator + fileBean.getFileUrl());
    Downloader downloader = ufopFactory.getDownloader(fileBean.getStorageType());
    DownloadFile downloadFile = new DownloadFile();
    downloadFile.setFileUrl(fileBean.getFileUrl());
    downloadFile.setFileSize(fileBean.getFileSize());
    InputStream inputStream = downloader.getInputStream(downloadFile);
    try {
        FileUtils.copyInputStreamToFile(inputStream, destFile);
    } catch (IOException e) {
        e.printStackTrace();
    }
    String extendName = userFile.getExtendName();
    String unzipUrl = UFOPUtils.getTempFile(fileBean.getFileUrl()).getAbsolutePath().replace("." + extendName, "");
    List<String> fileEntryNameList = new ArrayList<>();
    if ("zip".equals(extendName)) {
        fileEntryNameList = FileOperation.unzip(destFile, unzipUrl);
    } else if ("rar".equals(extendName)) {
        try {
            fileEntryNameList = FileOperation.unrar(destFile, unzipUrl);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("rar解压失败" + e);
            throw new QiwenException(500001, "rar解压异常:" + e.getMessage());
        }
    } else {
        throw new QiwenException(500002, "不支持的文件格式!");
    }
    if (destFile.exists()) {
        destFile.delete();
    }
    if (!fileEntryNameList.isEmpty() && unzipMode == 1) {
        UserFile qiwenDir = QiwenFileUtil.getQiwenDir(userFile.getUserId(), userFile.getFilePath(), userFile.getFileName());
        userFileMapper.insert(qiwenDir);
    }
    for (int i = 0; i < fileEntryNameList.size(); i++) {
        String entryName = fileEntryNameList.get(i);
        asyncTaskComp.saveUnzipFile(userFile, fileBean, unzipMode, entryName, filePath);
    }
}
Also used : DownloadFile(com.qiwenshare.ufop.operation.download.domain.DownloadFile) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Downloader(com.qiwenshare.ufop.operation.download.Downloader) IOException(java.io.IOException) QiwenException(com.qiwenshare.common.exception.QiwenException) IOException(java.io.IOException) QiwenException(com.qiwenshare.common.exception.QiwenException) UserFile(com.qiwenshare.file.domain.UserFile) FileBean(com.qiwenshare.file.domain.FileBean) UserFile(com.qiwenshare.file.domain.UserFile) DownloadFile(com.qiwenshare.ufop.operation.download.domain.DownloadFile) File(java.io.File)

Aggregations

DownloadFile (com.qiwenshare.ufop.operation.download.domain.DownloadFile)4 FileBean (com.qiwenshare.file.domain.FileBean)3 UserFile (com.qiwenshare.file.domain.UserFile)3 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 QiwenException (com.qiwenshare.common.exception.QiwenException)2 Downloader (com.qiwenshare.ufop.operation.download.Downloader)2 Writer (com.qiwenshare.ufop.operation.write.Writer)2 WriteFile (com.qiwenshare.ufop.operation.write.domain.WriteFile)2 JSONObject (com.alibaba.fastjson.JSONObject)1 LambdaQueryWrapper (com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper)1 LambdaUpdateWrapper (com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper)1 NotLoginException (com.qiwenshare.common.exception.NotLoginException)1 JwtUser (com.qiwenshare.common.util.security.JwtUser)1 DownloadException (com.qiwenshare.ufop.exception.operation.DownloadException)1 UploadException (com.qiwenshare.ufop.exception.operation.UploadException)1 DeleteFile (com.qiwenshare.ufop.operation.delete.domain.DeleteFile)1 PreviewFile (com.qiwenshare.ufop.operation.preview.domain.PreviewFile)1 UploadFile (com.qiwenshare.ufop.operation.upload.domain.UploadFile)1 Operation (io.swagger.v3.oas.annotations.Operation)1