@visionfi/server-cli
Version:
Command-line interface for VisionFI Server SDK
83 lines (82 loc) • 2.84 kB
JavaScript
/**
* Document management commands for VisionFI CLI
* Copyright (c) 2024-2025 VisionFI. All Rights Reserved.
*/
import { createClient } from '../utils/config.js';
import { Display } from '../utils/display.js';
import * as fs from 'fs';
import * as path from 'path';
/**
* Add documents to a package
*/
export async function addDocuments(packageId, options) {
const spinner = Display.spinner('Adding documents to package...');
try {
const client = createClient();
// Read files and convert to base64
const documents = await Promise.all(options.file.map(async (filePath) => {
const absolutePath = path.resolve(filePath);
if (!fs.existsSync(absolutePath)) {
throw new Error(`File not found: ${filePath}`);
}
const fileBuffer = fs.readFileSync(absolutePath);
const base64 = fileBuffer.toString('base64');
const fileName = path.basename(filePath);
return {
fileName,
fileBase64: base64,
mimeType: getMimeType(fileName)
};
}));
const response = await client.packages.documents.add(packageId, {
files: documents
});
spinner.succeed(`Added ${documents.length} document(s) to package ${packageId}`);
if (response.documents) {
Display.table(response.documents.map((doc) => ({
'Document ID': doc.documentUuid,
'File Name': doc.fileName,
'Status': doc.status
})));
}
}
catch (error) {
spinner.fail('Failed to add documents');
Display.error(error.message);
process.exit(1);
}
}
/**
* Delete a document from a package
*/
export async function deleteDocument(packageId, documentId) {
const spinner = Display.spinner('Deleting document...');
try {
const client = createClient();
await client.packages.documents.delete(packageId, documentId);
spinner.succeed(`Deleted document ${documentId} from package ${packageId}`);
}
catch (error) {
spinner.fail('Failed to delete document');
Display.error(error.message);
process.exit(1);
}
}
/**
* Helper function to determine MIME type from file extension
*/
function getMimeType(fileName) {
const ext = path.extname(fileName).toLowerCase();
const mimeTypes = {
'.pdf': 'application/pdf',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.jpeg': 'image/jpeg',
'.tiff': 'image/tiff',
'.tif': 'image/tiff',
'.doc': 'application/msword',
'.docx': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'.txt': 'text/plain'
};
return mimeTypes[ext] || 'application/octet-stream';
}