UNPKG

@ethersphere/swarm-cli

Version:
166 lines (165 loc) 6.97 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Download = void 0; const bee_js_1 = require("@ethersphere/bee-js"); const chalk_1 = __importDefault(require("chalk")); const fs_1 = __importDefault(require("fs")); const furious_commander_1 = require("furious-commander"); const path_1 = require("path"); const process_1 = require("process"); const utils_1 = require("../../utils"); const bzz_address_1 = require("../../utils/bzz-address"); const root_command_1 = require("../root-command"); class Download extends root_command_1.RootCommand { constructor() { super(...arguments); Object.defineProperty(this, "name", { enumerable: true, configurable: true, writable: true, value: 'download' }); Object.defineProperty(this, "description", { enumerable: true, configurable: true, writable: true, value: 'Download manifest content to a folder' }); Object.defineProperty(this, "address", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "bzzUrl", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "destination", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "stdout", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "act", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "actTimestamp", { enumerable: true, configurable: true, writable: true, value: void 0 }); // required if act is true Object.defineProperty(this, "actHistoryAddress", { enumerable: true, configurable: true, writable: true, value: void 0 }); // required if act is true Object.defineProperty(this, "actPublisher", { enumerable: true, configurable: true, writable: true, value: void 0 }); } async run() { super.init(); // can be already set from other command if (!this.address) { this.address = await (0, bzz_address_1.makeBzzAddress)(this.bee, this.bzzUrl); } const node = await bee_js_1.MantarayNode.unmarshal(this.bee, this.address.hash); await node.loadRecursively(this.bee); const nodes = node.collect().filter(x => x.fullPathString.startsWith(this.address.path || '')); if (nodes.length === 0) { this.console.error('No files found under the given path'); (0, process_1.exit)(1); } if (this.stdout && nodes.length > 1) { this.stdout = false; } for (const node of nodes) { await this.downloadNode(node, this.address); } } async downloadNode(node, address) { if (!this.stdout && !this.quiet) { if (this.curl) { this.console.log(chalk_1.default.dim(node.fullPathString)); } else { process.stdout.write(chalk_1.default.dim(node.fullPathString)); } } const parsedForkPath = (0, path_1.parse)(node.fullPathString); const data = await this.bee.downloadData(node.targetAddress); if (this.stdout) { process.stdout.write(data.toUtf8()); return; } const destination = this.destination || address.hash; const destinationFolder = (0, path_1.join)(destination, parsedForkPath.dir); if (!(0, utils_1.directoryExists)(destinationFolder)) { await fs_1.default.promises.mkdir(destinationFolder, { recursive: true }); } if (!this.stdout && !this.quiet && !this.curl) { process.stdout.write(' ' + chalk_1.default.green('OK') + '\n'); } await fs_1.default.promises.writeFile((0, path_1.join)(destination, node.fullPathString), data.toUint8Array()); } } __decorate([ (0, furious_commander_1.Argument)({ key: 'address', description: 'Manifest reference (with optional path)', required: true }), __metadata("design:type", String) ], Download.prototype, "bzzUrl", void 0); __decorate([ (0, furious_commander_1.Argument)({ key: 'destination', description: 'Destination folder' }), __metadata("design:type", String) ], Download.prototype, "destination", void 0); __decorate([ (0, furious_commander_1.Option)({ key: 'stdout', type: 'boolean', description: 'Print to stdout (single files only)' }), __metadata("design:type", Boolean) ], Download.prototype, "stdout", void 0); __decorate([ (0, furious_commander_1.Option)({ key: 'act', type: 'boolean', description: 'Download with ACT', default: false }), __metadata("design:type", Boolean) ], Download.prototype, "act", void 0); __decorate([ (0, furious_commander_1.Option)({ key: 'act-timestamp', type: 'string', description: 'ACT history timestamp', default: '1' }), __metadata("design:type", String) ], Download.prototype, "actTimestamp", void 0); __decorate([ (0, furious_commander_1.Option)({ key: 'act-history-address', type: 'string', description: 'ACT history address', required: { when: 'act' } }), __metadata("design:type", String) ], Download.prototype, "actHistoryAddress", void 0); __decorate([ (0, furious_commander_1.Option)({ key: 'act-publisher', type: 'string', description: 'ACT publisher', required: { when: 'act' } }), __metadata("design:type", String) ], Download.prototype, "actPublisher", void 0); exports.Download = Download;