Search in sources :

Example 1 with UploadException

use of com.qiwenshare.ufop.exception.operation.UploadException 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 2 with UploadException

use of com.qiwenshare.ufop.exception.operation.UploadException in project qiwen-file by qiwenshare.

the class FiletransferService method uploadFile.

@Override
public void uploadFile(HttpServletRequest request, UploadFileDTO uploadFileDto, Long userId) {
    UploadFile uploadFile = new UploadFile();
    uploadFile.setChunkNumber(uploadFileDto.getChunkNumber());
    uploadFile.setChunkSize(uploadFileDto.getChunkSize());
    uploadFile.setTotalChunks(uploadFileDto.getTotalChunks());
    uploadFile.setIdentifier(uploadFileDto.getIdentifier());
    uploadFile.setTotalSize(uploadFileDto.getTotalSize());
    uploadFile.setCurrentChunkSize(uploadFileDto.getCurrentChunkSize());
    Uploader uploader = ufopFactory.getUploader();
    if (uploader == null) {
        log.error("上传失败,请检查storageType是否配置正确");
        throw new UploadException("上传失败");
    }
    List<UploadFileResult> uploadFileResultList;
    try {
        uploadFileResultList = uploader.upload(request, uploadFile);
    } catch (Exception e) {
        log.error("上传失败,请检查UFOP连接配置是否正确");
        throw new UploadException("上传失败");
    }
    for (int i = 0; i < uploadFileResultList.size(); i++) {
        UploadFileResult uploadFileResult = uploadFileResultList.get(i);
        FileBean fileBean = new FileBean();
        BeanUtil.copyProperties(uploadFileDto, fileBean);
        String relativePath = uploadFileDto.getRelativePath();
        if (UploadFileStatusEnum.SUCCESS.equals(uploadFileResult.getStatus())) {
            fileBean.setFileUrl(uploadFileResult.getFileUrl());
            fileBean.setFileSize(uploadFileResult.getFileSize());
            fileBean.setStorageType(uploadFileResult.getStorageType().getCode());
            fileBean.setFileStatus(1);
            fileBean.setCreateTime(DateUtil.getCurrentTime());
            fileBean.setCreateUserId(userId);
            fileMapper.insert(fileBean);
            UserFile userFile = new UserFile();
            if (relativePath.contains("/")) {
                userFile.setFilePath(uploadFileDto.getFilePath() + UFOPUtils.getParentPath(relativePath) + "/");
                fileDealComp.restoreParentFilePath(uploadFileDto.getFilePath() + UFOPUtils.getParentPath(relativePath) + "/", userId);
                fileDealComp.deleteRepeatSubDirFile(uploadFileDto.getFilePath(), userId);
            } else {
                userFile.setFilePath(uploadFileDto.getFilePath());
            }
            userFile.setUserId(userId);
            userFile.setFileName(uploadFileResult.getFileName());
            userFile.setExtendName(uploadFileResult.getExtendName());
            userFile.setDeleteFlag(0);
            userFile.setIsDir(0);
            List<UserFile> userFileList = userFileMapper.selectList(new QueryWrapper<>(userFile));
            if (userFileList.size() > 0) {
                String fileName = fileDealComp.getRepeatFileName(userFile, userFile.getFilePath());
                userFile.setFileName(fileName);
            }
            userFile.setFileId(fileBean.getFileId());
            userFile.setUploadTime(DateUtil.getCurrentTime());
            userFileMapper.insert(userFile);
            fileDealComp.uploadESByUserFileId(userFile.getUserFileId());
            LambdaQueryWrapper<UploadTaskDetail> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(UploadTaskDetail::getIdentifier, uploadFileDto.getIdentifier());
            uploadTaskDetailMapper.delete(lambdaQueryWrapper);
            LambdaUpdateWrapper<UploadTask> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            lambdaUpdateWrapper.set(UploadTask::getUploadStatus, UploadFileStatusEnum.SUCCESS.getCode()).eq(UploadTask::getIdentifier, uploadFileDto.getIdentifier());
            uploadTaskMapper.update(null, lambdaUpdateWrapper);
            try {
                if (UFOPUtils.isImageFile(uploadFileResult.getExtendName())) {
                    BufferedImage src = uploadFileResult.getBufferedImage();
                    Image image = new Image();
                    image.setImageWidth(src.getWidth());
                    image.setImageHeight(src.getHeight());
                    image.setFileId(fileBean.getFileId());
                    imageMapper.insert(image);
                }
            } catch (Exception e) {
                log.error("生成图片缩略图失败!");
            }
        } else if (UploadFileStatusEnum.UNCOMPLATE.equals(uploadFileResult.getStatus())) {
            UploadTaskDetail uploadTaskDetail = new UploadTaskDetail();
            uploadTaskDetail.setFilePath(uploadFileDto.getFilePath());
            if (relativePath.contains("/")) {
                uploadTaskDetail.setFilePath(uploadFileDto.getFilePath() + UFOPUtils.getParentPath(relativePath) + "/");
            } else {
                uploadTaskDetail.setFilePath(uploadFileDto.getFilePath());
            }
            uploadTaskDetail.setFilename(uploadFileDto.getFilename());
            uploadTaskDetail.setChunkNumber(uploadFileDto.getChunkNumber());
            uploadTaskDetail.setChunkSize((int) uploadFileDto.getChunkSize());
            uploadTaskDetail.setRelativePath(uploadFileDto.getRelativePath());
            uploadTaskDetail.setTotalChunks(uploadFileDto.getTotalChunks());
            uploadTaskDetail.setTotalSize((int) uploadFileDto.getTotalSize());
            uploadTaskDetail.setIdentifier(uploadFileDto.getIdentifier());
            uploadTaskDetailMapper.insert(uploadTaskDetail);
        } else if (UploadFileStatusEnum.FAIL.equals(uploadFileResult.getStatus())) {
            LambdaQueryWrapper<UploadTaskDetail> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(UploadTaskDetail::getIdentifier, uploadFileDto.getIdentifier());
            uploadTaskDetailMapper.delete(lambdaQueryWrapper);
            LambdaUpdateWrapper<UploadTask> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            lambdaUpdateWrapper.set(UploadTask::getUploadStatus, UploadFileStatusEnum.FAIL.getCode()).eq(UploadTask::getIdentifier, uploadFileDto.getIdentifier());
            uploadTaskMapper.update(null, lambdaUpdateWrapper);
        }
    }
}
Also used : UploadException(com.qiwenshare.ufop.exception.operation.UploadException) BufferedImage(java.awt.image.BufferedImage) UploadException(com.qiwenshare.ufop.exception.operation.UploadException) DownloadException(com.qiwenshare.ufop.exception.operation.DownloadException) BufferedImage(java.awt.image.BufferedImage) LambdaQueryWrapper(com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper) LambdaUpdateWrapper(com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper) UploadFile(com.qiwenshare.ufop.operation.upload.domain.UploadFile) UploadFileResult(com.qiwenshare.ufop.operation.upload.domain.UploadFileResult) Uploader(com.qiwenshare.ufop.operation.upload.Uploader)

