From 3044a43a89c7ac57fdfb8cca1768c018b399392a Mon Sep 17 00:00:00 2001 From: icechen Date: Tue, 22 Feb 2022 00:53:55 +0800 Subject: [PATCH] update --- src/App.tsx | 3 +- src/component/countriesInput.tsx | 2 +- src/{ => telegram}/telegram.ts | 57 +++----------------------------- src/telegram/user.ts | 0 src/utils/rand.ts | 6 +++- 5 files changed, 11 insertions(+), 57 deletions(-) rename src/{ => telegram}/telegram.ts (77%) create mode 100644 src/telegram/user.ts diff --git a/src/App.tsx b/src/App.tsx index 46fff41..1024f86 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -14,11 +14,10 @@ import Telegram, { uploadBigFile, sendFile, downloadFile, -} from "./telegram"; +} from "./telegram/telegram"; import SendIcon from "@mui/icons-material/Send"; import { User } from "./user"; import { Download, Upload } from "@mui/icons-material"; -import ab2str from "./utils/arraybuffer2str"; function App() { let [phone, setPhone] = useState(""); diff --git a/src/component/countriesInput.tsx b/src/component/countriesInput.tsx index ef033ff..61d3e3d 100644 --- a/src/component/countriesInput.tsx +++ b/src/component/countriesInput.tsx @@ -1,6 +1,6 @@ import { Autocomplete, Box, TextField } from "@mui/material"; import React, { useEffect, useState } from "react"; -import Telegram from "../telegram"; +import Telegram from "../telegram/telegram"; export interface Country { default_name: string; diff --git a/src/telegram.ts b/src/telegram/telegram.ts similarity index 77% rename from src/telegram.ts rename to src/telegram/telegram.ts index a9f9817..22caa36 100644 --- a/src/telegram.ts +++ b/src/telegram/telegram.ts @@ -1,5 +1,5 @@ import Client from "mtproton/envs/browser"; -import between from "./utils/rand"; +import { rand_id } from "../utils/rand"; class TelegramHelper { private client: any; @@ -51,7 +51,7 @@ class TelegramHelper { async function uploadBigFile( bytes: ArrayBuffer ): Promise<{ file_id: number; total_part: number }> { - let file_id = between(10000, 99999); + let file_id = rand_id(); console.log("file_id", file_id); let uploadBytes = new Uint8Array(bytes); @@ -96,7 +96,7 @@ function inputFile(file_id: number, part_number: number, file_name: string) { } function sendFile(inputFile: any, type: string) { - let random_id = between(10000, 99999); + let random_id = rand_id(); console.log("random_id", random_id); return Telegram.call("messages.sendMedia", { peer: { @@ -113,7 +113,7 @@ function sendFile(inputFile: any, type: string) { }, ], }, - random_id: between(10000, 99999), + random_id: rand_id(), message: "test", }); } @@ -167,54 +167,5 @@ const appID = 18987971; const appHash = "fcfd9e6ed3f9e48a360bb57cc0d59d98"; let Telegram = new TelegramHelper(appID, appHash); -function hexStringToArrayBuffer(hexString: string) { - // remove the leading 0x - hexString = hexString.replace(/^0x/, ""); - - // ensure even number of characters - if (hexString.length % 2 != 0) { - console.log( - "WARNING: expecting an even number of characters in the hexString" - ); - } - - // check for some non-hex characters - let bad = hexString.match(/[G-Z\s]/i); - if (bad) { - console.log("WARNING: found non-hex characters", bad); - } - - // split the string into pairs of octets - let pairs = hexString.match(/[\dA-F]{2}/gi); - - // convert the octets to integers - // @ts-ignore - let integers = pairs.map(function (s: string) { - return parseInt(s, 16); - }); - - let array = new Uint8Array(integers); - console.log(array); - - return array.buffer; -} - -function arrayBufferToHex(arrayBuffer: ArrayBuffer) { - if (typeof arrayBuffer !== "object" || arrayBuffer === null) { - throw new TypeError("Expected input to be an ArrayBuffer"); - } - - let view = new Uint8Array(arrayBuffer); - let result = ""; - let value; - - for (let i = 0; i < view.length; i++) { - value = view[i].toString(16); - result += value.length === 1 ? "0" + value : value; - } - - return result; -} - export default Telegram; export { uploadBigFile, inputFile, sendFile, downloadFile }; diff --git a/src/telegram/user.ts b/src/telegram/user.ts new file mode 100644 index 0000000..e69de29 diff --git a/src/utils/rand.ts b/src/utils/rand.ts index 49ecb81..2c05b66 100644 --- a/src/utils/rand.ts +++ b/src/utils/rand.ts @@ -2,4 +2,8 @@ function between(min: number, max: number): number { return Math.floor(Math.random() * (max - min) + min); } -export default between; +function rand_id(): number { + return between(100000, 999999); +} + +export { between, rand_id };