-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.js
303 lines (263 loc) · 7.06 KB
/
util.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
const {remote} = require("electron");
const {dialog} = remote;
const fs = require("fs");
const path = require("path");
const { dir } = require("console");
const { getPriority } = require("os");
//
// Constant configs
//
const META_FILENAME = "comicviewermeta.json";
const THUMB_FILENAME = "comicviewerthumb.thumb";
const PREVIEW_FILENAME = "comicviewerpreviewer.thumb";
const READ_DIR = "(0";
const FOLDER_THUMB_PATH = "folder.png";
const CONFIG_PATH = "config.json";
const DEBUG_CONFIG_PATH = "debug_config.json";
//
// HTML concerned
//
function $(id) {
return document.getElementById(id);
}
//
// Image concerned
//
function createImg(img_path, max_height, max_width, callback) {
var canvas = document.createElement("canvas");
canvas.width = max_width;
canvas.height = max_height;
var ctx = canvas.getContext("2d");
ctx.fillStyle = "#fff";
ctx.fillRect(0, 0, canvas.width, canvas.height);
var img = new Image(max_width, max_height);
img.src = urlEscape(img_path);
img.onload = ()=>{
var nat_height = img.naturalHeight;
var nat_width = img.naturalWidth;
var tmp = adjustWidthHeight(nat_width, nat_height, max_width, max_height);
var dwidth = tmp[0];
var dheight = tmp[1];
canvas.width = dwidth;
canvas.height = dheight;
ctx.drawImage(img, 0, 0, dwidth, dheight);
if(callback)
callback(canvas, img);
};
return canvas;
}
function adjustWidthHeight(nat_width, nat_height, max_width, max_height) {
var max_ratio = max_height / max_width;
var width, height;
if(nat_height <= max_height && nat_width <= max_width)
return [nat_width, nat_height];
var nat_ratio = nat_height / nat_width;
if(nat_ratio > max_ratio) {
height = max_height;
width = max_height / nat_ratio;
}
else {
width = max_width;
height = max_width * nat_ratio;
}
return [width, height];
}
//
// Path concerned
//
function urlEscape(url) {
url = url.replace("%", "%25");
url = url.replace("+", "%2B");
url = url.replace(" ", "%20");
url = url.replace("#", "%23");
url = url.replace("&", "%26");
url = url.replace("=", "%3D");
return url;
}
function isDirectory(filepath) {
var stat = fs.statSync(filepath);
return stat.isDirectory();
}
function isPicture(filepath) {
const valid_ext_list = [
".jpg", ".png", ".gif",
];
var file_ext = path.extname(filepath);
for(var valid_ext of valid_ext_list) {
if(valid_ext == file_ext)
return true;
}
return false;
}
function getDirsAndPicsSync(dirpath) {
var alldirents = fs.readdirSync(dirpath, {withFileTypes:true});
var dirs = [];
var pics = [];
for(var dirent of alldirents) {
var filepath = path.join(dirpath, dirent.name);
if(dirent.isDirectory())
dirs.push(filepath);
else if(isPicture(dirent.name))
pics.push(filepath);
}
return [dirs, pics];
}
function getFirstPicSync(dirpath) {
var alldirents = fs.readdirSync(dirpath, {withFileTypes:true});
for(var dirent of alldirents) {
if(isPicture(dirent.name)) {
var filepath = path.join(dirpath, dirent.name);
return filepath;
}
}
}
function sortPicList(picList) {
picList.sort((a, b) => {
if(a.length != b.length)
return a.length - b.length;
for(var i=0; i<a.length; i++) {
if(a[i] != b[i])
return a[i] - b[i];
}
return 0;
})
}
function deleteDir(dirpath) {
fs.rmdirSync(dirpath, {recursive: true});
}
//
// Metadata concerned
//
/*
metadata = {
bookmark: string. path of the bookmark picture
read: boolean. flag for has read up
}
*/
function getMetapath(dirpath) {
return path.join(dirpath, META_FILENAME);
}
function loadMeta(dirpath) {
var metapath = getMetapath(dirpath);
var metadata = {};
if(fs.existsSync(metapath)) {
// Load if exists
var filedata = fs.readFileSync(metapath);
metadata = JSON.parse(filedata.toString());
}
return metadata;
}
function saveMeta(dirpath, metadata) {
var metapath = getMetapath(dirpath);
var metastr = JSON.stringify(metadata);
fs.writeFileSync(metapath, metastr);
}
/*
thumb
just a compressed jpg
*/
function getThumbPath(dirpath) {
return path.join(dirpath, THUMB_FILENAME);
}
function checkThumbExists(dirpath) {
var thumbpath = getThumbPath(dirpath);
return fs.existsSync(thumbpath);
}
function saveCanvas(canvas, filepath, quality) {
var data = canvas.toDataURL("image/jpeg", quality);
data = data.replace(/^data:image\/\w+;base64,/, "");
var buf = new Buffer(data, "base64");
fs.writeFile(filepath, buf, (err) => {
if(err)
throw err;
});
}
function saveThumb(dirpath, canvas) {
saveCanvas(canvas, getThumbPath(dirpath), 1.0);
}
/*
preview
just a compressed jpg
*/
function getPreviewPath(dirpath) {
return path.join(dirpath, PREVIEW_FILENAME);
}
function checkPreviewExists(dirpath) {
var previewpath = getPreviewPath(dirpath);
return fs.existsSync(previewpath);
}
function savePreview(dirpath, canvas) {
saveCanvas(canvas, getPreviewPath(dirpath), 1.0);
}
//
// Infomation concerned
//
function showInfoAndUpdateInfoText(container_obj, text_obj, info_text) {
text_obj.innerText = info_text;
var infoContainer = container_obj;
infoContainer.classList.add("infoShow");
infoContainer.addEventListener("transitionend", (ev) => {
var nowOpacity = window.getComputedStyle(infoContainer).opacity;
if(nowOpacity == 1) {
if(infoContainer.classList.contains("infoShow"))
infoContainer.classList.remove("infoShow");
}
});
}
function askForCheck(check_str, title) {
var msgBoxOptions = {
type: "question",
buttons: [
"Yes", "No"
],
message: check_str,
cancelId: 1
}
if(title)
msgBoxOptions.title = title;
var checkReplay = dialog.showMessageBoxSync(msgBoxOptions);
if(checkReplay == 0)
return true;
else if(checkReplay == 1)
return false;
else
throw "Error, invalid checkReply: " + checkReplay.toString();
}
//
// Config concerned
//
/*
config: {
root: last set root dir path
}
*/
function getConfigPath() {
if(remote.getGlobal("debug_flag"))
return DEBUG_CONFIG_PATH;
else
return CONFIG_PATH;
}
function loadConfig() {
var configdata = {};
if(fs.existsSync(getConfigPath())) {
// Load if exists
var filedata = fs.readFileSync(getConfigPath());
configdata = JSON.parse(filedata.toString());
}
return configdata;
}
function saveConfig(configdata) {
var configstr = JSON.stringify(configdata);
fs.writeFileSync(getConfigPath(), configstr);
}
function updateConfigItem(key, value) {
var config = loadConfig();
if(config[key] != value) {
config[key] = value;
saveConfig(config);
}
}
function getConfigItem(key) {
var config = loadConfig();
return config[key];
}