-
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathcli.ts
More file actions
180 lines (157 loc) · 5.85 KB
/
cli.ts
File metadata and controls
180 lines (157 loc) · 5.85 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/usr/bin/env node
import { Command } from 'commander';
import * as fs from 'fs';
import * as fsp from 'fs/promises';
import * as path from 'path';
import os from 'os';
import { parseImageFile } from './services/metadataEngine';
import { isSupportedMediaFileName } from './utils/mediaTypes.js';
const program = new Command();
program
.name('imagemetahub-cli')
.description('Image MetaHub CLI - Parse AI-generated image metadata')
.version('0.15.0');
type ParseOptions = { json: boolean; pretty: boolean; raw?: boolean; quiet?: boolean };
type IndexOptions = { out: string; recursive: boolean; raw?: boolean; quiet?: boolean; concurrency?: string };
function formatOutput(
result: Awaited<ReturnType<typeof parseImageFile>>,
includeRaw: boolean
) {
const base = {
file: result.file,
format: result.metadata?.generator || null,
raw_source: result.rawSource || null,
sha256: result.sha256,
dimensions: result.dimensions || null,
metadata: result.metadata,
schema_version: result.schema_version,
_telemetry: result._telemetry,
parsed_at: new Date().toISOString(),
errors: result.errors,
} as any;
if (includeRaw) {
base.raw_metadata = result.rawMetadata;
}
return base;
}
function isImageFile(entry: string) {
return isSupportedMediaFileName(entry);
}
async function collectFiles(dir: string, recursive: boolean): Promise<string[]> {
const results: string[] = [];
const entries = await fsp.readdir(dir, { withFileTypes: true });
for (const entry of entries) {
const fullPath = path.join(dir, entry.name);
if (entry.isDirectory() && recursive) {
const nested = await collectFiles(fullPath, recursive);
results.push(...nested);
} else if (entry.isFile() && isImageFile(entry.name)) {
results.push(fullPath);
}
}
return results;
}
program
.command('parse')
.description('Parse metadata from a single media file (image, video, or audio)')
.argument('<file>', 'Media file to parse')
.option('--json', 'Output as JSON', true)
.option('--pretty', 'Pretty-print JSON output', false)
.option('--raw', 'Include raw metadata payload when available', false)
.option('--quiet', 'Suppress informational logs', false)
.action(async (file: string, options: ParseOptions) => {
try {
const filePath = path.resolve(file);
if (!fs.existsSync(filePath)) {
console.error(`Error: File not found: ${filePath}`);
process.exit(1);
}
const result = await parseImageFile(filePath);
const output = formatOutput(result, Boolean(options.raw));
if (options.json) {
console.log(options.pretty ? JSON.stringify(output, null, 2) : JSON.stringify(output));
} else {
console.log(output);
}
} catch (error) {
console.error('Error parsing file:', error);
process.exit(1);
}
});
program
.command('index')
.description('Parse metadata from a directory of images and output JSONL')
.argument('<dir>', 'Directory to scan')
.option('--out <file>', 'Output JSONL file', 'index.jsonl')
.option('--recursive', 'Scan subdirectories recursively', false)
.option('--raw', 'Include raw metadata payload when available', false)
.option('--quiet', 'Suppress informational logs', false)
.option('--concurrency <number>', 'Number of files to process in parallel', undefined)
.action(async (dir: string, options: IndexOptions) => {
try {
const dirPath = path.resolve(dir);
if (!fs.existsSync(dirPath) || !fs.statSync(dirPath).isDirectory()) {
console.error(`Error: Directory not found: ${dirPath}`);
process.exit(1);
}
const files = await collectFiles(dirPath, options.recursive);
const outputPath = path.resolve(options.out);
const outputStream = fs.createWriteStream(outputPath);
let processedCount = 0;
let errorCount = 0;
const cpuCount = Math.max(1, os.cpus().length);
const concurrency = Math.max(1, Math.min(64, Number(options.concurrency || cpuCount) || cpuCount));
if (!options.quiet) {
console.log(`Scanning directory: ${dirPath}`);
console.log(`Output file: ${outputPath}`);
console.log(`Recursive: ${options.recursive}`);
console.log(`Images found: ${files.length}`);
console.log(`Concurrency: ${concurrency}`);
console.log('---');
}
const queue: Promise<void>[] = [];
let index = 0;
const runWorker = async (filePath: string) => {
try {
const result = await parseImageFile(filePath);
const entry = formatOutput(result, Boolean(options.raw));
outputStream.write(JSON.stringify(entry) + '\n');
processedCount++;
if (result.errors?.length) {
errorCount += 1;
}
if (!options.quiet && processedCount % 100 === 0) {
console.log(`Processed ${processedCount}/${files.length} images...`);
}
} catch (err) {
console.error(`Error parsing ${filePath}:`, err);
errorCount++;
}
};
while (index < files.length || queue.length > 0) {
while (index < files.length && queue.length < concurrency) {
const filePath = files[index++];
const p = runWorker(filePath).finally(() => {
const idx = queue.indexOf(p);
if (idx !== -1) queue.splice(idx, 1);
});
queue.push(p);
}
if (queue.length > 0) {
await Promise.race(queue);
}
}
outputStream.end();
if (!options.quiet) {
console.log('---');
console.log('✅ Indexing complete!');
console.log(` Processed: ${processedCount} images`);
console.log(` Errors: ${errorCount}`);
console.log(` Output: ${outputPath}`);
}
} catch (error) {
console.error('Error indexing directory:', error);
process.exit(1);
}
});
program.parse();