Browse Source

storage-node: cli: Move out the common code.

Shamil Gadelshin 4 years ago
parent
commit
044b66bc91

+ 2 - 2
storage-node/packages/cli/src/cli.ts

@@ -152,8 +152,8 @@ const commands = {
     // })
   },
 
-  upload: async (api: any, filePath: string) => {
-    await uploadCommand.run(api, filePath)
+  upload: async (api: any, filePath: string, dataObjectTypeId: string) => {
+    await uploadCommand.run(api, filePath, dataObjectTypeId)
   },
   // needs to be updated to take a content id and resolve it a potential set
   // of providers that has it, and select one (possibly try more than one provider)

+ 16 - 0
storage-node/packages/cli/src/commands/common.ts

@@ -0,0 +1,16 @@
+// Composes an asset URL and logs it to console.
+import chalk from "chalk";
+
+// Creates the Colossus asset URL and logs it.
+export function createAndLogAssetUrl(url: string, contentId: string) : string {
+    const assetUrl = `${url}/asset/v0/${contentId}`;
+    console.log(chalk.yellow('Generated asset URL:', assetUrl));
+
+    return assetUrl;
+}
+
+// Shows the error message and ends the process with error code.
+export function fail(message: string) {
+    console.log(chalk.red(message));
+    process.exit(1);
+}

+ 3 - 11
storage-node/packages/cli/src/commands/download.ts

@@ -1,18 +1,12 @@
 import axios from "axios";
 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 !== "";
 }
 
-function createAndLogAssetUrl(url: string, contentId: string) : string {
-    const assetUrl = `${url}/asset/v0/${contentId}`;
-    console.log(chalk.yellow('Asset URL:', assetUrl));
-
-    return assetUrl;
-}
-
 function showDownloadUsage() {
     console.log(chalk.yellow(`
         Invalid parameters for 'download' command.
@@ -32,9 +26,7 @@ export async function run(api: any, url: string, contentId: string, filePath: st
     // Create file write stream and set error handler.
     const writer = fs.createWriteStream(filePath)
         .on('error', (err) => {
-            const message = `File write failed: ${err}`;
-            console.log(chalk.red(message));
-            process.exit(1);
+            fail(`File write failed: ${err}`);
         });
 
     // Request file download.
@@ -54,6 +46,6 @@ export async function run(api: any, url: string, contentId: string, filePath: st
             });
         });
     } catch (err) {
-        console.log(chalk.red(`Colossus request failed: ${err.message}`));
+        fail(`Colossus request failed: ${err.message}`);
     }
 }

+ 2 - 8
storage-node/packages/cli/src/commands/head.ts

@@ -1,17 +1,11 @@
 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 !=="";
 }
 
-function createAndLogAssetUrl(url: string, contentId: string) : string {
-    const assetUrl = `${url}/asset/v0/${contentId}`;
-    console.log(chalk.yellow('Asset URL:', assetUrl));
-
-    return assetUrl;
-}
-
 function showHeadUsage() {
     console.log(chalk.yellow(`
         Invalid parameters for 'head' command.
@@ -33,7 +27,7 @@ export async function run(api: any, url: string, contentId: string) {
         console.log(chalk.green(`Content length: ${response.headers['content-length']}`));
 
     } catch (err) {
-        console.log(chalk.red(`Colossus request failed: ${err.message}`));
+        fail(`Colossus request failed: ${err.message}`);
     }
 }
 

+ 13 - 19
storage-node/packages/cli/src/commands/upload.ts

@@ -8,15 +8,12 @@ import { Option } from '@polkadot/types/codec';
 import Debug from "debug";
 const debug = Debug('joystream:storage-cli:upload');
 
+import {fail, createAndLogAssetUrl} from "./common";
 
 // Defines maximum content length for the assets (files). Limits the upload.
 const MAX_CONTENT_LENGTH = 500 * 1024 * 1024; // 500Mb
 
-function fail(message: string) {
-    console.log(chalk.red(message));
-    process.exit(1);
-}
-
+// Reads the file from the filesystem and computes IPFS hash.
 async function computeIpfsHash(filePath: string): Promise<string> {
     const file = fs.createReadStream(filePath)
         .on('error', (err) => {
@@ -26,12 +23,13 @@ async function computeIpfsHash(filePath: string): Promise<string> {
     return await ipfsHash.of(file);
 }
 
+// Read the file size from the file system.
 function getFileSize(filePath: string): number {
     const stats = fs.statSync(filePath);
     return stats.size;
 }
 
-//
+// Defines the necessary parameters for the AddContent runtime tx.
 interface AddContentParams {
     accountId: string,
     ipfsCid: string,
@@ -41,14 +39,6 @@ interface AddContentParams {
     memberId: number
 }
 
-// Composes an asset URL and logs it to console.
-function createAndLogAssetUrl(url: string, contentId: string) : string {
-    const assetUrl = `${url}/asset/v0/${contentId}`;
-    console.log(chalk.yellow('Generated asset URL:', assetUrl));
-
-    return assetUrl;
-}
-
 function getAccountId(api: any) : string {
     const ALICE_URI = '//Alice'
 
@@ -56,10 +46,14 @@ function getAccountId(api: any) : string {
 }
 
 // Creates parameters for the AddContent runtime tx.
-async function getAddContentParams(api: any, filePath: string): Promise<AddContentParams> {
-    const dataObjectTypeId = 1;
+async function getAddContentParams(api: any, filePath: string, dataObjectTypeIdString: string): Promise<AddContentParams> {
     const memberId = 0; // alice
     const accountId = getAccountId(api)
+    let dataObjectTypeId: number = parseInt(dataObjectTypeIdString);
+
+    if (isNaN(dataObjectTypeId)) {
+        fail(`Cannot parse dataObjectTypeId: ${dataObjectTypeIdString}`);
+    }
 
     return {
         accountId,
@@ -94,9 +88,9 @@ async function createContent(api: any, p: AddContentParams) : Promise<DataObject
 }
 
 // Command executor.
-export async function run(api: any, filePath: string){
-    let addContentParams = await getAddContentParams(api, filePath);
-    debug(`AddContent Tx params: ${addContentParams}`); // debug
+export async function run(api: any, filePath: string, dataObjectTypeId: string){
+    let addContentParams = await getAddContentParams(api, filePath, dataObjectTypeId);
+    debug(`AddContent Tx params: ${JSON.stringify(addContentParams)}`); // debug
     debug(`Decoded CID: ${ContentId.decode(addContentParams.contentId).toString()}`);
 
     let dataObject = await createContent(api, addContentParams);