mirror of
https://github.com/Kuingsmile/PicList.git
synced 2026-05-25 18:20:24 +08:00
@@ -7,6 +7,29 @@ import {
|
||||
import picgo from '@core/picgo'
|
||||
import logger from '@core/picgo/logger'
|
||||
import axios from 'axios'
|
||||
import multer from 'multer'
|
||||
import { app } from 'electron'
|
||||
import path from 'path'
|
||||
import fs from 'fs-extra'
|
||||
|
||||
const appPath = app.getPath('userData')
|
||||
const serverTempDir = path.join(appPath, 'serverTemp')
|
||||
|
||||
fs.ensureDirSync(serverTempDir)
|
||||
|
||||
const multerStorage = multer.diskStorage({
|
||||
destination: function (_req: any, _file: any, cb: (arg0: null, arg1: any) => void) {
|
||||
fs.ensureDirSync(serverTempDir)
|
||||
cb(null, serverTempDir)
|
||||
},
|
||||
filename: function (_req: any, file: { originalname: any }, cb: (arg0: null, arg1: any) => void) {
|
||||
cb(null, file.originalname)
|
||||
}
|
||||
})
|
||||
|
||||
const uploadMulter = multer({
|
||||
storage: multerStorage
|
||||
})
|
||||
|
||||
class Server {
|
||||
private httpServer: http.Server
|
||||
@@ -55,32 +78,59 @@ class Server {
|
||||
}
|
||||
})
|
||||
} else {
|
||||
let body: string = ''
|
||||
let postObj: IObj
|
||||
request.on('data', chunk => {
|
||||
body += chunk
|
||||
})
|
||||
request.on('end', () => {
|
||||
try {
|
||||
postObj = (body === '') ? {} : JSON.parse(body)
|
||||
} catch (err: any) {
|
||||
logger.error('[PicList Server]', err)
|
||||
return handleResponse({
|
||||
response,
|
||||
body: {
|
||||
success: false,
|
||||
message: 'Not sending data in JSON format'
|
||||
}
|
||||
})
|
||||
}
|
||||
logger.info('[PicList Server] get the request', body)
|
||||
const handler = routers.getHandler(url!)?.handler
|
||||
handler!({
|
||||
...postObj,
|
||||
response,
|
||||
urlparams: query ? new URLSearchParams(query) : undefined
|
||||
if (request.headers['content-type'] && request.headers['content-type'].startsWith('multipart/form-data')) {
|
||||
// @ts-ignore
|
||||
uploadMulter.any()(request, response, (err: any) => {
|
||||
if (err) {
|
||||
logger.info('[PicList Server]', err)
|
||||
return handleResponse({
|
||||
response,
|
||||
body: {
|
||||
success: false,
|
||||
message: 'Error processing formData'
|
||||
}
|
||||
})
|
||||
}
|
||||
// @ts-ignore
|
||||
const list = request.files.map(file => file.path)
|
||||
|
||||
const handler = routers.getHandler(url)?.handler
|
||||
if (handler) {
|
||||
handler({
|
||||
list,
|
||||
response,
|
||||
urlparams: query ? new URLSearchParams(query) : undefined
|
||||
})
|
||||
}
|
||||
})
|
||||
})
|
||||
} else {
|
||||
let body: string = ''
|
||||
let postObj: IObj
|
||||
request.on('data', chunk => {
|
||||
body += chunk
|
||||
})
|
||||
request.on('end', () => {
|
||||
try {
|
||||
postObj = (body === '') ? {} : JSON.parse(body)
|
||||
} catch (err: any) {
|
||||
logger.error('[PicList Server]', err)
|
||||
return handleResponse({
|
||||
response,
|
||||
body: {
|
||||
success: false,
|
||||
message: 'Not sending data in JSON format'
|
||||
}
|
||||
})
|
||||
}
|
||||
logger.info('[PicList Server] get the request', body)
|
||||
const handler = routers.getHandler(url!)?.handler
|
||||
handler!({
|
||||
...postObj,
|
||||
response,
|
||||
urlparams: query ? new URLSearchParams(query) : undefined
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
} else {
|
||||
logger.warn(`[PicList Server] don't support [${request.method}] method`)
|
||||
|
||||
@@ -9,6 +9,11 @@ import path from 'path'
|
||||
import { dbPathDir } from 'apis/core/datastore/dbChecker'
|
||||
import picgo from '@core/picgo'
|
||||
import { changeCurrentUploader } from '../utils/handleUploaderConfig'
|
||||
import { app } from 'electron'
|
||||
import fs from 'fs-extra'
|
||||
|
||||
const appPath = app.getPath('userData')
|
||||
const serverTempDir = path.join(appPath, 'serverTemp')
|
||||
|
||||
const STORE_PATH = dbPathDir()
|
||||
const LOG_PATH = path.join(STORE_PATH, 'piclist.log')
|
||||
@@ -112,6 +117,7 @@ router.post('/upload', async ({
|
||||
})
|
||||
}
|
||||
}
|
||||
fs.emptyDirSync(serverTempDir)
|
||||
if (needRestore) {
|
||||
changeCurrentUploader(currentPicBedType, currentPicBedConfig, currentPicBedConfigId)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user