Skip to content

Instantly share code, notes, and snippets.

@solisoft
Last active November 27, 2017 14:07
Show Gist options
  • Save solisoft/60bdf8f0f71c48871c353551bfc55982 to your computer and use it in GitHub Desktop.
Save solisoft/60bdf8f0f71c48871c353551bfc55982 to your computer and use it in GitHub Desktop.
ArangoDB Multipart/formdata
'use strict';
const db = require('@arangodb').db;
const joi = require('joi');
const _ = require('lodash');
const createRouter = require('@arangodb/foxx/router');
const router = createRouter();
const querystring = require('querystring');
const crypt = require('@arangodb/crypto');
const fs = require("fs")
module.context.use(router);
// POST /upload/:key/:type
router.post('/:key/:type', function(req, res) {
let docs = []
_.each(req.body, function(data) {
const uuid = crypt.genRandomAlphaNumbers(40)
let filename = querystring.parse(data.headers["Content-Disposition"]
.split(';')[2].trim().replace(/"/g, ''))
.filename
const filedest = "/Users/soli/"+uuid+"."+ _.last(filename.split('.'))
fs.write(filedest, data.data)
docs.push(
db.uploads.save({
filename: filedest,
object_key: req.pathParams.key,
object_type: req.pathParams.type,
length: data.data.length,
mime: data.headers["Content-Type"]
})
)
})
res.send(docs)
})
.body(['multipart/form-data'])
.description("Upload a file")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment