Skip to content

Commit

Permalink
✨ Feature(custom): picgo-server now support configName
Browse files Browse the repository at this point in the history
  • Loading branch information
Kuingsmile committed Jun 23, 2024
1 parent ef137fe commit 95f9b18
Showing 1 changed file with 47 additions and 14 deletions.
61 changes: 47 additions & 14 deletions bin/picgo-server
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,24 @@ const uploadMulter = multer({
storage: multerStorage
})

const changeCurrentUploader = (type, config, id) => {
if (!type) return
if (id) {
picgo.saveConfig({
[`uploader.${type}.defaultId`]: id
})
}
if (config) {
picgo.saveConfig({
[`picBed.${type}`]: config
})
}
picgo.saveConfig({
'picBed.current': type,
'picBed.uploader': type
})
}

router.get('/', responseForGet)
router.get('/upload', responseForGet)

Expand All @@ -173,20 +191,34 @@ router.post('/upload', async ({ response, list = [], urlparams }) => {
})
}
let currentPicBedType = ''
let currentPicBedConfig = {}
let currentPicBedConfigId = ''
const currentPicBed = picgo.getConfig('picBed') || {}
let needRestore = false
if (picbed) {
const currentPicBed = picgo.getConfig('picBed') || {}
currentPicBedType = currentPicBed?.uploader || currentPicBed?.current || 'smms'
if (picbed === currentPicBedType) {
currentPicBedType = currentPicBed.uploader || currentPicBed.current || 'smms'
currentPicBedConfig = currentPicBed[currentPicBedType] || {}
currentPicBedConfigId = currentPicBedConfig._id
const configName = urlparams?.get('configName') || currentPicBed[picbed]?._configName
if (picbed === currentPicBedType && currentPicBedConfig._configName === configName) {
// do nothing
} else {
needRestore = true
picgo.setConfig({
'picBed.current': picbed
})
picgo.setConfig({
'picBed.uploader': picbed
})
if (picbed !== currentPicBedType) {
needRestore = true
picgo.saveConfig({
'picBed.current': picbed,
'picBed.uploader': picbed
})
}
if (currentPicBed[picbed]?._configName) {
needRestore = true
const picBeds = picgo.getConfig('uploader')
const currentPicBedList = picBeds?.[picbed]?.configList
if (currentPicBedList) {
const currentConfig = currentPicBedList?.find((item) => item._configName === configName)
changeCurrentUploader(picbed, currentConfig, currentConfig._id)
}
}
}
}
if (list.length === 0) {
Expand Down Expand Up @@ -240,12 +272,13 @@ router.post('/upload', async ({ response, list = [], urlparams }) => {
}
fs.emptyDirSync(tempDir)
if (needRestore) {
picgo.setConfig({
'picBed.current': currentPicBedType
})
picgo.setConfig({
picgo.saveConfig({
'picBed.current': currentPicBedType,
'picBed.uploader': currentPicBedType
})
if (currentPicBed[picbed]?._configName) {
changeCurrentUploader(currentPicBedType, currentPicBedConfig, currentPicBedConfigId)
}
}
} catch (err) {
console.log(err)
Expand Down

0 comments on commit 95f9b18

Please sign in to comment.