浏览代码

旧文件水印添加

Aslee 3 年之前
父节点
当前提交
ed1c009968

+ 4 - 0
src/main/java/com/caimei/modules/archive/dao/CmProductArchiveContentDao.java

@@ -26,4 +26,8 @@ public interface CmProductArchiveContentDao extends CrudDao<CmProductArchiveCont
     void updateFile(@Param("fileId") Integer fileId, @Param("archiveContentId") Integer archiveContentId);
 
     Integer getDbFileId(String id);
+
+    List<CmProductArchiveFile> findAllArchiveFile();
+
+    void updateFileWaterMark(CmProductArchiveFile file);
 }

+ 10 - 0
src/main/java/com/caimei/modules/archive/entity/CmProductArchiveFile.java

@@ -22,6 +22,8 @@ public class CmProductArchiveFile extends DataEntity<CmProductArchiveFile> {
 	private String waterOssUrl;		//水印文件oss链接
 	private Date uploadTime;		// 上传时间
 
+    private Integer archiveContentType;   // 资料类型:1图片资料,2视频资料,3文件资料
+
 	public CmProductArchiveFile() {
 		super();
 	}
@@ -85,4 +87,12 @@ public class CmProductArchiveFile extends DataEntity<CmProductArchiveFile> {
 	public void setWaterOssUrl(String waterOssUrl) {
 		this.waterOssUrl = waterOssUrl;
 	}
+
+    public Integer getArchiveContentType() {
+        return archiveContentType;
+    }
+
+    public void setArchiveContentType(Integer archiveContentType) {
+        this.archiveContentType = archiveContentType;
+    }
 }

+ 42 - 44
src/main/java/com/caimei/modules/archive/service/CmProductArchiveContentService.java

@@ -2,6 +2,7 @@ package com.caimei.modules.archive.service;
 
 import java.awt.image.BufferedImage;
 import java.io.*;
+import java.net.URI;
 import java.net.URL;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
@@ -58,7 +59,7 @@ public class CmProductArchiveContentService extends CrudService<CmProductArchive
 		List<CmProductArchiveContent> archiveList = archivePage.getList();
 		archiveList.forEach(archive->{
 			if (cmProductArchiveContent.getType() != 1) {
-				archive.setFileUrl(OSSUtils.generateProductArchiveUrl(archive));
+				archive.setFileUrl(OSSUtils.generateProductArchiveUrl(archive.getOssName(),archive.getUploadTime()));
 			}
 		});
 		return archivePage;
@@ -84,7 +85,7 @@ public class CmProductArchiveContentService extends CrudService<CmProductArchive
 							e.printStackTrace();
 						}
 						image.setOssUrl(imageUrl);
