2020-01-10 09:34:08 -05:00
|
|
|
import {
|
|
|
|
Notification,
|
|
|
|
WebContents
|
|
|
|
} from 'electron'
|
2020-04-10 11:28:46 -04:00
|
|
|
import windowManager from 'apis/app/window/windowManager'
|
|
|
|
import { IWindowList } from 'apis/app/window/constants'
|
|
|
|
import uploader from '.'
|
2020-01-10 09:34:08 -05:00
|
|
|
import pasteTemplate from '#/utils/pasteTemplate'
|
2021-07-26 23:58:24 -04:00
|
|
|
import db, { GalleryDB } from '~/main/apis/core/datastore'
|
2020-03-19 09:03:21 -04:00
|
|
|
import { handleCopyUrl } from '~/main/utils/common'
|
2020-06-29 21:53:35 -04:00
|
|
|
import { handleUrlEncode } from '#/utils/common'
|
2020-01-10 09:34:08 -05:00
|
|
|
export const uploadClipboardFiles = async (): Promise<string> => {
|
|
|
|
const win = windowManager.getAvailableWindow()
|
|
|
|
let img = await uploader.setWebContents(win!.webContents).upload()
|
|
|
|
if (img !== false) {
|
|
|
|
if (img.length > 0) {
|
2021-04-25 11:28:08 -04:00
|
|
|
const trayWindow = windowManager.get(IWindowList.TRAY_WINDOW)
|
2020-01-10 09:34:08 -05:00
|
|
|
const pasteStyle = db.get('settings.pasteStyle') || 'markdown'
|
2021-08-01 02:50:25 -04:00
|
|
|
handleCopyUrl(pasteTemplate(pasteStyle, img[0], db.get('settings.customLink')))
|
2020-01-10 09:34:08 -05:00
|
|
|
const notification = new Notification({
|
|
|
|
title: '上传成功',
|
|
|
|
body: img[0].imgUrl!,
|
|
|
|
icon: img[0].imgUrl
|
|
|
|
})
|
|
|
|
notification.show()
|
2021-07-26 23:58:24 -04:00
|
|
|
await GalleryDB.getInstance().insert(img[0])
|
2021-04-25 11:28:08 -04:00
|
|
|
// trayWindow just be created in mac/windows, not in linux
|
|
|
|
trayWindow?.webContents?.send('clipboardFiles', [])
|
|
|
|
trayWindow?.webContents?.send('uploadFiles', img)
|
2020-01-10 09:34:08 -05:00
|
|
|
if (windowManager.has(IWindowList.SETTING_WINDOW)) {
|
|
|
|
windowManager.get(IWindowList.SETTING_WINDOW)!.webContents.send('updateGallery')
|
|
|
|
}
|
2020-06-29 21:53:35 -04:00
|
|
|
return handleUrlEncode(img[0].imgUrl as string)
|
2020-01-10 09:34:08 -05:00
|
|
|
} else {
|
|
|
|
const notification = new Notification({
|
|
|
|
title: '上传不成功',
|
|
|
|
body: '你剪贴板最新的一条记录不是图片哦'
|
|
|
|
})
|
|
|
|
notification.show()
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const uploadChoosedFiles = async (webContents: WebContents, files: IFileWithPath[]): Promise<string[]> => {
|
|
|
|
const input = files.map(item => item.path)
|
|
|
|
const imgs = await uploader.setWebContents(webContents).upload(input)
|
|
|
|
const result = []
|
|
|
|
if (imgs !== false) {
|
|
|
|
const pasteStyle = db.get('settings.pasteStyle') || 'markdown'
|
2020-05-04 04:50:29 -04:00
|
|
|
const pasteText: string[] = []
|
2020-01-10 09:34:08 -05:00
|
|
|
for (let i = 0; i < imgs.length; i++) {
|
2021-08-01 02:50:25 -04:00
|
|
|
pasteText.push(pasteTemplate(pasteStyle, imgs[i], db.get('settings.customLink')))
|
2020-01-10 09:34:08 -05:00
|
|
|
const notification = new Notification({
|
|
|
|
title: '上传成功',
|
|
|
|
body: imgs[i].imgUrl!,
|
|
|
|
icon: files[i].path
|
|
|
|
})
|
|
|
|
setTimeout(() => {
|
|
|
|
notification.show()
|
|
|
|
}, i * 100)
|
2021-07-26 23:58:24 -04:00
|
|
|
await GalleryDB.getInstance().insert(imgs[i])
|
2020-06-29 21:53:35 -04:00
|
|
|
result.push(handleUrlEncode(imgs[i].imgUrl!))
|
2020-01-10 09:34:08 -05:00
|
|
|
}
|
2020-05-04 04:50:29 -04:00
|
|
|
handleCopyUrl(pasteText.join('\n'))
|
2021-04-25 11:28:08 -04:00
|
|
|
// trayWindow just be created in mac/windows, not in linux
|
|
|
|
windowManager.get(IWindowList.TRAY_WINDOW)?.webContents?.send('uploadFiles', imgs)
|
2020-01-10 09:34:08 -05:00
|
|
|
if (windowManager.has(IWindowList.SETTING_WINDOW)) {
|
|
|
|
windowManager.get(IWindowList.SETTING_WINDOW)!.webContents.send('updateGallery')
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
} else {
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
}
|