UNPKG

@cocalc/project

Version:
190 lines (174 loc) 5.53 kB
// Generated by CoffeeScript 2.5.1 (function() { //######################################################################## // This file is part of CoCalc: Copyright © 2020 Sagemath, Inc. // License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details //######################################################################## var async, child_process, common, ensureContainingDirectoryExists, fs, message, misc, misc_node, temp, winston, writeFile; fs = require('fs'); temp = require('temp'); child_process = require('child_process'); async = require('async'); winston = require('./logger').getLogger('read-write-files'); message = require('@cocalc/util/message'); misc_node = require('@cocalc/backend/misc_node'); misc = require('@cocalc/util/misc'); common = require('./common'); ensureContainingDirectoryExists = require('@cocalc/backend/misc/ensure-containing-directory-exists').default; writeFile = require("fs/promises").writeFile; exports.read_file_from_project = function(socket, mesg) { var archive, data, id, is_dir, path, stats; //dbg = (m) -> winston.debug("read_file_from_project(path='#{mesg.path}'): #{m}") //dbg() data = void 0; path = misc_node.abspath(mesg.path); is_dir = void 0; id = void 0; archive = void 0; stats = void 0; return async.series([ function(cb) { //dbg("Determine whether the path '#{path}' is a directory or file.") return fs.stat(path, function(err, _stats) { if (err) { return cb(err); } else { stats = _stats; is_dir = stats.isDirectory(); return cb(); } }); }, function(cb) { // make sure the file isn't too large return cb(common.check_file_size(stats.size)); }, function(cb) { var args, split, target; if (is_dir) { if (mesg.archive !== 'tar.bz2') { cb("The only supported directory archive format is tar.bz2"); return; } target = temp.path({ suffix: '.' + mesg.archive }); //dbg("'#{path}' is a directory, so archive it to '#{target}', change path, and read that file") archive = mesg.archive; if (path[path.length - 1] === '/') { // common nuisance with paths to directories path = path.slice(0, path.length - 1); } split = misc.path_split(path); path = target; // same patterns also in project.coffee (TODO) args = ["--exclude=.sagemathcloud*", '--exclude=.forever', '--exclude=.node*', '--exclude=.npm', '--exclude=.sage', '-jcf', target, split.tail]; //dbg("tar #{args.join(' ')}") return child_process.execFile('tar', args, { cwd: split.head }, function(err, stdout, stderr) { if (err) { winston.debug(`Issue creating tarball: ${err}, ${stdout}, ${stderr}`); return cb(err); } else { return cb(); } }); } else { //dbg("It is a file.") return cb(); } }, function(cb) { //dbg("Read the file into memory.") return fs.readFile(path, function(err, _data) { data = _data; return cb(err); }); }, function(cb) { id = misc_node.uuidsha1(data); //dbg("sha1 hash = '#{id}'") return cb(); }, function(cb) { //dbg("send the file as a blob back to the hub.") socket.write_mesg('json', message.file_read_from_project({ id: mesg.id, data_uuid: id, archive: archive })); socket.write_mesg('blob', { uuid: id, blob: data, ttlSeconds: mesg.ttlSeconds }); return cb(); } ], function(err) { if (err && err !== 'file already known') { socket.write_mesg('json', message.error({ id: mesg.id, error: err })); } if (is_dir) { return fs.exists(path, function(exists) { if (exists) { //dbg("It was a directory, so remove the temporary archive '#{path}'.") return fs.unlink(path); } }); } }); }; exports.write_file_to_project = function(socket, mesg) { var data_uuid, path, write_file; //dbg = (m) -> winston.debug("write_file_to_project(path='#{mesg.path}'): #{m}") //dbg() data_uuid = mesg.data_uuid; path = misc_node.abspath(mesg.path); // Listen for the blob containing the actual content that we will write. write_file = async function(type, value) { var err; if (type === 'blob' && value.uuid === data_uuid) { socket.removeListener('mesg', write_file); try { await ensureContainingDirectoryExists(path); await writeFile(path, value.blob); return socket.write_mesg('json', message.file_written_to_project({ id: mesg.id })); } catch (error) { err = error; return socket.write_mesg('json', message.error({ id: mesg.id, error: err })); } } }; return socket.on('mesg', write_file); }; }).call(this); //# sourceMappingURL=read_write_files.js.map