Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/jupyter/blobs/get.ts
Views: 687
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45import { BlobStoreDisk } from "./disk";6import { BlobStoreSqlite } from "./sqlite";7import { blobstore } from "@cocalc/backend/data";8import Logger from "@cocalc/backend/logger";910const winston = Logger("jupyter-blobs:get");1112// TODO: are these the only base64 encoded types that jupyter kernels return?13export const BASE64_TYPES = [14"image/png",15"image/jpeg",16"application/pdf",17"base64",18] as const;1920let blob_store_sqlite: BlobStoreSqlite | undefined = undefined;21let blob_store_disk: BlobStoreDisk | undefined = undefined;2223// IMPORTANT: You *must* call the async function get_blob_store24// once before calling get_blob_store_sync, or it will throw an25// error breaking everything.26// The point of get_blob_store_sync is that it is a non async function27// that returns the blob store.28export function get_blob_store_sync(): BlobStoreSqlite | BlobStoreDisk {29switch (blobstore as string) {30case "sqlite":31if (blob_store_sqlite == null) {32throw Error(33"must call get_blob_store first to initialize the Jupyter blobstore before fetching it synchronously",34);35}36return blob_store_sqlite;37case "disk":38if (blob_store_disk == null) {39throw Error(40"must call get_blob_store first to initialize the Jupyter blobstore before fetching it synchronously",41);42}43return blob_store_disk;44default:45throw Error(`unknown blobstore type ${blobstore}`);46}47}4849export async function get_blob_store() {50winston.info(`blobstore type: ${blobstore}`);51if (blobstore === "sqlite") {52if (blob_store_sqlite != null) return blob_store_sqlite;53blob_store_sqlite = new BlobStoreSqlite();54return blob_store_sqlite;55} else if (blobstore === "disk") {56if (blob_store_disk != null) return blob_store_disk;57const disk = new BlobStoreDisk();58await disk.init();59blob_store_disk = disk;60return blob_store_disk;61} else {62const msg = `Unknown blobstore type: ${blobstore}`;63winston.error(msg);64throw new Error(msg);65}66}676869