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);
}
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;
}
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());
}
});
}
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;
}
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);
}
Aggregations