-
Notifications
You must be signed in to change notification settings - Fork 0
/
preload.js
154 lines (145 loc) · 4.73 KB
/
preload.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
const { clipboard, nativeImage } = require('electron')
const https = require('https')
const fs = require('fs')
const { sep } = require('path')
const wallpaper = require('wallpaper')
/**
* 检查文件是否存在
*
* 返回数组,第一个是标识;第二个是路径
*/
checkImageDone = (url, fileSize) => {
let tempDir = utools.getPath('temp')
const urlList = url.split('/')
const tempFileName = urlList[urlList.length - 1]
const dirPath = `${tempDir}${sep}wallhaven`
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath)
}
const filePath = `${dirPath}${sep}${tempFileName}`
if (fs.existsSync(filePath)) {
const stat = fs.statSync(filePath)
if (stat.isFile() && stat.size === fileSize) {
return [true, filePath]
}
}
return [false, filePath]
}
/**
* 下载图片到本地临时目录
*/
window.downloadImageFact = async function (url, fileSize) {
const imageDone = checkImageDone(url, fileSize)
const filePath = imageDone[1]
if (imageDone[0]) {
return filePath
}
const file = fs.createWriteStream(filePath)
return new Promise((resolve, reject) => {
https.get(url, function (res) {
if (res.statusCode !== 200) {
console.error('下载状态码错误:', res.statusCode)
utools.showNotification('下载出错', clickFeatureCode = null, silent = false)
reject(false)
}
res.pipe(file)
res.on('end', () => {
resolve(filePath)
})
}).on('error', e => {
console.error('下载出错', e)
utools.showNotification('下载出错', clickFeatureCode = null, silent = false)
reject(false)
})
})
}
/**
* 保存图片到用户图片目录
*
* 为啥不弹出对话框让用户选择? 因为utools把dialog类禁用了。
*/
window.saveImageFact = function (url, fileSize) {
const imageDone = checkImageDone(url, fileSize)
const filePath = imageDone[1]
if (!imageDone[0]) {
utools.showNotification('图片尚未下载完成,稍后再试', clickFeatureCode = null, silent = false)
return false
}
const pathList = filePath.split(sep)
const tempFileName = pathList[pathList.length - 1]
const dirPath = `${utools.getPath('pictures')}${sep}wallhaven`
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath)
}
const des = `${dirPath}${sep}${tempFileName}`
fs.copyFile(filePath, des, err => {
if (err) {
utools.showNotification(`保存图片到:${des}失败,请检查目录是否存在和可写`, clickFeatureCode = null, silent = false)
return false
}
utools.showNotification(`图片已经保存到:${des}`, clickFeatureCode = null, silent = false)
})
}
/**
* 把图片复制到剪切板
*/
window.copyImageFact = function (url, fileSize) {
const imageDone = checkImageDone(url, fileSize)
const filePath = imageDone[1]
if (!imageDone[0]) {
utools.showNotification('图片尚未下载完成,稍后再试', clickFeatureCode = null, silent = false)
return false
}
return clipboard.writeImage(nativeImage.createFromPath(filePath))
}
/**
* 把图片设置为壁纸
*/
window.setWallPaperFact = function (url, fileSize) {
const imageDone = checkImageDone(url, fileSize)
const filePath = imageDone[1]
if (!imageDone[0]) {
utools.showNotification('图片尚未下载完成,稍后再试', clickFeatureCode = null, silent = false)
return false
}
wallpaper.set(filePath)
}
/**
* 把图片设置为锁屏
*/
window.setLockScreenFact = function(url, fileSize) {
utools.showNotification('暂不支持设置锁屏', clickFeatureCode = null, silent = false)
}
/**
* 把图片设置为桌面和锁屏
*/
window.setBothFact = function(url, fileSize) {
utools.showNotification('暂不支持同时设置锁屏', clickFeatureCode = null, silent = false)
}
/**
* 设置定时更换壁纸任务
*/
window.setAutoChangeFact = function(timing, params, resMeta) {
let timingConfig = utools.db.get('timing_config')
const dbObj = {
_id: 'timing_config',
data: {
timing,
params,
resMeta
}
}
if (!timingConfig) {
timingConfig = utools.db.put(dbObj)
return timingConfig
}
dbObj._rev = timingConfig._rev
timingConfig = utools.db.put(dbObj)
return timingConfig
}
/**
* 获取定时更换壁纸任务
*/
window.getAutoChangeFact = function() {
return utools.db.get('timing_config')
}