mirror of
https://github.com/Kuingsmile/PicList.git
synced 2025-01-23 14:48:13 -05:00
✨ Feature: add /delete route for build in http server
This commit is contained in:
parent
a7361905f8
commit
9e8ae932d6
@ -10,6 +10,9 @@ import db, { GalleryDB } from '~/main/apis/core/datastore'
|
|||||||
import { handleCopyUrl } from '~/main/utils/common'
|
import { handleCopyUrl } from '~/main/utils/common'
|
||||||
import { handleUrlEncode } from '#/utils/common'
|
import { handleUrlEncode } from '#/utils/common'
|
||||||
import { T } from '~/main/i18n/index'
|
import { T } from '~/main/i18n/index'
|
||||||
|
import ALLApi from '@/apis/allApi'
|
||||||
|
import picgo from '@core/picgo'
|
||||||
|
import GuiApi from '../../gui'
|
||||||
|
|
||||||
const handleClipboardUploading = async (): Promise<false | ImgInfo[]> => {
|
const handleClipboardUploading = async (): Promise<false | ImgInfo[]> => {
|
||||||
const useBuiltinClipboard = !!db.get('settings.useBuiltinClipboard')
|
const useBuiltinClipboard = !!db.get('settings.useBuiltinClipboard')
|
||||||
@ -20,7 +23,7 @@ const handleClipboardUploading = async (): Promise<false | ImgInfo[]> => {
|
|||||||
return await uploader.setWebContents(win!.webContents).upload()
|
return await uploader.setWebContents(win!.webContents).upload()
|
||||||
}
|
}
|
||||||
|
|
||||||
export const uploadClipboardFiles = async (): Promise<string> => {
|
export const uploadClipboardFiles = async (): Promise<IStringKeyMap> => {
|
||||||
const img = await handleClipboardUploading()
|
const img = await handleClipboardUploading()
|
||||||
console.log(img)
|
console.log(img)
|
||||||
if (img !== false) {
|
if (img !== false) {
|
||||||
@ -43,21 +46,30 @@ export const uploadClipboardFiles = async (): Promise<string> => {
|
|||||||
if (windowManager.has(IWindowList.SETTING_WINDOW)) {
|
if (windowManager.has(IWindowList.SETTING_WINDOW)) {
|
||||||
windowManager.get(IWindowList.SETTING_WINDOW)!.webContents?.send('updateGallery')
|
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 {
|
} else {
|
||||||
const notification = new Notification({
|
const notification = new Notification({
|
||||||
title: T('UPLOAD_FAILED'),
|
title: T('UPLOAD_FAILED'),
|
||||||
body: T('TIPS_UPLOAD_NOT_PICTURES')
|
body: T('TIPS_UPLOAD_NOT_PICTURES')
|
||||||
})
|
})
|
||||||
notification.show()
|
notification.show()
|
||||||
return ''
|
return {
|
||||||
|
url: '',
|
||||||
|
fullResult: {}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return ''
|
return {
|
||||||
|
url: '',
|
||||||
|
fullResult: {}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const uploadChoosedFiles = async (webContents: WebContents, files: IFileWithPath[]): Promise<string[]> => {
|
export const uploadChoosedFiles = async (webContents: WebContents, files: IFileWithPath[]): Promise<IStringKeyMap[]> => {
|
||||||
const input = files.map(item => item.path)
|
const input = files.map(item => item.path)
|
||||||
const imgs = await uploader.setWebContents(webContents).upload(input)
|
const imgs = await uploader.setWebContents(webContents).upload(input)
|
||||||
const result = []
|
const result = []
|
||||||
@ -75,7 +87,10 @@ export const uploadChoosedFiles = async (webContents: WebContents, files: IFileW
|
|||||||
notification.show()
|
notification.show()
|
||||||
}, i * 100)
|
}, i * 100)
|
||||||
await GalleryDB.getInstance().insert(imgs[i])
|
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'))
|
handleCopyUrl(pasteText.join('\n'))
|
||||||
// trayWindow just be created in mac/windows, not in linux
|
// trayWindow just be created in mac/windows, not in linux
|
||||||
@ -88,3 +103,47 @@ export const uploadChoosedFiles = async (webContents: WebContents, files: IFileW
|
|||||||
return []
|
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
|
||||||
|
}
|
||||||
|
@ -34,13 +34,15 @@ function initEventCenter () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function busCallUploadClipboardFiles () {
|
async function busCallUploadClipboardFiles () {
|
||||||
const imgUrl = await uploadClipboardFiles()
|
const result = await uploadClipboardFiles()
|
||||||
|
const imgUrl = result.url
|
||||||
bus.emit(UPLOAD_WITH_CLIPBOARD_FILES_RESPONSE, imgUrl)
|
bus.emit(UPLOAD_WITH_CLIPBOARD_FILES_RESPONSE, imgUrl)
|
||||||
}
|
}
|
||||||
|
|
||||||
async function busCallUploadFiles (pathList: IFileWithPath[]) {
|
async function busCallUploadFiles (pathList: IFileWithPath[]) {
|
||||||
const win = windowManager.getAvailableWindow()
|
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)
|
bus.emit(UPLOAD_WITH_FILES_RESPONSE, urls)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,13 +4,15 @@ import {
|
|||||||
} from './utils'
|
} from './utils'
|
||||||
import logger from '@core/picgo/logger'
|
import logger from '@core/picgo/logger'
|
||||||
import windowManager from 'apis/app/window/windowManager'
|
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 path from 'path'
|
||||||
import { dbPathDir } from 'apis/core/datastore/dbChecker'
|
import { dbPathDir } from 'apis/core/datastore/dbChecker'
|
||||||
const STORE_PATH = dbPathDir()
|
const STORE_PATH = dbPathDir()
|
||||||
const LOG_PATH = path.join(STORE_PATH, 'piclist.log')
|
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 errorMessage = `upload error. see ${LOG_PATH} for more detail.`
|
||||||
|
const deleteErrorMessage = `delete error. see ${LOG_PATH} for more detail.`
|
||||||
|
|
||||||
router.post('/upload', async ({
|
router.post('/upload', async ({
|
||||||
response,
|
response,
|
||||||
@ -23,14 +25,17 @@ router.post('/upload', async ({
|
|||||||
if (list.length === 0) {
|
if (list.length === 0) {
|
||||||
// upload with clipboard
|
// upload with clipboard
|
||||||
logger.info('[PicList Server] upload clipboard file')
|
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)
|
logger.info('[PicList Server] upload result:', res)
|
||||||
if (res) {
|
if (res) {
|
||||||
handleResponse({
|
handleResponse({
|
||||||
response,
|
response,
|
||||||
body: {
|
body: {
|
||||||
success: true,
|
success: true,
|
||||||
result: [res]
|
result: [res],
|
||||||
|
fullResult: [fullResult]
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
@ -51,14 +56,21 @@ router.post('/upload', async ({
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
const win = windowManager.getAvailableWindow()
|
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(' ; '))
|
logger.info('[PicList Server] upload result', res.join(' ; '))
|
||||||
if (res.length) {
|
if (res.length) {
|
||||||
handleResponse({
|
handleResponse({
|
||||||
response,
|
response,
|
||||||
body: {
|
body: {
|
||||||
success: true,
|
success: true,
|
||||||
result: res
|
result: res,
|
||||||
|
fullResult
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
@ -83,6 +95,56 @@ router.post('/upload', async ({
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
router.post('/delete', async ({
|
||||||
|
response,
|
||||||
|
list = []
|
||||||
|
} : {
|
||||||
|
response: IHttpResponse,
|
||||||
|
list?: IStringKeyMap[]
|
||||||
|
}): Promise<void> => {
|
||||||
|
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 ({
|
router.post('/heartbeat', async ({
|
||||||
response
|
response
|
||||||
} : {
|
} : {
|
||||||
|
@ -532,7 +532,6 @@ async function handlePicBedNameClick (picBedName: string, picBedConfigName: stri
|
|||||||
defaultConfigId: currentPicBedConfig.defaultId || ''
|
defaultConfigId: currentPicBedConfig.defaultId || ''
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
console.log(configList, picBedConfigName)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
onBeforeUnmount(() => {
|
onBeforeUnmount(() => {
|
||||||
|
Loading…
Reference in New Issue
Block a user