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'
|
2022-01-08 02:44:09 -05:00
|
|
|
import { IWindowList } from '#/types/enum'
|
2020-04-10 11:28:46 -04:00
|
|
|
import uploader from '.'
|
2022-01-04 10:40:28 -05:00
|
|
|
import pasteTemplate from '~/main/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'
|
2022-01-11 08:50:29 -05:00
|
|
|
import { T } from '#/i18n/index'
|
2022-02-19 23:35:18 -05:00
|
|
|
// import dayjs from 'dayjs'
|
|
|
|
|
|
|
|
const handleClipboardUploading = async (): Promise<false | ImgInfo[]> => {
|
|
|
|
const useBuiltinClipboard = !!db.get('settings.useBuiltinClipboard')
|
2020-01-10 09:34:08 -05:00
|
|
|
const win = windowManager.getAvailableWindow()
|
2022-02-19 23:35:18 -05:00
|
|
|
if (useBuiltinClipboard) {
|
|
|
|
return await uploader.setWebContents(win!.webContents).uploadWithBuildInClipboard()
|
|
|
|
}
|
|
|
|
return await uploader.setWebContents(win!.webContents).upload()
|
|
|
|
}
|
|
|
|
|
|
|
|
export const uploadClipboardFiles = async (): Promise<string> => {
|
|
|
|
const img = await handleClipboardUploading()
|
2020-01-10 09:34:08 -05:00
|
|
|
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({
|
2022-01-11 08:50:29 -05:00
|
|
|
title: T('UPLOAD_SUCCEED'),
|
2020-01-10 09:34:08 -05:00
|
|
|
body: img[0].imgUrl!,
|
|
|
|
icon: img[0].imgUrl
|
|
|
|
})
|
2022-01-16 07:22:23 -05:00
|
|
|
setTimeout(() => {
|
|
|
|
notification.show()
|
|
|
|
}, 100)
|
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({
|
2022-01-11 08:50:29 -05:00
|
|
|
title: T('UPLOAD_FAILED'),
|
|
|
|
body: T('TIPS_UPLOAD_NOT_PICTURES')
|
2020-01-10 09:34:08 -05:00
|
|
|
})
|
|
|
|
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({
|
2022-01-11 08:50:29 -05:00
|
|
|
title: T('UPLOAD_SUCCEED'),
|
2020-01-10 09:34:08 -05:00
|
|
|
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 []
|
|
|
|
}
|
|
|
|
}
|