diff --git a/src/main/apis/app/uploader/apis.ts b/src/main/apis/app/uploader/apis.ts index e3d5350f..85d973c6 100644 --- a/src/main/apis/app/uploader/apis.ts +++ b/src/main/apis/app/uploader/apis.ts @@ -10,6 +10,9 @@ import db, { GalleryDB } from '~/main/apis/core/datastore' import { handleCopyUrl } from '~/main/utils/common' import { handleUrlEncode } from '#/utils/common' import { T } from '~/main/i18n/index' +import ALLApi from '@/apis/allApi' +import picgo from '@core/picgo' +import GuiApi from '../../gui' const handleClipboardUploading = async (): Promise => { const useBuiltinClipboard = !!db.get('settings.useBuiltinClipboard') @@ -20,7 +23,7 @@ const handleClipboardUploading = async (): Promise => { return await uploader.setWebContents(win!.webContents).upload() } -export const uploadClipboardFiles = async (): Promise => { +export const uploadClipboardFiles = async (): Promise => { const img = await handleClipboardUploading() console.log(img) if (img !== false) { @@ -43,21 +46,30 @@ export const uploadClipboardFiles = async (): Promise => { if (windowManager.has(IWindowList.SETTING_WINDOW)) { windowManager.get(IWindowList.SETTING_WINDOW)!.webContents?.send('updateGallery') } - return handleUrlEncode(img[0].imgUrl as string) + return { + url: handleUrlEncode(img[0].imgUrl as string), + fullResult: img[0] + } } else { const notification = new Notification({ title: T('UPLOAD_FAILED'), body: T('TIPS_UPLOAD_NOT_PICTURES') }) notification.show() - return '' + return { + url: '', + fullResult: {} + } } } else { - return '' + return { + url: '', + fullResult: {} + } } } -export const uploadChoosedFiles = async (webContents: WebContents, files: IFileWithPath[]): Promise => { +export const uploadChoosedFiles = async (webContents: WebContents, files: IFileWithPath[]): Promise => { const input = files.map(item => item.path) const imgs = await uploader.setWebContents(webContents).upload(input) const result = [] @@ -75,7 +87,10 @@ export const uploadChoosedFiles = async (webContents: WebContents, files: IFileW notification.show() }, i * 100) await GalleryDB.getInstance().insert(imgs[i]) - result.push(handleUrlEncode(imgs[i].imgUrl!)) + result.push({ + url: handleUrlEncode(imgs[i].imgUrl!), + fullResult: imgs[i] + }) } handleCopyUrl(pasteText.join('\n')) // trayWindow just be created in mac/windows, not in linux @@ -88,3 +103,47 @@ export const uploadChoosedFiles = async (webContents: WebContents, files: IFileW return [] } } + +export const deleteChoosedFiles = async (list: ImgInfo[]): Promise => { + 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 +} diff --git a/src/main/events/busEventList.ts b/src/main/events/busEventList.ts index 5eccd631..cb4585b0 100644 --- a/src/main/events/busEventList.ts +++ b/src/main/events/busEventList.ts @@ -34,13 +34,15 @@ function initEventCenter () { } async function busCallUploadClipboardFiles () { - const imgUrl = await uploadClipboardFiles() + const result = await uploadClipboardFiles() + const imgUrl = result.url bus.emit(UPLOAD_WITH_CLIPBOARD_FILES_RESPONSE, imgUrl) } async function busCallUploadFiles (pathList: IFileWithPath[]) { const win = windowManager.getAvailableWindow() - const urls = await uploadChoosedFiles(win.webContents, pathList) + const result = await uploadChoosedFiles(win.webContents, pathList) + const urls = result.map((item: any) => item.url) bus.emit(UPLOAD_WITH_FILES_RESPONSE, urls) } diff --git a/src/main/server/routerManager.ts b/src/main/server/routerManager.ts index 3504fa35..cea8aa23 100644 --- a/src/main/server/routerManager.ts +++ b/src/main/server/routerManager.ts @@ -4,13 +4,15 @@ import { } from './utils' import logger from '@core/picgo/logger' import windowManager from 'apis/app/window/windowManager' -import { uploadChoosedFiles, uploadClipboardFiles } from 'apis/app/uploader/apis' +import { uploadChoosedFiles, uploadClipboardFiles, deleteChoosedFiles } from 'apis/app/uploader/apis' import path from 'path' import { dbPathDir } from 'apis/core/datastore/dbChecker' const STORE_PATH = dbPathDir() const LOG_PATH = path.join(STORE_PATH, 'piclist.log') +// import AllAPI from '../../renderer/apis/allApi' const errorMessage = `upload error. see ${LOG_PATH} for more detail.` +const deleteErrorMessage = `delete error. see ${LOG_PATH} for more detail.` router.post('/upload', async ({ response, @@ -23,14 +25,17 @@ router.post('/upload', async ({ if (list.length === 0) { // upload with clipboard logger.info('[PicList Server] upload clipboard file') - const res = await uploadClipboardFiles() + const result = await uploadClipboardFiles() + const res = result.url + const fullResult = result.fullResult logger.info('[PicList Server] upload result:', res) if (res) { handleResponse({ response, body: { success: true, - result: [res] + result: [res], + fullResult: [fullResult] } }) } else { @@ -51,14 +56,21 @@ router.post('/upload', async ({ } }) const win = windowManager.getAvailableWindow() - const res = await uploadChoosedFiles(win.webContents, pathList) + const result = await uploadChoosedFiles(win.webContents, pathList) + const res = result.map(item => { + return item.url + }) + const fullResult = result.map((item: any) => { + return item.fullResult + }) logger.info('[PicList Server] upload result', res.join(' ; ')) if (res.length) { handleResponse({ response, body: { success: true, - result: res + result: res, + fullResult } }) } else { @@ -83,6 +95,56 @@ router.post('/upload', async ({ } }) +router.post('/delete', async ({ + response, + list = [] +} : { + response: IHttpResponse, + list?: IStringKeyMap[] +}): Promise => { + if (list.length === 0) { + handleResponse({ + response, + body: { + success: false, + message: 'no file to delete' + } + }) + return + } + try { + const result = await deleteChoosedFiles(list) + const successCount = result.filter(item => item).length + const failCount = result.filter(item => !item).length + if (successCount) { + handleResponse({ + response, + body: { + success: true, + message: `delete success: ${successCount}, fail: ${failCount}` + } + }) + } else { + handleResponse({ + response, + body: { + success: false, + message: deleteErrorMessage + } + }) + } + } catch (err: any) { + logger.error(err) + handleResponse({ + response, + body: { + success: false, + message: deleteErrorMessage + } + }) + } +}) + router.post('/heartbeat', async ({ response } : { diff --git a/src/renderer/pages/Upload.vue b/src/renderer/pages/Upload.vue index 85720491..d16e0e7f 100644 --- a/src/renderer/pages/Upload.vue +++ b/src/renderer/pages/Upload.vue @@ -532,7 +532,6 @@ async function handlePicBedNameClick (picBedName: string, picBedConfigName: stri defaultConfigId: currentPicBedConfig.defaultId || '' } }) - console.log(configList, picBedConfigName) } onBeforeUnmount(() => {