Search in sources :

Example 1 with Uploader

use of com.qiwenshare.ufop.operation.upload.Uploader 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)

Aggregations

LambdaQueryWrapper (com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper)1 LambdaUpdateWrapper (com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper)1 DownloadException (com.qiwenshare.ufop.exception.operation.DownloadException)1 UploadException (com.qiwenshare.ufop.exception.operation.UploadException)1 Uploader (com.qiwenshare.ufop.operation.upload.Uploader)1 UploadFile (com.qiwenshare.ufop.operation.upload.domain.UploadFile)1 UploadFileResult (com.qiwenshare.ufop.operation.upload.domain.UploadFileResult)1 BufferedImage (java.awt.image.BufferedImage)1