mirror of
https://github.com/Kuingsmile/PicList.git
synced 2025-03-13 00:18:13 -04:00
✨ Feature(custom): refactor upload and delete api
This commit is contained in:
parent
922f04f672
commit
475b85eb1f
@ -14,6 +14,8 @@ import fs from 'fs-extra'
|
||||
import { marked } from 'marked'
|
||||
import { markdownContent } from './apiDoc'
|
||||
|
||||
const DEFAULT_PORT = 36677
|
||||
const DEFAULT_HOST = '0.0.0.0'
|
||||
const appPath = app.getPath('userData')
|
||||
const serverTempDir = path.join(appPath, 'serverTemp')
|
||||
|
||||
@ -44,40 +46,43 @@ class Server {
|
||||
private config: IServerConfig
|
||||
|
||||
constructor () {
|
||||
let config = picgo.getConfig<IServerConfig>('settings.server')
|
||||
const result = this.checkIfConfigIsValid(config)
|
||||
if (result) {
|
||||
this.config = config
|
||||
if (this.config.host === '127.0.0.1') {
|
||||
this.config.host = '0.0.0.0'
|
||||
}
|
||||
} else {
|
||||
config = {
|
||||
port: 36677,
|
||||
host: '0.0.0.0',
|
||||
enable: true
|
||||
}
|
||||
this.config = config
|
||||
picgo.saveConfig({
|
||||
'settings.server': config
|
||||
})
|
||||
}
|
||||
this.config = this.getConfigWithDefaults()
|
||||
this.httpServer = http.createServer(this.handleRequest)
|
||||
}
|
||||
|
||||
private checkIfConfigIsValid (config: IObj | undefined) {
|
||||
getConfigWithDefaults () {
|
||||
let config = picgo.getConfig<IServerConfig>('settings.server')
|
||||
if (!this.isValidConfig(config)) {
|
||||
config = { port: DEFAULT_PORT, host: DEFAULT_HOST, enable: true }
|
||||
picgo.saveConfig({ 'settings.server': config })
|
||||
}
|
||||
config.host = config.host === '127.0.0.1' ? '0.0.0.0' : config.host
|
||||
return config
|
||||
}
|
||||
|
||||
private isValidConfig (config: IObj | undefined) {
|
||||
return config && config.port && config.host && (config.enable !== undefined)
|
||||
}
|
||||
|
||||
private handleRequest = (request: http.IncomingMessage, response: http.ServerResponse) => {
|
||||
if (request.method === 'OPTIONS') {
|
||||
handleResponse({
|
||||
response
|
||||
})
|
||||
return
|
||||
switch (request.method) {
|
||||
case 'OPTIONS':
|
||||
handleResponse({ response })
|
||||
break
|
||||
case 'POST':
|
||||
this.handlePostRequest(request, response)
|
||||
break
|
||||
case 'GET':
|
||||
this.handleGetRequest(request, response)
|
||||
break
|
||||
default:
|
||||
logger.warn(`[PicList Server] don't support [${request.method}] method`)
|
||||
response.statusCode = 405
|
||||
response.end()
|
||||
}
|
||||
}
|
||||
|
||||
if (request.method === 'POST') {
|
||||
private handlePostRequest = (request: http.IncomingMessage, response: http.ServerResponse) => {
|
||||
const [url, query] = request.url!.split('?')
|
||||
if (!routers.getHandler(url!)) {
|
||||
logger.warn(`[PicList Server] don't support [${url}] url`)
|
||||
@ -143,16 +148,13 @@ class Server {
|
||||
})
|
||||
}
|
||||
}
|
||||
} else if (request.method === 'GET') {
|
||||
}
|
||||
|
||||
private handleGetRequest = (_request: http.IncomingMessage, response: http.ServerResponse) => {
|
||||
response.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' })
|
||||
const htmlContent = marked(markdownContent)
|
||||
response.write(htmlContent)
|
||||
response.end()
|
||||
} else {
|
||||
logger.warn(`[PicList Server] don't support [${request.method}] method`)
|
||||
response.statusCode = 404
|
||||
response.end()
|
||||
}
|
||||
}
|
||||
|
||||
// port as string is a bug
|
||||
@ -173,12 +175,13 @@ class Server {
|
||||
// to solve the auto number problem
|
||||
this.listen((port as number) + 1)
|
||||
}
|
||||
} else {
|
||||
logger.error('[PicList Server]', err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
startup () {
|
||||
console.log('startup', this.config.enable)
|
||||
if (this.config.enable) {
|
||||
this.listen(this.config.port)
|
||||
}
|
||||
@ -192,11 +195,8 @@ class Server {
|
||||
}
|
||||
|
||||
restart () {
|
||||
this.config = picgo.getConfig('settings.server')
|
||||
if (this.config.host === '127.0.0.1') {
|
||||
this.config.host = '0.0.0.0'
|
||||
}
|
||||
this.shutdown()
|
||||
this.config = this.getConfigWithDefaults()
|
||||
this.startup()
|
||||
}
|
||||
}
|
||||
|
@ -32,7 +32,6 @@ router.post('/upload', async ({
|
||||
urlparams?: URLSearchParams
|
||||
}): Promise<void> => {
|
||||
try {
|
||||
const picbed = urlparams?.get('picbed')
|
||||
const passedKey = urlparams?.get('key')
|
||||
const serverKey = picgo.getConfig('settings.serverKey') || ''
|
||||
if (serverKey && passedKey !== serverKey) {
|
||||
@ -40,11 +39,12 @@ router.post('/upload', async ({
|
||||
response,
|
||||
body: {
|
||||
success: false,
|
||||
message: 'server key is not correct'
|
||||
message: 'server key is uncorrect'
|
||||
}
|
||||
})
|
||||
return
|
||||
}
|
||||
const picbed = urlparams?.get('picbed')
|
||||
let currentPicBedType = ''
|
||||
let currentPicBedConfig = {} as IStringKeyMap
|
||||
let currentPicBedConfigId = ''
|
||||
@ -176,36 +176,21 @@ router.post('/delete', async ({
|
||||
return
|
||||
}
|
||||
try {
|
||||
// 区分是否是加密的数据,如果不是直接传入list,如果是,解密后再传入
|
||||
const treatList = list.map(item => {
|
||||
if (item.isEncrypted) {
|
||||
if (!item.isEncrypted) return item
|
||||
const aesHelper = new AESHelper()
|
||||
const data = aesHelper.decrypt(item.EncryptedData)
|
||||
return JSON.parse(data)
|
||||
} else {
|
||||
return item
|
||||
}
|
||||
return JSON.parse(aesHelper.decrypt(item.EncryptedData))
|
||||
})
|
||||
const result = await deleteChoosedFiles(treatList)
|
||||
const successCount = result.filter(item => item).length
|
||||
const failCount = result.filter(item => !item).length
|
||||
if (successCount) {
|
||||
const failCount = result.length - successCount
|
||||
handleResponse({
|
||||
response,
|
||||
body: {
|
||||
success: true,
|
||||
message: `delete success: ${successCount}, fail: ${failCount}`
|
||||
success: !!successCount,
|
||||
message: successCount ? `delete success: ${successCount}, fail: ${failCount}` : deleteErrorMessage
|
||||
}
|
||||
})
|
||||
} else {
|
||||
handleResponse({
|
||||
response,
|
||||
body: {
|
||||
success: false,
|
||||
message: deleteErrorMessage
|
||||
}
|
||||
})
|
||||
}
|
||||
} catch (err: any) {
|
||||
logger.error(err)
|
||||
handleResponse({
|
||||
|
Loading…
Reference in New Issue
Block a user