Search in sources :

Example 1 with FileUploadDTO

use of com.github.lybgeek.upload.dto.FileUploadDTO in project springboot-learning by lyb-geek.

the class FileServiceImpl method sliceUpload.

@Override
public FileUploadDTO sliceUpload(FileUploadRequestDTO fileUploadRequestDTO) {
    try {
        completionService.submit(new FileCallable(UploadModeEnum.RANDOM_ACCESS, fileUploadRequestDTO));
        FileUploadDTO fileUploadDTO = completionService.take().get();
        return fileUploadDTO;
    } catch (InterruptedException e) {
        log.error(e.getMessage(), e);
        throw new BizException(e.getMessage(), 406);
    } catch (ExecutionException e) {
        log.error(e.getMessage(), e);
        throw new BizException(e.getMessage(), 406);
    }
}
Also used : FileUploadDTO(com.github.lybgeek.upload.dto.FileUploadDTO) FileCallable(com.github.lybgeek.upload.concurrent.FileCallable) BizException(com.github.lybgeek.common.exception.BizException) ExecutionException(java.util.concurrent.ExecutionException)

Example 2 with FileUploadDTO

use of com.github.lybgeek.upload.dto.FileUploadDTO in project springboot-learning by lyb-geek.

the class FileServiceImpl method checkFileMd5.

@Override
public FileUploadDTO checkFileMd5(FileUploadRequestDTO param) throws IOException {
    Object uploadProgressObj = redisUtil.hget(FileConstant.FILE_UPLOAD_STATUS, param.getMd5());
    if (uploadProgressObj == null) {
        FileUploadDTO fileMd5DTO = FileUploadDTO.builder().code(FileCheckMd5Status.FILE_NO_UPLOAD.getValue()).build();
        return fileMd5DTO;
    }
    String processingStr = uploadProgressObj.toString();
    boolean processing = Boolean.parseBoolean(processingStr);
    String value = String.valueOf(redisUtil.get(FileConstant.FILE_MD5_KEY + param.getMd5()));
    return fillFileUploadDTO(param, processing, value);
}
Also used : FileUploadDTO(com.github.lybgeek.upload.dto.FileUploadDTO)

Example 3 with FileUploadDTO

use of com.github.lybgeek.upload.dto.FileUploadDTO in project springboot-learning by lyb-geek.

the class SliceUploadTemplate method renameFile.

/**
 * 文件重命名
 *
 * @param toBeRenamed 将要修改名字的文件
 * @param toFileNewName 新的名字
 */
private FileUploadDTO renameFile(File toBeRenamed, String toFileNewName) {
    // 检查要重命名的文件是否存在,是否是文件
    FileUploadDTO fileUploadDTO = new FileUploadDTO();
    if (!toBeRenamed.exists() || toBeRenamed.isDirectory()) {
        log.info("File does not exist: {}", toBeRenamed.getName());
        fileUploadDTO.setUploadComplete(false);
        return fileUploadDTO;
    }
    String ext = FileUtil.getExtension(toFileNewName);
    String p = toBeRenamed.getParent();
    String filePath = p + FileConstant.FILE_SEPARATORCHAR + toFileNewName;
    File newFile = new File(filePath);
    // 修改文件名
    boolean uploadFlag = toBeRenamed.renameTo(newFile);
    fileUploadDTO.setMtime(DateUtil.getCurrentTimeStamp());
    fileUploadDTO.setUploadComplete(uploadFlag);
    fileUploadDTO.setPath(filePath);
    fileUploadDTO.setSize(newFile.length());
    fileUploadDTO.setFileExt(ext);
    fileUploadDTO.setFileId(toFileNewName);
    return fileUploadDTO;
}
Also used : FileUploadDTO(com.github.lybgeek.upload.dto.FileUploadDTO) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Example 4 with FileUploadDTO

use of com.github.lybgeek.upload.dto.FileUploadDTO in project springboot-learning by lyb-geek.

the class FileController method upload.

@PostMapping(value = "/upload")
@ResponseBody
public Result<FileUploadDTO> upload(FileUploadRequestDTO fileUploadRequestDTO) throws IOException {
    boolean isMultipart = ServletFileUpload.isMultipartContent(request);
    FileUploadDTO fileUploadDTO = null;
    if (isMultipart) {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start("upload");
        if (fileUploadRequestDTO.getChunk() != null && fileUploadRequestDTO.getChunks() > 0) {
            fileUploadDTO = fileService.sliceUpload(fileUploadRequestDTO);
        } else {
            fileUploadDTO = fileService.upload(fileUploadRequestDTO);
        }
        stopWatch.stop();
        log.info("{}", stopWatch.prettyPrint());
        return new Result<FileUploadDTO>().setData(fileUploadDTO);
    }
    throw new BizException("上传失败", 406);
}
Also used : FileUploadDTO(com.github.lybgeek.upload.dto.FileUploadDTO) BizException(com.github.lybgeek.common.exception.BizException) StopWatch(org.springframework.util.StopWatch) PostMapping(org.springframework.web.bind.annotation.PostMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 5 with FileUploadDTO

use of com.github.lybgeek.upload.dto.FileUploadDTO in project springboot-learning by lyb-geek.

the class FileController method checkFileMd5.

@RequestMapping(value = "checkFileMd5", method = RequestMethod.POST)
@ResponseBody
public Result<FileUploadDTO> checkFileMd5(String md5, String path) throws IOException {
    FileUploadRequestDTO param = new FileUploadRequestDTO().setPath(path).setMd5(md5);
    FileUploadDTO fileUploadDTO = fileService.checkFileMd5(param);
    return new Result<FileUploadDTO>().setData(fileUploadDTO);
}
Also used : FileUploadRequestDTO(com.github.lybgeek.upload.dto.FileUploadRequestDTO) FileUploadDTO(com.github.lybgeek.upload.dto.FileUploadDTO) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

FileUploadDTO (com.github.lybgeek.upload.dto.FileUploadDTO)6 BizException (com.github.lybgeek.common.exception.BizException)2 File (java.io.File)2 RandomAccessFile (java.io.RandomAccessFile)2 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)2 FileCallable (com.github.lybgeek.upload.concurrent.FileCallable)1 FileUploadRequestDTO (com.github.lybgeek.upload.dto.FileUploadRequestDTO)1 HashMap (java.util.HashMap)1 ExecutionException (java.util.concurrent.ExecutionException)1 StopWatch (org.springframework.util.StopWatch)1 PostMapping (org.springframework.web.bind.annotation.PostMapping)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1