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/database/filesystem-bucket.coffee
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
###
7
Filesystem based bucket -- same api as gcloud bucket.
8
9
Motivation: in KuCalc we use gcsfuse to just mount the smc-blobs bucket
10
defined in smc-gcloud.
11
###
12
13
fs = require('fs')
14
15
{defaults} = misc = require('@cocalc/util/misc')
16
required = defaults.required
17
18
exports.filesystem_bucket = (opts) ->
19
opts = defaults opts,
20
name : required
21
if not opts.name
22
throw Error("bucket name must be specified")
23
return new FilesystemBucket(opts.name)
24
25
class FilesystemBucket
26
constructor: (@path) ->
27
28
blob_path: (name) =>
29
return "#{@path}/#{name}"
30
31
write: (opts) =>
32
opts = defaults opts,
33
name : required
34
content : required
35
cb : required
36
fs.writeFile(@blob_path(opts.name), opts.content, opts.cb)
37
38
39
read: (opts) =>
40
opts = defaults opts,
41
name : required
42
cb : required
43
fs.readFile(@blob_path(opts.name), opts.cb)
44
45
delete: (opts) =>
46
opts = defaults opts,
47
name : required
48
cb : undefined
49
fs.unlink(@blob_path(opts.name), (err)->opts.cb?(err))
50