| | |
| | | package com.kidgrow.filecenter.service.impl; |
| | | |
| | | import cn.hutool.core.util.IdUtil; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | |
| | | import com.kidgrow.common.constant.CommonConstant; |
| | | import com.kidgrow.common.model.PageResult; |
| | | import com.kidgrow.common.utils.DateUtil; |
| | | import com.kidgrow.common.utils.MultipartFileUtils; |
| | | import com.kidgrow.common.utils.RandomValueUtils; |
| | | import com.kidgrow.common.utils.StringUtils; |
| | | import com.kidgrow.filecenter.mapper.FileMapper; |
| | | import com.kidgrow.filecenter.model.FileInfo; |
| | | import com.kidgrow.filecenter.service.IFileService; |
| | | import com.kidgrow.filecenter.util.FileUtil; |
| | | |
| | | import lombok.extern.slf4j.Slf4j; |
| | | |
| | | import net.coobird.thumbnailator.Thumbnails; |
| | | import org.apache.commons.collections4.MapUtils; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import java.io.File; |
| | | import java.io.FileInputStream; |
| | | import java.io.InputStream; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | |
| | | * @param fileInfo |
| | | */ |
| | | protected abstract void uploadFile(MultipartFile file, FileInfo fileInfo) throws Exception; |
| | | protected abstract void fileUpLoadOss(FileInfo fileInfo,String newFilePath, InputStream fileStream); |
| | | protected abstract String FilePath(String imgType,String folderByDate); |
| | | |
| | | /** |
| | | * 删除文件 |
| | |
| | | List<FileInfo> list = baseMapper.findList(page, params); |
| | | return PageResult.<FileInfo>builder().data(list).code(0).count(page.getTotal()).build(); |
| | | } |
| | | /** |
| | | * 上传图片-并缩略,其实是给文件服务器存储了两个文件 一个原文件 一个缩略图文件 |
| | | * @param file form内的文件数据 |
| | | * @param imgType 业务类型 例如logo |
| | | * @return 返回的实体中 path字段保存的是缩略图地址 |
| | | * @throws Exception |
| | | */ |
| | | @Override |
| | | public FileInfo uploadForThumbnails(MultipartFile file, String imgType) throws Exception { |
| | | FileInfo fileInfo=this.upload(file,imgType); |
| | | if (StringUtils.isNotBlank(fileInfo.getUrl())) { |
| | | //生成缩略图上传 |
| | | File nowFile= MultipartFileUtils.multipartFileToFile(file); |
| | | //暂存目录 发布后真实存在的磁盘目录 |
| | | String result = CommonConstant.TEMP_IMAGE_PATH; |
| | | //文件扩展名 |
| | | String suffix=file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")); |
| | | String newFileName= RandomValueUtils.uuid()+suffix; |
| | | //生成缩略图 |
| | | Thumbnails.of(nowFile).size(CommonConstant.TH_IMG_WIDTH, CommonConstant.TH_IMG_HEIGHT).toFile(result+newFileName); |
| | | File thuFile= new File(result+newFileName); |
| | | if (thuFile.exists()) { |
| | | //将生成的文件转换为流 |
| | | InputStream inputStream=new FileInputStream(new File(result+newFileName)); |
| | | String folderByDate = DateUtil.formatDate(new Date()); |
| | | String filefloder=this.FilePath(imgType,folderByDate); |
| | | FileInfo newfileInfo=new FileInfo(); |
| | | newfileInfo.setName(newFileName); |
| | | newfileInfo.setId(IdUtil.fastSimpleUUID()); |
| | | newfileInfo.setSize(thuFile.length()); |
| | | newfileInfo.setIsImg(true); |
| | | newfileInfo.setContentType(file.getContentType()); |
| | | newfileInfo.setSource(fileInfo.getSource()); |
| | | newfileInfo.setImgType(imgType); |
| | | newfileInfo.setCreateTime(new Date()); |
| | | //文件上传到oss |
| | | this.fileUpLoadOss(newfileInfo,filefloder+newFileName,inputStream); |
| | | // 将文件信息保存到数据库 |
| | | baseMapper.insert(newfileInfo); |
| | | fileInfo.setPath(newfileInfo.getUrl()); |
| | | //删除暂存文件 |
| | | MultipartFileUtils.delteTempFile(thuFile); |
| | | inputStream.close(); |
| | | } |
| | | return fileInfo; |
| | | } |
| | | else |
| | | { |
| | | return fileInfo; |
| | | } |
| | | } |
| | | } |
| | | |