Оптимизация ядра, исправление гонки потоков при получении версии
This commit is contained in:
@@ -14,6 +14,7 @@ import { DialogContext } from "../DialogProvider/DialogProvider";
|
||||
import { useSaveAvatar } from "../AvatarProvider/useSaveAvatar";
|
||||
import { AVATAR_PASSWORD_TO_ENCODE } from "@/app/constants";
|
||||
import { useDialog } from "../DialogProvider/useDialog";
|
||||
import { useCore } from "@/app/hooks/useCore";
|
||||
|
||||
export enum DownloadStatus {
|
||||
DOWNLOADED,
|
||||
@@ -37,6 +38,7 @@ export function useAttachment(attachment: Attachment, keyPlain: string) {
|
||||
const {updateAttachmentInDialogCache} = useDialogsCache();
|
||||
const {info} = useConsoleLogger('useAttachment');
|
||||
const {updateAttachmentsInMessagesByAttachmentId} = useDialog();
|
||||
const {getDownloadsPath} = useCore();
|
||||
|
||||
|
||||
const context = useContext(DialogContext);
|
||||
@@ -85,7 +87,8 @@ export function useAttachment(attachment: Attachment, keyPlain: string) {
|
||||
const preview = getPreview();
|
||||
const filesize = parseInt(preview.split("::")[0]);
|
||||
const filename = preview.split("::")[1];
|
||||
let pathInDownloads = window.downloadsPath + "/Rosetta Downloads/" + filename;
|
||||
const downloadsPath = await getDownloadsPath();
|
||||
let pathInDownloads = downloadsPath + "/Rosetta Downloads/" + filename;
|
||||
const exists = await fileExists(pathInDownloads, false);
|
||||
const existsLength = await size(pathInDownloads, false);
|
||||
if(exists && existsLength == filesize){
|
||||
@@ -161,8 +164,9 @@ export function useAttachment(attachment: Attachment, keyPlain: string) {
|
||||
*/
|
||||
const preview = getPreview();
|
||||
const filename = preview.split("::")[1];
|
||||
const downloadsPath = await getDownloadsPath();
|
||||
let buffer = Buffer.from(decrypted.split(",")[1], 'base64');
|
||||
let pathInDownloads = window.downloadsPath + "/Rosetta Downloads/" + filename;
|
||||
let pathInDownloads = downloadsPath + "/Rosetta Downloads/" + filename;
|
||||
/**
|
||||
* Пишем файл в загрузки, но перед этим выбираем ему название, если файл в загрузках
|
||||
* уже есть с таким названием то добавляем к названию (1), (2) и так далее, чтобы не перезаписать существующий файл
|
||||
@@ -170,7 +174,7 @@ export function useAttachment(attachment: Attachment, keyPlain: string) {
|
||||
let finalPath = pathInDownloads;
|
||||
let fileIndex = 1;
|
||||
while (await fileExists(finalPath, false)) {
|
||||
finalPath = window.downloadsPath + "/Rosetta Downloads/" + filename.split(".").slice(0, -1).join(".") + ` (${fileIndex})` + "." + filename.split(".").slice(-1);
|
||||
finalPath = downloadsPath + "/Rosetta Downloads/" + filename.split(".").slice(0, -1).join(".") + ` (${fileIndex})` + "." + filename.split(".").slice(-1);
|
||||
fileIndex++;
|
||||
}
|
||||
await writeFile(finalPath, buffer, false);
|
||||
|
||||
33
app/providers/DeviceProvider/useCoreDevice.ts
Normal file
33
app/providers/DeviceProvider/useCoreDevice.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { useCore } from "@/app/hooks/useCore";
|
||||
import { useEffect, useState } from "react";
|
||||
|
||||
export function useCoreDevice() : {
|
||||
deviceId: string;
|
||||
deviceName: string;
|
||||
platform: string;
|
||||
} {
|
||||
const { getDeviceId, getDeviceName, getPlatform } = useCore();
|
||||
const [deviceId, setDeviceId] = useState<string>("");
|
||||
const [deviceName, setDeviceName] = useState<string>("");
|
||||
const [platform, setPlatform] = useState<string>("");
|
||||
|
||||
useEffect(() => {
|
||||
fetchDeviceInfo();
|
||||
}, []);
|
||||
|
||||
const fetchDeviceInfo = async () => {
|
||||
const deviceId = await getDeviceId();
|
||||
const deviceName = await getDeviceName();
|
||||
const platform = await getPlatform();
|
||||
setDeviceId(deviceId);
|
||||
setDeviceName(deviceName);
|
||||
setPlatform(platform);
|
||||
console.info("Device info - ID:", deviceId, "Name:", deviceName);
|
||||
}
|
||||
|
||||
return {
|
||||
deviceId,
|
||||
deviceName,
|
||||
platform
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ import { decodeWithPassword, encodeWithPassword } from "@/app/workers/crypto/cry
|
||||
import { useFileStorage } from "@/app/hooks/useFileStorage";
|
||||
import { generateRandomKey } from "@/app/utils/utils";
|
||||
import { createContext, useEffect, useState } from "react";
|
||||
import { useCore } from "@/app/hooks/useCore";
|
||||
|
||||
interface SystemProviderContextValue {
|
||||
id: string;
|
||||
@@ -21,7 +22,10 @@ export interface SystemProviderProps {
|
||||
*/
|
||||
export function SystemProvider(props: SystemProviderProps) {
|
||||
const [deviceId, setDeviceId] = useState<string>("");
|
||||
const [deviceName, setDeviceName] = useState<string>("");
|
||||
const [deviceOs, setDeviceOs] = useState<string>("");
|
||||
const {writeFile, readFile} = useFileStorage();
|
||||
const { getDeviceId, getDeviceName, getPlatform } = useCore();
|
||||
|
||||
useEffect(() => {
|
||||
fetchDeviceId();
|
||||
@@ -29,6 +33,10 @@ export function SystemProvider(props: SystemProviderProps) {
|
||||
|
||||
const fetchDeviceId = async () => {
|
||||
const device = await readFile("device");
|
||||
const name = await getDeviceName();
|
||||
const platform = await getPlatform();
|
||||
setDeviceName(name);
|
||||
setDeviceOs(platform);
|
||||
if(device){
|
||||
const decoded = await decodeDevice(Buffer.from(device).toString('utf-8'));
|
||||
if(decoded){
|
||||
@@ -47,12 +55,11 @@ export function SystemProvider(props: SystemProviderProps) {
|
||||
}
|
||||
|
||||
const decodeDevice = async (data: string) => {
|
||||
const hwid = window.deviceId;
|
||||
const platform = window.deviceName;
|
||||
const hwid = await getDeviceId();
|
||||
const deviceName = await getDeviceName();
|
||||
const salt = "rosetta-device-salt";
|
||||
|
||||
try {
|
||||
const decoded = await decodeWithPassword(hwid + platform + salt, data);
|
||||
const decoded = await decodeWithPassword(hwid + deviceName + salt, data);
|
||||
return decoded;
|
||||
} catch (e) {
|
||||
console.error("Failed to decode device data:", e);
|
||||
@@ -61,29 +68,24 @@ export function SystemProvider(props: SystemProviderProps) {
|
||||
}
|
||||
|
||||
const encodeDevice = async (data: string) => {
|
||||
const hwid = window.deviceId;
|
||||
const platform = window.deviceName;
|
||||
const hwid = await getDeviceId();
|
||||
const deviceName = await getDeviceName();
|
||||
const salt = "rosetta-device-salt";
|
||||
|
||||
try {
|
||||
const encoded = await encodeWithPassword(hwid + platform + salt, data);
|
||||
const encoded = await encodeWithPassword(hwid + deviceName + salt, data);
|
||||
return encoded;
|
||||
} catch (e) {
|
||||
console.error("Failed to encode device data:", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
const systemName = window.deviceName || "Unknown Device";
|
||||
const systemOs = window.platform || "Unknown OS";
|
||||
|
||||
|
||||
|
||||
return (
|
||||
<SystemProviderContext.Provider value={{
|
||||
id: deviceId,
|
||||
name: systemName,
|
||||
os: systemOs
|
||||
name: deviceName || "Unknown Device",
|
||||
os: deviceOs || "Unknown OS"
|
||||
}}>
|
||||
{props.children}
|
||||
</SystemProviderContext.Provider>
|
||||
|
||||
@@ -4,8 +4,8 @@ import { useSender } from "../ProtocolProvider/useSender";
|
||||
import { usePacket } from "../ProtocolProvider/usePacket";
|
||||
import { useConsoleLogger } from "@/app/hooks/useConsoleLogger";
|
||||
import { useFileStorage } from "@/app/hooks/useFileStorage";
|
||||
import { APPLICATION_ARCH, APPLICATION_PLATFROM, CORE_VERSION } from "@/app/constants";
|
||||
import { APP_VERSION } from "@/app/version";
|
||||
import { useCore } from "@/app/hooks/useCore";
|
||||
|
||||
export interface UpdateProviderProps {
|
||||
children: React.ReactNode;
|
||||
@@ -58,6 +58,7 @@ export function UpdateProvider(props: UpdateProviderProps) {
|
||||
const [appUpdateUrl, setAppUpdateUrl] = useState<string>("");
|
||||
const [appActualVersion, setAppActualVersion] = useState<string>("");
|
||||
const {writeFile} = useFileStorage();
|
||||
const {getCoreVersion, getArch, getPlatform} = useCore();
|
||||
|
||||
useEffect(() => {
|
||||
let packet = new PacketRequestUpdate();
|
||||
@@ -75,6 +76,9 @@ export function UpdateProvider(props: UpdateProviderProps) {
|
||||
}, []);
|
||||
|
||||
const checkForUpdates = async () => {
|
||||
const coreVersion = await getCoreVersion();
|
||||
const arch = await getArch();
|
||||
const platform = await getPlatform();
|
||||
if(updateServerRef.current == null){
|
||||
/**
|
||||
* SDU еще не определен
|
||||
@@ -85,7 +89,7 @@ export function UpdateProvider(props: UpdateProviderProps) {
|
||||
* Запрашиваем обновления с SDU сервера
|
||||
*/
|
||||
let response = await fetch
|
||||
(`${updateServerRef.current}/updates/get?app=${APP_VERSION}&kernel=${CORE_VERSION}&arch=${APPLICATION_ARCH}&platform=${APPLICATION_PLATFROM}`).catch((e) => {
|
||||
(`${updateServerRef.current}/updates/get?app=${APP_VERSION}&kernel=${coreVersion}&arch=${arch}&platform=${platform}`).catch((e) => {
|
||||
error("Failed to check for updates: " + e.message);
|
||||
});
|
||||
if(!response || response.status != 200){
|
||||
|
||||
Reference in New Issue
Block a user