-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcsv-handler.ts
More file actions
53 lines (46 loc) · 1.47 KB
/
Copy pathcsv-handler.ts
File metadata and controls
53 lines (46 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import type { ArtifactHandler, ArtifactMetadata, ArtifactViewDescriptor } from "./registry.js";
export class CsvArtifactHandler implements ArtifactHandler {
readonly type = "csv";
readonly displayName = "CSV Table";
readonly extensions = [".csv"];
async extractMetadata(fileBuffer: Buffer, filePath: string): Promise<ArtifactMetadata | null> {
try {
// Just verify it's readable
fileBuffer.toString("utf-8");
return {
type: "csv",
path: filePath,
description: "CSV Table",
mimeType: "text/csv",
size: fileBuffer.length,
details: {},
};
} catch (err) {
console.error(`[CsvArtifactHandler] Failed to extract metadata from ${filePath}:`, err);
return null;
}
}
validate(artifact: Record<string, unknown>): string[] {
const errors: string[] = [];
if (!artifact.path) {
errors.push("CSV artifact must have a 'path' field");
} else {
const p = artifact.path as string;
const validExts = this.extensions;
if (!validExts.some((ext) => p.toLowerCase().endsWith(ext))) {
errors.push(`CSV path must end with ${validExts.join(", ")}, got: "${p}"`);
}
}
return errors;
}
getViewDescriptor(metadata: ArtifactMetadata): ArtifactViewDescriptor | null {
return {
viewer: "csv",
label: "View Table",
icon: "table",
config: {
url: metadata.path, // The frontend will fetch the CSV from the URL
},
};
}
}