From 1db74870e031e5f9525bb4be9e9136b67d7ccc8f Mon Sep 17 00:00:00 2001 From: zhangshuai Date: Mon, 26 Aug 2024 14:41:28 +0800 Subject: [PATCH] =?UTF-8?q?feat:attachment=20=E4=B8=8B=E8=BD=BD=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E5=BC=80=E5=8F=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../net/geedge/asw/common/util/RCode.java | 1 + .../app/controller/ApplicationController.java | 10 ++++++++++ .../service/ApplicationAttachmentService.java | 5 +++++ .../ApplicationAttachmentServiceImpl.java | 19 +++++++++++++++++++ .../resources/db/migration/R__AZ_sys_i18n.sql | 3 ++- 5 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/main/java/net/geedge/asw/common/util/RCode.java b/src/main/java/net/geedge/asw/common/util/RCode.java index 60c4a3a..d5f5c88 100644 --- a/src/main/java/net/geedge/asw/common/util/RCode.java +++ b/src/main/java/net/geedge/asw/common/util/RCode.java @@ -43,6 +43,7 @@ public enum RCode { APP_SIGNATURE_CONTENT_CANNOT_EMPTY(201012, "application signature content cannot be empty"), APP_SIGNATURE_NOT_EXIST(201013, "application signature does not exist"), APP_NOTE_CONTENT_CANNOT_EMPTY(201014, "application note content cannot be empty"), + APP_ATTACHMENT_NOT_EXIST(201015, "application attachment does not exist"), diff --git a/src/main/java/net/geedge/asw/module/app/controller/ApplicationController.java b/src/main/java/net/geedge/asw/module/app/controller/ApplicationController.java index f08233a..08a6abc 100644 --- a/src/main/java/net/geedge/asw/module/app/controller/ApplicationController.java +++ b/src/main/java/net/geedge/asw/module/app/controller/ApplicationController.java @@ -2,6 +2,7 @@ package net.geedge.asw.module.app.controller; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import jakarta.servlet.http.HttpServletResponse; import net.geedge.asw.common.util.ASWException; import net.geedge.asw.common.util.R; import net.geedge.asw.common.util.RCode; @@ -18,6 +19,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; +import java.io.IOException; import java.util.List; import java.util.Map; @@ -128,6 +130,14 @@ public class ApplicationController { } + @GetMapping("/{applicationId}/attachment/{attachmentId}") + public void downloadAttachment(HttpServletResponse response, @PathVariable String applicationId, @PathVariable String attachmentId) throws IOException { + T.VerifyUtil.is(applicationId).notNull() + .and(attachmentId).notNull(); + + attachmentService.download(response, applicationId, attachmentId); + } + @PostMapping("/{applicationId}/attachment") public R uploadAttachment(@PathVariable String applicationId, @RequestParam("files") List fileList) { diff --git a/src/main/java/net/geedge/asw/module/app/service/ApplicationAttachmentService.java b/src/main/java/net/geedge/asw/module/app/service/ApplicationAttachmentService.java index f4d4028..9b8cdc6 100644 --- a/src/main/java/net/geedge/asw/module/app/service/ApplicationAttachmentService.java +++ b/src/main/java/net/geedge/asw/module/app/service/ApplicationAttachmentService.java @@ -1,12 +1,17 @@ package net.geedge.asw.module.app.service; import com.baomidou.mybatisplus.extension.service.IService; +import jakarta.servlet.http.HttpServletResponse; import net.geedge.asw.module.app.entity.ApplicationAttachmentEntity; import org.springframework.core.io.Resource; +import java.io.IOException; + public interface ApplicationAttachmentService extends IService{ ApplicationAttachmentEntity saveAttachment(Resource fileResource, String applicationId); void removedAttachment(String applicationId, String ids); + + void download(HttpServletResponse response, String applicationId, String attachmentId) throws IOException; } diff --git a/src/main/java/net/geedge/asw/module/app/service/impl/ApplicationAttachmentServiceImpl.java b/src/main/java/net/geedge/asw/module/app/service/impl/ApplicationAttachmentServiceImpl.java index 4bd8e74..eee59fe 100644 --- a/src/main/java/net/geedge/asw/module/app/service/impl/ApplicationAttachmentServiceImpl.java +++ b/src/main/java/net/geedge/asw/module/app/service/impl/ApplicationAttachmentServiceImpl.java @@ -5,6 +5,7 @@ import cn.hutool.core.io.FileUtil; import cn.hutool.log.Log; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import jakarta.servlet.http.HttpServletResponse; import net.geedge.asw.common.util.ASWException; import net.geedge.asw.common.util.RCode; import net.geedge.asw.common.util.T; @@ -16,6 +17,7 @@ import net.geedge.asw.module.app.service.IApplicationService; import org.apache.commons.io.FileUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.io.Resource; +import org.springframework.http.MediaTypeFactory; import org.springframework.stereotype.Service; import java.io.File; @@ -79,4 +81,21 @@ public class ApplicationAttachmentServiceImpl extends ServiceImpl() + .eq(ApplicationAttachmentEntity::getApplicationId, applicationId) + .eq(ApplicationAttachmentEntity::getId, attachmentId)); + if (T.ObjectUtil.isNull(attachment)) { + throw new ASWException(RCode.APP_ATTACHMENT_NOT_EXIST); + } + File file = FileUtil.file(attachment.getPath()); + response.setStatus(200); + response.setContentType( MediaTypeFactory.getMediaType(file.getName()).toString()); + response.setContentLength(Integer.parseInt(String.valueOf(file.length()))); + response.setHeader("Content-disposition", "attachment; filename=" + file.getName()); + response.getOutputStream().write(T.FileUtil.readBytes(file)); + response.flushBuffer(); + } } diff --git a/src/main/resources/db/migration/R__AZ_sys_i18n.sql b/src/main/resources/db/migration/R__AZ_sys_i18n.sql index 7b5ba13..12c06d2 100644 --- a/src/main/resources/db/migration/R__AZ_sys_i18n.sql +++ b/src/main/resources/db/migration/R__AZ_sys_i18n.sql @@ -113,6 +113,7 @@ INSERT INTO `sys_i18n`(`id`, `name`, `code`, `value`, `lang`, `remark`, `update_ INSERT INTO `sys_i18n`(`id`, `name`, `code`, `value`, `lang`, `remark`, `update_user_id`, `update_timestamp`) VALUES (183, '201013', 'APP_SIGNATURE_NOT_EXIST', '应用特征不存在', 'zh', '', 'admin', 1724030366000); INSERT INTO `sys_i18n`(`id`, `name`, `code`, `value`, `lang`, `remark`, `update_user_id`, `update_timestamp`) VALUES (185, '201014', 'APP_NOTE_CONTENT_CANNOT_EMPTY', 'application note content cannot be empty', 'en', '', 'admin', 1724030366000); INSERT INTO `sys_i18n`(`id`, `name`, `code`, `value`, `lang`, `remark`, `update_user_id`, `update_timestamp`) VALUES (187, '201014', 'APP_NOTE_CONTENT_CANNOT_EMPTY', '应用说明内容不能为空', 'zh', '', 'admin', 1724030366000); - +INSERT INTO `sys_i18n`(`id`, `name`, `code`, `value`, `lang`, `remark`, `update_user_id`, `update_timestamp`) VALUES (189, '201015', 'APP_ATTACHMENT_NOT_EXIST', 'application attachment does not exist', 'en', '', 'admin', 1724030366000); +INSERT INTO `sys_i18n`(`id`, `name`, `code`, `value`, `lang`, `remark`, `update_user_id`, `update_timestamp`) VALUES (191, '201015', 'APP_ATTACHMENT_NOT_EXIST', '应用附件不存在', 'zh', '', 'admin', 1724030366000); SET FOREIGN_KEY_CHECKS = 1;