bowling-analysis-system
Version:
A comprehensive system for analyzing bowling techniques using video processing and metrics calculation
64 lines (55 loc) • 1.94 kB
JavaScript
/**
* File Loader Processor
*
* Loads files from the filesystem
* @module processors/FileLoaderProcessor
*/
const BaseProcessor = require('../core/BaseProcessor');
const fs = require('fs').promises;
const path = require('path');
/**
* @class FileLoaderProcessor
* @description Processor for loading files from the filesystem
* @extends BaseProcessor
*/
class FileLoaderProcessor extends BaseProcessor {
/**
* Create a new file loader processor
* @param {Object} config - Processor configuration
*/
constructor(config = {}) {
super('fileLoader', config);
this.filePath = config.filePath || config.path;
this.encoding = config.encoding || 'utf8';
}
/**
* Process input by loading a file
* @param {*} input - Input data (usually null or a path override)
* @param {Object} context - Processing context
* @returns {Promise<Object>} Loaded file data
*/
async process(input, context) {
const filePath = this.filePath ||
(input && typeof input === 'string' ? input : null) ||
(context?.pipeline?.options?.input?.filePath) ||
(context?.pipeline?.options?.inputPath);
if (!filePath) {
throw new Error('No file path specified');
}
try {
// Resolve path (support relative paths)
const resolvedPath = path.isAbsolute(filePath) ?
filePath : path.resolve(process.cwd(), filePath);
// Read file contents
const content = await fs.readFile(resolvedPath, this.encoding);
// Parse JSON if it's a JSON file
if (resolvedPath.toLowerCase().endsWith('.json')) {
return JSON.parse(content);
}
return content;
} catch (error) {
throw new Error(`Failed to load file ${filePath}: ${error.message}`);
}
}
}
module.exports = FileLoaderProcessor;