Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/app-middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,11 @@ module.exports = (app) => {
app.use(flash())
app.use(
bodyParser.urlencoded({
extended: true
extended: true,
limit: '50mb'
})
)
app.use(bodyParser.json())
app.use(bodyParser.json({ limit: '50mb' }))

Comment on lines 41 to 48
// Custom app middleware
//
Expand Down
83 changes: 77 additions & 6 deletions lib/plugins/pluginEndpoints.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
const { default: axios } = require('axios')
const fs = require('fs')
const os = require('os')
const path = require('path')

const config = require('../config')

Expand All @@ -7,11 +10,6 @@ module.exports = function (req, res) {
const endpoint = req.body.endpoint
const category = req.body.category

if (category === 'message') {
console.log(req.body.message)
return res.status(200).send('Message sent: ' + req.body.message)
}

let pluginUrl = findPlugin(name, category)

let prefix
Expand Down Expand Up @@ -106,6 +104,7 @@ module.exports = function (req, res) {
function getRun (pluginUrl, data, category, res) {
var pluginData
var responseType
let cleanup = null

switch (category) {
case 'rendering': {
Expand All @@ -119,7 +118,9 @@ module.exports = function (req, res) {
break
}
case 'submit': {
pluginData = data
const prepared = prepareSubmitPluginData(data)
pluginData = prepared.pluginData
cleanup = prepared.cleanup
responseType = 'arraybuffer'
break
}
Expand All @@ -145,7 +146,77 @@ module.exports = function (req, res) {
}).catch(error => {
res.header('Content-Type', 'text/plain')
return res.status(500).send(error)
}).finally(() => {
if (cleanup) {
cleanup()
}
})
}

function prepareSubmitPluginData (data) {
let parsedData = data

if (typeof parsedData === 'string') {
try {
parsedData = JSON.parse(parsedData)
} catch (error) {
return {
pluginData: data,
cleanup: null
}
}
}

const manifestFiles = parsedData && parsedData.manifest && Array.isArray(parsedData.manifest.files)
? parsedData.manifest.files
: null

if (!manifestFiles) {
return {
pluginData: parsedData,
cleanup: null
}
}

const hasInlineContent = manifestFiles.some(file => typeof file.contentBase64 === 'string' && file.contentBase64.length > 0)

if (!hasInlineContent) {
return {
pluginData: parsedData,
cleanup: null
}
}

const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'sbh-submit-plugin-'))
const preparedFiles = manifestFiles.map((file, index) => {
if (typeof file.contentBase64 === 'string' && file.contentBase64.length > 0) {
const safeFilename = path.basename(file.filename || `plugin_input_${index}`)
const localPath = path.join(tempDir, safeFilename)
fs.writeFileSync(localPath, Buffer.from(file.contentBase64, 'base64'))
return {
Comment on lines +190 to +196
...file,
url: localPath
}
}
Comment on lines +190 to +200
return file
Comment on lines +191 to +201
})

return {
pluginData: {
...parsedData,
manifest: {
...parsedData.manifest,
files: preparedFiles
}
},
cleanup: () => {
try {
fs.rmSync(tempDir, { recursive: true, force: true })
} catch (error) {
// best effort cleanup
}
}
Comment on lines +212 to +218
}
}

function getPublicDataFromURI (data) {
Expand Down
Loading