From 885290e4d0d0c7fad3f538d901c616e49c3d6985 Mon Sep 17 00:00:00 2001
From: 胡仁荣 <897853850@qq.com>
Date: 星期二, 18 七月 2023 18:15:58 +0800
Subject: [PATCH] oss分块上传

---
 src/main/java/com/qxueyou/scc/teach/res/service/impl/HweiYunOBSServiceImpl.java |  226 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 222 insertions(+), 4 deletions(-)

diff --git a/src/main/java/com/qxueyou/scc/teach/res/service/impl/HweiYunOBSServiceImpl.java b/src/main/java/com/qxueyou/scc/teach/res/service/impl/HweiYunOBSServiceImpl.java
index 3413b40..8964cf2 100644
--- a/src/main/java/com/qxueyou/scc/teach/res/service/impl/HweiYunOBSServiceImpl.java
+++ b/src/main/java/com/qxueyou/scc/teach/res/service/impl/HweiYunOBSServiceImpl.java
@@ -7,10 +7,12 @@
 import com.obs.services.model.*;
 import com.qxueyou.scc.base.model.FileMeta;
 import com.qxueyou.scc.base.model.Result;
-import com.qxueyou.scc.base.util.CollectionUtils;
-import com.qxueyou.scc.base.util.QFileUtils;
-import com.qxueyou.scc.base.util.UUIDUtils;
+import com.qxueyou.scc.base.service.ICacheService;
+import com.qxueyou.scc.base.service.impl.CommonAppService;
+import com.qxueyou.scc.base.util.*;
 import com.qxueyou.scc.config.HweiOBSConfig;
+import com.qxueyou.scc.teach.res.model.Res;
+import com.qxueyou.scc.teach.res.model.ResDir;
 import com.qxueyou.scc.teach.res.model.ResFile;
 import com.qxueyou.scc.teach.res.service.HweiYunOBSService;
 import com.qxueyou.scc.teach.res.service.IFileService;
@@ -21,6 +23,7 @@
 import org.apache.logging.log4j.Logger;
 import org.aspectj.util.FileUtil;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.redis.core.HashOperations;
 import org.springframework.stereotype.Service;
 import org.springframework.web.multipart.MultipartFile;
 
@@ -42,7 +45,7 @@
  */
 @Slf4j
 @Service
-public class HweiYunOBSServiceImpl implements HweiYunOBSService {
+public class HweiYunOBSServiceImpl extends CommonAppService implements HweiYunOBSService {
 
     private final Logger log = LogManager.getLogger(HweiYunOBSServiceImpl.class);
     @Autowired
@@ -50,6 +53,13 @@
 
     @Autowired
     FileService fileService;
+
+    /**
+     * redis 妯℃澘
+     */
+    @Autowired
+    ICacheService cache;
+
 
     public final static String FILE_TYPE_VIDEO = "video";
 
@@ -315,4 +325,212 @@
         }
         return null;
     }
