🔨 Refactor: picgo server the way of uploading

This commit is contained in:
PiEgg 2021-05-07 01:04:17 +08:00
parent ab762ef465
commit cf895deb8d
3 changed files with 21 additions and 22 deletions

View File

@ -54,7 +54,7 @@ const handleTalkingData = (webContents: WebContents, options: IAnalyticsData) =>
class Uploader { class Uploader {
private webContents: WebContents | null = null private webContents: WebContents | null = null
private uploading: boolean = false // private uploading: boolean = false
constructor () { constructor () {
this.init() this.init()
} }
@ -109,18 +109,18 @@ class Uploader {
} }
async upload (img?: IUploadOption): Promise<ImgInfo[]|false> { async upload (img?: IUploadOption): Promise<ImgInfo[]|false> {
if (this.uploading) { // if (this.uploading) {
showNotification({ // showNotification({
title: '上传失败', // title: '上传失败',
body: '前序上传还在继续,请稍后再试' // body: '前序上传还在继续,请稍后再试'
}) // })
return Promise.resolve(false) // return Promise.resolve(false)
} // }
try { try {
const startTime = Date.now() const startTime = Date.now()
this.uploading = true // this.uploading = true
const output = await picgo.upload(img) const output = await picgo.upload(img)
this.uploading = false // this.uploading = false
if (Array.isArray(output) && output.every((item: ImgInfo) => item.imgUrl)) { if (Array.isArray(output) && output.every((item: ImgInfo) => item.imgUrl)) {
if (this.webContents) { if (this.webContents) {
handleTalkingData(this.webContents, { handleTalkingData(this.webContents, {
@ -135,7 +135,7 @@ class Uploader {
return false return false
} }
} catch (e) { } catch (e) {
this.uploading = false // this.uploading = false
logger.error(e) logger.error(e)
setTimeout(() => { setTimeout(() => {
showNotification({ showNotification({

View File

@ -67,7 +67,7 @@ class Server {
} }
}) })
} }
logger.info(`[PicGo Server] get the request`) logger.info(`[PicGo Server] get the request`, body)
const handler = routers.getHandler(request.url!) const handler = routers.getHandler(request.url!)
handler!({ handler!({
...postObj, ...postObj,

View File

@ -1,12 +1,10 @@
import router from './router' import router from './router'
import {
uploadWithClipboardFiles,
uploadWithFiles
} from '@core/bus/apis'
import { import {
handleResponse handleResponse
} from './utils' } from './utils'
import logger from '@core/picgo/logger' import logger from '@core/picgo/logger'
import windowManager from 'apis/app/window/windowManager'
import { uploadChoosedFiles, uploadClipboardFiles } from 'apis/app/uploader/apis'
router.post('/upload', async ({ router.post('/upload', async ({
response, response,
@ -19,13 +17,13 @@ router.post('/upload', async ({
if (list.length === 0) { if (list.length === 0) {
// upload with clipboard // upload with clipboard
logger.info('[PicGo Server] upload clipboard file') logger.info('[PicGo Server] upload clipboard file')
const res = await uploadWithClipboardFiles() const res = await uploadClipboardFiles()
if (res.success) { if (res) {
handleResponse({ handleResponse({
response, response,
body: { body: {
success: true, success: true,
result: res.result result: res
} }
}) })
} else { } else {
@ -45,13 +43,14 @@ router.post('/upload', async ({
path: item path: item
} }
}) })
const res = await uploadWithFiles(pathList) const win = windowManager.getAvailableWindow()
if (res.success) { const res = await uploadChoosedFiles(win.webContents, pathList)
if (res.length) {
handleResponse({ handleResponse({
response, response,
body: { body: {
success: true, success: true,
result: res.result result: res
} }
}) })
} else { } else {