PicList/src/main/apis/app/uploader/apis.ts

171 lines
6.1 KiB
TypeScript
Raw Normal View History

// External dependencies
import fs from 'fs-extra'
import { cloneDeep } from 'lodash'
// Electron modules
import {
Notification,
WebContents
} from 'electron'
// Custom utilities and modules
import windowManager from 'apis/app/window/windowManager'
import pasteTemplate from '~/main/utils/pasteTemplate'
2021-07-26 23:58:24 -04:00
import db, { GalleryDB } from '~/main/apis/core/datastore'
import { handleCopyUrl, handleUrlEncodeWithSetting } from '~/main/utils/common'
2022-08-20 04:44:55 -04:00
import { T } from '~/main/i18n/index'
import ALLApi from '@/apis/allApi'
import picgo from '@core/picgo'
import GuiApi from '../../gui'
import uploader from '.'
import { IWindowList } from '#/types/enum'
const handleClipboardUploading = async (): Promise<false | ImgInfo[]> => {
const useBuiltinClipboard = db.get('settings.useBuiltinClipboard') === undefined ? true : !!db.get('settings.useBuiltinClipboard')
const win = windowManager.getAvailableWindow()
if (useBuiltinClipboard) {
return await uploader.setWebContents(win!.webContents).uploadWithBuildInClipboard()
}
return await uploader.setWebContents(win!.webContents).upload()
}
export const uploadClipboardFiles = async (): Promise<IStringKeyMap> => {
const img = await handleClipboardUploading()
console.log(img)
if (img !== false) {
if (img.length > 0) {
const trayWindow = windowManager.get(IWindowList.TRAY_WINDOW)
const pasteStyle = db.get('settings.pasteStyle') || 'markdown'
handleCopyUrl(await (pasteTemplate(pasteStyle, img[0], db.get('settings.customLink'))))
const isShowResultNotification = db.get('settings.uploadResultNotification') === undefined ? true : !!db.get('settings.uploadResultNotification')
if (isShowResultNotification) {
const notification = new Notification({
title: T('UPLOAD_SUCCEED'),
body: img[0].imgUrl!
// icon: img[0].imgUrl
})
setTimeout(() => {
notification.show()
}, 100)
}
2021-07-26 23:58:24 -04:00
await GalleryDB.getInstance().insert(img[0])
// trayWindow just be created in mac/windows, not in linux
trayWindow?.webContents?.send('clipboardFiles', [])
trayWindow?.webContents?.send('uploadFiles', img)
if (windowManager.has(IWindowList.SETTING_WINDOW)) {
windowManager.get(IWindowList.SETTING_WINDOW)!.webContents?.send('updateGallery')
}
return {
url: handleUrlEncodeWithSetting(img[0].imgUrl as string),
fullResult: img[0]
}
} else {
const notification = new Notification({
2022-01-11 08:50:29 -05:00
title: T('UPLOAD_FAILED'),
body: T('TIPS_UPLOAD_NOT_PICTURES')
})
notification.show()
return {
url: '',
fullResult: {}
}
}
} else {
return {
url: '',
fullResult: {}
}
}
}
export const uploadChoosedFiles = async (webContents: WebContents, files: IFileWithPath[]): Promise<IStringKeyMap[]> => {
const input = files.map(item => item.path)
const rawInput = cloneDeep(input)
const imgs = await uploader.setWebContents(webContents).upload(input)
const result = []
if (imgs !== false) {
const pasteStyle = db.get('settings.pasteStyle') || 'markdown'
const deleteLocalFile = db.get('settings.deleteLocalFile') || false
const pasteText: string[] = []
for (let i = 0; i < imgs.length; i++) {
if (deleteLocalFile) {
fs.remove(rawInput[i]).then(() => {
picgo.log.info(`delete local file: ${rawInput[i]}`)
}).catch((err: Error) => {
picgo.log.error(err)
})
}
pasteText.push(await (pasteTemplate(pasteStyle, imgs[i], db.get('settings.customLink'))))
const isShowResultNotification = db.get('settings.uploadResultNotification') === undefined ? true : !!db.get('settings.uploadResultNotification')
if (isShowResultNotification) {
const notification = new Notification({
title: T('UPLOAD_SUCCEED'),
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])
result.push({
url: handleUrlEncodeWithSetting(imgs[i].imgUrl!),
fullResult: imgs[i]
})
}
handleCopyUrl(pasteText.join('\n'))
// trayWindow just be created in mac/windows, not in linux
windowManager.get(IWindowList.TRAY_WINDOW)?.webContents?.send('uploadFiles', imgs)
if (windowManager.has(IWindowList.SETTING_WINDOW)) {
windowManager.get(IWindowList.SETTING_WINDOW)!.webContents?.send('updateGallery')
}
return result
} else {
return []
}
}
export const deleteChoosedFiles = async (list: ImgInfo[]): Promise<boolean[]> => {
const result = []
const picBedsCanbeDeleted = ['smms', 'github', 'imgur', 'tcyun', 'aliyun', 'qiniu', 'upyun', 'aws-s3', 'webdavplist']
for (const item of list) {
if (item.id) {
try {
const dbStore = GalleryDB.getInstance()
const file = await dbStore.removeById(item.id)
if (await picgo.getConfig('settings.deleteCloudFile')) {
if (item.type !== undefined && picBedsCanbeDeleted.includes(item.type)) {
setTimeout(() => {
ALLApi.delete(item).then((value: boolean) => {
if (value) {
const notification = new Notification({
title: T('MANAGE_BUCKET_BATCH_DELETE_ERROR_MSG_MSG2'),
body: T('GALLERY_SYNC_DELETE_NOTICE_SUCCEED')
})
notification.show()
} else {
const notification = new Notification({
title: T('MANAGE_BUCKET_BATCH_DELETE_ERROR_MSG_MSG2'),
body: T('GALLERY_SYNC_DELETE_NOTICE_FAILED')
})
notification.show()
}
})
}, 0)
}
}
setTimeout(() => {
picgo.emit('remove', [file], GuiApi.getInstance())
}, 500)
result.push(true)
} catch (e) {
result.push(false)
}
}
}
if (windowManager.has(IWindowList.SETTING_WINDOW)) {
windowManager.get(IWindowList.SETTING_WINDOW)!.webContents?.send('updateGallery')
}
return result
}