Browse Source

storage-node: cli: Convert head and download commands to classes.

Shamil Gadelshin 4 years ago
parent
commit
c00c371bc6

+ 9 - 5
storage-node/packages/cli/src/cli.ts

@@ -25,8 +25,8 @@ import _ from "lodash"
 
 // Commands
 import * as dev from "./commands/dev"
-import * as headCommand from "./commands/head";
-import * as downloadCommand from "./commands/download";
+import {HeadCommand} from "./commands/head";
+import {DownloadCommand} from "./commands/download";
 import * as uploadCommand from "./commands/upload";
 
 
@@ -140,12 +140,16 @@ const commands = {
   // of providers that has it, and select one (possibly try more than one provider)
   // to fetch it from the get api url of a provider..
   download: async (api: any, url: string, contentId: string, filePath: string) => {
-    await downloadCommand.run(api, url, contentId, filePath);
+    let downloadCmd = new DownloadCommand(api, url, contentId, filePath);
+
+    await downloadCmd.run();
   },
   // Shows asset information derived from request headers.
   // Accepts colossus URL and content ID.
-  head: async (api: any, url: string, contentId: string) => {
-    await headCommand.run(api, url, contentId);
+  head: async (api: any, storageNodeUrl: string, contentId: string) => {
+    let headCmd = new HeadCommand(api, storageNodeUrl, contentId);
+
+    await headCmd.run();
   }
 }
 

+ 51 - 37
storage-node/packages/cli/src/commands/download.ts

@@ -3,49 +3,63 @@ import chalk from "chalk"
 import fs from "fs";
 import {fail, createAndLogAssetUrl} from "./common";
 
-function validateDownloadParameters(url: string, contentId: string, filePath: string) : boolean {
-    return url && url !== "" && contentId && contentId !=="" && filePath && filePath !== "";
-}
+export class DownloadCommand {
+    private readonly api: any;
+    private readonly storageNodeUrl: string;
+    private readonly contentId: string;
+    private readonly filePath: string;
+
+    constructor(api: any, storageNodeUrl: string, contentId: string, filePath: string) {
+        this.api = api;
+        this.storageNodeUrl = storageNodeUrl;
+        this.contentId = contentId;
+        this.filePath = filePath;
+    }
 
-function showDownloadUsage() {
-    console.log(chalk.yellow(`
+    validateDownloadParameters(url: string, contentId: string, filePath: string): boolean {
+        return url && url !== "" && contentId && contentId !== "" && filePath && filePath !== "";
+    }
+
+    showDownloadUsage() {
+        console.log(chalk.yellow(`
         Invalid parameters for 'download' command.
         Usage:   storage-cli download colossusURL contentID filePath
         Example: storage-cli download http://localhost:3001 0x7a6ba7e9157e5fba190dc146fe1baa8180e29728a5c76779ed99655500cff795 ./movie.mp4
       `));
-}
-
-export async function run(api: any, url: string, contentId: string, filePath: string){
-    // Create, validate and show parameters.
-    if (!validateDownloadParameters(url, contentId, filePath)) {
-        return showDownloadUsage();
     }
-    const assetUrl = createAndLogAssetUrl(url, contentId);
-    console.log(chalk.yellow('File path:', filePath));
-
-    // Create file write stream and set error handler.
-    const writer = fs.createWriteStream(filePath)
-        .on('error', (err) => {
-            fail(`File write failed: ${err}`);
-        });
-
-    // Request file download.
-    try {
-        const response = await axios({
-            url: assetUrl,
-            method: 'GET',
-            responseType: 'stream'
-        });
-
-        response.data.pipe(writer);
-
-        return new Promise((resolve) => {
-            writer.on('finish', () => {
-                console.log("File downloaded.")
-                resolve();
+
+    async run() {
+        // Create, validate and show parameters.
+        if (!this.validateDownloadParameters(this.storageNodeUrl, this.contentId, this.filePath)) {
+            return this.showDownloadUsage();
+        }
+        const assetUrl = createAndLogAssetUrl(this.storageNodeUrl, this.contentId);
+        console.log(chalk.yellow('File path:', this.filePath));
+
+        // Create file write stream and set error handler.
+        const writer = fs.createWriteStream(this.filePath)
+            .on('error', (err) => {
+                fail(`File write failed: ${err}`);
+            });
+
+        // Request file download.
+        try {
+            const response = await axios({
+                url: assetUrl,
+                method: 'GET',
+                responseType: 'stream'
+            });
+
+            response.data.pipe(writer);
+
+            return new Promise((resolve) => {
+                writer.on('finish', () => {
+                    console.log("File downloaded.")
+                    resolve();
+                });
             });
-        });
-    } catch (err) {
-        fail(`Colossus request failed: ${err.message}`);
+        } catch (err) {
+            fail(`Colossus request failed: ${err.message}`);
+        }
     }
 }

+ 29 - 18
storage-node/packages/cli/src/commands/head.ts

@@ -2,32 +2,43 @@ import axios from "axios";
 import chalk from "chalk"
 import {fail, createAndLogAssetUrl} from "./common";
 
-function validateHeadParameters(url: string, contentId: string) : boolean {
-    return url && url !== "" && contentId && contentId !=="";
-}
+export class HeadCommand {
+    private readonly api: any;
+    private readonly storageNodeUrl: string;
+    private readonly contentId: string;
+
+    constructor(api: any, storageNodeUrl: string, contentId: string) {
+        this.api = api;
+        this.storageNodeUrl = storageNodeUrl;
+        this.contentId = contentId;
+    }
+
+    validateHeadParameters(url: string, contentId: string): boolean {
+        return url && url !== "" && contentId && contentId !== "";
+    }
 
-function showHeadUsage() {
-    console.log(chalk.yellow(`
+    showHeadUsage() {
+        console.log(chalk.yellow(`
         Invalid parameters for 'head' command.
         Usage:   storage-cli head colossusURL contentID
         Example: storage-cli head http://localhost:3001 0x7a6ba7e9157e5fba190dc146fe1baa8180e29728a5c76779ed99655500cff795
       `));
-}
-
-export async function run(api: any, url: string, contentId: string) {
-    if (!validateHeadParameters(url, contentId)){
-        return showHeadUsage();
     }
-    const assetUrl = createAndLogAssetUrl(url, contentId);
 
-    try {
-        const response = await axios.head(assetUrl);
+    async run() {
+        if (!this.validateHeadParameters(this.storageNodeUrl, this.contentId)) {
+            return this.showHeadUsage();
+        }
+        const assetUrl = createAndLogAssetUrl(this.storageNodeUrl, this.contentId);
 
-        console.log(chalk.green(`Content type: ${response.headers['content-type']}`));
-        console.log(chalk.green(`Content length: ${response.headers['content-length']}`));
+        try {
+            const response = await axios.head(assetUrl);
 
-    } catch (err) {
-        fail(`Colossus request failed: ${err.message}`);
+            console.log(chalk.green(`Content type: ${response.headers['content-type']}`));
+            console.log(chalk.green(`Content length: ${response.headers['content-length']}`));
+
+        } catch (err) {
+            fail(`Colossus request failed: ${err.message}`);
+        }
     }
 }
-