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/hub/blobs.coffee
Views: 687
#########################################################################1# This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2# License: MS-RSL – see LICENSE.md for details3#########################################################################45# Blobs67winston = require('./logger').getLogger('blobs')89misc_node = require('@cocalc/backend/misc_node')10misc = require('@cocalc/util/misc')11{defaults, required} = misc12{MAX_BLOB_SIZE} = require('@cocalc/util/db-schema/blobs')1314# save a blob in the blobstore database with given misc_node.uuidsha1 hash.15exports.save_blob = (opts) ->16opts = defaults opts,17uuid : undefined # uuid=sha1-based from blob; actually *required*, but instead of a traceback, get opts.cb(err)18blob : undefined # actually *required*, but instead of a traceback, get opts.cb(err)19ttl : undefined # object in blobstore will have *at least* this ttl in seconds;20# if there is already something, in blobstore with longer ttl, we leave it; undefined = infinite ttl21check : true # if true, return an error (via cb) if misc_node.uuidsha1(opts.blob) != opts.uuid.22# This is a check against bad user-supplied data.23project_id : undefined # also required24database : required25cb : required # cb(err, ttl actually used in seconds); ttl=0 for infinite ttl2627dbg = (m) -> winston.debug("save_blob(uuid=#{opts.uuid}): #{m}")28dbg()2930err = undefined3132if not opts.blob?33err = "save_blob: UG -- error in call to save_blob (uuid=#{opts.uuid}); received a save_blob request with undefined blob"3435else if not opts.uuid?36err = "save_blob: BUG -- error in call to save_blob; received a save_blob request without corresponding uuid"3738else if not opts.project_id?39err = "save_blob: BUG -- error in call to save_blob; received a save_blob request without corresponding project_id"4041else if opts.blob.length > MAX_BLOB_SIZE42err = "save_blob: blobs are limited to #{misc.human_readable_size(MAX_BLOB_SIZE)} and you just tried to save one of size #{opts.blob.length/1000000}MB"4344else if opts.check and opts.uuid != misc_node.uuidsha1(opts.blob)45err = "save_blob: uuid=#{opts.uuid} must be derived from the Sha1 hash of blob, but it is not (possible malicious attack)"4647if err48dbg(err)49opts.cb(err)50return5152# Store the blob in the database, if it isn't there already.53opts.database.save_blob54uuid : opts.uuid55blob : opts.blob56ttl : opts.ttl57project_id : opts.project_id58cb : (err, ttl) =>59if err60dbg("failed to store blob -- #{err}")61else62dbg("successfully stored blob")63opts.cb(err, ttl)646566