Browse Source

旧文件水印添加1

Aslee 3 years ago
parent
commit
93941f8b2d

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

@@ -57,11 +57,7 @@ public class CmProductArchiveContentService extends CrudService<CmProductArchive
 	public Page<CmProductArchiveContent> findPage(Page<CmProductArchiveContent> page, CmProductArchiveContent cmProductArchiveContent) {
 		Page<CmProductArchiveContent> archivePage = super.findPage(page, cmProductArchiveContent);
 		List<CmProductArchiveContent> archiveList = archivePage.getList();
-		archiveList.forEach(archive->{
-			if (cmProductArchiveContent.getType() != 1) {
-				archive.setFileUrl(OSSUtils.generateProductArchiveUrl(archive.getOssName(),archive.getUploadTime()));
-			}
-		});
+		archiveList.forEach(archive-> archive.setFileUrl(OssArchiveUtil.getOssUrl("archiveFile",archive.getOssName())));
 		return archivePage;
 	}
 
@@ -191,10 +187,10 @@ public class CmProductArchiveContentService extends CrudService<CmProductArchive
 
 
 	@Transactional(readOnly = false)
-	public void addAllWaterMark() {
+	public String addAllWaterMark() {
 		List<CmProductArchiveFile> fileList = cmProductArchiveContentDao.findAllArchiveFile();
 		fileList.forEach(file->{
-			if (file.getArchiveContentType() == 2 && StringUtils.isEmpty(file.getWaterOssName())) {
+			if (file.getArchiveContentType() != 2 && StringUtils.isEmpty(file.getWaterOssName())) {
 				String ossUrl = null;
 				if (file.getArchiveContentType() == 1) {
 					//图片添加水印
@@ -206,29 +202,55 @@ public class CmProductArchiveContentService extends CrudService<CmProductArchive
 				if (StringUtils.isNotEmpty(ossUrl)) {
 					try {
 						String fileName = file.getFileName();
+						if (file.getArchiveContentType() == 1) {
+							fileName = file.getOssUrl().split("\\?")[0];
+						}
 						String fileType = fileName.substring(fileName.lastIndexOf(".") + 1);
-						String uuid = UUID.randomUUID().toString().replaceAll("-", "");
-						String filePath = uuid + "." + fileType;
+						String uuid1 = UUID.randomUUID().toString().replaceAll("-", "");
+						String filePath1 = uuid1 + "." + fileType;
+						String uuid2 = UUID.randomUUID().toString().replaceAll("-", "");
+						String filePath2 = uuid2 + "." + 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);
+						if (!"doc".equals(contentType) && !"ppt".equals(contentType)) {
+							File archiveFile = new File(tempPath + filePath1);
+							archiveFile.createNewFile();
+							URL fileUrl = new URL(ossUrl);
+							InputStream inputStream = fileUrl.openStream();
+							OutputStream outputStream = new FileOutputStream(archiveFile);
+							int bytesRead = 0;
+							byte[] buffer = new byte[8192];
+							while ((bytesRead = inputStream.read(buffer, 0, 8192)) != -1) {
+								outputStream.write(buffer,0,bytesRead);
+							}
+							outputStream.close();
+							inputStream.close();
+
+							//添加水印
+							Map<String, Object> map = WaterMarkUtils.addWaterMark(archiveFile, "采美365网", filePath2, file.getArchiveContentType() == 1 ? "img" : fileType, ossUrl);
+							String waterMarkUrl = null;
+							if (file.getArchiveContentType() == 1) {
+								filePath2 = (String) map.get("ossName");
+								waterMarkUrl = (String) map.get("imagePath");
+							} else {
+								File waterMarkFile = new File(tempPath + filePath2);
+								//水印文件上传oss
+								waterMarkUrl = OssArchiveUtil.ossUpload(filePath2, "archiveFile/", waterMarkFile, contentType, contentDispositon);
+								logger.info(">>>>>>>>>>>>>>>>>" + waterMarkUrl);
+								OssArchiveUtil.deleteFile(waterMarkFile);
+							}
+							//删除本地文件
+							OssArchiveUtil.deleteFile(archiveFile);
+							file.setWaterOssName(filePath2);
+							file.setWaterOssUrl(waterMarkUrl);
+							cmProductArchiveContentDao.updateFileWaterMark(file);
+						}
 					} catch (Exception e) {
 						e.printStackTrace();
 					}
 				}
 			}
 		});
-
+		return "success";
 	}
 }

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

@@ -105,11 +105,11 @@ public class CmProductArchiveContentController extends BaseController {
 		return "modules/archive/previewFilePage";
 	}
 
-    @ResponseBody
-    @RequestMapping(value = "addWaterMark")
-    public void addAllWaterMark() {
-        cmProductArchiveContentService.addAllWaterMark();
-    }
+	@ResponseBody
+	@RequestMapping(value = "addWaterMark")
+	public String addAllWaterMark() {
+		return cmProductArchiveContentService.addAllWaterMark();
+	}
 
     @ResponseBody
     @RequestMapping(value = "getVideoSize")

+ 5 - 0
src/main/resources/config/prod/caimei.properties

@@ -182,3 +182,8 @@ aliyun.accessKeyId=LTAI4GBL3o4YkWnbKYgf2Xia
 aliyun.accessKeySecret=dBjAXqbYiEPP6Ukuk2ZsXQeET7FVkK
 aliyun.bucketName=caimei-oss
 aliyun.endpoint=https://oss-cn-shenzhen.aliyuncs.com
+
+#商品资料库文件临时路径
+archive.tempPath=/mnt/newdatadrive/data/runtime/tomcat-instance/manager/tempFile/
+#ffmpeg路径
+ffmpeg.path=/mnt/newdatadrive/apps/ffmpeg/ffmpeg-master/bin/ffmpeg