Skip to content

Commit

Permalink
✨ Feature: add logs for picgo-server
Browse files Browse the repository at this point in the history
ISSUES CLOSED: #627
  • Loading branch information
Molunerfinn committed Apr 24, 2021
1 parent a657c51 commit 2d9e9c0
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 45 deletions.
72 changes: 31 additions & 41 deletions src/main/apis/app/uploader/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import util from 'util'
import { IPicGo } from 'picgo/dist/src/types'
import { showNotification, calcDurationRange } from '~/main/utils/common'
import { TALKING_DATA_EVENT } from '~/universal/events/constants'
import logger from '@core/picgo/logger'

const waitForShow = (webcontent: WebContents) => {
return new Promise<void>((resolve, reject) => {
Expand Down Expand Up @@ -107,55 +108,44 @@ class Uploader {
return this
}

upload (img?: IUploadOption): Promise<ImgInfo[]|false> {
async upload (img?: IUploadOption): Promise<ImgInfo[]|false> {
if (this.uploading) {
showNotification({
title: '上传失败',
body: '前序上传还在继续,请稍后再试'
})
return Promise.resolve(false)
}
return new Promise((resolve) => {
try {
const startTime = Date.now()
this.uploading = true
picgo.upload(img)
picgo.once('finished', ctx => {
this.uploading = false
if (ctx.output.every((item: ImgInfo) => item.imgUrl)) {
if (this.webContents) {
handleTalkingData(this.webContents, {
fromClipboard: !img,
type: db.get('picBed.current') || 'smms',
count: img ? img.length : 1,
duration: Date.now() - startTime
} as IAnalyticsData)
}
resolve(ctx.output)
} else {
resolve(false)
}
picgo.removeAllListeners('failed')
})
picgo.once('failed', (e: Error) => {
this.uploading = false
setTimeout(() => {
showNotification({
title: '上传失败',
body: util.format(e.stack),
clickToCopy: true
})
}, 500)
picgo.removeAllListeners('finished')
resolve(false)
})
} catch (e) {
this.uploading = false
picgo.removeAllListeners('failed')
picgo.removeAllListeners('finished')
resolve([])
try {
const startTime = Date.now()
this.uploading = true
const output = await picgo.upload(img)
this.uploading = false
if (Array.isArray(output) && output.every((item: ImgInfo) => item.imgUrl)) {
if (this.webContents) {
handleTalkingData(this.webContents, {
fromClipboard: !img,
type: db.get('picBed.current') || 'smms',
count: img ? img.length : 1,
duration: Date.now() - startTime
} as IAnalyticsData)
}
return output
} else {
return false
}
})
} catch (e) {
this.uploading = false
logger.error(e)
setTimeout(() => {
showNotification({
title: '上传失败',
body: util.format(e.stack),
clickToCopy: true
})
}, 500)
return false
}
}
}

Expand Down
33 changes: 33 additions & 0 deletions src/main/lifeCycle/errorHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { app } from 'electron'
import fse from 'fs-extra'
import path from 'path'
import dayjs from 'dayjs'
import util from 'util'
const STORE_PATH = app.getPath('userData')
const LOG_PATH = path.join(STORE_PATH, '/picgo.log')

// since the error may occur in picgo-core
// so we can't use the log from picgo
const loggerWriter = (error: Error) => {
let log = `${dayjs().format('YYYY-MM-DD HH:mm:ss')} [PicGo ERROR] startup error`
if (error?.stack) {
log += `\n------Error Stack Begin------\n${util.format(error.stack)}\n-------Error Stack End-------\n`
} else {
const msg = JSON.stringify(error)
log += `${msg}\n`
}
fse.appendFileSync(LOG_PATH, log)
}

const handleProcessError = (error: Error) => {
console.error(error)
loggerWriter(error)
}

process.on('uncaughtException', error => {
handleProcessError(error)
})

process.on('unhandledRejection', (error: any) => {
handleProcessError(error)
})
3 changes: 1 addition & 2 deletions src/main/lifeCycle/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import './errorHandler'
import {
app,
globalShortcut,
Expand Down Expand Up @@ -152,13 +153,11 @@ class LifeCycle {
process.on('message', data => {
if (data === 'graceful-exit') {
app.quit()
server.shutdown()
}
})
} else {
process.on('SIGTERM', () => {
app.quit()
server.shutdown()
})
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class Server {
private handleRequest = (request: http.IncomingMessage, response: http.ServerResponse) => {
if (request.method === 'POST') {
if (!routers.getHandler(request.url!)) {
logger.warn(`[PicGo Server] don't support [${request.url}] url`)
handleResponse({
response,
statusCode: 404,
Expand All @@ -57,6 +58,7 @@ class Server {
try {
postObj = (body === '') ? {} : JSON.parse(body)
} catch (err) {
logger.error(`[PicGo Server]`, err)
return handleResponse({
response,
body: {
Expand All @@ -65,6 +67,7 @@ class Server {
}
})
}
logger.info(`[PicGo Server] get the request`)
const handler = routers.getHandler(request.url!)
handler!({
...postObj,
Expand All @@ -73,6 +76,7 @@ class Server {
})
}
} else {
logger.warn(`[PicGo Server] don't support [${request.method}] method`)
response.statusCode = 404
response.end()
}
Expand Down
14 changes: 12 additions & 2 deletions src/main/server/routerManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ router.post('/upload', async ({
try {
if (list.length === 0) {
// upload with clipboard
logger.info('[PicGo Server] upload clipboard file')
const res = await uploadWithClipboardFiles()
if (res.success) {
handleResponse({
Expand All @@ -29,10 +30,15 @@ router.post('/upload', async ({
})
} else {
handleResponse({
response
response,
body: {
success: false,
message: 'upload error'
}
})
}
} else {
logger.info('[PicGo Server] upload files in list')
// upload with files
const pathList = list.map(item => {
return {
Expand All @@ -50,7 +56,11 @@ router.post('/upload', async ({
})
} else {
handleResponse({
response
response,
body: {
success: false,
message: 'upload error'
}
})
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/server/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import logger from '@core/picgo/logger'

export const handleResponse = ({
response,
statusCode = 200,
Expand All @@ -13,6 +15,9 @@ export const handleResponse = ({
header?: IObj,
body?: any
}) => {
if (body?.success === false) {
logger.warn('[PicGo Server] upload failed, see picgo.log for more detail ↑')
}
response.writeHead(statusCode, header)
response.write(JSON.stringify(body))
response.end()
Expand Down

0 comments on commit 2d9e9c0

Please sign in to comment.