react-native-electron-platform 0.0.25 → 0.0.26
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -1,6 +1,14 @@
|
|
|
1
1
|
import { ipcMain, dialog } from "electron";
|
|
2
|
+
import fs from "fs";
|
|
3
|
+
import https from "https";
|
|
4
|
+
import http from "http";
|
|
5
|
+
import path from "path";
|
|
2
6
|
|
|
3
7
|
export function registerFileHandlers() {
|
|
8
|
+
|
|
9
|
+
// ======================================================
|
|
10
|
+
// SELECT FILE
|
|
11
|
+
// ======================================================
|
|
4
12
|
ipcMain.handle("select-file", async () => {
|
|
5
13
|
try {
|
|
6
14
|
const result = await dialog.showOpenDialog({
|
|
@@ -22,12 +30,82 @@ export function registerFileHandlers() {
|
|
|
22
30
|
status: "selected",
|
|
23
31
|
filePath: result.filePaths[0],
|
|
24
32
|
};
|
|
33
|
+
|
|
25
34
|
} catch (err) {
|
|
35
|
+
|
|
26
36
|
console.error("select-file error:", err);
|
|
37
|
+
|
|
38
|
+
return {
|
|
39
|
+
status: "error",
|
|
40
|
+
message: err.message,
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
// ======================================================
|
|
47
|
+
// UNIVERSAL FILE DOWNLOAD
|
|
48
|
+
// ======================================================
|
|
49
|
+
ipcMain.handle("download-file", async (_event, { url, filename }) => {
|
|
50
|
+
try {
|
|
51
|
+
|
|
52
|
+
const ext = path.extname(url) || "";
|
|
53
|
+
|
|
54
|
+
const { filePath } = await dialog.showSaveDialog({
|
|
55
|
+
title: "Save File",
|
|
56
|
+
defaultPath: filename || `download${ext}`,
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
if (!filePath) {
|
|
60
|
+
return {
|
|
61
|
+
status: "cancelled",
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const protocol = url.startsWith("https") ? https : http;
|
|
66
|
+
|
|
67
|
+
return new Promise((resolve, reject) => {
|
|
68
|
+
|
|
69
|
+
const file = fs.createWriteStream(filePath);
|
|
70
|
+
|
|
71
|
+
protocol.get(url, (response) => {
|
|
72
|
+
|
|
73
|
+
response.pipe(file);
|
|
74
|
+
|
|
75
|
+
file.on("finish", () => {
|
|
76
|
+
|
|
77
|
+
file.close();
|
|
78
|
+
|
|
79
|
+
resolve({
|
|
80
|
+
status: "success",
|
|
81
|
+
filePath: filePath,
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
}).on("error", (err) => {
|
|
87
|
+
|
|
88
|
+
console.error("download-file error:", err);
|
|
89
|
+
|
|
90
|
+
reject({
|
|
91
|
+
status: "error",
|
|
92
|
+
message: err.message,
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
} catch (err) {
|
|
100
|
+
|
|
101
|
+
console.error("download-file error:", err);
|
|
102
|
+
|
|
27
103
|
return {
|
|
28
104
|
status: "error",
|
|
29
105
|
message: err.message,
|
|
30
106
|
};
|
|
107
|
+
|
|
31
108
|
}
|
|
32
109
|
});
|
|
110
|
+
|
|
33
111
|
}
|
|
@@ -23,7 +23,7 @@ export function createMainWindow(__dirname) {
|
|
|
23
23
|
nodeIntegration: false,
|
|
24
24
|
contextIsolation: true,
|
|
25
25
|
sandbox: false,
|
|
26
|
-
webSecurity:
|
|
26
|
+
webSecurity: false,
|
|
27
27
|
disableBlinkFeatures: "AutoLoadIconsForPage",
|
|
28
28
|
nativeWindowOpen: true,
|
|
29
29
|
spellcheck: true,
|
package/src/preload.mjs
CHANGED
|
@@ -46,6 +46,15 @@ contextBridge.exposeInMainWorld("electronAPI", {
|
|
|
46
46
|
*/
|
|
47
47
|
openURL: (url) => ipcRenderer.invoke('react-native-open-url', url),
|
|
48
48
|
|
|
49
|
+
/**
|
|
50
|
+
* UNIVERSAL FILE DOWNLOAD
|
|
51
|
+
* Supports csv, pdf, excel, images, zip, etc.
|
|
52
|
+
* @param {string} url - File URL
|
|
53
|
+
* @param {string} filename - Suggested filename
|
|
54
|
+
*/
|
|
55
|
+
downloadFile: (url, filename) =>
|
|
56
|
+
ipcRenderer.invoke("download-file", { url, filename }),
|
|
57
|
+
|
|
49
58
|
// ======================================================
|
|
50
59
|
// CLIPBOARD OPERATIONS
|
|
51
60
|
// ======================================================
|