@cocalc/server
Version:
CoCalc server functionality: functions used by either the hub and the next.js server
55 lines • 2.33 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const misc_1 = require("@cocalc/util/misc");
//import { uuidsha1 } from "@cocalc/backend/misc_node";
const { uuidsha1 } = require("@cocalc/backend/misc_node");
const database_1 = require("@cocalc/database");
const async_utils_1 = require("@cocalc/util/async-utils");
const message_1 = require("@cocalc/util/message");
const logger_1 = __importDefault(require("@cocalc/backend/logger"));
const logger = (0, logger_1.default)("project-connection:handle-blob");
// Blobs (e.g., files and images dynamically appearing as output in worksheets) are kept for this
// many seconds before being discarded. If the worksheet is saved (e.g., by a user's autosave),
// then the BLOB is saved indefinitely.
const TTL = 60 * 60 * 24; // 1 day
const MAX_BLOB_SIZE = 15000000;
const MAX_BLOB_SIZE_HUMAN = "15MB";
async function handleBlob({ socket, project_id, uuid, blob, ttlSeconds, }) {
let resp;
try {
await saveBlob({ project_id, uuid, blob });
resp = (0, message_1.save_blob)({ sha1: uuid, ttl: ttlSeconds ?? TTL });
}
catch (err) {
resp = (0, message_1.save_blob)({ sha1: uuid, error: `${err}` });
}
socket.write_mesg("json", resp);
}
exports.default = handleBlob;
async function saveBlob({ project_id, uuid, blob }) {
logger.debug("saving blob in ", project_id, " with uuid ", uuid);
// return ttl in seconds.
if (!(0, misc_1.is_valid_uuid_string)(project_id))
throw Error("project_id is invalid");
if (!(0, misc_1.is_valid_uuid_string)(uuid))
throw Error("uuid is invalid");
if (!blob)
throw Error("blob is required");
if (uuid != uuidsha1(blob)) {
throw Error("uuid must be the sha1-uuid of blob");
}
if (blob.length > MAX_BLOB_SIZE) {
throw Error(`saveBlob: blobs are limited to ${MAX_BLOB_SIZE_HUMAN} and you just tried to save one of size ${blob.length / 1000000}MB`);
}
const database = (0, database_1.db)();
return await (0, async_utils_1.callback2)(database.save_blob, {
uuid,
blob,
ttl: TTL,
project_id,
});
}
//# sourceMappingURL=handle-blob.js.map