You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
cvm/routes/disk-images.js

71 lines
1.7 KiB
JavaScript

'use strict';
const Promise = require("bluebird");
const checkit = require("checkit");
const debounce = require("debounce");
const validatorAdd = require("../lib/form-validators/disk-images/add");
module.exports = function({db, imageStore}) {
let router = require("express-promise-router")();
router.get("/", (req, res) => {
return Promise.try(() => {
return db("images");
}).then((results) => {
res.render("disk-images/list", {
images: results
});
});
});
router.get("/add", (req, res) => {
res.render("disk-images/add");
});
router.post("/add", (req, res) => {
return Promise.try(() => {
return validatorAdd.run(req.body);
}).then((result) => {
/* FIXME: Only allow 'local' for administrators! */
if (req.body.source === "http") {
return Promise.try(() => {
return imageStore.addFromUrl(req.body.url);
}).then((downloadTracker) => {
downloadTracker.on("progress", debounce((progress, description) => {
console.log("Download progress:", (Math.round(progress * 10000) / 100), "%");
}, 200));
return downloadTracker.await();
});
} else if (req.body.source === "local") {
return imageStore.addFromPath(req.body.path);
}
}).then((imageId) => {
let source;
if (req.body.source === "http") {
source = req.body.url;
} else if (req.body.source === "local") {
source = req.body.path;
}
return db("images").insert({
userId: 0,
fileId: imageId,
name: req.body.name,
description: req.body.description,
sourceType: req.body.source,
source: source,
imageType: "disk",
public: true,
isInstallMedium: true
});
}).then(() => {
res.redirect("/disk-images");
});
});
return router;
}