-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
230 lines (191 loc) · 6.16 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// main.js
const {
app,
BrowserWindow,
screen,
ipcMain,
dialog,
session,
} = require("electron");
const fs = require("fs");
const path = require("path");
const { LocalFileData } = require("get-file-object-from-local-path");
const exec = require("child_process").exec;
const { default: axios } = require("axios");
let readinPath = undefined;
function createWindow() {
// 創建瀏覽器窗口
const mainWindow = new BrowserWindow({
width: 350,
height: 390,
icon: path.join(__dirname, "/public/icon.ico"),
// frame: false,
skipTaskbar: false,
transparent: false,
backgroundColor: "#1C1917",
resizable: true,
maximizable: true,
webPreferences: {
preload: path.join(__dirname, "preload.js"),
experimentalFeatures: true,
nodeIntegration: true,
},
titleBarStyle: "hidden",
titleBarOverlay: {
color: "rgba(28,25,23,1)",
// color: "transparent",
symbolColor: "white",
},
});
const primaryDisplay = screen.getPrimaryDisplay();
const { width, height } = primaryDisplay.workAreaSize;
// Create a window that fills the screen's available work area.
let wWidth = Math.round(width * 0.6);
let wHieght = Math.round(height * 0.8);
mainWindow.setSize(wWidth, wHieght, true);
mainWindow.setPosition(
Math.round((width - wWidth) / 2),
Math.round((height - wHieght) / 2),
true
);
let appPath = app.getAppPath();
ipcMain.on("selectDir", async (event, arg) => {
try {
const paths = await dialog.showOpenDialog({
title: "選擇相片來源資料夾",
properties: ["openDirectory"],
});
if (!paths.canceled) {
readinPath = paths.filePaths[0];
}
event.returnValue = !paths.canceled;
} catch (e) {
console.log(e);
event.returnValue = false;
}
});
ipcMain.handle("preloadDirectory", async (event) => {
try {
// const d = "C:\\Users\\narut\\Desktop\\test";
// console.log(appPath);
console.log(`readinPath = ${readinPath}`);
console.log(`appPath = ${appPath}`);
const d = readinPath ?? appPath;
const fileList = fs.readdirSync(d);
const files = [];
// console.log(fileList);
for (filePath of fileList)
if (filePath.split(".").pop() === "jpg")
files.push(new LocalFileData(d + "\\" + filePath));
if (files.length > 0) return [files, d];
else return [null, undefined, "no photo"];
} catch (e) {
console.log(e);
return [null, e];
}
});
ipcMain.on("save", (evt, files) => {
const dir = readinPath;
const renamePromises = files.map((f) => {
return new Promise((resolve) => {
fs.rename(`${dir}/${f.oldName}`, `${dir}/${f.newName}`, () => {
resolve();
});
});
});
Promise.all(renamePromises).then(() => {
mainWindow.close();
});
});
ipcMain.on("checkUpdate", (evt, arg) => {
axios
.get(
"https://api.github.com/repos/huibizhang/photolisting/releases/latest"
)
.then((r) => {
evt.reply("getUpdateInfo", {
currentVersion: app.getVersion(),
targetVersion: r.data.name,
publishedAt: r.data.published_at,
url: r.data.assets.find(
(asset) => asset.name.split(".").pop() === "exe"
).browser_download_url,
});
})
.catch((e) => {
evt.reply("getUpdateInfo", null);
});
});
let downloadItem = null;
ipcMain.on("updating", (evt, url) => {
if (downloadItem) if (downloadItem.getState() === "progressing") return;
mainWindow.webContents.downloadURL(url);
session.defaultSession.on("will-download", (event, item, webContents) => {
downloadItem = item;
try {
fs.rmSync(appPath + "/update.exe");
} catch (error) {
console.error(error);
}
item.setSavePath(appPath + "/update.exe");
item.on("updated", (event, state) => {
console.log(
"Downloading",
item.getReceivedBytes() / item.getTotalBytes()
);
evt.reply("updatingProgress", {
size: Number(item.getTotalBytes() / 1024 / 1024),
progress: item.getReceivedBytes() / item.getTotalBytes(),
done: false,
});
});
item.on("done", (event, state) => {
console.log(state);
const workerProcess = exec("update.exe", { cwd: appPath });
});
});
});
if (process.env.NODE_ENV === "dev") {
// mainWindow.loadURL("https://pccu-life-widget.vercel.app/");
mainWindow.loadURL("http://127.0.0.1:3000");
} else {
// 加載 index.html
mainWindow.loadFile("./dist/index.html"); // 此處跟electron官網路徑不同,需要註意
}
// 打開開發工具
// mainWindow.webContents.openDevTools();
}
app.on("ready", function () {
// console.log(" ==================== ready ====================");
if (process.argv.length > 1) {
const temp = process.argv[1];
// const temp = "C:\\Users\\narut\\Desktop\\test2";
if (fs.existsSync(temp) && fs.lstatSync(temp).isDirectory()) {
readinPath = temp;
}
}
});
// 這段程序將會在 Electron 結束初始化
// 和創建瀏覽器窗口的時候調用
// 部分 API 在 ready 事件觸發後才能使用。
app.whenReady().then(() => {
// if (process.argv.length > 1) {
// const temp = process.argv[1];
// if (fs.existsSync(temp) && fs.lstatSync(temp).isDirectory()) {
// readinPath = temp;
// }
// }
createWindow();
app.on("activate", function () {
// 通常在 macOS 上,當點擊 dock 中的應用程序圖標時,如果沒有其他
// 打開的窗口,那麼程序會重新創建一個窗口。
if (BrowserWindow.getAllWindows().length === 0) createWindow();
});
});
// 除瞭 macOS 外,當所有窗口都被關閉的時候退出程序。 因此,通常對程序和它們在
// 任務欄上的圖標來說,應當保持活躍狀態,直到用戶使用 Cmd + Q 退出。
app.on("window-all-closed", function () {
if (process.platform !== "darwin") app.quit();
});
// 在這個文件中,你可以包含應用程序剩餘的所有部分的代碼,
// 也可以拆分成幾個文件,然後用 require 導入。