|
|
@@ -1,16 +1,18 @@
|
|
|
import cn.reghao.dfs.store.db.mapper.*;
|
|
|
import cn.reghao.dfs.store.model.constant.VideoUrlType;
|
|
|
-import cn.reghao.dfs.store.model.dto.UploadedFile;
|
|
|
+import cn.reghao.dfs.store.model.dto.UploadingFile;
|
|
|
+import cn.reghao.dfs.store.model.po.ImageUrl;
|
|
|
import cn.reghao.dfs.store.model.po.VideoUrl;
|
|
|
-import cn.reghao.dfs.store.model.vo.UploadFileRet;
|
|
|
+import cn.reghao.dfs.store.model.vo.ImageFileRet;
|
|
|
+import cn.reghao.dfs.store.model.dto.UploadedFile;
|
|
|
import cn.reghao.dfs.store.service.FileUploadService;
|
|
|
+import cn.reghao.dfs.store.service.media.ImageFileService;
|
|
|
import cn.reghao.dfs.store.service.media.MediaQuality;
|
|
|
-import cn.reghao.jutil.jdk.security.DigestUtil;
|
|
|
+import cn.reghao.jutil.jdk.http.util.UrlFormatter;
|
|
|
import cn.reghao.dfs.store.DfsStoreApplication;
|
|
|
-import cn.reghao.dfs.store.model.po.FileInfo;
|
|
|
import cn.reghao.dfs.store.model.po.FileUrl;
|
|
|
-import cn.reghao.dfs.store.util.store.LocalStores;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.io.FileUtils;
|
|
|
import org.junit.Test;
|
|
|
import org.junit.runner.RunWith;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
@@ -19,9 +21,11 @@ import org.springframework.test.context.ActiveProfiles;
|
|
|
import org.springframework.test.context.junit4.SpringRunner;
|
|
|
|
|
|
import java.io.*;
|
|
|
-import java.nio.file.*;
|
|
|
-import java.nio.file.attribute.BasicFileAttributes;
|
|
|
-import java.security.NoSuchAlgorithmException;
|
|
|
+import java.net.URI;
|
|
|
+import java.net.http.HttpClient;
|
|
|
+import java.net.http.HttpRequest;
|
|
|
+import java.net.http.HttpResponse;
|
|
|
+import java.time.Duration;
|
|
|
import java.util.*;
|
|
|
|
|
|
/**
|
|
|
@@ -34,25 +38,83 @@ import java.util.*;
|
|
|
@RunWith(SpringRunner.class)
|
|
|
public class ConsistentCheckTest {
|
|
|
@Autowired
|
|
|
- FileInfoMapper fileInfoMapper;
|
|
|
- @Autowired
|
|
|
- FileUrlMapper fileUrlMapper;
|
|
|
+ FileUploadService fileUploadService;
|
|
|
@Autowired
|
|
|
- FileUserMapper fileUserMapper;
|
|
|
+ ImageFileService imageFileService;
|
|
|
@Autowired
|
|
|
- VideoFileMapper videoFileMapper;
|
|
|
+ VideoUrlMapper videoUrlMapper;
|
|
|
@Autowired
|
|
|
- ImageFileMapper imageFileMapper;
|
|
|
+ ImageUrlMapper imageUrlMapper;
|
|
|
|
|
|
+ HttpClient client = HttpClient.newBuilder()
|
|
|
+ .version(HttpClient.Version.HTTP_1_1)
|
|
|
+ .build();
|
|
|
@Test
|
|
|
- public void test() throws IOException {
|
|
|
+ public void test() {
|
|
|
+ List<String> urls = imageUrlMapper.getImageUrls();
|
|
|
+ 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 fileRet = fileUploadService.put(uploadingFile);
|
|
|
+
|
|
|
+ ImageFileRet imageFileRet = imageFileService.process(fileRet.getFileId(), fileRet.getPathUrl());
|
|
|
+ String imageFileId = imageFileRet.getImageFileId();
|
|
|
+ String url1 = imageFileRet.getThumbnailUrl();
|
|
|
+ imageUrlMapper.updateImageUrl(url, imageFileId, url1);
|
|
|
+ //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();
|
|
|
}
|
|
|
|
|
|
- @Autowired
|
|
|
- FileUploadService fileUploadService;
|
|
|
- @Autowired
|
|
|
- VideoUrlMapper videoUrlMapper;
|
|
|
@Test
|
|
|
public void saveLocalVideoFile() throws Exception {
|
|
|
String videoFileId = "wOdK71938E";
|
|
|
@@ -68,8 +130,8 @@ public class ConsistentCheckTest {
|
|
|
String contentType = "video/mp4";
|
|
|
InputStream inputStream = new FileInputStream(file);
|
|
|
|
|
|
- UploadedFile uploadedFile = new UploadedFile(filename, size, contentType, inputStream);
|
|
|
- UploadFileRet fileRet = fileUploadService.put(uploadedFile);
|
|
|
+ UploadingFile uploadingFile = new UploadingFile(filename, size, contentType, inputStream);
|
|
|
+ UploadedFile fileRet = fileUploadService.put(uploadingFile);
|
|
|
|
|
|
String fileId = fileRet.getFileId();
|
|
|
int width = 1280;
|
|
|
@@ -79,4 +141,30 @@ public class ConsistentCheckTest {
|
|
|
VideoUrl videoUrl = new VideoUrl(videoFileId, fileId, VideoUrlType.mp4.getName(), width, height, quality);
|
|
|
videoUrlMapper.save(videoUrl);
|
|
|
}
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ FileUrlMapper fileUrlMapper;
|
|
|
+ @Test
|
|
|
+ public void imgTest() {
|
|
|
+ List<ImageUrl> list = imageUrlMapper.findAll();
|
|
|
+ list.forEach(imageUrl -> {
|
|
|
+ String fileId = imageUrl.getFileId();
|
|
|
+ FileUrl fileUrl = fileUrlMapper.findFileUrl(fileId, 0, 0);
|
|
|
+ String url = fileUrl.getUrl();
|
|
|
+
|
|
|
+ imageUrlMapper.updateSetUrl(fileId, url);
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void vidTest() {
|
|
|
+ List<VideoUrl> list = videoUrlMapper.findAll();
|
|
|
+ list.forEach(videoUrl -> {
|
|
|
+ String fileId = videoUrl.getFileId();
|
|
|
+ FileUrl fileUrl = fileUrlMapper.findFileUrl(fileId, 0, 0);
|
|
|
+ String url = fileUrl.getUrl();
|
|
|
+
|
|
|
+ videoUrlMapper.updateSetUrl(fileId, url);
|
|
|
+ });
|
|
|
+ }
|
|
|
}
|