feat[文件上传和订单模块功能]:已测试通过
This commit is contained in:
+17
-11
@@ -1,21 +1,14 @@
|
||||
package com.central.file.controller;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import com.central.common.model.PageResult;
|
||||
import com.central.common.model.Result;
|
||||
import com.central.file.model.FileInfo;
|
||||
import com.central.file.service.IFileService;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import com.central.common.model.PageResult;
|
||||
import com.central.file.model.FileInfo;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 文件上传
|
||||
@@ -40,6 +33,19 @@ public class FileController {
|
||||
return fileService.upload(file);
|
||||
}
|
||||
|
||||
/**
|
||||
* 文件上传
|
||||
* 根据fileType选择上传方式
|
||||
*
|
||||
* @param id
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
/* @GetMapping("/files/{id}")
|
||||
public Result upload(@PathVariable String id) throws Exception {
|
||||
return "fileService.download(file)";
|
||||
}*/
|
||||
|
||||
/**
|
||||
* 文件删除
|
||||
*
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
package com.central.file.service.impl;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.central.common.constant.CommonConstant;
|
||||
import com.central.file.model.FileInfo;
|
||||
import com.central.oss.model.ObjectInfo;
|
||||
import com.central.oss.properties.FileServerProperties;
|
||||
import com.central.oss.template.CosTemplate;
|
||||
import com.qcloud.cos.model.COSObject;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.OutputStream;
|
||||
|
||||
/**
|
||||
* @author lsl
|
||||
* @date 2025/1/13
|
||||
* <p>
|
||||
*/
|
||||
@Service
|
||||
@ConditionalOnProperty(prefix = FileServerProperties.PREFIX, name = "type", havingValue = FileServerProperties.TYPE_COS)
|
||||
public class CosService extends AbstractIFileService {
|
||||
|
||||
@Resource
|
||||
private CosTemplate cosTemplate;
|
||||
|
||||
@Override
|
||||
protected String fileType() {
|
||||
return FileServerProperties.TYPE_COS;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ObjectInfo uploadFile(MultipartFile file) {
|
||||
return cosTemplate.upload(file);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void deleteFile(String objectPath) {
|
||||
COSObject cosObject = parsePath(objectPath);
|
||||
cosTemplate.delete(cosObject.getBucketName(), cosObject.getKey());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void out(String id, OutputStream os) {
|
||||
FileInfo fileInfo = baseMapper.selectById(id);
|
||||
if (fileInfo != null) {
|
||||
COSObject cosObject = parsePath(fileInfo.getPath());
|
||||
cosTemplate.out(cosObject.getBucketName(), cosObject.getKey(), os);
|
||||
}
|
||||
}
|
||||
|
||||
@Setter
|
||||
@Getter
|
||||
private class S3Object {
|
||||
private String bucketName;
|
||||
private String objectName;
|
||||
}
|
||||
|
||||
private COSObject parsePath(String path) {
|
||||
COSObject cosObject = new COSObject();
|
||||
if (StrUtil.isNotEmpty(path)) {
|
||||
int splitIndex = path.lastIndexOf(CommonConstant.PATH_SPLIT);
|
||||
if (splitIndex != -1) {
|
||||
cosObject.setBucketName(path.substring(0, splitIndex));
|
||||
cosObject.setKey(path.substring(splitIndex + 1));
|
||||
}
|
||||
}
|
||||
return cosObject;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user