|
1 |
| -import { HydraApi, logger, Ludusavi, WindowManager } from "@main/services"; |
| 1 | +import { CloudSync } from "@main/services"; |
2 | 2 | import { registerEvent } from "../register-event";
|
3 |
| -import fs from "node:fs"; |
4 |
| -import path from "node:path"; |
5 |
| -import * as tar from "tar"; |
6 |
| -import crypto from "node:crypto"; |
7 | 3 | import type { GameShop } from "@types";
|
8 |
| -import axios from "axios"; |
9 |
| -import os from "node:os"; |
10 |
| -import { backupsPath } from "@main/constants"; |
11 |
| -import { app } from "electron"; |
12 |
| -import { normalizePath } from "@main/helpers"; |
13 |
| -import { gamesSublevel, levelKeys } from "@main/level"; |
14 |
| - |
15 |
| -const bundleBackup = async ( |
16 |
| - shop: GameShop, |
17 |
| - objectId: string, |
18 |
| - winePrefix: string | null |
19 |
| -) => { |
20 |
| - const backupPath = path.join(backupsPath, `${shop}-${objectId}`); |
21 |
| - |
22 |
| - // Remove existing backup |
23 |
| - if (fs.existsSync(backupPath)) { |
24 |
| - fs.rmSync(backupPath, { recursive: true }); |
25 |
| - } |
26 |
| - |
27 |
| - await Ludusavi.backupGame(shop, objectId, backupPath, winePrefix); |
28 |
| - |
29 |
| - const tarLocation = path.join(backupsPath, `${crypto.randomUUID()}.tar`); |
30 |
| - |
31 |
| - await tar.create( |
32 |
| - { |
33 |
| - gzip: false, |
34 |
| - file: tarLocation, |
35 |
| - cwd: backupPath, |
36 |
| - }, |
37 |
| - ["."] |
38 |
| - ); |
39 |
| - |
40 |
| - return tarLocation; |
41 |
| -}; |
| 4 | +import { t } from "i18next"; |
| 5 | +import { format } from "date-fns"; |
42 | 6 |
|
43 | 7 | const uploadSaveGame = async (
|
44 | 8 | _event: Electron.IpcMainInvokeEvent,
|
45 | 9 | objectId: string,
|
46 | 10 | shop: GameShop,
|
47 | 11 | downloadOptionTitle: string | null
|
48 | 12 | ) => {
|
49 |
| - const game = await gamesSublevel.get(levelKeys.game(shop, objectId)); |
50 |
| - |
51 |
| - const bundleLocation = await bundleBackup( |
52 |
| - shop, |
| 13 | + return CloudSync.uploadSaveGame( |
53 | 14 | objectId,
|
54 |
| - game?.winePrefixPath ?? null |
| 15 | + shop, |
| 16 | + downloadOptionTitle, |
| 17 | + t("backup_from", { |
| 18 | + ns: "game_details", |
| 19 | + date: format(new Date(), "dd/MM/yyyy"), |
| 20 | + }) |
55 | 21 | );
|
56 |
| - |
57 |
| - fs.stat(bundleLocation, async (err, stat) => { |
58 |
| - if (err) { |
59 |
| - logger.error("Failed to get zip file stats", err); |
60 |
| - throw err; |
61 |
| - } |
62 |
| - |
63 |
| - const { uploadUrl } = await HydraApi.post<{ |
64 |
| - id: string; |
65 |
| - uploadUrl: string; |
66 |
| - }>("/profile/games/artifacts", { |
67 |
| - artifactLengthInBytes: stat.size, |
68 |
| - shop, |
69 |
| - objectId, |
70 |
| - hostname: os.hostname(), |
71 |
| - homeDir: normalizePath(app.getPath("home")), |
72 |
| - downloadOptionTitle, |
73 |
| - platform: os.platform(), |
74 |
| - }); |
75 |
| - |
76 |
| - fs.readFile(bundleLocation, async (err, fileBuffer) => { |
77 |
| - if (err) { |
78 |
| - logger.error("Failed to read zip file", err); |
79 |
| - throw err; |
80 |
| - } |
81 |
| - |
82 |
| - await axios.put(uploadUrl, fileBuffer, { |
83 |
| - headers: { |
84 |
| - "Content-Type": "application/tar", |
85 |
| - }, |
86 |
| - onUploadProgress: (progressEvent) => { |
87 |
| - logger.log(progressEvent); |
88 |
| - }, |
89 |
| - }); |
90 |
| - |
91 |
| - WindowManager.mainWindow?.webContents.send( |
92 |
| - `on-upload-complete-${objectId}-${shop}`, |
93 |
| - true |
94 |
| - ); |
95 |
| - |
96 |
| - fs.rm(bundleLocation, (err) => { |
97 |
| - if (err) { |
98 |
| - logger.error("Failed to remove tar file", err); |
99 |
| - throw err; |
100 |
| - } |
101 |
| - }); |
102 |
| - }); |
103 |
| - }); |
104 | 22 | };
|
105 | 23 |
|
106 | 24 | registerEvent("uploadSaveGame", uploadSaveGame);
|
0 commit comments