feat:attachment 下载接口开发

This commit is contained in:
zhangshuai
2024-08-26 14:41:28 +08:00
parent 3d95329f01
commit 1db74870e0
5 changed files with 37 additions and 1 deletions

View File

@@ -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"),

View File

@@ -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) {

View File

@@ -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;
}

View File

@@ -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();
}
}

View File

@@ -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;