2023-08-07 04:36:27 -04:00
|
|
|
// External dependencies
|
|
|
|
import fs from 'fs-extra'
|
|
|
|
import { cloneDeep } from 'lodash'
|
|
|
|
|
|
|
|
// Electron modules
|
2020-01-10 09:34:08 -05:00
|
|
|
import {
|
|
|
|
Notification,
|
|
|
|
WebContents
|
|
|
|
} from 'electron'
|
2023-08-07 04:36:27 -04:00
|
|
|
|
|
|
|
// Custom utilities and modules
|
2020-04-10 11:28:46 -04:00
|
|
|
import windowManager from 'apis/app/window/windowManager'
|
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'
|
2023-05-07 05:05:58 -04:00
|
|
|
import { handleCopyUrl, handleUrlEncodeWithSetting } from '~/main/utils/common'
|
2022-08-20 04:44:55 -04:00
|
|
|
import { T } from '~/main/i18n/index'
|
2023-03-28 02:44:48 -04:00
|
|
|
import ALLApi from '@/apis/allApi'
|
|
|
|
import picgo from '@core/picgo'
|
|
|
|
import GuiApi from '../../gui'
|
2023-08-07 04:36:27 -04:00
|
|
|
import uploader from '.'
|
|
|
|
import { IWindowList } from '#/types/enum'
|
2023-08-10 08:30:46 -04:00
|
|
|
import { picBedsCanbeDeleted } from '#/utils/static'
|
|
|
|
import path from 'path'
|
|
|
|
import SSHClient from '~/main/utils/sshClient'
|
|
|
|
import { ISftpPlistConfig } from 'piclist'
|
|
|
|
import { getRawData } from '~/renderer/utils/common'
|
2022-02-19 23:35:18 -05:00
|
|
|
|
|
|
|
const handleClipboardUploading = async (): Promise<false | ImgInfo[]> => {
|
2023-04-10 04:06:37 -04:00
|
|
|
const useBuiltinClipboard = db.get('settings.useBuiltinClipboard') === undefined ? true : !!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()
|
|
|
|
}
|
|
|
|
|
2023-03-28 02:44:48 -04:00
|
|
|
export const uploadClipboardFiles = async (): Promise<IStringKeyMap> => {
|
2022-02-19 23:35:18 -05:00
|
|
|
const img = await handleClipboardUploading()
|
2023-01-06 04:21:27 -05:00
|
|
|
console.log(img)
|
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'
|
2023-04-17 05:26:49 -04:00
|
|
|
handleCopyUrl(await (pasteTemplate(pasteStyle, img[0], db.get('settings.customLink'))))
|
2023-07-18 02:54:50 -04:00
|
|
|
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])
|
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)) {
|
2022-04-04 07:22:30 -04:00
|
|
|
windowManager.get(IWindowList.SETTING_WINDOW)!.webContents?.send('updateGallery')
|
2020-01-10 09:34:08 -05:00
|
|
|
}
|
2023-03-28 02:44:48 -04:00
|
|
|
return {
|
2023-05-07 05:05:58 -04:00
|
|
|
url: handleUrlEncodeWithSetting(img[0].imgUrl as string),
|
2023-03-28 02:44:48 -04:00
|
|
|
fullResult: img[0]
|
|
|
|
}
|
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()
|
2023-03-28 02:44:48 -04:00
|
|
|
return {
|
|
|
|
url: '',
|
|
|
|
fullResult: {}
|
|
|
|
}
|
2020-01-10 09:34:08 -05:00
|
|
|
}
|
|
|
|
} else {
|
2023-03-28 02:44:48 -04:00
|
|
|
return {
|
|
|
|
url: '',
|
|
|
|
fullResult: {}
|
|
|
|
}
|
2020-01-10 09:34:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-28 02:44:48 -04:00
|
|
|
export const uploadChoosedFiles = async (webContents: WebContents, files: IFileWithPath[]): Promise<IStringKeyMap[]> => {
|
2020-01-10 09:34:08 -05:00
|
|
|
const input = files.map(item => item.path)
|
2023-04-18 07:33:49 -04:00
|
|
|
const rawInput = cloneDeep(input)
|
2020-01-10 09:34:08 -05:00
|
|
|
const imgs = await uploader.setWebContents(webContents).upload(input)
|
|
|
|
const result = []
|
|
|
|
if (imgs !== false) {
|
|
|
|
const pasteStyle = db.get('settings.pasteStyle') || 'markdown'
|
2023-04-18 07:33:49 -04:00
|
|
|
const deleteLocalFile = db.get('settings.deleteLocalFile') || false
|
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++) {
|
2023-04-18 07:33:49 -04:00
|
|
|
if (deleteLocalFile) {
|
|
|
|
fs.remove(rawInput[i]).then(() => {
|
|
|
|
picgo.log.info(`delete local file: ${rawInput[i]}`)
|
|
|
|
}).catch((err: Error) => {
|
|
|
|
picgo.log.error(err)
|
|
|
|
})
|
|
|
|
}
|
2023-04-17 05:26:49 -04:00
|
|
|
pasteText.push(await (pasteTemplate(pasteStyle, imgs[i], db.get('settings.customLink'))))
|
2023-07-18 02:54:50 -04:00
|
|
|
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])
|
2023-03-28 02:44:48 -04:00
|
|
|
result.push({
|
2023-05-07 05:05:58 -04:00
|
|
|
url: handleUrlEncodeWithSetting(imgs[i].imgUrl!),
|
2023-03-28 02:44:48 -04:00
|
|
|
fullResult: imgs[i]
|
|
|
|
})
|
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)) {
|
2022-04-04 07:22:30 -04:00
|
|
|
windowManager.get(IWindowList.SETTING_WINDOW)!.webContents?.send('updateGallery')
|
2020-01-10 09:34:08 -05:00
|
|
|
}
|
|
|
|
return result
|
|
|
|
} else {
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
}
|
2023-03-28 02:44:48 -04:00
|
|
|
|
2023-08-10 08:30:46 -04:00
|
|
|
async function deleteWebdavFile (config: ISftpPlistConfig, fileName: string) {
|
|
|
|
try {
|
|
|
|
const client = SSHClient.instance
|
|
|
|
await client.connect(config)
|
|
|
|
const uploadPath = `/${(config.uploadPath || '')}/`.replace(/\/+/g, '/')
|
|
|
|
const remote = path.join(uploadPath, fileName)
|
|
|
|
const deleteResult = await client.deleteFile(remote)
|
|
|
|
client.close()
|
|
|
|
return deleteResult
|
|
|
|
} catch (err: any) {
|
|
|
|
console.error(err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-28 02:44:48 -04:00
|
|
|
export const deleteChoosedFiles = async (list: ImgInfo[]): Promise<boolean[]> => {
|
|
|
|
const result = []
|
|
|
|
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)) {
|
2023-08-10 08:30:46 -04:00
|
|
|
if (item.type === 'webdavplist') {
|
|
|
|
const { fileName, config } = item
|
|
|
|
setTimeout(() => {
|
|
|
|
deleteWebdavFile(getRawData(config), fileName || '').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)
|
|
|
|
} else {
|
|
|
|
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)
|
|
|
|
}
|
2023-03-28 02:44:48 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|