-						Map<String, Object> map = WaterMarkUtils.addWaterMark(null, null, imageUrl, "img", null);
+						Map<String, Object> map = WaterMarkUtils.addWaterMark(null, null, null, "img", imageUrl);
 						if (null != map) {
 							String ossName = (String)map.get("ossName");
 							String waterUrl = (String) map.get("imagePath");
@@ -190,47 +191,44 @@ public class CmProductArchiveContentService extends CrudService<CmProductArchive
 
 
 	@Transactional(readOnly = false)
-	public Map<String, Object> addWaterMark(MultipartFile multipartFile, String fileName) {
-		Map<String, Object> map = new HashMap<>();
-		String fileAllName = multipartFile.getOriginalFilename();
-		String fileType = fileAllName.substring(fileAllName.lastIndexOf(".") + 1);
-		String uuid_1 = UUID.randomUUID().toString().replaceAll("-", "");
-		String filePath_1 = uuid_1 + "." + fileType;
-		String uuid_2 = UUID.randomUUID().toString().replaceAll("-", "");
-		String filePath_2 = uuid_2 + "." + fileType;
-		String contentType = OssArchiveUtil.getContentType(fileAllName);
-		try {
-			//保存本地
-			File file = OssArchiveUtil.ossUpload(multipartFile);
-			logger.info("默认路径>>>" + file.getAbsolutePath());
-			WaterMarkUtils.addWaterMark(file, "采美365网", filePath_2, fileType, null);
-			File waterMarkFile = new File(tempPath + filePath_2);
-			String contentDispositon = "pdf".equals(fileType) ? "inline" : "attachment";
-			//上传oss
-//			String url = OssArchiveUtil.ossUpload(filePath_1, "archiveFile/", file, contentType, contentDispositon);
-			String waterMarkUrl = OssArchiveUtil.ossUpload(filePath_2, "archiveFile/", waterMarkFile, contentType, contentDispositon);
-            logger.info(">>>>>>>>>>>>>>>>>" + waterMarkUrl);
-			//删除本地文件
-			OssArchiveUtil.deleteFile(file);
-			OssArchiveUtil.deleteFile(waterMarkFile);
-			//保存关联关系
-			/*CmProductArchiveFile archiveFile = new CmProductArchiveFile();
-			archiveFile.setFileName(fileName);
-			archiveFile.setOssName(filePath_1);
-            archiveFile.setWaterOssName(filePath_2);
-			archiveFile.setOssUrl(url);
-            archiveFile.setWaterOssUrl(waterMarkUrl);
-			archiveFile.setUploadTime(new Date());
-			cmProductArchiveContentDao.insertFile(archiveFile);*/
-			map.put("success", true);
-			map.put("msg", "操作成功");
-//			map.put("archiveFile", archiveFile);
-		} catch (Exception e) {
-			e.printStackTrace();
-			map.put("success", false);
-			map.put("msg", "操作失败");
-			logger.info("上传异常!!!");
-		}
-		return map;
+	public void addAllWaterMark() {
+		List<CmProductArchiveFile> fileList = cmProductArchiveContentDao.findAllArchiveFile();
+		fileList.forEach(file->{
+			if (file.getArchiveContentType() == 2 && StringUtils.isEmpty(file.getWaterOssName())) {
+				String ossUrl = null;
+				if (file.getArchiveContentType() == 1) {
+					//图片添加水印
+					ossUrl = file.getOssUrl();
+				} else if (file.getArchiveContentType() == 3) {
+					//文件添加水印
+					ossUrl = OSSUtils.generateProductArchiveUrl(file.getOssName(), file.getUploadTime());
+				}
+				if (StringUtils.isNotEmpty(ossUrl)) {
+					try {
+						String fileName = file.getFileName();
+						String fileType = fileName.substring(fileName.lastIndexOf(".") + 1);
+						String uuid = UUID.randomUUID().toString().replaceAll("-", "");
+						String filePath = uuid + "." + fileType;
+						String contentType = OssArchiveUtil.getContentType(fileName);
+						String contentDispositon = "pdf".equals(fileType) ? "inline" : "attachment";
+						File archiveFile = new File(new URI(ossUrl));
+						//添加水印
+						WaterMarkUtils.addWaterMark(archiveFile, "采美365网", filePath, file.getArchiveContentType() == 1 ? "img" : fileType, ossUrl);
+						File waterMarkFile = new File(tempPath + filePath);
+						//水印文件上传oss
+						String waterMarkUrl = OssArchiveUtil.ossUpload(filePath, "archiveFile/", waterMarkFile, contentType, contentDispositon);
+						logger.info(">>>>>>>>>>>>>>>>>" + waterMarkUrl);
+						//删除本地文件
+						OssArchiveUtil.deleteFile(waterMarkFile);
+						file.setWaterOssName(filePath);
+						file.setWaterOssUrl(waterMarkUrl);
+						cmProductArchiveContentDao.updateFileWaterMark(file);
+					} catch (Exception e) {
+						e.printStackTrace();
+					}
+				}
+			}
+		});
+
 	}
 }

+ 1 - 1
src/main/java/com/caimei/modules/archive/utils/WaterMarkUtils.java

@@ -676,7 +676,7 @@ public class WaterMarkUtils {
         } else if (fileType.contains("pdf")) {
             setPdfWatermark(inputFile, filePath, markStr);
         } else if (fileType.contains("img")) {
-            return setImageWaterMark(filePath);
+            return setImageWaterMark(fileUrl);
         } else if (fileType.contains("mp4")) {
             return FFMPEG.setVideoWaterMark(fileUrl, outPath + filePath);
         }

+ 2 - 2
src/main/java/com/caimei/modules/archive/web/CmProductArchiveContentController.java

@@ -107,8 +107,8 @@ public class CmProductArchiveContentController extends BaseController {
 
     @ResponseBody
     @RequestMapping(value = "addWaterMark")
-    public Map<String, Object> addWaterMark(@RequestParam("file") MultipartFile file, @RequestParam("fileName") String fileName) {
-        return cmProductArchiveContentService.addWaterMark(file, fileName);
+    public void addAllWaterMark() {
+        cmProductArchiveContentService.addAllWaterMark();
     }
 
     @ResponseBody

+ 4 - 4
src/main/java/com/caimei/modules/oss/utils/OSSUtils.java

@@ -155,12 +155,12 @@ public class OSSUtils {
 
     /**
      * 生成商品资料库链接
+     *
      * @param archiveFile
      * @return
      */
-    public static String generateProductArchiveUrl(CmProductArchiveContent archiveFile) {
+    public static String generateProductArchiveUrl(String ossName, Date uploadTime) {
         OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
-        String ossName = archiveFile.getOssName();
         // 设置URL过期时间为1个小时
         Date expiration = new Date(System.currentTimeMillis() + 3600L * 1000);
         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@@ -168,9 +168,9 @@ public class OSSUtils {
             Date dateOne = format.parse("2021-06-01 00:00:00");
             Date dateTwo = format.parse("2021-09-17 18:00:00");
             String active = "product".equals(config) ? "prod" : config;
-            if (archiveFile.getUploadTime() != null && archiveFile.getUploadTime().compareTo(dateOne) > 0 && archiveFile.getUploadTime().compareTo(dateTwo) < 0) {
+            if (uploadTime != null && uploadTime.compareTo(dateOne) > 0 && uploadTime.compareTo(dateTwo) < 0) {
                 ossName = active + "/" + ossName;
-            } else if (archiveFile.getUploadTime() != null && archiveFile.getUploadTime().compareTo(dateTwo)>0){
+            } else if (uploadTime != null && uploadTime.compareTo(dateTwo) > 0) {
                 ossName = active + "/archiveFile/" + ossName;
             }
         } catch (ParseException e) {

+ 10 - 1
src/main/resources/mappings/modules/archive/CmProductArchiveContentMapper.xml

@@ -71,8 +71,13 @@
 	<select id="getDbFileId" resultType="java.lang.Integer">
 		select id from cm_product_archive_file where archiveContentId = #{id} limit 1
 	</select>
+    <select id="findAllArchiveFile" resultType="com.caimei.modules.archive.entity.CmProductArchiveFile">
+		select cf.id,fileName, ossName,waterOssName,ossUrl,uploadTime,cc.type as archiveContentType
+		from cm_product_archive_file cf left join cm_product_archive_content cc on cf.archiveContentId = cc.id
+		where cc.type != 2;
+	</select>
 
-	<insert id="insert" parameterType="CmProductArchiveContent"  keyProperty="id" useGeneratedKeys="true">
+    <insert id="insert" parameterType="CmProductArchiveContent"  keyProperty="id" useGeneratedKeys="true">
 		INSERT INTO cm_product_archive_content(
 			productArchiveId,
 			title,
@@ -101,6 +106,10 @@
 		set archiveContentId = #{archiveContentId}
 		where id = #{fileId}
 	</update>
+	<update id="updateFileWaterMark">
+		update cm_product_archive_file set waterOssName = #{waterOssName}, waterOssUrl = #{waterOssUrl}
+		where id = #{id}
+	</update>
 
 	<delete id="delete">
 		DELETE FROM cm_product_archive_content