-
Notifications
You must be signed in to change notification settings - Fork 23
/
imageManager.js
70 lines (65 loc) · 1.62 KB
/
imageManager.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
import axios from 'axios'
import fs from 'fs'
import path from 'path'
import {
promisify
} from 'util'
import {
IMAGE_SAVE_DIR
} from '@/constants'
import copyGif from '@/common/copyGif'
import isGif from 'is-gif'
import {
clipboard,
nativeImage
} from 'electron'
class ImageManger {
constructor (url) {
this.url = url
this.imageData = axios.get(url, { responseType: 'blob' })
}
async copy () {
let filename = this.url.split('/').pop()
// 如果没有后缀,给一个默认的 .jpg
if (!filename.includes('.')) {
filename += '.jpg'
}
try {
const { data } = await this.imageData
const reader = new FileReader()
reader.readAsArrayBuffer(data)
// 先将文件下载到本地
const buffer = await new Promise(resolve => {
reader.onloadend = () => {
const buffer = Buffer.from(reader.result)
const filePath = path.resolve(IMAGE_SAVE_DIR, filename)
promisify(fs.writeFile)(filePath, buffer)
resolve(buffer)
}
})
// 拷贝到剪切板
if (isGif(buffer)) {
copyGif(filename)
} else {
clipboard.writeImage(nativeImage.createFromBuffer(buffer))
}
} catch (e) {
console.error(e)
// todo: error handler
}
}
async toBase64 () {
try {
const { data } = await this.imageData
const reader = new FileReader()
reader.readAsDataURL(data)
return new Promise(resolve => {
reader.onloadend = () => resolve(reader.result)
})
} catch (e) {
console.error(e)
// todo: error handler
}
}
}
export default ImageManger