+
+    /**
+     * 鏂囦欢鍒嗙墖涓婁紶瀹炵幇閫昏緫锛屽悇鍒嗙墖鏄绾跨▼涓婁紶
+     *
+     * @param input       杈撳叆娴�
+     * @param uniqueId    璧勬簮鍞竴id(鍓嶇浼犲叆)
+     * @param chunkNumber 鍒嗙墖缂栧彿
+     * @param chunkSize   鍒嗙墖澶у皬
+     * @param totalChunk  鍒嗙墖鎬绘暟
+     * @param fileName    鏂囦欢鍚�
+     * @return
+     */
+    public Result uploadChunk(InputStream input, String uniqueId, int chunkNumber, long chunkSize, int totalChunk,
+                              String fileName) {
+
+        HashOperations<Object, Object, Object> _cacheMap = cache.template().opsForHash();
+
+        ObsClient obsClient = new ObsClient(hweiOBSConfig.getAccessKey(),hweiOBSConfig.getSecurityKey(),hweiOBSConfig.getEndPoint());
+
+        //缁欏垵濮嬪寲棰勭暀1.2绉掗挓鐨勬椂闂�
+        try {
+            if (chunkNumber < 4) {
+                Thread.sleep(1200l);
+            }
+        } catch (InterruptedException e) {
+            log.error(e.getMessage(), e);
+        }
+
+        //鑾峰彇鍒嗙墖涓婁紶id(闃块噷浜慜SS鍒濆鍖栧垎鐗囦笂浼犱换鍔¤繑鍥�,鍜寀niqueId鍙妅ey涓�涓�瀵瑰簲),鑻ヨ幏鍙栦笉鍒帮紝鍒�10绉掑唴寰幆鍗佹鐩磋嚦鑾峰彇鍒帮紝鍚﹀垯杩斿洖閿欒
+        String uploadId = getUploadId(uniqueId);
+
+        if (uploadId == null) {
+            return new Result(false);
+        }
+
+        String key = (String) _cacheMap.get(uniqueId + ClientUtils.getUserId(), "key");
+        boolean exists = true;
+
+        //涓婁紶鐗囨
+        if (!_cacheMap.hasKey(uploadId, String.valueOf(chunkNumber))) {
+            //寮�濮嬩笂浼�
+            UploadPartRequest uploadPartRequest = new UploadPartRequest(hweiOBSConfig.getBucketName(), key);
+            uploadPartRequest.setUploadId(uploadId);
+            // 璁剧疆鍒嗘鍙凤紝鑼冨洿鏄�1~10000
+            uploadPartRequest.setPartNumber(chunkNumber);
+            // 璁剧疆鍒嗘澶у皬
+            uploadPartRequest.setPartSize(chunkSize);
+            uploadPartRequest.setInput(input);
+
+            UploadPartResult partResult = obsClient.uploadPart(uploadPartRequest);
+            exists = false;
+
+            _cacheMap.put(uploadId, String.valueOf(chunkNumber), partResult.getEtag());
+        }
+
+
+        //鍏ㄩ儴鐗囨涓婁紶瀹屾垚,璋冪敤client complete鏂规硶瀹屾垚鍒嗙墖涓婁紶浠诲姟锛屽苟鎻掑叆鏁版嵁搴�
+        if (_cacheMap.size(uploadId) != null
+                && _cacheMap.size(uploadId) >= totalChunk
+                && !_cacheMap.hasKey(uniqueId + ClientUtils.getUserId(), "finish")) {
+            List<PartEtag> tags = new ArrayList<>(5);
+            _cacheMap.entries(uploadId).forEach((k, v) -> {
+                tags.add(new PartEtag((String) v, Integer.parseInt((String) k)));
+            });
+
+            CompleteMultipartUploadResult completeMultipartUploadResult = obsClient.completeMultipartUpload(new CompleteMultipartUploadRequest(hweiOBSConfig.getBucketName(), key, uploadId,
+                    tags));
+
+            System.out.println(completeMultipartUploadResult.getObjectUrl());
+
+            ObjectMetadata metadata = obsClient.getObjectMetadata(hweiOBSConfig.getBucketName(), key);
+
+
+
+
+//            obsClient.setObjectAcl(hweiOBSConfig.getBucketName(), key, AccessControlList.REST_CANNED_PUBLIC_READ);
+            //鏍囪瀹屾垚
+            _cacheMap.put(uniqueId + ClientUtils.getUserId(), "finish", "true");
+
+//            //鏇存柊鏁版嵁搴撲俊鎭�
+            updateFile((String) _cacheMap.get(uniqueId + ClientUtils.getUserId(), "fileId"), metadata.getContentLength(), metadata.getContentMd5(), key, fileName);
+
+            //閿�姣佺紦瀛�
+            cache.template().expire(uniqueId + ClientUtils.getUserId(), 1, TimeUnit.MINUTES);
+        }
+
+        return new Result(true, "hit", exists, "fileId", _cacheMap.get(uniqueId + ClientUtils.getUserId(), "fileId"), "path", _cacheMap.get(uniqueId + ClientUtils.getUserId(), "path"));
+    }
+
+    @Override
+    public Result initUploadChunk(String uniqueId, String fileName, String md5) {
+        String path = getDestPath(fileName);
+        String key = chopPath(path);
+
+        //娓呯紦瀛�
+        cache.template().delete(uniqueId + ClientUtils.getUserId());
+
+        InitiateMultipartUploadRequest initiateMultipartUploadRequest = new InitiateMultipartUploadRequest(hweiOBSConfig.getBucketName(), key);
+        ObjectMetadata objectMetadata = new ObjectMetadata();
+        objectMetadata.setObjectStorageClass(StorageClassEnum.STANDARD);
+        initiateMultipartUploadRequest.setMetadata(new ObjectMetadata());
+
+        //璋冪敤OSS SDK 鎺ュ彛杩斿洖uploadId
+        ObsClient obsClient = new ObsClient(hweiOBSConfig.getAccessKey(),hweiOBSConfig.getSecurityKey(),hweiOBSConfig.getEndPoint());
+
+        String uploadId = obsClient.initiateMultipartUpload(initiateMultipartUploadRequest).getUploadId();
+
+        //涓洪厤鍚堝垎鐗囦笂浼狅紝鍚庡彴鍏堟柊寤烘枃浠惰褰�
+
+        ResFile file = newFileToDB(fileName, path, getFileType(fileName), 0l, md5);
+
+        //娣诲姞鍒扮紦瀛�
+        cache.template().opsForHash().put(uniqueId + ClientUtils.getUserId(), "uploadId", uploadId);
+        cache.template().opsForHash().put(uniqueId + ClientUtils.getUserId(), "key", key);
+        cache.template().opsForHash().put(uniqueId + ClientUtils.getUserId(), "path", path);
+        cache.template().opsForHash().put(uniqueId + ClientUtils.getUserId(), "fileId", file.getFileId());
+
+        //闃叉浜х敓杩囧缂撳瓨鍨冨溇
+        cache.template().expire(uniqueId + ClientUtils.getUserId(), 1, TimeUnit.DAYS);
+        ResFile file1 = new ResFile();
+        file1.setUpdateId(uploadId);
+        file1.setFileId(file.getFileId());
+        file1.setPath(file.getPath());
+        return new Result(true,"cs",file1);
+    }
+
+    /**
+     * 鏇存柊鏂囦欢淇℃伅鍒版暟鎹簱
+     *
+     * @param fileId
+     * @param fileLength
+     * @param md5
+     * @param path
+     * @param fileName
+     * @return
+     */
+    private ResFile updateFile(String fileId, long fileLength, String md5, String path, String fileName) {
+
+        ResFile file = new ResFile();
+
+        file.setFileId(fileId);
+        file.setSize(fileLength);
+        file.setMd5Hash(md5);
+        file.setPath(path);
+        file.setFileName(fileName);
+
+        cache.template().opsForList().rightPush("BaseOssServiceUpdate", file);
+
+        return file;
+
+    }
+    /**
+     * 鎻掑叆鏂囦欢淇℃伅鍒版暟鎹簱
+     *
+     * @param name
+     * @param relativePath 鐩稿璺緞
+     * @param type         鏂囦欢绫诲瀷锛岄潪鏂囦欢鏍煎紡
+     * @return
+     */
+    private ResFile newFileToDB(String name, String relativePath, String type, Long fileSize, String md5) {
+        ResFile file = new ResFile();
+
+        TraceUtils.setCreateTrace(file);
+        file.setFileName(name);
+        file.setFileFormat(QFileUtils.getFileFormat(name));
+        file.setFileType(type);
+        file.setMd5Hash(md5);
+        file.setPath(relativePath);
+        file.setSize(fileSize == null ? 0 : fileSize);
+        file.setDeleteFlag(false);
+        save(file);
+        return file;
+
+    }
+
+    /**
+     * 濡傛灉璺緞浠� / 鎴� \ 寮�澶达紝闇�瑕佹埅鍙�
+     *
+     * @param destPath
+     * @return
+     */
+    public String chopPath(String destPath) {
+        if (destPath.startsWith("/") || destPath.startsWith("\\")) {
+            return destPath.substring(1);
+        }
+        return destPath;
+    }
+    /**
+     * 鑾峰彇鍒嗙墖涓婁紶id(闃块噷浜慜SS鍒濆鍖栧垎鐗囦笂浼犱换鍔¤繑鍥�,鍜寀niqueId鍙妅ey涓�涓�瀵瑰簲),鑻ヨ幏鍙栦笉鍒帮紝鍒�10绉掑唴寰幆鍗佹鐩磋嚦鑾峰彇鍒帮紝鍚﹀垯杩斿洖閿欒
+     *
+     * @param uniqueId
+     * @return
+     */
+    private String getUploadId(String uniqueId) {
+        HashOperations<Object, Object, Object> _cacheMap = cache.template().opsForHash();
+        String uploadId = (String) _cacheMap.get(uniqueId + ClientUtils.getUserId(), "uploadId");
+        int tryCount = 0;
+        while (uploadId == null && tryCount < 10) {
+            try {
+                Thread.sleep(1000l);
+                uploadId = (String) _cacheMap.get(uniqueId + ClientUtils.getUserId(), "uploadId");
+                tryCount++;
+            } catch (InterruptedException e) {
+                log.error(e, e);
+            }
+        }
+        return uploadId;
+    }
 }

--
Gitblit v1.8.0