feat: 文件单个上传
Signed-off-by: skyyemperor <skyyemperor@qq.com>
This commit is contained in:
父節點
4f8f119ffa
當前提交
8de89de837
@ -2,14 +2,18 @@ package com.weilab.biology.controller;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
||||
import com.weilab.biology.core.data.dto.JobDto;
|
||||
import com.weilab.biology.core.data.enums.JobStatusEnum;
|
||||
import com.weilab.biology.core.data.po.Job;
|
||||
import com.weilab.biology.core.data.vo.result.CommonError;
|
||||
import com.weilab.biology.core.data.vo.result.Result;
|
||||
import com.weilab.biology.core.data.vo.result.error.JobError;
|
||||
import com.weilab.biology.core.validation.EnumValidation;
|
||||
import com.weilab.biology.mapper.JobMapper;
|
||||
import com.weilab.biology.service.JobService;
|
||||
import com.weilab.biology.util.FileUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@ -19,6 +23,7 @@ import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.IOException;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
@ -30,11 +35,15 @@ public class JobController {
|
||||
@Autowired
|
||||
private JobService jobService;
|
||||
|
||||
@Autowired
|
||||
private JobMapper jobMapper;
|
||||
|
||||
@Value("${biology.request-path}")
|
||||
private String requestPath;
|
||||
|
||||
@PostMapping("/submit")
|
||||
public Result submit(@RequestParam(required = false) String dataStr,
|
||||
public Result submit(@RequestParam(required = false) Long jobId,
|
||||
@RequestParam(required = false) String dataStr,
|
||||
@RequestParam(required = false) MultipartFile dataFile,
|
||||
@RequestParam String param,
|
||||
@RequestParam String mail,
|
||||
@ -59,30 +68,29 @@ public class JobController {
|
||||
@RequestParam(required = false) MultipartFile file18,
|
||||
@RequestParam(required = false) MultipartFile file19,
|
||||
@RequestParam(required = false) MultipartFile file20) {
|
||||
if (dataFile == null && StringUtils.isBlank(dataStr))
|
||||
if (dataFile == null && StringUtils.isBlank(dataStr)) {
|
||||
return Result.getResult(JobError.PARAM_CAN_NOT_BE_EMPTY);
|
||||
}
|
||||
|
||||
JSONObject obj = null;
|
||||
// 解析param字符串为map对象
|
||||
JSONObject reqParamMap = null;
|
||||
try {
|
||||
obj = JSON.parseObject(param);
|
||||
reqParamMap = JSON.parseObject(param);
|
||||
} catch (Exception e) {
|
||||
return Result.getResult(CommonError.PARAM_WRONG);
|
||||
}
|
||||
|
||||
BufferedInputStream dataStream = null;
|
||||
if (dataFile != null) {
|
||||
dataStream = FileUtil.getInputStream(FileUtils.multipartToFile(dataFile));
|
||||
}
|
||||
|
||||
// 写入file文件,并将文件路径放入paramMap
|
||||
try {
|
||||
List<MultipartFile> files = Arrays.asList(file1, file2, file3, file4, file5,
|
||||
file6, file7, file8, file9, file10, file11, file12, file13, file14, file15,
|
||||
file16, file17, file18, file19, file20);
|
||||
for (MultipartFile file : files) {
|
||||
if (file != null) {
|
||||
String filePath = requestPath + FileUtil.FILE_SEPARATOR + "file" + FileUtil.FILE_SEPARATOR + IdUtil.fastUUID() + "." + FileUtil.extName(file.getOriginalFilename());
|
||||
String filePath = requestPath + FileUtil.FILE_SEPARATOR + "file" + FileUtil.FILE_SEPARATOR
|
||||
+ IdUtil.fastUUID() + "." + FileUtil.extName(file.getOriginalFilename());
|
||||
FileUtil.writeFromStream(file.getInputStream(), filePath);
|
||||
obj.put(file.getName(), filePath);
|
||||
reqParamMap.put(file.getName(), filePath);
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
@ -90,7 +98,78 @@ public class JobController {
|
||||
return Result.getResult(JobError.FILE_READ_FAIL);
|
||||
}
|
||||
|
||||
return jobService.submit(dataStr, dataStream, obj, mail, type);
|
||||
// 将请求数据file转为InputStream
|
||||
BufferedInputStream dataStream = null;
|
||||
if (dataFile != null) {
|
||||
dataStream = FileUtil.getInputStream(FileUtils.multipartToFile(dataFile));
|
||||
}
|
||||
|
||||
Job job = null;
|
||||
if (jobId != null) {
|
||||
// jobId不为空,从数据库获取已创建的任务
|
||||
job = jobMapper.selectById(jobId);
|
||||
if (job == null) {
|
||||
return Result.getResult(JobError.JOB_NOT_FOUND);
|
||||
}
|
||||
} else {
|
||||
job = new Job();
|
||||
}
|
||||
|
||||
if (!StrUtil.isEmpty(job.getParam())) {
|
||||
JSONObject params = JSON.parseObject(job.getParam());
|
||||
params.putAll(reqParamMap);
|
||||
reqParamMap = params;
|
||||
job.setParam(JSON.toJSONString(reqParamMap));
|
||||
}
|
||||
|
||||
job.setData("");
|
||||
job.setMail(mail);
|
||||
job.setStatus(JobStatusEnum.WAIT.getKey());
|
||||
job.setRequestTime(LocalDateTime.now());
|
||||
job.setType(type);
|
||||
|
||||
return jobService.submit(dataStr, dataStream, reqParamMap, job);
|
||||
}
|
||||
|
||||
@PostMapping("/create")
|
||||
public Result createJob() {
|
||||
Job job = new Job();
|
||||
job.setParam("{}");
|
||||
job.setStatus(JobStatusEnum.CREATING.getKey());
|
||||
job.setRequestTime(LocalDateTime.now());
|
||||
jobMapper.insert(job);
|
||||
return Result.success(JobDto.parseJob(job));
|
||||
}
|
||||
|
||||
@PostMapping("/file/upload")
|
||||
public Result uploadFile(@RequestParam Long jobId,
|
||||
@RequestParam String fileKey,
|
||||
@RequestParam MultipartFile file) {
|
||||
Job job = jobMapper.selectById(jobId);
|
||||
if (job == null || !job.getStatus().equals(JobStatusEnum.CREATING.getKey())) {
|
||||
return Result.getResult(JobError.JOB_NOT_FOUND);
|
||||
}
|
||||
|
||||
JSONObject params = JSON.parseObject(job.getParam());
|
||||
try {
|
||||
if (file != null) {
|
||||
String extName = FileUtil.extName(file.getOriginalFilename());
|
||||
if (!StrUtil.isEmpty(extName)) {
|
||||
extName = StrUtil.DOT + extName;
|
||||
}
|
||||
String filePath = requestPath + FileUtil.FILE_SEPARATOR + jobId + FileUtil.FILE_SEPARATOR
|
||||
+ IdUtil.fastUUID() + extName;
|
||||
FileUtil.writeFromStream(file.getInputStream(), filePath);
|
||||
params.put(fileKey, filePath);
|
||||
|
||||
job.setParam(JSON.toJSONString(params));
|
||||
jobMapper.updateById(job);
|
||||
return Result.success(job);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return Result.fail();
|
||||
}
|
||||
|
||||
@PostMapping("/status/update")
|
||||
@ -124,8 +203,9 @@ public class JobController {
|
||||
|
||||
@GetMapping("/list/v2")
|
||||
public Result getJobList(@RequestParam(required = false) Integer type,
|
||||
@RequestParam(defaultValue = "true") Boolean filterCreating,
|
||||
@RequestParam(defaultValue = "1") Integer page,
|
||||
@RequestParam(defaultValue = "100") Integer size) {
|
||||
return jobService.getJobList(type, page, size);
|
||||
return jobService.getJobList(type, filterCreating, page, size);
|
||||
}
|
||||
}
|
||||
|
@ -2,41 +2,45 @@ package com.weilab.biology.core.data.enums;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 校区的枚举类
|
||||
*/
|
||||
@Getter
|
||||
public enum JobStatusEnum {
|
||||
WAIT(0, "waiting"),
|
||||
REQED(3, "requested"),
|
||||
RUNNING(1, "running"),
|
||||
SUCCESS(2, "success"),
|
||||
REQED(3, "requested"),
|
||||
FAIL(-1, "failed"),
|
||||
TIMEOUT(-2, "timeout"),
|
||||
CREATING(-3, "creating"),
|
||||
;
|
||||
|
||||
private final Integer key;
|
||||
private final String remark;
|
||||
|
||||
private final static Map<Integer, JobStatusEnum> HOLDER = new HashMap<>();
|
||||
|
||||
static {
|
||||
for (JobStatusEnum e : JobStatusEnum.values()) {
|
||||
HOLDER.put(e.key, e);
|
||||
}
|
||||
}
|
||||
|
||||
private JobStatusEnum(Integer key, String remark) {
|
||||
this.key = key;
|
||||
this.remark = remark;
|
||||
}
|
||||
|
||||
public static String getRemark(Integer key) {
|
||||
for (JobStatusEnum enums : JobStatusEnum.values()) {
|
||||
if (enums.key.equals(key))
|
||||
return enums.getRemark();
|
||||
}
|
||||
return null;
|
||||
return HOLDER.get(key).remark;
|
||||
}
|
||||
|
||||
public static JobStatusEnum getEnumByKey(Integer key) {
|
||||
for (JobStatusEnum enums : JobStatusEnum.values()) {
|
||||
if (enums.key.equals(key))
|
||||
return enums;
|
||||
}
|
||||
return null;
|
||||
return HOLDER.get(key);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -10,6 +10,7 @@ public enum JobError implements ResultError {
|
||||
PARAM_CAN_NOT_BE_EMPTY(40100, "文本框和文件不能同时为空"),
|
||||
FILE_READ_FAIL(40101, "文件读取出错"),
|
||||
STATUS_UPDATE_FAIL(40102,"当前状态下不允许更新为指定状态"),
|
||||
JOB_NOT_FOUND(40103,"当前状态下不允许更新为指定状态"),
|
||||
;
|
||||
|
||||
private int code;
|
||||
|
@ -21,6 +21,7 @@ public interface JobMapper extends BaseMapper<Job> {
|
||||
List<Job> selectJobList(@Param("type") Integer type);
|
||||
|
||||
List<Job> selectJobListByPage(@Param("type") Integer type,
|
||||
@Param("filterCreating") Boolean filterCreating,
|
||||
@Param("offset") Integer offset,
|
||||
@Param("count") Integer count);
|
||||
|
||||
|
@ -80,18 +80,11 @@ public class JobService {
|
||||
|
||||
/**
|
||||
* 提交job
|
||||
*
|
||||
* @param dataStr 文本内容
|
||||
* @param param 其他参数(json格式)
|
||||
* @param mail 邮箱
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
@Transactional
|
||||
public Result submit(String dataStr, BufferedInputStream dataStream, JSONObject param, String mail, Integer type) {
|
||||
Job job = new Job("", JSON.toJSONString(param), mail, JobStatusEnum.WAIT.getKey(), LocalDateTime.now(), type);
|
||||
public Result submit(String dataStr, BufferedInputStream dataStream, JSONObject param, Job job) {
|
||||
jobMapper.insert(job);
|
||||
sendEmail(job, JobStatusEnum.WAIT, mail);
|
||||
sendEmail(job, JobStatusEnum.WAIT, job.getMail());
|
||||
|
||||
try {
|
||||
//将请求数据写入本地文件,之后向python传递文件路径参数
|
||||
@ -172,8 +165,8 @@ public class JobService {
|
||||
return Result.success(jobs.stream().map(JobLessDto::parseJob).collect(Collectors.toList()));
|
||||
}
|
||||
|
||||
public Result getJobList(Integer type, Integer page, Integer size) {
|
||||
List<Job> jobs = jobMapper.selectJobListByPage(type, (page - 1) * size, size);
|
||||
public Result getJobList(Integer type, Boolean filterCreating, Integer page, Integer size) {
|
||||
List<Job> jobs = jobMapper.selectJobListByPage(type, filterCreating, (page - 1) * size, size);
|
||||
return Result.success(jobs.stream().map(JobLessDto::parseJob).collect(Collectors.toList()));
|
||||
}
|
||||
|
||||
|
@ -63,6 +63,9 @@
|
||||
<if test="type != null">
|
||||
AND type = #{type}
|
||||
</if>
|
||||
<if test="filterCreating">
|
||||
AND `status` != '${@com.weilab.biology.core.data.enums.JobStatusEnum@CREATING.getKey()}'
|
||||
</if>
|
||||
</where>
|
||||
ORDER BY job_id DESC
|
||||
LIMIT #{offset}, #{count}
|
||||
|
載入中…
x
新增問題並參考
Block a user