working upload

This commit is contained in:
Ben 2025-05-26 11:39:57 +02:00
parent 94c504067d
commit b13089c0b2
No known key found for this signature in database
GPG Key ID: 0F16E812E736C24B
2 changed files with 13 additions and 11 deletions

View File

@ -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;
};

View File

@ -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);