소스 검색

上传时增加计算视频时长方法;查询视频时长接口

yangfeng 1 년 전
부모
커밋
00d6bd5b2e

+ 8 - 4
core/src/main/java/org/jeecg/common/util/CommonUtils.java

@@ -28,12 +28,12 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.math.BigDecimal;
 import java.math.RoundingMode;
-import java.net.URL;
 import java.sql.Connection;
 import java.sql.DatabaseMetaData;
 import java.sql.SQLException;
 import java.util.List;
 import java.util.Map;
+import java.util.Objects;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
@@ -461,14 +461,14 @@ public class CommonUtils {
     /**
      * 获取视频时长 秒
      *
-     * @param videoPath
      * @return
      */
-    public static long getVideoDuration(String videoPath) {
+    public static long getVideoDuration(File file) {
+        long startTime = System.currentTimeMillis();
         // 视频时长
         long time = 0;
         try {
-            MultimediaObject media = new MultimediaObject(new URL("http://localhost:8080/exam-boot/sys/common/static/" + videoPath));
+            MultimediaObject media = new MultimediaObject(file);
             MultimediaInfo info = media.getInfo();
             // 时长,毫秒级
             long duration = info.getDuration();
@@ -480,6 +480,10 @@ public class CommonUtils {
         } catch (Exception e) {
             e.printStackTrace();
         }
+        log.info("获取视频时长,耗时:{}秒", (System.currentTimeMillis() - startTime) / 1000);
+        if (!Objects.isNull(file) && file.exists()) {
+            file.delete();
+        }
         return time;
     }
 }

+ 233 - 0
core/src/main/java/org/jeecg/common/util/HttpDownUtils.java

@@ -0,0 +1,233 @@
+package org.jeecg.common.util;
+
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.io.IOUtils;
+
+import javax.servlet.http.HttpServletResponse;
+import java.io.*;
+import java.net.HttpURLConnection;
+import java.net.URL;
+
+@Slf4j
+public class HttpDownUtils {
+
+    /**
+     * 从Minio下载文件
+     *
+     * @param fileName
+     * @param savePath
+     */
+    public static void downLoadFromMinio(String fileId, String fileName, String savePath) throws Exception {
+
+        InputStream inputStream = null;
+        FileOutputStream fos = null;
+        try {
+            //得到输入流
+            inputStream = MinioUtil.getMinioFile(fileId);
+            //获取自己数组
+            byte[] getData = readInputStream(inputStream);
+
+            //文件保存位置
+            File saveDir = new File(savePath);
+            if (!saveDir.exists()) {
+                saveDir.mkdirs();
+            }
+            File file = new File(saveDir + File.separator + fileName);
+            fos = new FileOutputStream(file);
+            fos.write(getData);
+        } finally {
+            if (fos != null) {
+                fos.close();
+            }
+            if (inputStream != null) {
+                inputStream.close();
+            }
+        }
+    }
+
+
+    /**
+     * 从网络Url中下载文件
+     *
+     * @param urlStr
+     * @param fileName
+     * @param savePath
+     * @throws IOException
+     */
+    public static void downLoadFromUrl(String urlStr, String fileName, String savePath) throws Exception {
+        URL url = new URL(urlStr);
+        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
+        //设置超时间为3秒
+        conn.setConnectTimeout(10 * 1000);
+        conn.setReadTimeout(30 * 1000);
+        //防止屏蔽程序抓取而返回403错误
+        conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
+        InputStream inputStream = null;
+        FileOutputStream fos = null;
+        try {
+            //得到输入流
+            inputStream = conn.getInputStream();
+            //获取自己数组
+            byte[] getData = readInputStream(inputStream);
+
+            //文件保存位置
+            File saveDir = new File(savePath);
+            if (!saveDir.exists()) {
+                saveDir.mkdirs();
+            }
+            File file = new File(saveDir + File.separator + fileName);
+            fos = new FileOutputStream(file);
+            fos.write(getData);
+        } finally {
+            if (fos != null) {
+                fos.close();
+            }
+            if (inputStream != null) {
+                inputStream.close();
+            }
+            if (conn != null) {
+                conn.disconnect();
+            }
+        }
+    }
+
+    /**
+     * 从网络Url中下载文件,返回流
+     *
+     * @param urlStr
+     * @param fileName
+     * @throws IOException
+     */
+    public static void downLoadFromUrl(String urlStr, String fileName, HttpServletResponse response) {
+        OutputStream outputStream = null;
+        InputStream inputStream = null;
+        try {
+            response.setContentType("application/octet-stream");
+            response.addHeader("Content-Disposition", "attachment;fileName=" +
+                    new String(fileName.getBytes("UTF-8"), "iso-8859-1"));
+            URL url = new URL(urlStr);
+            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
+            //设置超时间为3秒
+            conn.setConnectTimeout(10 * 1000);
+            conn.setReadTimeout(30 * 1000);
+            //防止屏蔽程序抓取而返回403错误
+            conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
+
+            outputStream = response.getOutputStream();
+            //得到输入流
+            inputStream = conn.getInputStream();
+            //获取自己数组
+            byte[] getData = readInputStream(inputStream);
+            outputStream.write(getData);
+            response.flushBuffer();
+        } catch (Exception e) {
+            log.error("{}下载失败:{}", fileName, e.getMessage());
+            e.printStackTrace();
+        } finally {
+            if (inputStream != null) {
+                try {
+                    inputStream.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+            if (outputStream != null) {
+                try {
+                    outputStream.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+        }
+    }
+
+
+    /**
+     * 从minio下载文件,返回流
+     *
+     * @param fileName
+     */
+    public static void downLoadFromMinio(String fileId, String fileName, HttpServletResponse response) {
+        OutputStream outputStream = null;
+        InputStream inputStream = null;
+        try {
+            response.setContentType("application/octet-stream");
+            response.addHeader("Content-Disposition", "attachment;fileName=" +
+                    new String(fileName.getBytes("UTF-8"), "iso-8859-1"));
+            inputStream = MinioUtil.getMinioFile(fileId);
+            //获取自己数组
+            byte[] getData = readInputStream(inputStream);
+            outputStream = response.getOutputStream();
+            outputStream.write(getData);
+            response.flushBuffer();
+        } catch (Exception e) {
+            log.error("{}下载失败:{}", fileName, e.getMessage());
+            e.printStackTrace();
+        } finally {
+            if (inputStream != null) {
+                try {
+                    inputStream.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+            if (outputStream != null) {
+                try {
+                    outputStream.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+        }
+    }
+
+
+    /**
+     * 从输入流中获取字节数组
+     *
+     * @param inputStream
+     * @return
+     * @throws IOException
+     */
+    public static byte[] readInputStream(InputStream inputStream) throws IOException {
+        byte[] buffer = new byte[1024];
+        int len = 0;
+        ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        while ((len = inputStream.read(buffer)) != -1) {
+            bos.write(buffer, 0, len);
+        }
+        bos.close();
+        return bos.toByteArray();
+    }
+
+    /**
+     * 从网络Url中读取文件内容
+     *
+     * @param urlStr
+     * @throws IOException
+     */
+    public static String readFromUrl(String urlStr) throws Exception {
+        InputStream inputStream = null;
+        try {
+            URL url = new URL(urlStr);
+            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
+            //设置超时间为3秒
+            conn.setConnectTimeout(10 * 1000);
+            conn.setReadTimeout(30 * 1000);
+            //防止屏蔽程序抓取而返回403错误
+            conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
+
+            //得到输入流
+            inputStream = conn.getInputStream();
+            return IOUtils.toString(inputStream, "utf-8");
+        } finally {
+            if (inputStream != null) {
+                try {
+                    inputStream.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+        }
+    }
+}

+ 15 - 1
system/system-biz/src/main/java/org/jeecg/modules/system/controller/CommonController.java

@@ -1,6 +1,7 @@
 package org.jeecg.modules.system.controller;
 
 import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.io.FileUtils;
 import org.jeecg.common.api.vo.Result;
 import org.jeecg.common.constant.CommonConstant;
 import org.jeecg.common.constant.SymbolConstant;
@@ -62,6 +63,9 @@ public class CommonController {
     @Autowired
     private IFileInfoService fileInfoService;
 
+    @Value("${tmpPath}")
+    private String tmpPath;
+
     /**
      * 文件上传统一方法
      *
@@ -97,8 +101,18 @@ public class CommonController {
         } else {
             savePath = CommonUtils.upload(file, bizPath, uploadType);
         }
+
+        //是否计算视频时长
+        Boolean isVideoDuration = Boolean.parseBoolean(request.getParameter("isVideoDuration"));
+        Long videoDuration = null;
+        if (isVideoDuration) {
+            File f = new File(tmpPath + File.separator + file.getOriginalFilename());
+            FileUtils.copyInputStreamToFile(file.getInputStream(), f);
+            videoDuration = CommonUtils.getVideoDuration(f);
+        }
+
         //保存文件信息
-        fileInfoService.saveFile(savePath, file.getOriginalFilename());
+        fileInfoService.saveFile(savePath, file.getOriginalFilename(), videoDuration);
         if (oConvertUtils.isNotEmpty(savePath)) {
             return Result.ok(savePath);
         } else {

+ 7 - 0
system/system-biz/src/main/java/org/jeecg/modules/system/entity/FileInfo.java

@@ -71,4 +71,11 @@ public class FileInfo implements Serializable {
     @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
     @ApiModelProperty(value = "更新日期")
     private Date updateTime;
+
+    /**
+     * 视频文件时长
+     */
+    @Excel(name = "视频文件时长", width = 15)
+    @ApiModelProperty(value = "视频文件时长")
+    private Long videoDuration;
 }

+ 1 - 1
system/system-biz/src/main/java/org/jeecg/modules/system/service/IFileInfoService.java

@@ -27,7 +27,7 @@ public interface IFileInfoService extends IService<FileInfo> {
      * @param fileName
      * @return
      */
-    FileInfo saveFile(String fileSaveId, String fileName);
+    FileInfo saveFile(String fileSaveId, String fileName, Long videoDuration);
 
 
     /**

+ 2 - 1
system/system-biz/src/main/java/org/jeecg/modules/system/service/impl/FileInfoServiceImpl.java

@@ -48,10 +48,11 @@ public class FileInfoServiceImpl extends ServiceImpl<FileInfoMapper, FileInfo> i
      * @return
      */
     @Override
-    public FileInfo saveFile(String fileSaveId, String fileName) {
+    public FileInfo saveFile(String fileSaveId, String fileName, Long videoDuration) {
         FileInfo fileInfo = new FileInfo();
         fileInfo.setFileSaveId(fileSaveId);
         fileInfo.setFileName(fileName);
+        fileInfo.setVideoDuration(videoDuration);
         save(fileInfo);
         return fileInfo;
     }

+ 9 - 5
web/src/main/java/com/ynfy/buss/course/coursecatalog/controller/CourseCatalogController.java

@@ -4,7 +4,6 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.ynfy.buss.course.coursecatalog.entity.CourseCatalog;
-import com.ynfy.buss.course.coursecatalog.enums.ResourceType;
 import com.ynfy.buss.course.coursecatalog.service.ICourseCatalogService;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
@@ -13,7 +12,8 @@ import org.jeecg.common.api.vo.Result;
 import org.jeecg.common.aspect.annotation.AutoLog;
 import org.jeecg.common.system.base.controller.JeecgController;
 import org.jeecg.common.system.query.QueryGenerator;
-import org.jeecg.common.util.CommonUtils;
+import org.jeecg.modules.system.entity.FileInfo;
+import org.jeecg.modules.system.service.IFileInfoService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
 import org.springframework.web.servlet.ModelAndView;
@@ -36,6 +36,10 @@ public class CourseCatalogController extends JeecgController<CourseCatalog, ICou
     @Autowired
     private ICourseCatalogService courseCatalogService;
 
+    @Autowired
+    private IFileInfoService fileInfoService;
+
+
     /**
      * 分页列表查询
      *
@@ -163,12 +167,12 @@ public class CourseCatalogController extends JeecgController<CourseCatalog, ICou
     /**
      * 获取视频时长 秒
      *
-     * @param resourceId
      * @return
      */
     @GetMapping("/getVideoDuration")
-    public Result<?> getVideoDuration(String resourceId) {
-        return Result.OK(CommonUtils.getVideoDuration(resourceId));
+    public Result<?> getVideoDuration(@RequestParam String fileSaveId) {
+        FileInfo fileInfo = fileInfoService.listByFileSaveId(fileSaveId);
+        return Result.OK("", fileInfo.getVideoDuration());
     }
 
     /**

+ 1 - 0
web/src/main/resources/application-dev.yml

@@ -275,3 +275,4 @@ third-app:
       # appSecret
       client-secret: ??
       agent-id: ??
+tmpPath: /Users/yangfeng/Documents

+ 1 - 0
web/src/main/resources/application-prod.yml

@@ -274,3 +274,4 @@ third-app:
       # appSecret
       client-secret: ??
       agent-id: ??
+tmpPath: /home/app/exam/backend/tmpfile