|
|
@@ -1,14 +1,33 @@
|
|
|
package top.lvzhiqiang.service.impl;
|
|
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
import com.github.pagehelper.PageInfo;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import net.coobird.thumbnailator.Thumbnails;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
import top.lvzhiqiang.config.InitRunner;
|
|
|
+import top.lvzhiqiang.dto.R;
|
|
|
import top.lvzhiqiang.entity.FileImage;
|
|
|
+import top.lvzhiqiang.enumeration.ResultCodeEnum;
|
|
|
+import top.lvzhiqiang.exception.BusinessException;
|
|
|
import top.lvzhiqiang.mapper.PictureInfoMapper;
|
|
|
import top.lvzhiqiang.service.PictureInfoService;
|
|
|
+import top.lvzhiqiang.util.DateUtils;
|
|
|
+import top.lvzhiqiang.util.FtpUtil;
|
|
|
+import top.lvzhiqiang.util.StringUtils;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
+import javax.imageio.ImageIO;
|
|
|
+import java.awt.image.BufferedImage;
|
|
|
+import java.io.ByteArrayInputStream;
|
|
|
+import java.io.ByteArrayOutputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.math.RoundingMode;
|
|
|
+import java.time.LocalDate;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
@@ -20,6 +39,7 @@ import java.util.Map;
|
|
|
* 2024/8/26 11:02
|
|
|
*/
|
|
|
@Service
|
|
|
+@Slf4j
|
|
|
public class PictureInfoServiceImpl extends BaseServiceImpl<Object> implements PictureInfoService {
|
|
|
|
|
|
@Resource
|
|
|
@@ -43,8 +63,12 @@ public class PictureInfoServiceImpl extends BaseServiceImpl<Object> implements P
|
|
|
pictureInfoList = pictureInfoMapper.getUploadImageInfoList(params);
|
|
|
|
|
|
String bpicsUrl = InitRunner.dicCodeMap.get("bpics_url").getCodeValue();
|
|
|
+ String ftpBasePath = InitRunner.dicCodeMap.get("ftp_basepath").getCodeValue();
|
|
|
+ String ftpThumbnailBasePath = InitRunner.dicCodeMap.get("ftp_thumbnail_basepath").getCodeValue();
|
|
|
pictureInfoList.stream().forEach(e -> {
|
|
|
- e.setPath(bpicsUrl + e.getPath());
|
|
|
+ String path = e.getPath();
|
|
|
+ e.setPath(bpicsUrl + ftpBasePath + path);
|
|
|
+ e.setThumbnailPath(bpicsUrl + ftpThumbnailBasePath + path);
|
|
|
});
|
|
|
}
|
|
|
|
|
|
@@ -52,4 +76,135 @@ public class PictureInfoServiceImpl extends BaseServiceImpl<Object> implements P
|
|
|
return pictureInfoPageInfo;
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public R insertOrUpdateImg(MultipartFile file, String remark, Long categoryId, String id) {
|
|
|
+ String imageUrl = "";
|
|
|
+ String imageSize = "";
|
|
|
+ String ftpBasePath = InitRunner.dicCodeMap.get("ftp_basepath").getCodeValue();
|
|
|
+ String ftpThumbnailBasePath = InitRunner.dicCodeMap.get("ftp_thumbnail_basepath").getCodeValue();
|
|
|
+ String ftpBaseUrl = InitRunner.dicCodeMap.get("ftp_baseurl").getCodeValue();
|
|
|
+ String parentPath = LocalDate.now().format(DateUtils.dateFormatter5);
|
|
|
+ if (StringUtils.isEmpty(id)) {
|
|
|
+ try {
|
|
|
+ // 1、给上传的图片生成新的文件名
|
|
|
+ // 1.1获取原始文件名
|
|
|
+ String oldName = file.getOriginalFilename();
|
|
|
+ // 1.2使用FtpUtil工具类生成新的文件名,新文件名 = newName + 文件后缀
|
|
|
+ String newName = FtpUtil.genImageName();
|
|
|
+ newName = newName + oldName.substring(oldName.lastIndexOf("."));
|
|
|
+
|
|
|
+ // 2、把图片上传到图片服务器
|
|
|
+ // 2.1获取上传的io流
|
|
|
+ InputStream input = file.getInputStream();
|
|
|
+ // 2.2调用FtpUtil工具类进行上传
|
|
|
+ boolean result = FtpUtil.uploadFile(ftpBasePath, parentPath, newName, input);
|
|
|
+ // 2.3缩略图
|
|
|
+ BufferedImage originalImage = ImageIO.read(file.getInputStream());
|
|
|
+ BufferedImage thumbnailImage = Thumbnails.of(originalImage).size(300, 200).asBufferedImage();
|
|
|
+ ByteArrayOutputStream thumbnailOutputStream = new ByteArrayOutputStream();
|
|
|
+ ImageIO.write(thumbnailImage, "jpg", thumbnailOutputStream);
|
|
|
+ ByteArrayInputStream thumbnailInputStream = new ByteArrayInputStream(thumbnailOutputStream.toByteArray());
|
|
|
+ FtpUtil.uploadFile(ftpThumbnailBasePath, parentPath, newName, thumbnailInputStream);
|
|
|
+ if (result) {
|
|
|
+ //返回给前端图片访问路径
|
|
|
+ imageUrl = parentPath + "/" + newName;
|
|
|
+ imageSize = BigDecimal.valueOf(file.getSize()).divide(new BigDecimal("1024")).setScale(0, RoundingMode.UP).toPlainString().concat("KB");
|
|
|
+
|
|
|
+ FileImage fileImage = new FileImage();
|
|
|
+ fileImage.setOldName(oldName);
|
|
|
+ fileImage.setNewName(newName);
|
|
|
+ fileImage.setSize(imageSize);
|
|
|
+ fileImage.setPath(imageUrl);
|
|
|
+ fileImage.setRemark(remark);
|
|
|
+ fileImage.setCategoryId(categoryId);
|
|
|
+ pictureInfoMapper.insertFileImage(fileImage);
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error("insertOrUpdateImg Exception,", e);
|
|
|
+ }
|
|
|
+
|
|
|
+ JSONObject result = new JSONObject();
|
|
|
+ result.put("imageUrl", ftpBaseUrl + ftpBasePath + imageUrl);
|
|
|
+ result.put("imageSize", imageSize);
|
|
|
+
|
|
|
+ return R.ok().data(result);
|
|
|
+ } else {
|
|
|
+ FileImage fileImage = pictureInfoMapper.findFileImageById(Long.valueOf(id));
|
|
|
+ if (fileImage == null) {
|
|
|
+ throw new BusinessException(ResultCodeEnum.UNKNOWN_ERROR.getCode(), "ID 不存在!");
|
|
|
+ }
|
|
|
+
|
|
|
+ fileImage.setCategoryId(categoryId);
|
|
|
+ if (StringUtils.isNotEmpty(remark)) {
|
|
|
+ fileImage.setRemark(remark);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (file != null && file.getSize() > 0) {
|
|
|
+ try {
|
|
|
+ boolean flag = FtpUtil.delFile(ftpBasePath + fileImage.getPath());
|
|
|
+ FtpUtil.delFile(ftpThumbnailBasePath + fileImage.getPath());
|
|
|
+ if (flag) {
|
|
|
+ String oldName = file.getOriginalFilename();
|
|
|
+ String newName = FtpUtil.genImageName();
|
|
|
+ newName = newName + oldName.substring(oldName.lastIndexOf("."));
|
|
|
+ InputStream input = file.getInputStream();
|
|
|
+ // 2.2调用FtpUtil工具类进行上传
|
|
|
+ boolean result = FtpUtil.uploadFile(ftpBasePath, parentPath, newName, input);
|
|
|
+ // 2.3缩略图
|
|
|
+ BufferedImage originalImage = ImageIO.read(file.getInputStream());
|
|
|
+ BufferedImage thumbnailImage = Thumbnails.of(originalImage).size(300, 200).asBufferedImage();
|
|
|
+ ByteArrayOutputStream thumbnailOutputStream = new ByteArrayOutputStream();
|
|
|
+ ImageIO.write(thumbnailImage, "jpg", thumbnailOutputStream);
|
|
|
+ ByteArrayInputStream thumbnailInputStream = new ByteArrayInputStream(thumbnailOutputStream.toByteArray());
|
|
|
+ FtpUtil.uploadFile(ftpThumbnailBasePath, parentPath, newName, thumbnailInputStream);
|
|
|
+ if (result) {
|
|
|
+ imageUrl = parentPath + "/" + newName;
|
|
|
+ imageSize = BigDecimal.valueOf(file.getSize()).divide(new BigDecimal("1024")).setScale(0, RoundingMode.UP).toPlainString().concat("KB");
|
|
|
+
|
|
|
+ fileImage.setOldName(oldName);
|
|
|
+ fileImage.setNewName(newName);
|
|
|
+ fileImage.setSize(imageSize);
|
|
|
+ fileImage.setPath(imageUrl);
|
|
|
+ } else {
|
|
|
+ throw new BusinessException(ResultCodeEnum.UNKNOWN_ERROR.getCode(), "上传新文件失败!");
|
|
|
+ }
|
|
|
+
|
|
|
+ } else {
|
|
|
+ throw new BusinessException(ResultCodeEnum.UNKNOWN_ERROR.getCode(), "删除旧文件失败!");
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return R.error().message(e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ pictureInfoMapper.updateFileImage(fileImage);
|
|
|
+ return R.ok().data("success");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public R deleteImgs(Long imageId) {
|
|
|
+ FileImage fileImage = pictureInfoMapper.findFileImageById(imageId);
|
|
|
+ if (fileImage == null) {
|
|
|
+ throw new BusinessException(ResultCodeEnum.UNKNOWN_ERROR.getCode(), "ID 不存在!");
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ String ftpBasePath = InitRunner.dicCodeMap.get("ftp_basepath").getCodeValue();
|
|
|
+ String ftpThumbnailBasePath = InitRunner.dicCodeMap.get("ftp_thumbnail_basepath").getCodeValue();
|
|
|
+ boolean flag = FtpUtil.delFile(ftpBasePath + fileImage.getPath());
|
|
|
+ FtpUtil.delFile(ftpThumbnailBasePath + fileImage.getPath());
|
|
|
+ if (flag) {
|
|
|
+ pictureInfoMapper.deleteFileImageById(imageId);
|
|
|
+ return R.ok();
|
|
|
+ } else {
|
|
|
+ return R.error().message("删除失败");
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return R.error().message(e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
}
|