Example 3 with UploadException

use of com.qiwenshare.ufop.exception.operation.UploadException in project qiwen-file by qiwenshare.

the class FiletransferService method previewPictureFile.

@Override
public void previewPictureFile(HttpServletResponse httpServletResponse, PreviewDTO previewDTO) {
    byte[] bytesUrl = Base64.getDecoder().decode(previewDTO.getUrl());
    PictureFile pictureFile = new PictureFile();
    pictureFile.setFileUrl(new String(bytesUrl));
    pictureFile = pictureFileMapper.selectOne(new QueryWrapper<>(pictureFile));
    Previewer previewer = ufopFactory.getPreviewer(pictureFile.getStorageType());
    if (previewer == null) {
        log.error("预览失败,文件存储类型不支持预览,storageType:{}", pictureFile.getStorageType());
        throw new UploadException("预览失败");
    }
    PreviewFile previewFile = new PreviewFile();
    previewFile.setFileUrl(pictureFile.getFileUrl());
    // previewFile.setFileSize(pictureFile.getFileSize());
    try {
        String mime = MimeUtils.getMime(pictureFile.getExtendName());
        httpServletResponse.setHeader("Content-Type", mime);
        String fileName = pictureFile.getFileName() + "." + pictureFile.getExtendName();
        try {
            fileName = new String(fileName.getBytes("utf-8"), "ISO-8859-1");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        // 设置文件名
        httpServletResponse.addHeader("Content-Disposition", "fileName=" + fileName);
        previewer.imageOriginalPreview(httpServletResponse, previewFile);
    } catch (Exception e) {
        // org.apache.catalina.connector.ClientAbortException: java.io.IOException: 你的主机中的软件中止了一个已建立的连接。
        if (e.getMessage().contains("ClientAbortException")) {
        // 该异常忽略不做处理
        } else {
            log.error("预览文件出现异常:{}", e.getMessage());
        }
    }
}
Also used : Previewer(com.qiwenshare.ufop.operation.preview.Previewer) LambdaQueryWrapper(com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) UploadException(com.qiwenshare.ufop.exception.operation.UploadException) PreviewFile(com.qiwenshare.ufop.operation.preview.domain.PreviewFile) UploadException(com.qiwenshare.ufop.exception.operation.UploadException) DownloadException(com.qiwenshare.ufop.exception.operation.DownloadException)

Example 4 with UploadException

use of com.qiwenshare.ufop.exception.operation.UploadException in project qiwen-file by qiwenshare.

the class FiletransferService method previewFile.

@Override
public void previewFile(HttpServletResponse httpServletResponse, PreviewDTO previewDTO) {
    UserFile userFile = userFileMapper.selectById(previewDTO.getUserFileId());
    FileBean fileBean = fileMapper.selectById(userFile.getFileId());
    Previewer previewer = ufopFactory.getPreviewer(fileBean.getStorageType());
    if (previewer == null) {
        log.error("预览失败,文件存储类型不支持预览,storageType:{}", fileBean.getStorageType());
        throw new UploadException("预览失败");
    }
    PreviewFile previewFile = new PreviewFile();
    previewFile.setFileUrl(fileBean.getFileUrl());
    try {
        if ("true".equals(previewDTO.getIsMin())) {
            previewer.imageThumbnailPreview(httpServletResponse, previewFile);
        } else {
            previewer.imageOriginalPreview(httpServletResponse, previewFile);
        }
    } catch (Exception e) {
        // org.apache.catalina.connector.ClientAbortException: java.io.IOException: 你的主机中的软件中止了一个已建立的连接。
        if (e.getMessage().contains("ClientAbortException")) {
        // 该异常忽略不做处理
        } else {
            log.error("预览文件出现异常:{}", e.getMessage());
        }
    }
}
Also used : Previewer(com.qiwenshare.ufop.operation.preview.Previewer) UploadException(com.qiwenshare.ufop.exception.operation.UploadException) PreviewFile(com.qiwenshare.ufop.operation.preview.domain.PreviewFile) UploadException(com.qiwenshare.ufop.exception.operation.UploadException) DownloadException(com.qiwenshare.ufop.exception.operation.DownloadException)

Aggregations

DownloadException (com.qiwenshare.ufop.exception.operation.DownloadException)4 UploadException (com.qiwenshare.ufop.exception.operation.UploadException)4 LambdaQueryWrapper (com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper)3 PreviewFile (com.qiwenshare.ufop.operation.preview.domain.PreviewFile)3 Previewer (com.qiwenshare.ufop.operation.preview.Previewer)2 UploadFile (com.qiwenshare.ufop.operation.upload.domain.UploadFile)2 QueryWrapper (com.baomidou.mybatisplus.core.conditions.query.QueryWrapper)1 LambdaUpdateWrapper (com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper)1 DeleteFile (com.qiwenshare.ufop.operation.delete.domain.DeleteFile)1 Downloader (com.qiwenshare.ufop.operation.download.Downloader)1 DownloadFile (com.qiwenshare.ufop.operation.download.domain.DownloadFile)1 Uploader (com.qiwenshare.ufop.operation.upload.Uploader)1 UploadFileResult (com.qiwenshare.ufop.operation.upload.domain.UploadFileResult)1 BufferedImage (java.awt.image.BufferedImage)1 Adler32 (java.util.zip.Adler32)1 CheckedOutputStream (java.util.zip.CheckedOutputStream)1 ZipEntry (java.util.zip.ZipEntry)1 ZipOutputStream (java.util.zip.ZipOutputStream)1 ImageInputStream (javax.imageio.stream.ImageInputStream)1