CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/jupyter/blobs/get.ts
Views: 687
1
/*
2
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
import { BlobStoreDisk } from "./disk";
7
import { BlobStoreSqlite } from "./sqlite";
8
import { blobstore } from "@cocalc/backend/data";
9
import Logger from "@cocalc/backend/logger";
10
11
const winston = Logger("jupyter-blobs:get");
12
13
// TODO: are these the only base64 encoded types that jupyter kernels return?
14
export const BASE64_TYPES = [
15
"image/png",
16
"image/jpeg",
17
"application/pdf",
18
"base64",
19
] as const;
20
21
let blob_store_sqlite: BlobStoreSqlite | undefined = undefined;
22
let blob_store_disk: BlobStoreDisk | undefined = undefined;
23
24
// IMPORTANT: You *must* call the async function get_blob_store
25
// once before calling get_blob_store_sync, or it will throw an
26
// error breaking everything.
27
// The point of get_blob_store_sync is that it is a non async function
28
// that returns the blob store.
29
export function get_blob_store_sync(): BlobStoreSqlite | BlobStoreDisk {
30
switch (blobstore as string) {
31
case "sqlite":
32
if (blob_store_sqlite == null) {
33
throw Error(
34
"must call get_blob_store first to initialize the Jupyter blobstore before fetching it synchronously",
35
);
36
}
37
return blob_store_sqlite;
38
case "disk":
39
if (blob_store_disk == null) {
40
throw Error(
41
"must call get_blob_store first to initialize the Jupyter blobstore before fetching it synchronously",
42
);
43
}
44
return blob_store_disk;
45
default:
46
throw Error(`unknown blobstore type ${blobstore}`);
47
}
48
}
49
50
export async function get_blob_store() {
51
winston.info(`blobstore type: ${blobstore}`);
52
if (blobstore === "sqlite") {
53
if (blob_store_sqlite != null) return blob_store_sqlite;
54
blob_store_sqlite = new BlobStoreSqlite();
55
return blob_store_sqlite;
56
} else if (blobstore === "disk") {
57
if (blob_store_disk != null) return blob_store_disk;
58
const disk = new BlobStoreDisk();
59
await disk.init();
60
blob_store_disk = disk;
61
return blob_store_disk;
62
} else {
63
const msg = `Unknown blobstore type: ${blobstore}`;
64
winston.error(msg);
65
throw new Error(msg);
66
}
67
}
68
69