|
@@ -1,273 +0,0 @@
|
|
|
-import cn.reghao.jutil.jdk.converter.DateTimeConverter;
|
|
|
|
|
-import cn.reghao.jutil.jdk.serializer.JsonConverter;
|
|
|
|
|
-import cn.reghao.jutil.jdk.string.SnowFlake;
|
|
|
|
|
-import cn.reghao.oss.api.dto.ObjectInfo;
|
|
|
|
|
-import cn.reghao.oss.api.dto.ServerInfo;
|
|
|
|
|
-import cn.reghao.oss.api.dto.rest.UploadFileRet;
|
|
|
|
|
-import cn.reghao.oss.sdk.OssClient;
|
|
|
|
|
-import cn.reghao.tnb.file.app.FileApplication;
|
|
|
|
|
-import cn.reghao.tnb.file.app.zchat.db.mapper.ChatFileMapper;
|
|
|
|
|
-import cn.reghao.tnb.file.app.zchat.db.mapper.ChatMessageMapper;
|
|
|
|
|
-import cn.reghao.tnb.file.app.zchat.model.constant.ChatMsgType;
|
|
|
|
|
-import cn.reghao.tnb.file.app.zchat.model.po.ChatMessage;
|
|
|
|
|
-import cn.reghao.tnb.file.app.zchat.model.po.ChatCall;
|
|
|
|
|
-import cn.reghao.tnb.file.app.zchat.model.po.ChatFile;
|
|
|
|
|
-import com.google.gson.JsonArray;
|
|
|
|
|
-import com.google.gson.JsonElement;
|
|
|
|
|
-import com.google.gson.JsonObject;
|
|
|
|
|
-import com.google.gson.JsonPrimitive;
|
|
|
|
|
-import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
-import org.junit.jupiter.api.Test;
|
|
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
-import org.springframework.boot.test.context.SpringBootTest;
|
|
|
|
|
-import org.springframework.test.context.ActiveProfiles;
|
|
|
|
|
-
|
|
|
|
|
-import java.io.File;
|
|
|
|
|
-import java.io.IOException;
|
|
|
|
|
-import java.nio.file.*;
|
|
|
|
|
-import java.nio.file.attribute.BasicFileAttributes;
|
|
|
|
|
-import java.time.LocalDateTime;
|
|
|
|
|
-import java.util.*;
|
|
|
|
|
-
|
|
|
|
|
-@Slf4j
|
|
|
|
|
-@ActiveProfiles("dev")
|
|
|
|
|
-@SpringBootTest(classes = FileApplication.class)
|
|
|
|
|
-public class DiskUnitTest {
|
|
|
|
|
- @Autowired
|
|
|
|
|
- ChatMessageMapper chatMessageMapper;
|
|
|
|
|
- @Autowired
|
|
|
|
|
- ChatFileMapper chatFileMapper;
|
|
|
|
|
-
|
|
|
|
|
- void walkDir(Path path) throws IOException {
|
|
|
|
|
- Files.walkFileTree(path, new SimpleFileVisitor<>() {
|
|
|
|
|
- @Override
|
|
|
|
|
- public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
|
|
|
|
|
- File file1 = file.toFile();
|
|
|
|
|
- process(file1);
|
|
|
|
|
- return FileVisitResult.CONTINUE;
|
|
|
|
|
- }
|
|
|
|
|
- });
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- private void process(File file) {
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- @Test
|
|
|
|
|
- public void fileWalk() throws Exception {
|
|
|
|
|
- String baseDir = "/disk/1";
|
|
|
|
|
- //Path path = Paths.get(baseDir);
|
|
|
|
|
- //walkDir(path);
|
|
|
|
|
-
|
|
|
|
|
- /*String filePath = "/home/reghao/Downloads/v2-cb256.jpg";
|
|
|
|
|
- UploadFileRet uploadFileRet = uploadFile(new File(filePath));
|
|
|
|
|
- String objectId = uploadFileRet.getUploadId();
|
|
|
|
|
- String signedUrl = getOssClient().getSignedUrl(objectId, ObjectAction.access.getName(), null);
|
|
|
|
|
- System.out.println(signedUrl);*/
|
|
|
|
|
-
|
|
|
|
|
- parseTelegram();
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- String baseDir = "/home/reghao/Downloads/0/tg/tg_json";
|
|
|
|
|
- private void parseTelegram() {
|
|
|
|
|
- List<ChatMessage> chatMessageList = new ArrayList<>();
|
|
|
|
|
- List<ChatFile> chatFileList = new ArrayList<>();
|
|
|
|
|
- List<ChatCall> chatCallList = new ArrayList<>();
|
|
|
|
|
-
|
|
|
|
|
- String path = baseDir + "/result.json";
|
|
|
|
|
- File jsonFile = new File(path);
|
|
|
|
|
- JsonObject jsonObject = JsonConverter.jsonFileToObject(jsonFile, JsonObject.class);
|
|
|
|
|
- JsonArray jsonArray = jsonObject.get("chats").getAsJsonObject().get("list").getAsJsonArray();
|
|
|
|
|
- JsonObject chat = jsonArray.get(0).getAsJsonObject();
|
|
|
|
|
- JsonArray messages = chat.get("messages").getAsJsonArray();
|
|
|
|
|
- for (JsonElement jsonElement : messages) {
|
|
|
|
|
- JsonObject jsonObject1 = jsonElement.getAsJsonObject();
|
|
|
|
|
- try {
|
|
|
|
|
- long id = jsonObject1.get("id").getAsLong();
|
|
|
|
|
- String date = jsonObject1.get("date").getAsString();
|
|
|
|
|
- LocalDateTime createAt = DateTimeConverter.localDateTime2(date.replace("T", " "));
|
|
|
|
|
- String type = jsonObject1.get("type").getAsString();
|
|
|
|
|
- if ("service".equals(type)) {
|
|
|
|
|
- long senderId = jsonObject1.get("actor_id").getAsLong();
|
|
|
|
|
- long messageId = snowFlake.nextId();
|
|
|
|
|
- ChatMessage chatMessage = new ChatMessage(messageId, chatId, senderId, createAt);
|
|
|
|
|
- chatMessage.setMsgType(ChatMsgType.audioCall.getCode());
|
|
|
|
|
-
|
|
|
|
|
- String callType = "audio";
|
|
|
|
|
- double duration = 0.0D;
|
|
|
|
|
- String discardReason = jsonObject1.get("discard_reason").getAsString();
|
|
|
|
|
- if ("hangup".equals(discardReason)) {
|
|
|
|
|
- duration = jsonObject1.get("duration_seconds").getAsDouble();
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- ChatCall chatCall = new ChatCall(messageId, callType, duration, discardReason);
|
|
|
|
|
- chatCallList.add(chatCall);
|
|
|
|
|
- chatMessageList.add(chatMessage);
|
|
|
|
|
- continue;
|
|
|
|
|
- }
|
|
|
|
|
- String from = jsonObject1.get("from").getAsString();
|
|
|
|
|
- long fromId = jsonObject1.get("from_id").getAsLong();
|
|
|
|
|
-
|
|
|
|
|
- long senderId = userMap.get(fromId);
|
|
|
|
|
- long messageId = snowFlake.nextId();
|
|
|
|
|
- ChatMessage chatMessage = new ChatMessage(messageId, chatId, senderId, createAt);
|
|
|
|
|
-
|
|
|
|
|
- JsonElement jsonElement1 = jsonObject1.get("text");
|
|
|
|
|
- if (jsonElement1 instanceof JsonPrimitive) {
|
|
|
|
|
- String text = jsonObject1.get("text").getAsString();
|
|
|
|
|
- chatMessage.setMsgType(ChatMsgType.text.getCode());
|
|
|
|
|
- chatMessage.setContent(text);
|
|
|
|
|
- } else if (jsonElement1 instanceof JsonArray) {
|
|
|
|
|
- String text1 = "";
|
|
|
|
|
- for (JsonElement jsonElement2 : jsonElement1.getAsJsonArray()) {
|
|
|
|
|
- if (jsonElement2 instanceof JsonPrimitive) {
|
|
|
|
|
- text1 = jsonElement2.getAsString();
|
|
|
|
|
- } else if (jsonElement2 instanceof JsonObject) {
|
|
|
|
|
- try {
|
|
|
|
|
- JsonObject jsonObject2 = jsonElement2.getAsJsonObject();
|
|
|
|
|
- String type1 = jsonObject2.get("type").getAsString();
|
|
|
|
|
- String link1 = jsonObject2.get("text").getAsString();
|
|
|
|
|
- if (type1.equals("link")) {
|
|
|
|
|
- chatMessage.setMsgType(ChatMsgType.chatLink.getCode());
|
|
|
|
|
- chatMessage.setContent(text1);
|
|
|
|
|
- chatMessage.setUrl(link1);
|
|
|
|
|
- } else if (type1.equals("email")) {
|
|
|
|
|
- chatMessage.setMsgType(ChatMsgType.chatLink.getCode());
|
|
|
|
|
- chatMessage.setContent(text1);
|
|
|
|
|
- chatMessage.setUrl(link1);
|
|
|
|
|
- } else {
|
|
|
|
|
- System.out.printf("unknown type1: %s\n", type1);
|
|
|
|
|
- }
|
|
|
|
|
- } catch (Exception e) {
|
|
|
|
|
- System.out.println(e.getMessage());
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- } else {
|
|
|
|
|
- System.out.printf("text field is unknown type: %s\n", jsonObject1.get("text"));
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- String mediaType = "NoMedia";
|
|
|
|
|
- if (jsonObject1.get("media_type") != null) {
|
|
|
|
|
- mediaType = jsonObject1.get("media_type").getAsString();
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- if (!mediaType.equals("NoMedia")) {
|
|
|
|
|
- String filePath = jsonObject1.get("file").getAsString();
|
|
|
|
|
- File file = new File(baseDir + "/" + filePath);
|
|
|
|
|
- if (!file.exists()) {
|
|
|
|
|
- System.out.printf("file message %s not exist, continue next loop\n", filePath);
|
|
|
|
|
- continue;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- if ("voice_message".equals(mediaType)) {
|
|
|
|
|
- chatMessage.setMsgType(ChatMsgType.audio.getCode());
|
|
|
|
|
-
|
|
|
|
|
- UploadFileRet uploadFileRet = uploadFile(file);
|
|
|
|
|
- String objectId = uploadFileRet.getUploadId();
|
|
|
|
|
- ObjectInfo objectInfo = getOssClient().getObjectInfo(objectId);
|
|
|
|
|
- ChatFile chatFile = new ChatFile(chatMessage.getMessageId(), objectInfo);
|
|
|
|
|
- chatFileList.add(chatFile);
|
|
|
|
|
- } else if ("video_message".equals(mediaType)) {
|
|
|
|
|
- chatMessage.setMsgType(ChatMsgType.video.getCode());
|
|
|
|
|
-
|
|
|
|
|
- UploadFileRet uploadFileRet = uploadFile(file);
|
|
|
|
|
- String objectId = uploadFileRet.getUploadId();
|
|
|
|
|
- ObjectInfo objectInfo = getOssClient().getObjectInfo(objectId);
|
|
|
|
|
- ChatFile chatFile = new ChatFile(chatMessage.getMessageId(), objectInfo);
|
|
|
|
|
- chatFileList.add(chatFile);
|
|
|
|
|
- } else if ("video_file".equals(mediaType)) {
|
|
|
|
|
- chatMessage.setMsgType(ChatMsgType.video.getCode());
|
|
|
|
|
-
|
|
|
|
|
- UploadFileRet uploadFileRet = uploadFile(file);
|
|
|
|
|
- String objectId = uploadFileRet.getUploadId();
|
|
|
|
|
- ObjectInfo objectInfo = getOssClient().getObjectInfo(objectId);
|
|
|
|
|
- ChatFile chatFile = new ChatFile(chatMessage.getMessageId(), objectInfo);
|
|
|
|
|
- chatFileList.add(chatFile);
|
|
|
|
|
- } else if ("animation".equals(mediaType)) {
|
|
|
|
|
- if (filePath.endsWith("gif.mp4")) {
|
|
|
|
|
- String filePath1 = filePath.replace(".mp4", "");
|
|
|
|
|
- File file1 = new File(baseDir + "/" + filePath1);
|
|
|
|
|
- if (!file1.exists()) {
|
|
|
|
|
- System.out.printf("file message %s not exist, continue next loop\n", filePath1);
|
|
|
|
|
- continue;
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- chatMessage.setMsgType(ChatMsgType.image.getCode());
|
|
|
|
|
-
|
|
|
|
|
- UploadFileRet uploadFileRet = uploadFile(file);
|
|
|
|
|
- String objectId = uploadFileRet.getUploadId();
|
|
|
|
|
- ObjectInfo objectInfo = getOssClient().getObjectInfo(objectId);
|
|
|
|
|
- ChatFile chatFile = new ChatFile(chatMessage.getMessageId(), objectInfo);
|
|
|
|
|
- chatFileList.add(chatFile);
|
|
|
|
|
- } else if ("sticker".equals(mediaType)) {
|
|
|
|
|
- if (filePath.endsWith(".tgs")) {
|
|
|
|
|
- String filePath1 = filePath + "_thumb.jpg";
|
|
|
|
|
- File file1 = new File(baseDir + "/" + filePath1);
|
|
|
|
|
- if (!file1.exists()) {
|
|
|
|
|
- System.out.printf("file message %s not exist, continue next loop\n", filePath1);
|
|
|
|
|
- continue;
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- chatMessage.setMsgType(ChatMsgType.image.getCode());
|
|
|
|
|
-
|
|
|
|
|
- UploadFileRet uploadFileRet = uploadFile(file);
|
|
|
|
|
- String objectId = uploadFileRet.getUploadId();
|
|
|
|
|
- ObjectInfo objectInfo = getOssClient().getObjectInfo(objectId);
|
|
|
|
|
- ChatFile chatFile = new ChatFile(chatMessage.getMessageId(), objectInfo);
|
|
|
|
|
- chatFileList.add(chatFile);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- if (jsonObject1.get("photo") != null) {
|
|
|
|
|
- String photoPath = jsonObject1.get("photo").getAsString();
|
|
|
|
|
- File photoFile = new File(baseDir + "/" + photoPath);
|
|
|
|
|
- if (!photoFile.exists()) {
|
|
|
|
|
- System.out.printf("file message %s not exist, continue next loop\n", photoPath);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- chatMessage.setMsgType(ChatMsgType.image.getCode());
|
|
|
|
|
-
|
|
|
|
|
- UploadFileRet uploadFileRet = uploadFile(photoFile);
|
|
|
|
|
- String objectId = uploadFileRet.getUploadId();
|
|
|
|
|
- ObjectInfo objectInfo = getOssClient().getObjectInfo(objectId);
|
|
|
|
|
- ChatFile chatFile = new ChatFile(chatMessage.getMessageId(), objectInfo);
|
|
|
|
|
- chatFileList.add(chatFile);
|
|
|
|
|
- }
|
|
|
|
|
- //System.out.printf("%s - %s[%s]\n", date, fromId, from);
|
|
|
|
|
- chatMessageList.add(chatMessage);
|
|
|
|
|
- } catch (Exception e) {
|
|
|
|
|
- System.out.println(e.getMessage());
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- System.out.println();
|
|
|
|
|
- /*chatFileMapper.saveAll(chatFileList);
|
|
|
|
|
- chatMessageMapper.saveAll(chatMessageList);*/
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- long chatId = 10002L;
|
|
|
|
|
- SnowFlake snowFlake = new SnowFlake(1L, 1L);
|
|
|
|
|
- Map<Long, Long> userMap = Map.of(
|
|
|
|
|
- 10001L, 10001L,
|
|
|
|
|
- 10002L, 10002L
|
|
|
|
|
- );
|
|
|
|
|
-
|
|
|
|
|
- private UploadFileRet uploadFile(File file) throws Exception {
|
|
|
|
|
- OssClient ossClient = getOssClient();
|
|
|
|
|
- int channelCode = 109;
|
|
|
|
|
- ServerInfo serverInfo = ossClient.getServerInfo(channelCode);
|
|
|
|
|
-
|
|
|
|
|
- UploadFileRet uploadFileRet = ossClient.putObject(serverInfo, file);
|
|
|
|
|
- return uploadFileRet;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- private OssClient getOssClient() {
|
|
|
|
|
- String endpoint = "https://ossconsole.reghao.cn";
|
|
|
|
|
- String ak = "PmvonhHI";
|
|
|
|
|
- String sk = "28aQEK7wJ6U5m7E1u7";
|
|
|
|
|
- OssClient ossClient = new OssClient(endpoint, ak, sk);
|
|
|
|
|
- return ossClient;
|
|
|
|
|
- }
|
|
|
|
|
-}
|
|
|