Commit f86a04c7 authored by anton's avatar anton

updated import

parent 3fcac04e
This diff is collapsed.
......@@ -41,6 +41,7 @@
"mbjs-utils": "0.0.5",
"mongodb": "^3.1.0",
"morgan": "^1.9.0",
"multer": "^1.3.1",
"mz": "^2.7.0",
"nedb": "^1.8.0",
"polka": "^0.4.0",
......
......@@ -5,10 +5,12 @@ const
fs = require('mz/fs'),
rimraf = require('rimraf'),
os = require('os'),
multer = require('multer'),
send = require('@polka/send-type'),
{ Assert } = require('mbjs-utils')
module.exports.setupArchives = (app, mapService, annotationService) => {
const upload = multer({ dest: os.tmpdir() })
app.post('/archives/maps', async (req, res) => {
let data = {}
let request = {
......@@ -42,6 +44,55 @@ module.exports.setupArchives = (app, mapService, annotationService) => {
res.setHeader('Content-Disposition', `attachment; filename="${filename}"`)
file.pipe(res)
})
app.post('/archives/maps/upload', async function (req, res) {
upload.single('file')(req, res, async () => {
const results = await exports.readArchive(req.file.path)
console.log(results)
let hasDuplicates = false
if (results.maps) {
for (let map of results.maps) {
const getRequest = {
params: { id: map.uuid },
user: req.user
}
const item = await mapService.getHandler(getRequest)
if (!item.error) hasDuplicates = true
}
}
if (results.annotations) {
for (let annotation of results.annotations) {
const getRequest = {
params: { id: annotation.uuid },
user: req.user
}
const item = await annotationService.getHandler(getRequest)
if (!item.error) hasDuplicates = true
}
}
if (hasDuplicates) send(res, 400, { message: 'errors.has_duplicates' })
else {
if (results.maps) {
for (let map of results.maps) {
const postRequest = {
body: map.uuid,
user: req.user
}
await mapService.postHandler(postRequest)
}
}
if (results.annotations) {
for (let annotation of results.annotations) {
const postRequest = {
body: annotation,
user: req.user
}
await annotationService.postHandler(postRequest)
}
}
send(res, 200)
}
})
})
}
module.exports.createArchive = async (data) => {
......@@ -90,25 +141,37 @@ module.exports.createArchive = async (data) => {
}
module.exports.readArchive = archivePath => {
yauzl.open(archivePath, {lazyEntries: true}, function (err, zipfile) {
if (err) throw err
zipfile.readEntry()
zipfile.on('entry', function (entry) {
if (/\/$/.test(entry.fileName)) {
// Directory file names end with '/'.
// Note that entires for directories themselves are optional.
// An entry's fileName implicitly requires its parent directories to exist.
zipfile.readEntry()
}
else {
// file entry
zipfile.openReadStream(entry, function (err, readStream) {
if (err) throw err
readStream.on('end', function () {
zipfile.readEntry()
})
const results = {}
const getFile = (entry, zipfile) => {
return new Promise((rs, rj) => {
let data = ''
zipfile.openReadStream(entry, function (err, readStream) {
if (err) return rj(err)
readStream.on('data', chunk => {
data += chunk.toString()
})
}
readStream.on('end', () => rs(data))
readStream.on('error', err => rj(err))
})
})
}
return new Promise((resolve, reject) => {
yauzl.open(archivePath, {lazyEntries: true}, async (err, zipfile) => {
if (err) return reject(err)
zipfile.readEntry()
zipfile.on('end', () => resolve(results))
zipfile.on('error', err => reject(err))
zipfile.on('entry', async entry => {
if (/\/$/.test(entry.fileName)) zipfile.readEntry()
else {
const type = path.dirname(entry.fileName)
const data = await getFile(entry, zipfile)
const obj = JSON.parse(data)
if (!results[type]) results[type] = []
results[type].push(obj)
zipfile.readEntry()
}
})
})
})
}
......@@ -5,7 +5,7 @@ const
const setup = async function (app) {
app.use(async (req, res, next) => {
if (req.method.toLowerCase() === 'post') {
if (req.body) {
if (req.body && req.headers.authorization) {
const profile = await axios.get(`${config.auth.jwt.issuer}userinfo`, {
headers: {
Authorization: req.headers.authorization
......
......@@ -43,7 +43,7 @@ class Service extends TinyEmitter {
}
if (allowed) items.push(entry)
}
this._response(req, res, { items })
return this._response(req, res, { items })
}
async getHandler (req, res) {
......@@ -64,9 +64,9 @@ class Service extends TinyEmitter {
const instance = new this.ModelConstructor(result, `${req.params.id}`)
return this._response(req, res, instance)
}
this._errorResponse(res, 403)
return this._errorResponse(res, 403)
}
else this._errorResponse(res, 404)
else return this._errorResponse(res, 404)
}
async postHandler (req, res) {
......@@ -83,7 +83,7 @@ class Service extends TinyEmitter {
const instance = new this.ModelConstructor(data),
result = await this.client.create(instance, req.params)
instance.populate(result)
this._response(req, res, instance)
return this._response(req, res, instance)
}
async putHandler (req, res) {
......@@ -96,7 +96,7 @@ class Service extends TinyEmitter {
await this.client.update(req.params.id, instance, req.params)
return this._response(req, res, instance)
}
else this._errorResponse(res, 404)
else return this._errorResponse(res, 404)
}
async patchHandler (req, res) {
......@@ -108,7 +108,7 @@ class Service extends TinyEmitter {
await this.client.update(req.params.id, instance, req.params)
return this._response(req, res, instance)
}
else this._errorResponse(res, 404)
else return this._errorResponse(res, 404)
}
async deleteHandler (req, res) {
......@@ -119,17 +119,19 @@ class Service extends TinyEmitter {
return this._response(req, res, existing)
}
}
else this._errorResponse(res, 404)
else return this._errorResponse(res, 404)
}
_response (req, res, data = {}) {
this.emit('message', { method: req.method, id: data.id })
if (typeof res === 'function') res({ data })
else if (typeof res === 'undefined') return Promise.resolve({ data })
else send(res, 200, data)
}
_errorResponse (res, code, message = undefined) {
if (typeof res === 'function') res({ error: true, code })
else if (typeof res === 'undefined') return Promise.resolve({ error: true, code })
else send(res, code, message)
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment