Search in sources :

Example 6 with UploadFile

use of com.diboot.file.entity.UploadFile in project diboot by dibo-software.

the class BaseExcelFileController method excelPreview.

/**
 * excel数据预览
 *
 * @param file        excel文件
 * @param entityClass 对应实体class
 * @param params      请求参数
 * @return
 * @throws Exception
 */
public JsonResult<Map<String, Object>> excelPreview(MultipartFile file, Class<?> entityClass, Map<String, Object> params) throws Exception {
    checkIsExcel(file);
    // 保存文件
    UploadFile uploadFile = super.saveFile(file, entityClass);
    uploadFile.setDescription(params.compute("description", (k, v) -> S.defaultIfEmpty(S.valueOf(v), "Excel预览数据")).toString());
    ReadExcelListener<?> listener = getExcelDataListener();
    listener.setUploadFileUuid(uploadFile.getUuid());
    listener.setRequestParams(params);
    // 预览
    listener.setPreview(true);
    // 读取excel
    readExcelFile(file.getInputStream(), uploadFile.getFileType(), listener);
    uploadFile.setDataCount(listener.getTotalCount());
    // 保存文件上传记录
    uploadFileService.createEntity(uploadFile);
    Map<String, Object> dataMap = new HashMap<>(8);
    dataMap.put("uuid", uploadFile.getUuid());
    dataMap.put("tableHead", listener.getTableHead());
    dataMap.put("dataList", listener.getPreviewDataList());
    dataMap.put("totalCount", listener.getTotalCount());
    if (listener.getErrorCount() > 0) {
        dataMap.put("errorCount", listener.getErrorCount());
        dataMap.put("errorMsgs", listener.getErrorMsgs());
    }
    return JsonResult.OK(dataMap);
}
Also used : UploadFile(com.diboot.file.entity.UploadFile) HashMap(java.util.HashMap)

Example 7 with UploadFile

use of com.diboot.file.entity.UploadFile in project diboot by dibo-software.

the class BaseFileController method saveFile.

/**
 * 保存文件
 * @param file
 * @param entityClass
 * @param <T>
 * @return
 * @throws Exception
 */
protected <T> UploadFile saveFile(MultipartFile file, Class<T> entityClass) throws Exception {
    UploadFileResult uploadFileResult = fileStorageService.upload(file);
    UploadFile uploadFile = new UploadFile();
    uploadFile.setUuid(uploadFileResult.getUuid()).setFileName(uploadFileResult.getOriginalFilename()).setFileType(uploadFileResult.getExt()).setRelObjType(entityClass.getSimpleName()).setStoragePath(uploadFileResult.getStorageFullPath());
    String accessUrl = buildAccessUrl(uploadFileResult.getFilename());
    uploadFile.setAccessUrl(accessUrl);
    String description = getString("description");
    uploadFile.setDescription(description);
    // 返回uploadFile对象
    return uploadFile;
}
Also used : UploadFile(com.diboot.file.entity.UploadFile) UploadFileResult(com.diboot.file.dto.UploadFileResult)

Example 8 with UploadFile

use of com.diboot.file.entity.UploadFile in project diboot by dibo-software.

the class BaseFileController method uploadFile.

/**
 * 直接上传文件
 * @param
 * @return
 * @throws Exception
 */
public <T> JsonResult uploadFile(MultipartFile file, Class<T> entityClass) throws Exception {
    if (file == null) {
        throw new BusinessException(Status.FAIL_INVALID_PARAM, "未获取待处理的文件!");
    }
    String originFileName = file.getOriginalFilename();
    if (V.isEmpty(originFileName) || !isValidFileExt(file)) {
        log.debug("非法的文件类型: " + originFileName);
        throw new BusinessException(Status.FAIL_VALIDATION, "请上传合法的文件格式!");
    }
    // 保存文件
    UploadFile uploadFile = saveFile(file, entityClass);
    // 保存上传记录
    createUploadFile(uploadFile);
    // 返回结果
    return JsonResult.OK(new HashMap(16) {

        {
            put("uuid", uploadFile.getUuid());
            put("accessUrl", uploadFile.getAccessUrl());
            put("fileName", uploadFile.getFileName());
        }
    });
}
Also used : BusinessException(com.diboot.core.exception.BusinessException) UploadFile(com.diboot.file.entity.UploadFile) HashMap(java.util.HashMap)

Example 9 with UploadFile

use of com.diboot.file.entity.UploadFile in project diboot by dibo-software.

the class BaseFileController method saveFile.

/**
 * 保存文件
 * @param uploadFileFormDTO
 * @return
 * @throws Exception
 */
protected UploadFile saveFile(UploadFileFormDTO uploadFileFormDTO) throws Exception {
    UploadFileResult uploadFileResult = fileStorageService.upload(uploadFileFormDTO.getFile());
    String accessUrl = buildAccessUrl(uploadFileResult.getFilename());
    UploadFile uploadFile = new UploadFile();
    uploadFile.setUuid(uploadFileResult.getUuid()).setFileName(uploadFileResult.getOriginalFilename()).setFileType(uploadFileResult.getExt()).setRelObjType(uploadFileFormDTO.getRelObjType()).setRelObjField(uploadFileFormDTO.getRelObjField()).setStoragePath(uploadFileResult.getStorageFullPath()).setAccessUrl(accessUrl).setDescription(uploadFileFormDTO.getDescription());
    // 返回uploadFile对象
    return uploadFile;
}
Also used : UploadFile(com.diboot.file.entity.UploadFile) UploadFileResult(com.diboot.file.dto.UploadFileResult)

Example 10 with UploadFile

use of com.diboot.file.entity.UploadFile in project diboot by dibo-software.

the class UploadFileServiceTest method testCreateAndQuery.

@Test
@Transactional
public void testCreateAndQuery() {
    UploadFile uploadFile = new UploadFile();
    uploadFile.setFileName("测试").setFileType("xlsx").setRelObjType(Department.class.getSimpleName()).setRelObjId("1001").setDataCount(10).setStoragePath("/123.xlsx");
    boolean success = uploadFileService.createEntity(uploadFile);
    Assert.assertTrue(success);
    String uuid = uploadFile.getUuid();
    UploadFile uploadFile1 = uploadFileService.getEntity(uuid);
    Assert.assertTrue(uploadFile1 != null);
    List<UploadFile> list = uploadFileService.getUploadedFiles(Department.class.getSimpleName(), 1001L);
    Assert.assertTrue(list.size() > 0);
}
Also used : Department(com.diboot.file.example.custom.Department) UploadFile(com.diboot.file.entity.UploadFile) ApplicationTest(com.diboot.file.example.ApplicationTest) Test(org.junit.Test) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

UploadFile (com.diboot.file.entity.UploadFile)10 BusinessException (com.diboot.core.exception.BusinessException)3 UploadFileResult (com.diboot.file.dto.UploadFileResult)3 HashMap (java.util.HashMap)3 ApplicationTest (com.diboot.file.example.ApplicationTest)1 Department (com.diboot.file.example.custom.Department)1 FileInputStream (java.io.FileInputStream)1 Test (org.junit.Test)1 Transactional (org.springframework.transaction.annotation.Transactional)1 MultipartFile (org.springframework.web.multipart.MultipartFile)1