From 462e9edaab7e1bd0354dca673565853ec3755f63 Mon Sep 17 00:00:00 2001 From: mntmn Date: Mon, 8 Jan 2018 15:57:59 +0100 Subject: [PATCH] first importer WIP --- helpers/importer.js | 103 ++++++++++++++++++++++++++++++++++ package.json | 1 + routes/api/space_artifacts.js | 4 +- routes/api/users.js | 10 ++++ 4 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 helpers/importer.js diff --git a/helpers/importer.js b/helpers/importer.js new file mode 100644 index 0000000..90eb2a9 --- /dev/null +++ b/helpers/importer.js @@ -0,0 +1,103 @@ +'use strict'; + +const extract = require('extract-zip') +const config = require('config') +const fs = require('fs') +const path = require('path') + +require('../models/schema') + +module.exports = { + importZIP: function(user, zipPath) { + + // 1. extract zip to local storage folder + // 2. read spaces.json from this folder + // 3. iterate through spaces and read all their artifact jsons + // 4. fixup storage paths + // 5. replace creator id by user._id + + let relativeImportDir = 'import_'+user._id + let importDir = path.resolve(config.get('storage_local_path')+'/'+config.get('storage_bucket')+'/'+relativeImportDir) + + if (!fs.existsSync(importDir)) { + fs.mkdirSync(importDir) + } + + extract(zipPath, {dir: importDir}, function(err) { + if (err) { + console.log(err) + return + } + console.log('[import] extracted to',importDir) + + let spacesJson = fs.readFileSync(importDir+'/spaces.json') + let spaces = JSON.parse(spacesJson) + var homeFolderId = null + + console.log('[import] spaces:',spaces.length) + + // pass 1: find homefolder + for (var i=0; i { "nickname": 1, "email": 1 }).exec((err, user) => { - a['user'] = user.toObject(); + if (user) { + a['user'] = user.toObject(); + } cb(err, a); }); } else { diff --git a/routes/api/users.js b/routes/api/users.js index 8140bcc..d8e995c 100644 --- a/routes/api/users.js +++ b/routes/api/users.js @@ -5,6 +5,7 @@ require('../../models/schema'); var mailer = require('../../helpers/mailer'); var uploader = require('../../helpers/uploader'); +var importer = require('../../helpers/importer'); var bcrypt = require('bcryptjs'); var crypo = require('crypto'); @@ -467,4 +468,13 @@ router.post('/:user_id/confirm', function(req, res, next) { res.sendStatus(201); }); +router.get('/:user_id/import', function(req, res, next) { + if (req.query.zip) { + res.send("importing"); + importer.importZIP(req.user, req.query.zip); + } else { + res.sendStatus(400); + } +}); + module.exports = router;