浏览代码

算法库模型功能开发

zhaoyan 3 年之前
父节点
当前提交
09cb6be774

+ 25 - 0
simulation-resource-common/src/main/java/com/css/simulation/resource/common/controller/MinioController.java

@@ -7,6 +7,7 @@ import com.css.simulation.resource.common.util.MinioUtil;
 import io.minio.MinioClient;
 import io.minio.errors.*;
 import io.minio.http.Method;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.http.MediaType;
 import org.springframework.validation.annotation.Validated;
@@ -30,6 +31,9 @@ public class MinioController {
     @Value("${minio.bucket-name}")
     private String bucketName;
 
+    @Autowired
+    RedisController redisController;
+
 
     @PostMapping("/hello")
     public String hello() {
@@ -52,6 +56,27 @@ public class MinioController {
         return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS, "请求成功!", previewUrl);
     }
 
+
+    @PostMapping(value = "/uploadProcessBar", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
+    public ResponseBodyVO<String> uploadProcessBar(
+            @RequestPart("file") MultipartFile file,
+            @RequestParam("objectName") String objectName,//  "/xx/xxx/x/xx"
+            @RequestParam("objectPath") String objectPath
+    ) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
+
+        MinioUtil.uploadProcessBar(
+                minioClient,
+                file,
+                bucketName,
+                objectName,
+                objectPath,
+                redisController
+        );
+        String previewUrl = MinioUtil.getPreviewUrl(minioClient, Method.GET, bucketName, objectName);
+
+        return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS, "请求成功!", previewUrl);
+    }
+
 //    @PostMapping("/download")
 //    public ResponseBodyVO<String> download(
 //            @RequestBody @Validated MinioParameter minioParameter,

+ 26 - 0
simulation-resource-common/src/main/java/com/css/simulation/resource/common/util/MinioUtil.java

@@ -1,5 +1,7 @@
 package com.css.simulation.resource.common.util;
 
+import com.css.simulation.resource.common.controller.RedisController;
+import com.css.simulation.resource.common.util.processbar.ProgressStream;
 import io.minio.*;
 import io.minio.errors.ErrorResponseException;
 import io.minio.errors.InsufficientDataException;
@@ -9,6 +11,8 @@ import io.minio.http.Method;
 import jdk.nashorn.internal.runtime.regexp.joni.exception.InternalException;
 import org.springframework.web.multipart.MultipartFile;
 
+import java.io.BufferedInputStream;
+import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.security.InvalidKeyException;
@@ -116,6 +120,28 @@ public class MinioUtil {
                 .build());
     }
 
+    /**
+     * 文件上传,保存进度
+     */
+    public static void uploadProcessBar(
+            MinioClient minioClient,
+            MultipartFile multipartFile,
+            String bucketName,
+            String objectName,
+            String objectPath,
+            RedisController redisController
+    ) throws IOException, InsufficientDataException, ErrorResponseException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException, io.minio.errors.ServerException, io.minio.errors.InternalException {
+        InputStream inputStream = multipartFile.getInputStream();
+        InputStream pis =
+                new BufferedInputStream(
+                        new ProgressStream(redisController, objectPath, inputStream));
+        minioClient.putObject(
+                PutObjectArgs.builder().bucket(bucketName).object(objectName).stream(
+                        pis, pis.available(), -1)
+                        .build());
+        pis.close();
+    }
+
     /**
      * 下载文件
      */

+ 88 - 0
simulation-resource-common/src/main/java/com/css/simulation/resource/common/util/processbar/ProgressBar.java

@@ -0,0 +1,88 @@
+//
+// Source code recreated from a .class file by IntelliJ IDEA
+// (powered by Fernflower decompiler)
+//
+
+package com.css.simulation.resource.common.util.processbar;
+
+import com.css.simulation.resource.common.controller.RedisController;
+
+import java.io.PrintStream;
+import java.time.LocalDateTime;
+
+public class ProgressBar {
+    private ProgressState progress;
+    private ProgressThread target;
+    private Thread thread;
+
+    public ProgressBar(RedisController redisController, String task, long initialMax) {
+        this(redisController, task, initialMax, 1000, System.err);
+    }
+
+
+    public ProgressBar(RedisController redisController, String task, long initialMax, int updateIntervalMillis, PrintStream os) {
+        this.progress = new ProgressState(task, initialMax);
+        this.target = new ProgressThread(this.progress, (long)updateIntervalMillis, os, redisController);
+        this.thread = new Thread(this.target);
+    }
+
+    public ProgressBar start() {
+        this.progress.startTime = LocalDateTime.now();
+        this.thread.start();
+        return this;
+    }
+
+    public ProgressBar stepBy(long n) {
+        this.progress.stepBy(n);
+        return this;
+    }
+
+    public ProgressBar stepTo(long n) {
+        this.progress.stepTo(n);
+        return this;
+    }
+
+    public ProgressBar step() {
+        this.progress.stepBy(1L);
+        return this;
+    }
+
+    public ProgressBar maxHint(long n) {
+        this.progress.maxHint(n);
+        return this;
+    }
+
+    public ProgressBar stop() {
+        this.target.kill();
+
+        try {
+            this.thread.join();
+            this.target.consoleStream.print("\n");
+            this.target.consoleStream.flush();
+        } catch (InterruptedException var2) {
+        }
+
+        return this;
+    }
+
+    public ProgressBar setExtraMessage(String msg) {
+        this.progress.setExtraMessage(msg);
+        return this;
+    }
+
+    public long getCurrent() {
+        return this.progress.getCurrent();
+    }
+
+    public long getMax() {
+        return this.progress.getMax();
+    }
+
+    public String getTask() {
+        return this.progress.getTask();
+    }
+
+    public String getExtraMessage() {
+        return this.progress.getExtraMessage();
+    }
+}

