UNPKG

@cocalc/hub

Version:
47 lines 1.88 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const database_1 = require("../database"); const misc_1 = require("@cocalc/util/misc"); const hub_register_1 = require("@cocalc/hub/hub_register"); const async_utils_1 = require("@cocalc/util/async-utils"); const logger_1 = require("@cocalc/hub/logger"); function init(router) { const winston = (0, logger_1.getLogger)("get-blob"); // return uuid-indexed blobs (mainly used for graphics) router.get("/blobs/*", async (req, res) => { winston.debug(`${JSON.stringify(req.query)}, ${req.path}`); const uuid = `${req.query.uuid}`; if (!(0, misc_1.is_valid_uuid_string)(uuid)) { res.status(404).send(`invalid uuid=${uuid}`); return; } if (!(0, hub_register_1.database_is_working)()) { res.status(404).send("can't get blob -- not connected to database"); return; } try { const data = await (0, async_utils_1.callback2)(database_1.database.get_blob, { uuid }); if (data == null) { res.status(404).send(`blob ${uuid} not found`); } else { const filename = req.path.slice(req.path.lastIndexOf("/") + 1); if (req.query.download != null) { // tell browser to download the link as a file instead // of displaying it in browser res.attachment(filename); } else { res.type(filename); } res.send(data); } } catch (err) { winston.error(`internal error ${err} getting blob ${uuid}`); res.status(500).send(`internal error: ${err}`); } }); } exports.default = init; //# sourceMappingURL=blobs.js.map