fix PDF import

This commit is contained in:
Lukas F. Hartmann 2018-05-03 15:35:51 +02:00
parent fb8d3ac654
commit 5f4d41f3a4

View File

@ -1,5 +1,6 @@
"use strict"; "use strict";
var config = require('config'); var config = require('config');
const os = require('os');
const db = require('../../models/db'); const db = require('../../models/db');
const Sequelize = require('sequelize'); const Sequelize = require('sequelize');
const Op = Sequelize.Op; const Op = Sequelize.Op;
@ -422,17 +423,16 @@ router.post('/:id/artifacts-pdf', function(req, res, next) {
var withZones = (req.query.zones) ? req.query.zones == "true" : false; var withZones = (req.query.zones) ? req.query.zones == "true" : false;
var fileName = (req.query.filename || "upload.bin").replace(/[^a-zA-Z0-9\.]/g, ''); var fileName = (req.query.filename || "upload.bin").replace(/[^a-zA-Z0-9\.]/g, '');
var localFilePath = "/tmp/" + fileName; var localFilePath = os.tmpdir() + "/" + fileName;
var writeStream = fs.createWriteStream(localFilePath); var writeStream = fs.createWriteStream(localFilePath);
var stream = req.pipe(writeStream); var stream = req.pipe(writeStream);
req.on('end', function() { req.on('end', function() {
var rawName = fileName.slice(0, fileName.length - 4); var rawName = fileName.slice(0, fileName.length - 4);
var outputFolder = "/tmp/" + rawName; var outputFolder = os.tmpdir() + "/" + rawName;
var rights = 777;
fs.mkdir(outputFolder, function(db) { fs.mkdir(outputFolder, function(err) {
var images = outputFolder + "/" + rawName + "-page-%03d.jpeg"; var images = outputFolder + "/" + rawName + "-page-%03d.jpeg";
// FIXME not portable // FIXME not portable
@ -455,7 +455,7 @@ router.post('/:id/artifacts-pdf', function(req, res, next) {
var number = parseInt(baseName.slice(baseName.length - 3, baseName.length), 10); var number = parseInt(baseName.slice(baseName.length - 3, baseName.length), 10);
gm(localFilePath).size(function(err, size) { gm(localFilePath).size((err, size) => {
var w = 350; var w = 350;
var h = w; var h = w;
@ -463,57 +463,52 @@ router.post('/:id/artifacts-pdf', function(req, res, next) {
var y = startY + ((parseInt(((number - 1) / limitPerRow), 10) + 1) * w); var y = startY + ((parseInt(((number - 1) / limitPerRow), 10) + 1) * w);
var userId; var userId;
if (req.user) if (req.user) userId = req.user._id;
userId = req.user._id;
var a = new Artifact({ var a = db.Artifact.create({
_id: uuidv4(),
mime: "image/jpg", mime: "image/jpg",
space_id: req.space._id, space_id: req.space._id,
user_id: userId, user_id: userId,
editor_name: req.guest_name, editor_name: req.guest_name,
board: {
w: w, w: w,
h: h, h: h,
x: x, x: x,
y: y, y: y,
z: (number + (count + 100)) z: (number + (count + 100))
} }).then(a => {
});
payloadConverter.convert(a, fileName, localfilePath, (error, artifact) => { payloadConverter.convert(a, fileName, localfilePath, (error, artifact) => {
if (error) res.status(400).json(error); if (error) res.status(400).json(error);
else { else {
if (withZones) { if (withZones) {
var zone = new Artifact({ var zone = {
_id: uuidv4(),
mime: "x-spacedeck/zone", mime: "x-spacedeck/zone",
description: "Zone " + (number), description: "Zone " + (number),
space_id: req.space._id, space_id: req.space._id,
user_id: userId, user_id: userId,
editor_name: req.guest_name, editor_name: req.guest_name,
board: {
w: artifact.board.w + 20, w: artifact.board.w + 20,
h: artifact.board.h + 40, h: artifact.board.h + 40,
x: x - 10, x: x - 10,
y: y - 30, y: y - 30,
z: number z: number,
},
style: {
order: number, order: number,
valign: "middle", valign: "middle",
align: "center" align: "center"
} };
});
zone.save((err) => { db.Artifact.create(zone,((z) => {
redis.sendMessage("create", "Artifact", zone.toJSON(), req.channelId); redis.sendMessage("create", "Artifact", zone.toJSON(), req.channelId);
cb(null, [artifact, zone]); cb(null, [artifact, zone]);
}); }));
} else { } else {
cb(null, [artifact]); cb(null, [artifact]);
} }
} }
}); });
});
}); });