mirror of
https://github.com/Kuingsmile/PicList.git
synced 2026-06-01 11:19:44 +08:00
Partly finished: weibo picbed
This commit is contained in:
@@ -2,7 +2,8 @@
|
||||
|
||||
import { weiboUpload } from './utils/weiboUpload.js'
|
||||
import { app, BrowserWindow, Tray, Menu, Notification, clipboard, ipcMain } from 'electron'
|
||||
import db from '../datastore/index'
|
||||
import db from '../datastore'
|
||||
import pasteTemplate from './utils/pasteTemplate'
|
||||
/**
|
||||
* Set `__static` path to static files in production
|
||||
* https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-static-assets.html
|
||||
@@ -29,7 +30,7 @@ function createTray () {
|
||||
label: 'Quit'
|
||||
},
|
||||
{
|
||||
label: 'Open setting Window',
|
||||
label: '打开详细窗口',
|
||||
click () {
|
||||
if (settingWindow === null) {
|
||||
createSettingWindow()
|
||||
@@ -38,6 +39,26 @@ function createTray () {
|
||||
settingWindow.focus()
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
label: '选择默认图床',
|
||||
type: 'submenu',
|
||||
submenu: [
|
||||
{
|
||||
label: '微博图床',
|
||||
type: 'radio',
|
||||
checked: db.read().get('picBed.current').value() === 'weibo'
|
||||
},
|
||||
{
|
||||
label: '七牛图床',
|
||||
type: 'radio',
|
||||
checked: db.read().get('picBed.current').value() === 'qiniu',
|
||||
click () {
|
||||
db.read().set('picBed.current', 'qiniu')
|
||||
.write()
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
])
|
||||
tray.on('right-click', () => {
|
||||
@@ -69,9 +90,10 @@ function createTray () {
|
||||
})
|
||||
|
||||
tray.on('drop-files', async (event, files) => {
|
||||
const imgs = await weiboUpload(files, 'imgFromPath')
|
||||
const pasteStyle = db.read().get('picBed.pasteStyle') || 'markdown'
|
||||
const imgs = await weiboUpload(files, 'imgFromPath', window.webContents)
|
||||
for (let i in imgs) {
|
||||
clipboard.writeText(imgs[i].imgUrl)
|
||||
clipboard.writeText(pasteTemplate(pasteStyle, imgs[i].imgUrl))
|
||||
const notification = new Notification({
|
||||
title: '上传成功',
|
||||
body: imgs[i].imgUrl,
|
||||
@@ -81,7 +103,6 @@ function createTray () {
|
||||
notification.show()
|
||||
}, i * 100)
|
||||
}
|
||||
console.log('drag-files')
|
||||
window.webContents.send('dragFiles', imgs)
|
||||
})
|
||||
toggleWindow()
|
||||
@@ -160,15 +181,6 @@ const toggleWindow = () => {
|
||||
}
|
||||
}
|
||||
|
||||
// const toggleSettingWindow = () => {
|
||||
// if (settingWindow.isVisible()) {
|
||||
// settingWindow.hide()
|
||||
// } else {
|
||||
// settingWindow.show()
|
||||
// settingWindow.focus()
|
||||
// }
|
||||
// }
|
||||
|
||||
const showWindow = () => {
|
||||
const position = getWindowPosition()
|
||||
window.setPosition(position.x, position.y, false)
|
||||
@@ -177,8 +189,9 @@ const showWindow = () => {
|
||||
}
|
||||
|
||||
ipcMain.on('uploadClipboardFiles', async (evt, file) => {
|
||||
const img = await weiboUpload(file, 'imgFromClipboard')
|
||||
clipboard.writeText(img[0].imgUrl)
|
||||
const img = await weiboUpload(file, 'imgFromClipboard', window.webContents)
|
||||
const pasteStyle = db.read().get('picBed.pasteStyle') || 'markdown'
|
||||
clipboard.writeText(pasteTemplate(pasteStyle, img[0].imgUrl))
|
||||
const notification = new Notification({
|
||||
title: '上传成功',
|
||||
body: img[0].imgUrl,
|
||||
@@ -187,8 +200,26 @@ ipcMain.on('uploadClipboardFiles', async (evt, file) => {
|
||||
notification.show()
|
||||
clipboard.clear()
|
||||
window.webContents.send('clipboardFiles', [])
|
||||
window.webContents.send('uploadClipboardFiles', img)
|
||||
console.log('clipboard-upload')
|
||||
window.webContents.send('uploadFiles', img)
|
||||
})
|
||||
|
||||
ipcMain.on('uploadChoosedFiles', async (evt, files) => {
|
||||
const imgs = await weiboUpload(files, 'imgFromUploader', settingWindow.webContents)
|
||||
const pasteStyle = db.read().get('picBed.pasteStyle') || 'markdown'
|
||||
let pasteText = ''
|
||||
for (let i in imgs) {
|
||||
pasteText += pasteTemplate(pasteStyle, imgs[i].imgUrl) + '\r\n'
|
||||
const notification = new Notification({
|
||||
title: '上传成功',
|
||||
body: imgs[i].imgUrl,
|
||||
icon: files[i].path
|
||||
})
|
||||
setTimeout(() => {
|
||||
notification.show()
|
||||
}, i * 100)
|
||||
}
|
||||
clipboard.writeText(pasteText)
|
||||
window.webContents.send('uploadFiles', imgs)
|
||||
})
|
||||
|
||||
app.on('ready', () => {
|
||||
|
||||
@@ -29,7 +29,26 @@ const imgFromClipboard = (file) => {
|
||||
return result
|
||||
}
|
||||
|
||||
const imgFromUploader = async (files) => {
|
||||
console.log(files)
|
||||
let results = []
|
||||
await Promise.all(files.map(async item => {
|
||||
let buffer = await fs.readFile(item.path)
|
||||
let base64Image = Buffer.from(buffer, 'binary').toString('base64')
|
||||
let fileName = item.name
|
||||
let imgSize = sizeOf(item.path)
|
||||
results.push({
|
||||
base64Image,
|
||||
fileName,
|
||||
width: imgSize.width,
|
||||
height: imgSize.height
|
||||
})
|
||||
}))
|
||||
return results
|
||||
}
|
||||
|
||||
export {
|
||||
imgFromPath,
|
||||
imgFromClipboard
|
||||
imgFromClipboard,
|
||||
imgFromUploader
|
||||
}
|
||||
|
||||
9
src/main/utils/pasteTemplate.js
Normal file
9
src/main/utils/pasteTemplate.js
Normal file
@@ -0,0 +1,9 @@
|
||||
export default (style, url) => {
|
||||
const tpl = {
|
||||
'markdown': ``,
|
||||
'HTML': `<img src="${url}"/>`,
|
||||
'URL': url,
|
||||
'UBB': `[IMG]${url}[/IMG]`
|
||||
}
|
||||
return tpl[style]
|
||||
}
|
||||
@@ -20,35 +20,39 @@ function postOptions (formData) {
|
||||
}
|
||||
}
|
||||
|
||||
const weiboUpload = async function (img, type) {
|
||||
const weiboUpload = async function (img, type, webContents) {
|
||||
try {
|
||||
webContents.send('uploadProgress', 0)
|
||||
const formData = {
|
||||
username: db.read().get('picBed.weibo.username').value(),
|
||||
password: db.read().get('picBed.weibo.password').value()
|
||||
}
|
||||
const quality = db.read().get('picBed.weibo.quality').value()
|
||||
const options = postOptions(formData)
|
||||
const res = await rp(options)
|
||||
webContents.send('uploadProgress', 30)
|
||||
if (res.body.retcode === 20000000) {
|
||||
for (let i in res.body.data.crossdomainlist) {
|
||||
await rp.get(res.body.data.crossdomainlist[i])
|
||||
}
|
||||
webContents.send('uploadProgress', 60)
|
||||
const imgList = await img2Base64[type](img)
|
||||
let resText = []
|
||||
for (let i in imgList) {
|
||||
let result = await rp.post(UPLOAD_URL, {
|
||||
formData: {
|
||||
b64_data: imgList[i].base64Image
|
||||
}
|
||||
})
|
||||
resText.push(result.replace(/<.*?\/>/, '').replace(/<(\w+).*?>.*?<\/\1>/, ''))
|
||||
}
|
||||
for (let i in imgList) {
|
||||
const resTextJson = JSON.parse(resText[i])
|
||||
imgList[i]['imgUrl'] = `https://ws1.sinaimg.cn/large/${resTextJson.data.pics.pic_1.pid}`
|
||||
result = result.replace(/<.*?\/>/, '').replace(/<(\w+).*?>.*?<\/\1>/, '')
|
||||
delete imgList[i].base64Image
|
||||
const resTextJson = JSON.parse(result)
|
||||
imgList[i]['imgUrl'] = `https://ws1.sinaimg.cn/${quality}/${resTextJson.data.pics.pic_1.pid}`
|
||||
imgList[i]['type'] = 'weibo'
|
||||
}
|
||||
webContents.send('uploadProgress', 100)
|
||||
return imgList
|
||||
} else {
|
||||
webContents.send('uploadProgress', -1)
|
||||
const notification = new Notification({
|
||||
title: '上传失败!',
|
||||
body: res.body.msg
|
||||
@@ -56,7 +60,12 @@ const weiboUpload = async function (img, type) {
|
||||
notification.show()
|
||||
}
|
||||
} catch (err) {
|
||||
console.log('This is error', err, err.name === 'RequestError')
|
||||
webContents.send('uploadProgress', -1)
|
||||
const notification = new Notification({
|
||||
title: '上传失败!',
|
||||
body: '服务端出错,请重试'
|
||||
})
|
||||
notification.show()
|
||||
throw new Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user