package jnpf.controller; import com.alibaba.fastjson.JSONObject; import io.swagger.v3.oas.annotations.tags.Tag; import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.Parameters; import io.swagger.v3.oas.annotations.Operation; import jnpf.base.ActionResult; import jnpf.base.DictionaryDataApi; import jnpf.model.UploaderVO; import jnpf.base.vo.DownloadVO; import jnpf.config.ConfigValueUtil; import jnpf.constant.FileTypeConstant; import jnpf.constant.MsgCode; import jnpf.entity.FileParameter; import jnpf.service.FileService; import jnpf.util.FileExport; import jnpf.exception.DataException; import jnpf.file.FileApi; import jnpf.file.FileUploadApi; import jnpf.model.*; import jnpf.provider.file.FileUploadProvider; import jnpf.util.*; import lombok.extern.slf4j.Slf4j; import org.apache.dubbo.config.annotation.DubboReference; import org.dromara.x.file.storage.core.FileInfo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import java.io.*; import java.util.*; /** * 通用控制器 * * @author JNPF开发平台组 * @version V3.1.0 * @copyright 引迈信息技术有限公司(https://www.jnpfsoft.com) * @date 2021-03-23 */ @Slf4j @Tag(name = "公共", description = "file") @RestController public class UtilsController implements FileApi { @Autowired private ConfigValueUtil configValueUtil; @Autowired private RedisUtil redisUtil; @Autowired private DictionaryDataApi dictionaryDataApi; @DubboReference private FileUploadProvider fileUploadProvider; @Autowired private FileUploadApi fileUploadApi; @Autowired private FileApi fileApi; @Autowired private FileExport fileExport; @Autowired private FileService fileService; /** * 语言列表 * * @return */ // @NoDataSourceBind // @Operation(summary = "语言列表") // @GetMapping("/Language") // public ActionResult> getList() { // String dictionaryTypeId = "dc6b2542d94b407cac61ec1d59592901"; // List list = dictionaryDataApi.getList(dictionaryTypeId); // List language = JsonUtil.getJsonToList(list, LanguageVO.class); // ListVO vo = new ListVO(); // vo.setList(language); // return ActionResult.success(vo); // } /** * 图形验证码 * * @return */ @NoDataSourceBind @Operation(summary = "图形验证码") @GetMapping("/ImageCode/{timestamp}") @Parameters({ @Parameter(name = "timestamp", description = "时间戳",required = true), }) public void imageCode(@PathVariable("timestamp") String timestamp) { DownUtil.downCode(null); redisUtil.insert(timestamp, ServletUtil.getSession().getAttribute(CodeUtil.RANDOMCODEKEY), 120); } /** * 上传文件/图片 * * @return */ @NoDataSourceBind @Operation(summary = "上传文件/图片") @PostMapping(value = "/Uploader/{type}", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) @Parameters({ @Parameter(name = "type", description = "类型",required = true), }) public ActionResult uploader(@PathVariable("type") String type, MergeChunkDto mergeChunkDto, MultipartFile file) { mergeChunkDto.setType(type); return ActionResult.success(fileService.uploadFile(mergeChunkDto, file)); } /** * 图片转成base64 * * @return */ @NoDataSourceBind() @Operation(summary = "图片转成base64") @PostMapping(value = "/Uploader/imgToBase64", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) public ActionResult imgToBase64(@RequestParam("file") MultipartFile file) throws IOException { String encode = cn.hutool.core.codec.Base64.encode(file.getBytes()); Object base64Name = ""; if (StringUtil.isNotEmpty(encode)) { base64Name = "data:image/jpeg;base64,"+encode; } return ActionResult.success(base64Name); } /** * 获取下载文件链接 * * @return */ @NoDataSourceBind @Operation(summary = "获取下载文件链接") @GetMapping("/Download/{type}/{fileName}") @Parameters({ @Parameter(name = "type", description = "类型",required = true), @Parameter(name = "fileName", description = "文件名称",required = true), }) public ActionResult downloadUrl(@PathVariable("type") String type, @PathVariable("fileName") String fileName) { boolean exists = FileUploadUtils.exists(new FileParameter(type, fileName)); if (exists) { jnpf.base.vo.DownloadVO vo = DownloadVO.builder().name(fileName).url(UploaderUtil.uploaderFile(fileName + "#" + type)).build(); return ActionResult.success(vo); } return ActionResult.fail(MsgCode.FA018.get()); } /** * 获取全部下载文件链接(打包下载) * * @return */ @NoDataSourceBind @Operation(summary = "获取全部下载文件链接(打包下载)") @PostMapping("/PackDownload/{type}") @Parameters({ @Parameter(name = "type", description = "类型",required = true), }) public ActionResult packDownloadUrl(@PathVariable("type") String type, @RequestBody List> fileInfoList) throws Exception { type = XSSEscape.escape(type); if(fileInfoList == null || fileInfoList.isEmpty()) { return ActionResult.fail(MsgCode.FA047.get()); } String zipTempFilePath = null; String zipFileId = RandomUtil.uuId() + ".zip"; for(Map fileInfoMap : fileInfoList) { String fileId = XSSEscape.escape((String)fileInfoMap.get("fileId")).trim(); String fileName = XSSEscape.escape((String)fileInfoMap.get("fileName")).trim(); if(StringUtil.isEmpty(fileId) || StringUtil.isEmpty(fileName)) { continue; } FileParameter fileParameter = new FileParameter(type, fileId); if(FileUploadUtils.exists(fileParameter)) { // String typePath =getPath(type); // if(fileId.indexOf(",") >= 0) { // typePath += fileId.substring(0, fileId.lastIndexOf(",")+1).replaceAll(",", "/"); // fileId = fileId.substring(fileId.lastIndexOf(",")+1); // } if(zipTempFilePath == null) { zipTempFilePath = FileUploadUtils.getLocalBasePath() + getPath(FileTypeConstant.FILEZIPDOWNTEMPPATH); if(!new File(zipTempFilePath).exists()) { new File(zipTempFilePath).mkdirs(); } zipTempFilePath += zipFileId; } // fileParameter.setRemotePath(typePath); String finalZipTempFilePath = zipTempFilePath; String finalFileName = fileName; FileUploadUtils.downloadFile(fileParameter, inputStream -> { try { ZipUtil.fileAddToZip(finalZipTempFilePath, inputStream, finalFileName); } catch (Exception e) { throw new RuntimeException(e); } }); } } if(zipTempFilePath == null) { return ActionResult.fail(MsgCode.FA018.get()); } //将文件上传到默认文件服务器 String newFileId = zipFileId; if(!"local".equals(FileUploadUtils.getDefaultPlatform())) { //不是本地,说明是其他文件服务器,将zip文件上传到其他服务器里,方便下载 File zipFile = new File(zipTempFilePath); FileInfo fileInfo = FileUploadUtils.uploadFile(new FileParameter(FileTypeConstant.FILEZIPDOWNTEMPPATH, zipFileId), zipFile); zipFile.delete(); newFileId = fileInfo.getFilename(); } jnpf.base.vo.DownloadVO vo = DownloadVO.builder().name(zipFileId).url(UploaderUtil.uploaderFile(newFileId + "#" + FileTypeConstant.FILEZIPDOWNTEMPPATH)).build(); Map map = new HashMap(); map.put("downloadVo", vo); map.put("downloadName", "文件" + zipFileId); return ActionResult.success(map); } /** * 下载文件链接 * * @return */ @NoDataSourceBind @Operation(summary = "下载文件链接") @GetMapping("/Download") public void downloadFile(@RequestParam("encryption") String encryption, @RequestParam("name") String downName) throws DataException { fileService.downloadFile(encryption, downName); } /** * 下载文件链接 * * @return */ // @NoDataSourceBind // @Operation(summary = "下载模板文件链接") // @GetMapping("/DownloadModel") // public void downloadModel() { // HttpServletRequest request = ServletUtil.getRequest(); // String reqJson = request.getParameter("encryption"); // String fileNameAll = DesUtil.aesDecode(reqJson); // if (!StringUtil.isEmpty(fileNameAll)) { // String token = fileNameAll.split("#")[0]; // if (TicketUtil.parseTicket(token) != null) { // TicketUtil.deleteTicket(token); // String fileName = fileNameAll.split("#")[1]; // String filePath = FilePathUtil.getFilePath(FileTypeConstant.TEMPLATEFILE); // // 下载文件 // FileUploadUtils.downloadFile(new FileParameter(filePath, fileName), inputStream -> { // FileDownloadUtil.outFile(inputStream, fileName); // }); // }else { // throw new RuntimeException(MsgCode.FA039.get()); // } // }else { // throw new RuntimeException(MsgCode.FA039.get()); // } // } /** * 获取图片 * * @param fileName * @param type * @return */ @NoDataSourceBind @Operation(summary = "获取图片") @GetMapping("/Image/{type}/{fileName}") @Parameters({ @Parameter(name = "type", description = "类型",required = true), @Parameter(name = "fileName", description = "名称",required = true), }) public void downLoadImg(@PathVariable("type") String type, @PathVariable("fileName") String fileName, @RequestParam(name = "s", required = false) String securityKey, @RequestParam(name = "t", required = false) String t) throws DataException { fileService.flushFile(type, fileName, securityKey, StringUtil.isEmpty(t)); } /** * 获取IM聊天图片 * 注意 后缀名前端故意把 .替换@ * * @param fileName * @return */ // @NoDataSourceBind // @Operation(summary = "获取IM聊天图片") // @GetMapping("/IMImage/{fileName}") // @Parameters({ // @Parameter(name = "fileName", description = "名称",required = true), // }) // public void imImage(@PathVariable("fileName") String fileName) { // FileUploadUtils.downloadFile(new FileParameter(configValueUtil.getImContentFilePath(), fileName), inputStream -> { // FileDownloadUtil.flushFile(inputStream, fileName); // }); // } /** * 获取IM聊天语音 * 注意 后缀名前端故意把 .替换@ * * @param fileName * @return */ // @NoDataSourceBind // @Operation(summary = "获取IM聊天语音") // @GetMapping("/IMVoice/{fileName}") // @Parameters({ // @Parameter(name = "fileName", description = "名称",required = true), // }) // public void imVoice(@PathVariable("fileName") String fileName) { // fileName = fileName.replaceAll("@", "."); // String finalFileName = fileName; // FileUploadUtils.downloadFile(new FileParameter(configValueUtil.getImContentFilePath(), fileName), inputStream -> { // FileDownloadUtil.flushFile(inputStream, finalFileName); // }); // } /** * app启动获取信息 * * @param appName * @return */ @NoDataSourceBind @Operation(summary = "app启动获取信息") @GetMapping("/AppStartInfo/{appName}") @Parameters({ @Parameter(name = "appName", description = "名称",required = true), }) public ActionResult getAppStartInfo(@PathVariable("appName") String appName) { JSONObject object = new JSONObject(); object.put("AppVersion", configValueUtil.getAppVersion()); object.put("AppUpdateContent", configValueUtil.getAppUpdateContent()); return ActionResult.success(object); } /** * 大屏图片下载 * * @param type * @param fileName */ @NoDataSourceBind @Operation(summary = "获取图片") @GetMapping("/VisusalImg/{bivisualpath}/{type}/{fileName}") @Parameters({ @Parameter(name = "type", description = "类型",required = true), @Parameter(name = "bivisualpath", description = "路径",required = true), @Parameter(name = "fileName", description = "名称",required = true), }) public void downVisusalImg(@PathVariable("type") String type,@PathVariable("bivisualpath") String bivisualpath, @PathVariable("fileName") String fileName) { fileName = XSSEscape.escapePath(fileName); type = XSSEscape.escapePath(type); String filePath = getPath(FileTypeConstant.BIVISUALPATH); String finalFileName = fileName; FileUploadUtils.downloadFile(new FileParameter(filePath + type + "/", fileName), inputStream -> { FileDownloadUtil.flushFile(inputStream, finalFileName); }); } @Operation(summary = "分片上传获取") @GetMapping("/chunk") public ActionResult checkChunk(Chunk chunk) { return ActionResult.success(fileService.checkChunk(chunk)); } @Operation(summary = "分片上传附件") @PostMapping("/chunk") public ActionResult upload(Chunk chunk, MultipartFile file) { return ActionResult.success(fileService.uploadChunk(chunk, file)); } @Operation(summary = "分片组装") @PostMapping("/merge") public ActionResult merge(MergeChunkDto mergeChunkDto) { return ActionResult.success(fileService.mergeChunk(mergeChunkDto)); } //---------------------- /** * 通过type获取路径 * * @param type 类型 * @return */ @Override @NoDataSourceBind() @GetMapping("/getPath/{type}") public String getPath(@PathVariable("type") String type) { return fileService.getPath(type); } /** * 导出 * * @param exportModel * @return */ @Override @NoDataSourceBind @PostMapping("/export") public jnpf.base.vo.DownloadVO exportFile(@RequestBody ExportModel exportModel) { jnpf.base.vo.DownloadVO downloadVO = fileExport.exportFile(exportModel.getClazz(), exportModel.getFilePath(), exportModel.getFileName(), exportModel.getTableName()); return downloadVO; } /** * 获取代码生成器默认保存路径 * * @return */ @Override @NoDataSourceBind() @GetMapping("/getLocalBasePath") public String getLocalBasePath() { return fileService.getLocalBasePath(); } /** * 获取文件服务器基础路径 * * @return */ // @Override // @NoDataSourceBind() // @GetMapping("/getBasePath") // public String getBasePath() { // return FileUploadUtils.getBasePath(); // } /** * 获取允许文件预览类型 * * @return */ @Override @NoDataSourceBind() @GetMapping("/getAllowPreviewFileType") public String getAllowPreviewFileType() { return configValueUtil.getAllowPreviewFileType(); } @Override @NoDataSourceBind() @GetMapping("/fileExists") public Boolean fileExists(@RequestParam("type") String path, @RequestParam("fileName") String fileName) { return fileService.fileExists(path, fileName); } @Operation(summary = "预览文件") @GetMapping("/Uploader/Preview") public ActionResult Preview(PreviewParams previewParams) { return ActionResult.success(MsgCode.SU000.get(), fileService.previewFile(previewParams)); } }