diff --git a/src/services/dataService.js b/src/services/dataService.js index abc1a00..ff9ded2 100644 --- a/src/services/dataService.js +++ b/src/services/dataService.js @@ -1,6 +1,5 @@ import { Codex } from "@codex-storage/sdk-js"; import { NodeUploadStategy } from "@codex-storage/sdk-js/node"; -import mime from "mime-types"; import path from "path"; import fs from "fs"; @@ -11,19 +10,22 @@ export class DataService { upload = async (filePath) => { const data = this.getCodexData(); + + // We can use mime util to determine the content type of the file. But Codex will reject some + // mimetypes. So we set it to octet-stream always. + const contentType = "application/octet-stream"; + const filename = path.basename(filePath); - const contentType = mime.lookup(filePath) || "application/octet-stream"; const fileData = fs.readFileSync(filePath); - const strategy = new NodeUploadStategy(fileData, { - filename: filename, - mimetype: contentType, - }); + const metadata = { filename: filename, mimetype: contentType }; + + const strategy = new NodeUploadStategy(fileData, metadata); const uploadResponse = data.upload(strategy); const res = await uploadResponse.result; if (res.error) { - throw new Exception(res.data); + throw new Error(res.data); } return res.data; }; diff --git a/src/ui/dataMenu.js b/src/ui/dataMenu.js index 456b97b..7ea0d2c 100644 --- a/src/ui/dataMenu.js +++ b/src/ui/dataMenu.js @@ -10,12 +10,12 @@ export class DataMenu { "⚠️ Codex does not encrypt files. Anything uploaded will be available publicly on testnet.", ); - const filePath = this.ui.askPrompt("Enter the file path"); + const filePath = await this.ui.askPrompt("Enter the file path"); if (!this.fs.isFile(filePath)) { this.ui.showErrorMessage("File not found"); } else { try { - const cid = this.dataService.upload(filePath); + const cid = await this.dataService.upload(filePath); this.ui.showInfoMessage(`Upload successful.\n CID: '${cid}'`); } catch (exception) { this.ui.showErrorMessage("Error during upload: " + exception); @@ -24,10 +24,10 @@ export class DataMenu { }; performDownload = async () => { - const cid = this.ui.askPrompt("Enter the CID"); + const cid = await this.ui.askPrompt("Enter the CID"); if (cid.length < 1) return; try { - const filename = this.dataService.download(cid); + const filename = await this.dataService.download(cid); this.ui.showInfoMessage(`Download successful.\n File: '${filename}'`); } catch (exception) { this.ui.showErrorMessage("Error during download: " + exception);