summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorzhangshuai <[email protected]>2024-08-26 14:41:28 +0800
committerzhangshuai <[email protected]>2024-08-26 14:41:28 +0800
commit1db74870e031e5f9525bb4be9e9136b67d7ccc8f (patch)
treea466e3cada43fa3af5a9eba08de3027d2c58499d
parent3d95329f01e2621f7176dfd73be18251e024b77f (diff)
feat:attachment 下载接口开发
-rw-r--r--src/main/java/net/geedge/asw/common/util/RCode.java1
-rw-r--r--src/main/java/net/geedge/asw/module/app/controller/ApplicationController.java10
-rw-r--r--src/main/java/net/geedge/asw/module/app/service/ApplicationAttachmentService.java5
-rw-r--r--src/main/java/net/geedge/asw/module/app/service/impl/ApplicationAttachmentServiceImpl.java19
-rw-r--r--src/main/resources/db/migration/R__AZ_sys_i18n.sql3
5 files changed, 37 insertions, 1 deletions
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<MultipartFile> 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>{
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<ApplicationAtt
this.removeById(id);
}
}
+
+ @Override
+ public void download(HttpServletResponse response, String applicationId, String attachmentId) throws IOException {
+ ApplicationAttachmentEntity attachment = this.getOne(new LambdaQueryWrapper<ApplicationAttachmentEntity>()
+ .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;