+ 62 - 0
simulation-resource-common/src/main/java/com/css/simulation/resource/common/util/processbar/ProgressState.java

@@ -0,0 +1,62 @@
+//
+// Source code recreated from a .class file by IntelliJ IDEA
+// (powered by Fernflower decompiler)
+//
+
+package com.css.simulation.resource.common.util.processbar;
+
+import java.time.LocalDateTime;
+
+class ProgressState {
+    String task;
+    long current = 0L;
+    long max = 0L;
+    LocalDateTime startTime = null;
+    String extraMessage = "";
+
+    ProgressState(String task, long initialMax) {
+        this.task = task;
+        this.max = initialMax;
+    }
+
+    synchronized void maxHint(long n) {
+        this.max = n;
+    }
+
+    synchronized void stepBy(long n) {
+        this.current += n;
+        if (this.current > this.max) {
+            this.max = this.current;
+        }
+
+    }
+
+    synchronized void stepTo(long n) {
+        this.current = n;
+        if (this.current > this.max) {
+            this.max = this.current;
+        }
+
+    }
+
+    synchronized void setExtraMessage(String msg) {
+        this.extraMessage = msg;
+    }
+
+    String getTask() {
+        return this.task;
+    }
+
+    synchronized String getExtraMessage() {
+        return this.extraMessage;
+    }
+
+    synchronized long getCurrent() {
+        return this.current;
+    }
+
+    synchronized long getMax() {
+        return this.max;
+    }
+}
+

+ 103 - 0
simulation-resource-common/src/main/java/com/css/simulation/resource/common/util/processbar/ProgressStream.java

@@ -0,0 +1,103 @@
+package com.css.simulation.resource.common.util.processbar;/*
+ * MinIO Java SDK for Amazon S3 Compatible Cloud Storage, (C) 2017 MinIO, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import com.css.simulation.resource.common.controller.RedisController;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+public class ProgressStream extends InputStream {
+    private InputStream in;
+    private ProgressBar pb;
+
+    /**
+     * ProgressStream implements an extends InputStream while also writing out the read progress on
+     * console. ProgressStream can be used as a direct replacement for any InputStream compatible
+     * input.
+     *
+     * @param msg Custom message string.
+     * @param stream InputStream to be wrapped.
+     * @throws IOException For any exception generated by the InputStream.
+     */
+    public ProgressStream(RedisController redisController, String msg, InputStream stream) throws IOException {
+        this(redisController, msg, (long) stream.available(), stream);
+    }
+
+    /**
+     * ProgressStream implements an extends InputStream while also writing out the read progress on
+     * console. ProgressStream can be used as a direct replacement for any InputStream compatible
+     * input.
+     *
+     * @param msg Custom message string.
+     * @param size Size of the progress bar.
+     * @param stream InputStream to be wrapped.
+     * @throws IOException For any exception generated by the InputStream.
+     */
+    public ProgressStream(RedisController redisController, String msg, long size, InputStream stream)
+            throws IOException {
+        super();
+
+        // Allocate the reader.
+        this.in = stream;
+
+        // Initialize progress bar.
+        this.pb = new ProgressBar(redisController, msg, size);
+        this.pb.start();
+    }
+
+    @Override
+    public int available() throws IOException {
+        return this.in.available();
+    }
+
+    @Override
+    public void close() throws IOException {
+        this.pb.stop();
+        this.in.close();
+        return;
+    }
+
+    @Override
+    public int read() throws IOException {
+        this.pb.step();
+        return this.in.read();
+    }
+
+    @Override
+    public int read(byte[] toStore) throws IOException {
+        int readBytes = this.in.read(toStore);
+        this.pb.stepBy(readBytes); // Update progress bar.
+        return readBytes;
+    }
+
+    @Override
+    public int read(byte[] toStore, int off, int len) throws IOException {
+        int readBytes = this.in.read(toStore, off, len);
+        this.pb.stepBy(readBytes);
+        return readBytes;
+    }
+
+    @Override
+    public long skip(long n) throws IOException {
+        this.pb.stepTo(n);
+        return this.in.skip(n);
+    }
+
+    @Override
+    public boolean markSupported() {
+        return false;
+    }
+}

+ 88 - 0
simulation-resource-common/src/main/java/com/css/simulation/resource/common/util/processbar/ProgressThread.java

