提交 2d24ab67 authored 作者: hzh's avatar hzh

文件下载接口增加水印

上级 c8bc53f5
...@@ -2,11 +2,17 @@ package org.dromara.resource.controller; ...@@ -2,11 +2,17 @@ package org.dromara.resource.controller;
import cn.dev33.satoken.annotation.SaCheckPermission; import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.codec.Base64;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
import jakarta.servlet.http.HttpServletResponse; import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty; import jakarta.validation.constraints.NotEmpty;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R; import org.dromara.common.core.domain.R;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.file.FileUtils;
import org.dromara.common.core.utils.file.WatermarkUtil;
import org.dromara.common.core.utils.file.watermark.config.WatermarkConfig;
import org.dromara.common.core.validate.QueryGroup; import org.dromara.common.core.validate.QueryGroup;
import org.dromara.common.log.annotation.Log; import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType; import org.dromara.common.log.enums.BusinessType;
...@@ -22,6 +28,8 @@ import org.springframework.validation.annotation.Validated; ...@@ -22,6 +28,8 @@ import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import java.awt.*;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
...@@ -89,6 +97,50 @@ public class SysOssController extends BaseController { ...@@ -89,6 +97,50 @@ public class SysOssController extends BaseController {
iSysOssService.download(ossId, response); iSysOssService.download(ossId, response);
} }
/**
* 下载水印文件
*
* @param ossId OSS对象ID
*/
@GetMapping("/watermark/{ossId}")
public R<String> download(@PathVariable Long ossId, @RequestParam String watermarkText) {
SysOssVo oss = iSysOssService.getById(ossId);
if (oss == null) {
throw new ServiceException("文件不存在");
}
File originFile = null;
File watermarkFile = null;
try {
originFile = FileUtils.convert(oss.getUrl(), oss.getFileName());
watermarkFile = FileUtil.touch(FileUtils.addFolderBeforeFileName(originFile.getPath(), "watermark"));
WatermarkConfig watermarkConfig = new WatermarkConfig.Builder()
.watermarkText(watermarkText)
.fontName(WatermarkUtil.getFontName(originFile.getPath(), watermarkText))
.color(Color.RED)
.alpha(0.3f)
.angle(45)
.build();
boolean result = WatermarkUtil.addTextWatermark(originFile, watermarkFile, watermarkConfig);
String base64;
if (result) {
base64 = Base64.encode(FileUtil.readBytes(watermarkFile));
} else {
base64 = Base64.encode(FileUtil.readBytes(originFile));
}
return R.ok("操作成功", base64);
} catch (Exception e) {
e.printStackTrace();
throw new ServiceException("文件添加水印失败");
} finally {
if (originFile != null) {
originFile.delete();
}
if (watermarkFile != null) {
watermarkFile.delete();
}
}
}
/** /**
* 删除OSS对象存储 * 删除OSS对象存储
* *
......
...@@ -16,7 +16,6 @@ import org.dromara.common.core.utils.file.watermark.config.WatermarkConfig; ...@@ -16,7 +16,6 @@ import org.dromara.common.core.utils.file.watermark.config.WatermarkConfig;
import org.dromara.common.mail.utils.MailUtils; import org.dromara.common.mail.utils.MailUtils;
import org.dromara.common.mybatis.core.mapper.BaseMapperPlus; import org.dromara.common.mybatis.core.mapper.BaseMapperPlus;
import org.dromara.common.mybatis.service.AbstractBaseService; import org.dromara.common.mybatis.service.AbstractBaseService;
import org.dromara.resource.api.RemoteFileService;
import org.dromara.workflow.api.domain.RemoteWorkflowService; import org.dromara.workflow.api.domain.RemoteWorkflowService;
import org.dromara.workflow.api.domain.event.ProcessEvent; import org.dromara.workflow.api.domain.event.ProcessEvent;
import org.dromara.workflow.api.domain.event.ProcessTaskEvent; import org.dromara.workflow.api.domain.event.ProcessTaskEvent;
...@@ -52,8 +51,6 @@ public class FileApproveServiceImpl extends AbstractBaseService<FileApproveVo, F ...@@ -52,8 +51,6 @@ public class FileApproveServiceImpl extends AbstractBaseService<FileApproveVo, F
private final FileApproveMapper baseMapper; private final FileApproveMapper baseMapper;
@DubboReference @DubboReference
private final RemoteWorkflowService workflowService; private final RemoteWorkflowService workflowService;
@DubboReference
private final RemoteFileService remoteFileService;
@Override @Override
public BaseMapperPlus<FileApprove, FileApproveVo> mapper() { public BaseMapperPlus<FileApprove, FileApproveVo> mapper() {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论