|
|
@@ -1,166 +1,86 @@
|
|
|
package cn.reghao.dfs.store.oss.controller;
|
|
|
|
|
|
-import cn.reghao.dfs.store.oss.db.mapper.FileMetaMapper;
|
|
|
-import cn.reghao.dfs.store.oss.db.mapper.FileTypeMapper;
|
|
|
-import cn.reghao.dfs.store.oss.model.dto.json.*;
|
|
|
-import cn.reghao.dfs.store.oss.model.po.FileMeta;
|
|
|
-import cn.reghao.jutil.jdk.converter.DateTimeConverter;
|
|
|
+import cn.reghao.dfs.store.oss.db.mapper.BucketMapper;
|
|
|
+import cn.reghao.dfs.store.oss.db.mapper.RegionMapper;
|
|
|
+import cn.reghao.dfs.store.oss.model.dto.json.CreateBucket;
|
|
|
+import cn.reghao.dfs.store.oss.model.po.Bucket;
|
|
|
+import cn.reghao.dfs.store.oss.model.vo.json.BucketGetResult;
|
|
|
+import cn.reghao.dfs.store.oss.model.vo.json.BucketListResult;
|
|
|
import cn.reghao.jutil.jdk.result.WebBody;
|
|
|
-import cn.reghao.jutil.tool.id.IdGenerator;
|
|
|
import io.swagger.annotations.Api;
|
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
import org.springframework.http.MediaType;
|
|
|
import org.springframework.validation.annotation.Validated;
|
|
|
-import org.springframework.web.bind.annotation.GetMapping;
|
|
|
-import org.springframework.web.bind.annotation.PostMapping;
|
|
|
-import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
-import org.springframework.web.bind.annotation.RestController;
|
|
|
-
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.List;
|
|
|
-import java.util.stream.Collectors;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
/**
|
|
|
* @author reghao
|
|
|
- * @date 2023-01-02 17:47:08
|
|
|
+ * @date 2023-01-04 16:46:45
|
|
|
*/
|
|
|
@Api(tags = "存储桶基础操作接口")
|
|
|
@RestController
|
|
|
@RequestMapping("/ajax")
|
|
|
public class BucketBasicJsonController {
|
|
|
- private final FileMetaMapper fileMetaMapper;
|
|
|
- private final FileTypeMapper fileTypeMapper;
|
|
|
- private final IdGenerator objectIdGenerator;
|
|
|
-
|
|
|
- public BucketBasicJsonController(FileMetaMapper fileMetaMapper, FileTypeMapper fileTypeMapper) {
|
|
|
- this.fileMetaMapper = fileMetaMapper;
|
|
|
- this.fileTypeMapper = fileTypeMapper;
|
|
|
- this.objectIdGenerator = new IdGenerator(32, "object-id");
|
|
|
- }
|
|
|
+ private final RegionMapper regionMapper;
|
|
|
+ private final BucketMapper bucketMapper;
|
|
|
|
|
|
- @ApiOperation("获取文件图标")
|
|
|
- @GetMapping(value = "/bucket/file/file_icon.json", produces = MediaType.APPLICATION_JSON_VALUE)
|
|
|
- public String getFileIcon() {
|
|
|
- List<FileIcon> list = fileTypeMapper.findAll1();
|
|
|
- return WebBody.success(list);
|
|
|
+ public BucketBasicJsonController(RegionMapper regionMapper, BucketMapper bucketMapper) {
|
|
|
+ this.regionMapper = regionMapper;
|
|
|
+ this.bucketMapper = bucketMapper;
|
|
|
}
|
|
|
|
|
|
- @ApiOperation("获取存储桶中的部分对象")
|
|
|
- @GetMapping(value = "/bucket/file/list_objects.json", produces = MediaType.APPLICATION_JSON_VALUE)
|
|
|
- public String getObjects(@Validated GetObjects getObjects) {
|
|
|
- String bucket = getObjects.getBucket();
|
|
|
- String prefix = getObjects.getPrefix();
|
|
|
- String startAfter = getObjects.getMarker();
|
|
|
- String delimiter = getObjects.getDelimiter();
|
|
|
- Integer maxKeys = getObjects.getMaxKeys();
|
|
|
-
|
|
|
- int count = fileMetaMapper.count();
|
|
|
- List<FileMeta> list = fileMetaMapper.findAll1(prefix);
|
|
|
- List<ObjectJson> list1 = list.stream().map(fileMeta -> {
|
|
|
- ObjectJson objectJson = new ObjectJson();
|
|
|
- Integer fileTypeId = fileMeta.getFileTypeId();
|
|
|
- objectJson.setDir(fileTypeId == 1000);
|
|
|
-
|
|
|
- String objectName = fileMeta.getObjectName();
|
|
|
- String[] names = objectName.split("/");
|
|
|
- String name = names[names.length-1];
|
|
|
- objectJson.setName(name);
|
|
|
- objectJson.setPath(objectName);
|
|
|
- long size = fileMeta.getSize();
|
|
|
- objectJson.setSize(size);
|
|
|
- objectJson.setFormattedSize(String.valueOf(size));
|
|
|
- objectJson.setObjectType("Normal");
|
|
|
- objectJson.setType(fileTypeId);
|
|
|
- objectJson.setStorageClass("Standard");
|
|
|
- objectJson.setTimeModified(DateTimeConverter.msTimestamp(fileMeta.getUpdateTime()));
|
|
|
- return objectJson;
|
|
|
- }).collect(Collectors.toList());
|
|
|
-
|
|
|
- ListObjects listObjects = new ListObjects();
|
|
|
- listObjects.setObjectList(list1);
|
|
|
- listObjects.setBucketName(bucket);
|
|
|
- listObjects.setDelimiter(delimiter);
|
|
|
- listObjects.setMaxKeys(maxKeys);
|
|
|
- listObjects.setObjectSize(0L);
|
|
|
- listObjects.setTruncated(true);
|
|
|
- return WebBody.success(listObjects);
|
|
|
+ @ApiOperation("查找存储桶名字是否存在")
|
|
|
+ @GetMapping(value = "/bucket/judge_exist.json", produces = MediaType.APPLICATION_JSON_VALUE)
|
|
|
+ public String judgeExist(@RequestParam("region") String region, @RequestParam("bucketName") String bucketName) {
|
|
|
+ Bucket bucket = bucketMapper.findByName(bucketName);
|
|
|
+ return bucket == null ? WebBody.success() : WebBody.fail(null);
|
|
|
}
|
|
|
|
|
|
- @ApiOperation("创建目录")
|
|
|
- @PostMapping(value = "/bucket/file/create_folder.json", produces = MediaType.APPLICATION_JSON_VALUE)
|
|
|
- public String createFolder(@Validated CreateFolder createFolder) {
|
|
|
- String objectName = createFolder.getObjectName();
|
|
|
- List<String> objectNames = new ArrayList<>();
|
|
|
- String[] names = objectName.split("/");
|
|
|
- for (int i = 0; i < names.length; i++) {
|
|
|
- if (i == 0) {
|
|
|
- objectNames.add(names[i] + "/");
|
|
|
- } else {
|
|
|
- String tmp = objectNames.get(i-1) + names[i] + "/";
|
|
|
- objectNames.add(tmp);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- FileMeta fileMeta;
|
|
|
- int i = 0;
|
|
|
- for (; i < objectNames.size(); i++) {
|
|
|
- fileMeta = fileMetaMapper.findByObjectName(objectNames.get(i));
|
|
|
- if (fileMeta == null) {
|
|
|
- break;
|
|
|
- }
|
|
|
+ @ApiOperation("创建存储桶")
|
|
|
+ @PostMapping(value = "/bucket/new_create_bucket.json", produces = MediaType.APPLICATION_JSON_VALUE)
|
|
|
+ public String createBucket(@Validated CreateBucket createBucket) {
|
|
|
+ String region = createBucket.getRegion();
|
|
|
+ String bucketName = createBucket.getBucket();
|
|
|
+ String storageClass = createBucket.getStorage();
|
|
|
+ String acl = createBucket.getAcl();
|
|
|
+
|
|
|
+ Integer regionId = regionMapper.findIdByLocation(region);
|
|
|
+ if (regionId == null) {
|
|
|
+ return WebBody.fail(region + " 不存在");
|
|
|
}
|
|
|
|
|
|
- List<FileMeta> list = new ArrayList<>();
|
|
|
- for (int j = i; j < objectNames.size(); j++) {
|
|
|
- String objectName1 = objectNames.get(j);
|
|
|
- String objectId = objectIdGenerator.stringId();
|
|
|
- String[] names1 = objectName1.split("/");
|
|
|
- String filename = names1[names1.length-1];
|
|
|
- list.add(new FileMeta(objectName1, objectId, filename, "tnb"));
|
|
|
+ Bucket bucket = bucketMapper.findByName(bucketName);
|
|
|
+ if (bucket == null) {
|
|
|
+ bucket = new Bucket();
|
|
|
+ bucket.setRegionId(regionId);
|
|
|
+ bucket.setName(bucketName);
|
|
|
+ bucket.setStorage(storageClass);
|
|
|
+ bucket.setAcl(acl);
|
|
|
+ bucketMapper.save(bucket);
|
|
|
+ return WebBody.success();
|
|
|
+ } else {
|
|
|
+ return WebBody.fail(bucketName + " 已存在");
|
|
|
}
|
|
|
-
|
|
|
- if (!list.isEmpty()) {
|
|
|
- fileMetaMapper.saveAll(list);
|
|
|
- }
|
|
|
- return WebBody.success();
|
|
|
- }
|
|
|
-
|
|
|
- @ApiOperation("获取域名列表")
|
|
|
- @GetMapping(value = "/bucket/domain/list.json", produces = MediaType.APPLICATION_JSON_VALUE)
|
|
|
- public String domainList() {
|
|
|
- return WebBody.success();
|
|
|
}
|
|
|
|
|
|
- @ApiOperation("获取 bucket ACL")
|
|
|
- @GetMapping(value = "/bucket/acl.json", produces = MediaType.APPLICATION_JSON_VALUE)
|
|
|
- public String bucketAcl() {
|
|
|
- return WebBody.success();
|
|
|
+ @ApiOperation("获取存储桶列表")
|
|
|
+ @GetMapping(value = "/bucket/list.json", produces = MediaType.APPLICATION_JSON_VALUE)
|
|
|
+ public String getBucketList() {
|
|
|
+ BucketListResult bucketListResult = new BucketListResult();
|
|
|
+ return WebBody.success(bucketListResult);
|
|
|
}
|
|
|
|
|
|
- @ApiOperation("获取对象 ACL")
|
|
|
- @GetMapping(value = "/bucket/file/get_object_acl.json", produces = MediaType.APPLICATION_JSON_VALUE)
|
|
|
- public String obejctAcl() {
|
|
|
- return WebBody.success();
|
|
|
+ @ApiOperation("获取存储桶标签")
|
|
|
+ @GetMapping(value = "/bucket/tag.json", produces = MediaType.APPLICATION_JSON_VALUE)
|
|
|
+ public String getTag(@RequestParam("region") String region, @RequestParam("bucket") String bucket) {
|
|
|
+ BucketListResult bucketListResult = new BucketListResult();
|
|
|
+ return WebBody.success(bucketListResult);
|
|
|
}
|
|
|
|
|
|
- @ApiOperation("获取对象信息")
|
|
|
- @GetMapping(value = "/bucket/file/head_object.json", produces = MediaType.APPLICATION_JSON_VALUE)
|
|
|
- public String headObject(@Validated HeadObject headObject) {
|
|
|
- return WebBody.success();
|
|
|
- }
|
|
|
-
|
|
|
- @ApiOperation("生成带签名的 URL")
|
|
|
- @PostMapping(value = "/bucket/file/generate_url_with_signed.json", produces = MediaType.APPLICATION_JSON_VALUE)
|
|
|
- public String generateSignedUrl(@Validated GenerateSignedUrl generateSignedUrl) {
|
|
|
- String objectName = generateSignedUrl.getObjectName();
|
|
|
- String url = String.format("//oss.reghao.cn/object/%s", objectName);
|
|
|
- // 5 分钟后链接失效
|
|
|
- long expires = System.currentTimeMillis()/1000 + 300;
|
|
|
- String ossAccessId = "TMP.3KeSkQJhXm4fZNupmzTEFx8HQ7QobdYnbc4A5do6zvqDnkkHdwyKj2rvJzPCfprxBSfHM6H3tqci21rCnHwWEmVje3PHaA";
|
|
|
- String signature = "C1NieNpY%2FrmgaHNM2PXTPU3Tshw%3D";
|
|
|
- String param = String.format("Expires=%s&OSSAccessId=%s&Signature=%s", expires, ossAccessId, signature);
|
|
|
-
|
|
|
- GenerateSignedUrlResult generateSignedUrlResult = new GenerateSignedUrlResult(url, param);
|
|
|
- return WebBody.success(generateSignedUrlResult);
|
|
|
+ @ApiOperation("获取存储桶详情")
|
|
|
+ @GetMapping(value = "/bucket/get.json", produces = MediaType.APPLICATION_JSON_VALUE)
|
|
|
+ public String getBucket(@RequestParam("region") String region, @RequestParam("bucket") String bucket) {
|
|
|
+ BucketGetResult bucketGetResult = new BucketGetResult();
|
|
|
+ return WebBody.success(bucketGetResult);
|
|
|
}
|
|
|
}
|