@@ -0,0 +1,88 @@
+//
+// Source code recreated from a .class file by IntelliJ IDEA
+// (powered by Fernflower decompiler)
+//
+
+package com.css.simulation.resource.common.util.processbar;
+
+import api.common.pojo.param.RedisParameter;
+import com.css.simulation.resource.common.controller.RedisController;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.data.redis.core.StringRedisTemplate;
+import org.springframework.stereotype.Component;
+
+import java.io.PrintStream;
+import java.math.BigDecimal;
+import java.math.RoundingMode;
+import java.time.Duration;
+
+
+public class ProgressThread implements Runnable {
+    private final RedisController redisController;
+    volatile boolean running;
+    ProgressState progress;
+    long updateInterval;
+    PrintStream consoleStream;
+
+//    @Autowired
+//    RedisController redisController;
+
+//    @Autowired
+//    StringRedisTemplate redisTemplate;
+
+
+    ProgressThread(ProgressState progress, long updateInterval, PrintStream consoleStream,RedisController redisController) {
+        this.progress = progress;
+        this.updateInterval = updateInterval;
+        this.consoleStream = consoleStream;
+        this.redisController = redisController;
+    }
+
+    double progress() {
+        return this.progress.max == 0L ? 0.0D : (double)this.progress.current / (double)this.progress.max;
+    }
+
+
+    /**
+     * 保存文件进度
+     */
+    void refresh() {
+
+        //保存进度
+        Long progressMax = this.progress.max;
+        Long progressCurrent = this.progress.current;
+        String task = this.progress.task;
+//        System.out.println("上传进度:"+task);
+        Double radio = new BigDecimal(Double.valueOf(progressCurrent)/Double.valueOf(progressMax)).setScale(2, RoundingMode.UP).doubleValue();
+//        System.out.println("上传进度:"+radio);
+        RedisParameter redisParameter = new RedisParameter();
+        redisParameter.setKey(task);
+        redisParameter.setValue(radio.toString());
+        redisParameter.setMinutes(1440);//24小时清除
+
+        this.redisController.set(redisParameter);
+//        redisTemplate.opsForValue().set(redisParameter.getKey(), redisParameter.getValue(), Duration.ofMinutes(redisParameter.getMinutes()));
+
+
+    }
+
+    void kill() {
+        this.running = false;
+    }
+
+    public void run() {
+        this.running = true;
+
+        try {
+            while(this.running) {
+                this.refresh();
+                Thread.sleep(this.updateInterval);
+            }
+
+            this.refresh();
+        } catch (InterruptedException var2) {
+        }
+
+    }
+}

+ 4 - 0
simulation-resource-server/src/main/java/com/css/simulation/resource/feign/FileDownService.java

@@ -29,6 +29,10 @@ public interface FileDownService {
     ResponseBodyVO<String> upload(@RequestPart("file") MultipartFile file,
                                   @RequestParam("objectName") String objectName);
 
+    @PostMapping(value = "/minio/uploadProcessBar", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
+    ResponseBodyVO<String> uploadProcessBar(@RequestPart("file") MultipartFile file,
+                                  @RequestParam("objectName") String objectName,  @RequestParam("objectName") String objectPath);
+
     @PostMapping("/minio/download")
     Response download(@RequestBody @Validated MinioParameter minioParameter);
 }

+ 5 - 0
simulation-resource-server/src/main/java/com/css/simulation/resource/feign/fallback/FileDownServiceFallback.java

@@ -24,6 +24,11 @@ public class FileDownServiceFallback implements FileDownService {
         return new ResponseBodyVO<>(ResponseBodyVO.Response.SERVER_FAILURE);
     }
 
+    @Override
+    public ResponseBodyVO<String> uploadProcessBar(MultipartFile file, String objectName) {
+        return null;
+    }
+
     @Override
     public Response download(@RequestBody @Validated MinioParameter minioParameter) {
         log.error("------- 下载错误:" + minioParameter.getObjectName());

+ 7 - 2
simulation-resource-server/src/main/java/com/css/simulation/resource/scene/ctrl/FileController.java

@@ -48,11 +48,16 @@ public class FileController {
             } else if (type.equals(DictConstants.SCENE_GENERAL)) {
                 fileName = "泛化场景/" + nowTime + "/" + objectPath + "/" + multipartFile.getOriginalFilename();
             } else if (type.equals(DictConstants.ALGORITHM_FILE)) {
-                fileName = "algorithm/" + nowTime + "/" + multipartFile.getOriginalFilename();
+                fileName = "algorithm/" + nowTime + "/"  + objectPath + "/" + multipartFile.getOriginalFilename();
             } else {
                 fileName = multipartFile.getOriginalFilename();
             }
-            ResponseBodyVO<String> respon = fileDownService.upload(multipartFile, fileName);
+            ResponseBodyVO<String> respon;
+            if (type.equals(DictConstants.ALGORITHM_FILE)) {
+                respon = fileDownService.uploadProcessBar(multipartFile, fileName, objectPath);
+            }else {
+                respon = fileDownService.upload(multipartFile, fileName);
+            }
             String videoPreview = respon.getMessage();
             Map map = new HashMap();
            // map.put("videoPreview", videoPreview);