Feature: build-in server support formdata now

ISSUES CLOSED: #103
This commit is contained in:
Kuingsmile 2023-10-09 18:37:29 +08:00
parent df4d39e9ed
commit 615cbc5f07
4 changed files with 152 additions and 34 deletions

View File

@ -65,9 +65,10 @@
"marked": "^8.0.0",
"mime-types": "^2.1.35",
"mitt": "^3.0.1",
"multer": "^1.4.5-lts.1",
"node-ssh-no-cpu-features": "^1.0.1",
"nodejs-file-downloader": "^4.12.1",
"piclist": "^1.2.2",
"piclist": "^1.3.1",
"pinia": "^2.1.6",
"pinia-plugin-persistedstate": "^3.2.0",
"qiniu": "^7.9.0",
@ -96,6 +97,7 @@
"@types/js-yaml": "^4.0.6",
"@types/lowdb": "^1.0.12",
"@types/mime-types": "^2.1.2",
"@types/multer": "^1.4.8",
"@types/node": "^16.10.2",
"@types/request-promise-native": "^1.0.19",
"@types/semver": "^7.5.3",

View File

@ -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`)

View File

@ -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)
}

View File

@ -3510,6 +3510,13 @@
resolved "https://registry.npmjs.org/@types/ms/-/ms-0.7.31.tgz#31b7ca6407128a3d2bbc27fe2d21b345397f6197"
integrity sha512-iiUgKzV9AuaEkZqkOLDIvlQiL6ltuZd9tGcW3gwpnX8JbuiuhFlEGmmFXEXkN50Cvq7Os88IY2v0dkDqXYWVgA==
"@types/multer@^1.4.8":
version "1.4.8"
resolved "https://registry.yarnpkg.com/@types/multer/-/multer-1.4.8.tgz#8d98c36f6a4e0b228a9f262cd66e881d7cd64039"
integrity sha512-VMZOW6mnmMMhA5m3fsCdXBwFwC+a+27/8gctNMuQC4f7UtWcF79KAFGoIfKZ4iqrElgWIa3j5vhMJDp0iikQ1g==
dependencies:
"@types/express" "*"
"@types/node@*", "@types/node@^16.10.2", "@types/node@^16.11.26", "@types/node@^18.11.18":
version "16.11.18"
resolved "https://registry.npmjs.org/@types/node/-/node-16.11.18.tgz#39ed7c52943b0cee6d7299b717707bd51b1f90b9"
@ -4808,6 +4815,11 @@ app-builder-lib@23.3.3:
tar "^6.1.11"
temp-file "^3.4.0"
append-field@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/append-field/-/append-field-1.0.0.tgz#1e3440e915f0b1203d23748e78edd7b9b5b43e56"
integrity sha512-klpgFSWLW1ZEs8svjfb7g4qWY0YS5imI82dTg+QahUvJ8YqAY0P10Uk8tTyh9ZGuYEZEMaeJYCF5BFuX552hsw==
"aproba@^1.0.3 || ^2.0.0":
version "2.0.0"
resolved "https://registry.npmjs.org/aproba/-/aproba-2.0.0.tgz#52520b8ae5b569215b354efc0caa3fe1e45a8adc"
@ -5458,6 +5470,13 @@ builtins@^5.0.0, builtins@^5.0.1:
dependencies:
semver "^7.0.0"
busboy@^1.0.0:
version "1.6.0"
resolved "https://registry.yarnpkg.com/busboy/-/busboy-1.6.0.tgz#966ea36a9502e43cdb9146962523b92f531f6893"
integrity sha512-8SFQbg/0hQ9xy3UNTB0YEnsNBbWfhf7RtnzpL7TkBiTBRfrQ9Fxcnz7VJsleJpyp6rVLvXiuORqjlHi5q+PYuA==
dependencies:
streamsearch "^1.1.0"
byte-length@^1.0.2:
version "1.0.2"
resolved "https://registry.npmjs.org/byte-length/-/byte-length-1.0.2.tgz#ba5a5909240b0121c079b7f7b15248d6f08223cc"
@ -6104,6 +6123,16 @@ concat-map@0.0.1:
resolved "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b"
integrity sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=
concat-stream@^1.5.2:
version "1.6.2"
resolved "https://registry.yarnpkg.com/concat-stream/-/concat-stream-1.6.2.tgz#904bdf194cd3122fc675c77fc4ac3d4ff0fd1a34"
integrity sha512-27HBghJxjiZtIk3Ycvn/4kbJk/1uZuJFfuPEns6LaEvpvG1f0hTea8lilrouyo9mVc2GWdcEZ8OLoGmSADlrCw==
dependencies:
buffer-from "^1.0.0"
inherits "^2.0.3"
readable-stream "^2.2.2"
typedarray "^0.0.6"
conf@^9.0.0:
version "9.0.2"
resolved "https://registry.npmmirror.com/conf/-/conf-9.0.2.tgz#943589602b1ce274d9234265314336a698972bc5"
@ -11550,6 +11579,13 @@ mkdirp@^0.5.1, mkdirp@^0.5.5:
dependencies:
minimist "^1.2.5"
mkdirp@^0.5.4:
version "0.5.6"
resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-0.5.6.tgz#7def03d2432dcae4ba1d611445c48396062255f6"
integrity sha512-FP+p8RB8OWpF3YZBCrP5gtADmtXApB5AMLn+vdyA+PyxCjrCs00mjyUozssO33cwDeT3wNGdLxJ5M//YqtHAJw==
dependencies:
minimist "^1.2.6"
mkdirp@^1.0.3:
version "1.0.4"
resolved "https://registry.npmjs.org/mkdirp/-/mkdirp-1.0.4.tgz#3eb5ed62622756d79a5f0e2a221dfebad75c2f7e"
@ -11610,6 +11646,19 @@ ms@2.1.3, ms@^2.0.0, ms@^2.1.1:
resolved "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz#574c8138ce1d2b5861f0b44579dbadd60c6615b2"
integrity sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==
multer@^1.4.5-lts.1:
version "1.4.5-lts.1"
resolved "https://registry.yarnpkg.com/multer/-/multer-1.4.5-lts.1.tgz#803e24ad1984f58edffbc79f56e305aec5cfd1ac"
integrity sha512-ywPWvcDMeH+z9gQq5qYHCCy+ethsk4goepZ45GLD63fOu0YcNecQxi64nDs3qluZB+murG3/D4dJ7+dGctcCQQ==
dependencies:
append-field "^1.0.0"
busboy "^1.0.0"
concat-stream "^1.5.2"
mkdirp "^0.5.4"
object-assign "^4.1.1"
type-is "^1.6.4"
xtend "^4.0.0"
multicast-dns@^7.2.5:
version "7.2.5"
resolved "https://registry.npmjs.org/multicast-dns/-/multicast-dns-7.2.5.tgz#77eb46057f4d7adbd16d9290fa7299f6fa64cced"
@ -12050,7 +12099,7 @@ oauth-sign@~0.9.0:
resolved "https://registry.npmmirror.com/oauth-sign/-/oauth-sign-0.9.0.tgz#47a7b016baa68b5fa0ecf3dee08a85c679ac6455"
integrity sha512-fexhUFFPTGV8ybAtSIGbV6gOkSv8UtRbDBnAyLQw4QPKkgNlsH2ByPGtMUqdWkos6YCRmAqViwgZrJc/mRDzZQ==
object-assign@^4.0.1, object-assign@^4.1.0:
object-assign@^4.0.1, object-assign@^4.1.0, object-assign@^4.1.1:
version "4.1.1"
resolved "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz#2109adc7965887cfc05cbbd442cac8bfbb360863"
integrity sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM=
@ -12639,10 +12688,10 @@ performance-now@^2.1.0:
resolved "https://registry.npmmirror.com/performance-now/-/performance-now-2.1.0.tgz#6309f4e0e5fa913ec1c69307ae364b4b377c9e7b"
integrity sha512-7EAHlyLHI56VEIdK57uwHdHKIaAGbnXPiw0yWbarQZOKaKpvUIgW0jWRVLiatnM+XXlSwsanIBH/hzGMJulMow==
piclist@^1.2.2:
version "1.2.2"
resolved "https://registry.yarnpkg.com/piclist/-/piclist-1.2.2.tgz#cf32591baf2a094b20decec51973c46e9212942b"
integrity sha512-bKzOzRUBYn5nuBcZDVouXweatohID9jW6L1+YjmFdnKqWt0UJ2H8+jpyVWZQF3FSDvoWMW0ChgXouNrHtrIO7w==
piclist@^1.3.1:
version "1.3.1"
resolved "https://registry.yarnpkg.com/piclist/-/piclist-1.3.1.tgz#1a626a3c5cf253c47fe88207a4b9822f6a89c580"
integrity sha512-huF+IOm9R7AwO7OZexHUsv9y5pJuHUlLE1W1FTVJCB0bkdUMw4hCz2OiXcujIXXSJxQ6UUUSE4F8GK7MvTD8YQ==
dependencies:
"@picgo/i18n" "^1.0.0"
"@picgo/store" "^2.1.0"
@ -12665,6 +12714,7 @@ piclist@^1.2.2:
mime-types "2.1.35"
minimatch "^3.0.4"
minimist "^1.2.8"
multer "^1.4.5-lts.1"
node-ssh-no-cpu-features "^1.0.1"
qiniu "^7.9.0"
resolve "^1.8.1"
@ -13481,7 +13531,7 @@ readable-stream@3, readable-stream@^3.0.0, readable-stream@^3.0.6, readable-stre
string_decoder "^1.1.1"
util-deprecate "^1.0.1"
readable-stream@^2.0.0, readable-stream@^2.3.0, readable-stream@^2.3.5:
readable-stream@^2.0.0, readable-stream@^2.2.2, readable-stream@^2.3.0, readable-stream@^2.3.5:
version "2.3.8"
resolved "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.8.tgz#91125e8042bba1b9887f49345f6277027ce8be9b"
integrity sha512-8p0AUk4XODgIewSi0l8Epjs+EVnWiK7NoDIEGU0HhE7+ZyY8D1IMY7odu5lRrFXGg71L15KG8QrPmum45RTtdA==
@ -14660,6 +14710,11 @@ stream-wormhole@^1.0.4:
resolved "https://registry.npmmirror.com/stream-wormhole/-/stream-wormhole-1.1.0.tgz#300aff46ced553cfec642a05251885417693c33d"
integrity sha512-gHFfL3px0Kctd6Po0M8TzEvt3De/xu6cnRrjlfYNhwbhLPLwigI2t1nc6jrzNuaYg5C4YF78PPFuQPzRiqn9ew==
streamsearch@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/streamsearch/-/streamsearch-1.1.0.tgz#404dd1e2247ca94af554e841a8ef0eaa238da764"
integrity sha512-Mcc5wHehp9aXz1ax6bZUyY5afg9u2rv5cqQI3mRrYkGC8rW2hM02jWuwjtL++LS5qinSyhj2QfLyNsuc+VsExg==
strict-uri-encode@^1.0.0:
version "1.1.0"
resolved "https://registry.npmjs.org/strict-uri-encode/-/strict-uri-encode-1.1.0.tgz#279b225df1d582b1f54e65addd4352e18faa0713"
@ -15398,9 +15453,9 @@ type-fest@^4.2.0:
resolved "https://registry.npmjs.org/type-fest/-/type-fest-4.3.0.tgz#5cb4dc1c05f868a2a24313893b847b0443a9d504"
integrity sha512-XbMcLhoaaX/vw1S8jTKysTlznqSPxDXj1Jf56neDMksT1xoKr02pFAhHhDbW9bFejktlwKto18/UsdXlnUCBMg==
type-is@~1.6.18:
type-is@^1.6.4, type-is@~1.6.18:
version "1.6.18"
resolved "https://registry.npmjs.org/type-is/-/type-is-1.6.18.tgz#4e552cd05df09467dcbc4ef739de89f2cf37c131"
resolved "https://registry.yarnpkg.com/type-is/-/type-is-1.6.18.tgz#4e552cd05df09467dcbc4ef739de89f2cf37c131"
integrity sha512-TkRKr9sUTxEH8MdfuCSP7VizJyzRNMjj2J2do2Jr3Kym598JVdEksuzPQCnlFPW4ky9Q+iA+ma9BGm06XQBy8g==
dependencies:
media-typer "0.3.0"
@ -15452,6 +15507,11 @@ typedarray-to-buffer@^3.1.5:
dependencies:
is-typedarray "^1.0.0"
typedarray@^0.0.6:
version "0.0.6"
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"
integrity sha512-/aCDEGatGvZ2BIk+HmLf4ifCJFwvKFNb9/JeZPMulfgFracn9QFcAf5GO8B/mweUjSoblS5In0cWhqpfs/5PQA==
typescript@^4.4.3:
version "4.5.4"
resolved "https://registry.npmjs.org/typescript/-/typescript-4.5.4.tgz#a17d3a0263bf5c8723b9c52f43c5084edf13c2e8"