2025-05-26 11:22:29 +02:00
|
|
|
import { Codex } from "@codex-storage/sdk-js";
|
|
|
|
|
import { NodeUploadStategy } from "@codex-storage/sdk-js/node";
|
|
|
|
|
import path from "path";
|
|
|
|
|
import fs from "fs";
|
|
|
|
|
|
|
|
|
|
export class DataService {
|
|
|
|
|
constructor(configService) {
|
|
|
|
|
this.configService = configService;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
upload = async (filePath) => {
|
|
|
|
|
const data = this.getCodexData();
|
2025-05-26 11:39:57 +02:00
|
|
|
|
|
|
|
|
// 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";
|
|
|
|
|
|
2025-05-26 11:22:29 +02:00
|
|
|
const filename = path.basename(filePath);
|
|
|
|
|
const fileData = fs.readFileSync(filePath);
|
|
|
|
|
|
2025-05-26 11:39:57 +02:00
|
|
|
const metadata = { filename: filename, mimetype: contentType };
|
|
|
|
|
|
2025-05-26 12:51:16 +02:00
|
|
|
const strategy = new NodeUploadStategy(fileData, metadata);
|
2025-05-26 11:22:29 +02:00
|
|
|
const uploadResponse = data.upload(strategy);
|
|
|
|
|
const res = await uploadResponse.result;
|
|
|
|
|
|
|
|
|
|
if (res.error) {
|
2025-05-26 11:39:57 +02:00
|
|
|
throw new Error(res.data);
|
2025-05-26 11:22:29 +02:00
|
|
|
}
|
|
|
|
|
return res.data;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
download = async (cid) => {
|
|
|
|
|
const data = this.getCodexData();
|
|
|
|
|
const manifest = await data.fetchManifest(cid);
|
|
|
|
|
const filename = this.getFilename(manifest);
|
|
|
|
|
|
|
|
|
|
const response = await data.networkDownloadStream(cid);
|
2025-05-26 13:30:31 +02:00
|
|
|
const fileData = await response.data.text();
|
2025-05-26 11:22:29 +02:00
|
|
|
|
|
|
|
|
fs.writeFileSync(filename, fileData);
|
2025-05-26 13:30:31 +02:00
|
|
|
return filename;
|
2025-05-26 11:22:29 +02:00
|
|
|
};
|
|
|
|
|
|
2025-06-04 13:16:31 +02:00
|
|
|
debugInfo = async () => {
|
|
|
|
|
const debug = this.getCodexDebug();
|
|
|
|
|
const res = await debug.info();
|
|
|
|
|
if (res.error) {
|
|
|
|
|
throw new Error(res.data);
|
|
|
|
|
}
|
|
|
|
|
return res.data;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
localData = async () => {
|
|
|
|
|
const data = this.getCodexData();
|
|
|
|
|
const res = await data.cids();
|
|
|
|
|
if (res.error) {
|
|
|
|
|
throw new Error(res.data);
|
|
|
|
|
}
|
|
|
|
|
return res.data;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
getCodex = () => {
|
2025-05-26 11:22:29 +02:00
|
|
|
const config = this.configService.get();
|
|
|
|
|
const url = `http://localhost:${config.ports.apiPort}`;
|
|
|
|
|
const codex = new Codex(url);
|
2025-06-04 13:16:31 +02:00
|
|
|
return codex;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
getCodexData = () => {
|
|
|
|
|
return this.getCodex().data;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
getCodexDebug = () => {
|
|
|
|
|
return this.getCodex().debug;
|
2025-05-26 11:22:29 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
getFilename = (manifest) => {
|
|
|
|
|
const defaultFilename = "unknown_" + Math.random();
|
|
|
|
|
const filename = manifest?.data?.manifest?.filename;
|
|
|
|
|
|
|
|
|
|
if (filename == undefined || filename.length < 1) return defaultFilename;
|
|
|
|
|
return filename;
|
|
|
|
|
};
|
|
|
|
|
}
|