reghao 3 лет назад
Родитель
Сommit
1ec7a95cae
2 измененных файлов с 68 добавлено и 166 удалено
  1. 1 166
      src/test/java/ConsistentCheckTest.java
  2. 67 0
      src/test/java/VideoFileTest.java

+ 1 - 166
src/test/java/ConsistentCheckTest.java

@@ -41,102 +41,6 @@ public class ConsistentCheckTest {
     FileUploadService fileUploadService;
     @Autowired
     ImageFileService imageFileService;
-
-    HttpClient client = HttpClient.newBuilder()
-            .version(HttpClient.Version.HTTP_1_1)
-            .build();
-    @Test
-    public void test() {
-        List<String> urls = new ArrayList<>();
-        for (String url : urls) {
-            try {
-                HttpRequest.Builder builder = HttpRequest.newBuilder()
-                        .uri(URI.create(url))
-                        .timeout(Duration.ofSeconds(30))
-                        .GET();
-                HttpResponse<InputStream> streamBody = client.send(builder.build(), HttpResponse.BodyHandlers.ofInputStream());
-                if (streamBody.statusCode() != 200) {
-                    log.error("{} 获取失败", url);
-                    continue;
-                }
-
-                byte[] imageBytes = getBytes(streamBody.body());
-                String filename = UrlFormatter.getFilename(url);
-                long size = imageBytes.length;
-                String contentType = "image/jpeg";
-
-                ByteArrayInputStream inputStream = new ByteArrayInputStream(imageBytes);
-                UploadingFile uploadingFile = new UploadingFile(filename, size, contentType, inputStream);
-                UploadedFile uploadedFile = fileUploadService.put(uploadingFile);
-
-                /*ImageFileRet imageFileRet = imageFileService.process(uploadedFile);
-                String imageFileId = imageFileRet.getImageFileId();
-                String url1 = imageFileRet.getThumbnailUrl();*/
-                //saveFile(inputStream, new File(String.format("/home/reghao/Downloads/0/%s", filename)));
-            } catch (Exception e) {
-                e.printStackTrace();
-            }
-        }
-    }
-
-    private void saveFile(InputStream in, File file) throws IOException {
-        File parentDir = file.getParentFile();
-        if (!parentDir.exists()) {
-            FileUtils.forceMkdir(parentDir);
-        }
-
-        FileOutputStream fos = new FileOutputStream(file);
-        // 1MiB
-        int len = 1024*1024;
-        byte[] buf = new byte[len];
-        int readLen;
-        while ((readLen = in.read(buf, 0, len)) != -1) {
-            fos.write(buf, 0, readLen);
-        }
-        fos.close();
-    }
-
-    private byte[] getBytes(InputStream in) throws IOException {
-        ByteArrayOutputStream baos = new ByteArrayOutputStream();
-        // 1MiB
-        int len = 1024*1024;
-        byte[] buf = new byte[len];
-        int readLen;
-        while ((readLen = in.read(buf, 0, len)) != -1) {
-            baos.write(buf, 0, readLen);
-        }
-        baos.close();
-        in.close();
-        return baos.toByteArray();
-    }
-
-    @Test
-    public void saveLocalVideoFile() throws Exception {
-        String videoFileId = "wOdK71938E";
-        String filePath = "/home/reghao/Downloads/mp4/0/video_720.mp4";
-        saveVideoFile(videoFileId, filePath);
-    }
-
-    private void saveVideoFile(String videoFileId, String filePath) throws Exception {
-        File file = new File(filePath);
-
-        String filename = file.getName();
-        long size = file.length();
-        String contentType = "video/mp4";
-        InputStream inputStream = new FileInputStream(file);
-
-        UploadingFile uploadingFile = new UploadingFile(filename, size, contentType, inputStream);
-        UploadedFile fileRet = fileUploadService.put(uploadingFile);
-
-        String fileId = fileRet.getFileId();
-        int width = 1280;
-        int height = 720;
-        MediaResolution mediaResolution = MediaQuality.getQuality(width, height);
-        VideoUrl videoUrl = new VideoUrl(videoFileId, fileId, VideoUrlType.mp4.getName(),
-                width, height, mediaResolution.getQuality());
-        videoUrlMapper.save(videoUrl);
-    }
-
     @Autowired
     FileInfoMapper fileInfoMapper;
     @Autowired
@@ -146,76 +50,7 @@ public class ConsistentCheckTest {
     @Autowired
     VideoUrlMapper videoUrlMapper;
 
-    /**
-     * 检查 FileInfo 和 FileUrl 的一致性
-     * FileInfo 和 FileUrl 是 one-to-many 关系
-     *
-     * @param
-     * @return
-     * @date 2022-08-14 下午2:30
-     */
-    @Test
-    public void fileTest() {
-        int i = 0;
-        List<FileInfo> list = fileInfoMapper.findAll();
-        for (FileInfo fileInfo : list) {
-            String fileId = fileInfo.getFileId();
-            List<FileUrl> fileUrls = fileUrlMapper.findByFileId(fileId);
-            if (fileUrls.size() == 0) {
-                log.error("{} 没有 FileUrl", fileId);
-            } else if (fileUrls.size() > 1) {
-                log.error("{} 有 {} 个 FileUrl", fileId, fileUrls.size());
-            }
-
-            log.info("{}", i++);
-        }
-    }
-
-    @Test
-    public void videoFileTest() {
-        int i = 0;
-        List<FileUrl> list = fileUrlMapper.findAllVideoUrl();
-        for (FileUrl fileUrl : list) {
-            String fileId = fileUrl.getFileId();
-
-            VideoUrl videoUrl = videoUrlMapper.findByFileId(fileId);
-            if (videoUrl != null) {
-                String videoFileId = videoUrl.getVideoFileId();
-                videoUrlMapper.updateSetUrl(fileId, fileUrl.getUrl());
-
-                VideoFile videoFile = videoFileMapper.findByVideoFileId(videoFileId);
-                if (videoFile == null) {
-                    log.error("{} 没有 VideoFile", fileId);
-                }
-            } else {
-                log.error("{} 没有 VideoUrl", fileId);
-            }
-
-            log.info("{}", i++);
-        }
-    }
-
-    IdGenerator idGenerator = new IdGenerator("image-file-id");
     @Test
-    public void imageFileTest() {
-        int i = 0;
-        List<FileUrl> list = fileUrlMapper.findAllImageUrl();
-        for (FileUrl fileUrl : list) {
-            String fileId = fileUrl.getFileId();
-            log.error("{} 没有 ImageUrl", fileId);
-            log.info("{}", i++);
-        }
-    }
-
-    @Test
-    public void videoFileTest1() {
-        int i = 0;
-
-        List<VideoFile> videoFiles = videoFileMapper.findAll();
-        for (VideoFile videoFile : videoFiles) {
-            String coverUrl = videoFile.getCoverUrl();
-            log.error("{} 没有 ImageUrl", videoFile.getVideoFileId());
-            //log.info("{}", i++);
-        }
+    public void test() {
     }
 }

+ 67 - 0
src/test/java/VideoFileTest.java

@@ -0,0 +1,67 @@
+import cn.reghao.dfs.store.db.mapper.*;
+import cn.reghao.dfs.store.model.constant.VideoUrlType;
+import cn.reghao.dfs.store.model.dto.UploadingFile;
+import cn.reghao.dfs.store.model.po.*;
+import cn.reghao.dfs.store.model.dto.UploadedFile;
+import cn.reghao.dfs.store.service.FileUploadService;
+import cn.reghao.dfs.store.service.media.MediaQuality;
+import cn.reghao.dfs.store.service.media.MediaResolution;
+import cn.reghao.dfs.store.DfsStoreApplication;
+import lombok.extern.slf4j.Slf4j;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.context.ActiveProfiles;
+import org.springframework.test.context.junit4.SpringRunner;
+
+import java.io.*;
+
+/**
+ * @author reghao
+ * @date 2022-08-19 11:13:56
+ */
+@Slf4j
+@ActiveProfiles("dev")
+@SpringBootTest(classes = DfsStoreApplication.class)
+@RunWith(SpringRunner.class)
+public class VideoFileTest {
+    @Autowired
+    FileUploadService fileUploadService;
+    @Autowired
+    VideoUrlMapper videoUrlMapper;
+
+    /**
+     * 同一个视频的不同分辨率版本
+     *
+     * @param
+     * @return
+     * @date 2022-08-19 上午11:16
+     */
+    @Test
+    public void saveLocalVideoFile() throws Exception {
+        String videoFileId = "wOdK71938E";
+        String filePath = "/home/reghao/Downloads/mp4/0/video_720.mp4";
+        saveVideoFile(videoFileId, filePath);
+    }
+
+    private void saveVideoFile(String videoFileId, String filePath) throws Exception {
+        File file = new File(filePath);
+
+        String filename = file.getName();
+        long size = file.length();
+        String contentType = "video/mp4";
+        InputStream inputStream = new FileInputStream(file);
+
+        UploadingFile uploadingFile = new UploadingFile(filename, size, contentType, inputStream);
+        UploadedFile fileRet = fileUploadService.put(uploadingFile);
+
+        String fileId = fileRet.getFileId();
+        int width = 1280;
+        int height = 720;
+        MediaResolution mediaResolution = MediaQuality.getQuality(width, height);
+        VideoUrl videoUrl = new VideoUrl(videoFileId, fileId, VideoUrlType.mp4.getName(),
+                width, height, mediaResolution.getQuality());
+        videoUrlMapper.save(videoUrl);
+    }
+}