initial commit
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
name: Build and Push Docker Image
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [main, master]
|
||||
tags: ['v*']
|
||||
pull_request:
|
||||
branches: [main, master]
|
||||
|
||||
env:
|
||||
REGISTRY: ghcr.io
|
||||
IMAGE_NAME: ${{ github.repository }}
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
permissions:
|
||||
contents: read
|
||||
packages: write
|
||||
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Set up Docker Buildx
|
||||
uses: docker/setup-buildx-action@v3
|
||||
|
||||
- name: Log in to Container Registry
|
||||
if: github.event_name != 'pull_request'
|
||||
uses: docker/login-action@v3
|
||||
with:
|
||||
registry: ${{ env.REGISTRY }}
|
||||
username: ${{ github.actor }}
|
||||
password: ${{ secrets.GITHUB_TOKEN }}
|
||||
|
||||
- name: Extract metadata
|
||||
id: meta
|
||||
uses: docker/metadata-action@v5
|
||||
with:
|
||||
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
|
||||
tags: |
|
||||
type=ref,event=branch
|
||||
type=ref,event=pr
|
||||
type=semver,pattern={{version}}
|
||||
type=semver,pattern={{major}}.{{minor}}
|
||||
type=sha
|
||||
|
||||
- name: Build and push
|
||||
uses: docker/build-push-action@v5
|
||||
with:
|
||||
context: .
|
||||
push: ${{ github.event_name != 'pull_request' }}
|
||||
tags: ${{ steps.meta.outputs.tags }}
|
||||
labels: ${{ steps.meta.outputs.labels }}
|
||||
cache-from: type=gha
|
||||
cache-to: type=gha,mode=max
|
||||
@@ -0,0 +1,5 @@
|
||||
node_modules/
|
||||
dist/
|
||||
*.log
|
||||
.env
|
||||
.DS_Store
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
# MetaMesh Plugin: file-info
|
||||
# Extracts basic file information (type, MIME, size)
|
||||
|
||||
FROM node:20-slim AS builder
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
COPY package.json tsconfig.json ./
|
||||
RUN npm install
|
||||
|
||||
COPY src/ ./src/
|
||||
RUN npm run build
|
||||
|
||||
# Production
|
||||
FROM node:20-slim
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
COPY package.json ./
|
||||
RUN npm install --omit=dev
|
||||
|
||||
COPY --from=builder /app/dist ./dist
|
||||
|
||||
EXPOSE 8080
|
||||
|
||||
HEALTHCHECK --interval=30s --timeout=5s --start-period=5s --retries=3 \
|
||||
CMD node -e "require('http').get('http://localhost:8080/health', (r) => process.exit(r.statusCode === 200 ? 0 : 1))"
|
||||
|
||||
CMD ["node", "dist/index.js"]
|
||||
@@ -0,0 +1,65 @@
|
||||
# MetaMesh Plugin: File Info
|
||||
|
||||
A MetaMesh plugin that extracts basic file information including type, MIME type, and size.
|
||||
|
||||
## Description
|
||||
|
||||
This is the first plugin in the processing chain. It extracts fundamental file metadata:
|
||||
|
||||
- **File type detection**: Uses magic bytes (`file-type`) with extension fallback
|
||||
- **MIME type**: Detected from content or extension
|
||||
- **File stats**: Size, name, extension
|
||||
|
||||
## Metadata Fields
|
||||
|
||||
| Field | Description |
|
||||
|-------|-------------|
|
||||
| `fileType` | Category: `video`, `audio`, `document`, `subtitle`, `torrent`, `archive`, `other` |
|
||||
| `mimeType` | MIME type (e.g., `video/mp4`) |
|
||||
| `sizeByte` | File size in bytes |
|
||||
| `fileName` | Base filename |
|
||||
| `extension` | File extension (lowercase) |
|
||||
| `filePath` | Original file path |
|
||||
|
||||
## Dependencies
|
||||
|
||||
- No plugin dependencies (this runs first)
|
||||
|
||||
## Configuration
|
||||
|
||||
No configuration required.
|
||||
|
||||
## API Endpoints
|
||||
|
||||
| Endpoint | Method | Description |
|
||||
|----------|--------|-------------|
|
||||
| `/health` | GET | Health check |
|
||||
| `/manifest` | GET | Plugin manifest |
|
||||
| `/configure` | POST | Update configuration |
|
||||
| `/process` | POST | Process a file |
|
||||
|
||||
## Running Locally
|
||||
|
||||
```bash
|
||||
npm install
|
||||
npm run build
|
||||
npm start
|
||||
```
|
||||
|
||||
## Docker
|
||||
|
||||
```bash
|
||||
docker build -t metamesh-plugin-file-info .
|
||||
docker run -p 8080:8080 metamesh-plugin-file-info
|
||||
```
|
||||
|
||||
## Environment Variables
|
||||
|
||||
| Variable | Default | Description |
|
||||
|----------|---------|-------------|
|
||||
| `PORT` | `8080` | HTTP server port |
|
||||
| `HOST` | `0.0.0.0` | HTTP server host |
|
||||
|
||||
## License
|
||||
|
||||
MIT
|
||||
@@ -0,0 +1,26 @@
|
||||
{
|
||||
"name": "@metazla/plugin-file-info",
|
||||
"version": "1.0.0",
|
||||
"description": "MetaMesh plugin: Extracts basic file information (type, MIME, size)",
|
||||
"type": "module",
|
||||
"main": "dist/index.js",
|
||||
"scripts": {
|
||||
"build": "tsc",
|
||||
"start": "node dist/index.js",
|
||||
"dev": "tsx watch src/index.ts"
|
||||
},
|
||||
"dependencies": {
|
||||
"fastify": "^5.0.0",
|
||||
"file-type": "^19.0.0",
|
||||
"mime-types": "^2.1.35"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/mime-types": "^2.1.4",
|
||||
"@types/node": "^20.0.0",
|
||||
"tsx": "^4.0.0",
|
||||
"typescript": "^5.3.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=20.0.0"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
/**
|
||||
* MetaMesh Plugin: file-info
|
||||
* Entry point
|
||||
*/
|
||||
|
||||
import Fastify from 'fastify';
|
||||
import type {
|
||||
HealthResponse,
|
||||
ProcessRequest,
|
||||
ProcessResponse,
|
||||
CallbackPayload,
|
||||
ConfigureRequest,
|
||||
ConfigureResponse,
|
||||
} from './types.js';
|
||||
import { manifest, process as processFile } from './plugin.js';
|
||||
|
||||
const app = Fastify({ logger: true });
|
||||
let ready = false;
|
||||
let pluginConfig: Record<string, unknown> = {};
|
||||
|
||||
// Health check
|
||||
app.get('/health', async (): Promise<HealthResponse> => ({
|
||||
status: 'healthy',
|
||||
ready,
|
||||
version: manifest.version,
|
||||
}));
|
||||
|
||||
// Manifest
|
||||
app.get('/manifest', async () => manifest);
|
||||
|
||||
// Configure
|
||||
app.post<{ Body: ConfigureRequest }>('/configure', async (request): Promise<ConfigureResponse> => {
|
||||
try {
|
||||
pluginConfig = request.body.config || {};
|
||||
console.log('[file-info] Configuration updated');
|
||||
return { status: 'ok' };
|
||||
} catch (error) {
|
||||
return { status: 'error', error: error instanceof Error ? error.message : String(error) };
|
||||
}
|
||||
});
|
||||
|
||||
// Process
|
||||
app.post<{ Body: ProcessRequest }>('/process', async (request, reply) => {
|
||||
const { taskId, cid, filePath, callbackUrl, metaCoreUrl } = request.body;
|
||||
|
||||
if (!taskId || !cid || !filePath || !callbackUrl || !metaCoreUrl) {
|
||||
return reply.send({ status: 'rejected', error: 'Missing required fields' } as ProcessResponse);
|
||||
}
|
||||
|
||||
// Process in background (don't await)
|
||||
processFile(request.body, async (payload: CallbackPayload) => {
|
||||
try {
|
||||
await fetch(callbackUrl, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(payload),
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('[file-info] Callback error:', error);
|
||||
}
|
||||
}).catch(console.error);
|
||||
|
||||
// Accept immediately
|
||||
return reply.send({ status: 'accepted' } as ProcessResponse);
|
||||
});
|
||||
|
||||
// Start server
|
||||
const port = parseInt(process.env.PORT || '8080', 10);
|
||||
const host = process.env.HOST || '0.0.0.0';
|
||||
|
||||
app.listen({ port, host }).then(() => {
|
||||
ready = true;
|
||||
console.log(`[file-info] Plugin listening on http://${host}:${port}`);
|
||||
});
|
||||
|
||||
// Graceful shutdown
|
||||
process.on('SIGTERM', async () => {
|
||||
ready = false;
|
||||
await app.close();
|
||||
process.exit(0);
|
||||
});
|
||||
@@ -0,0 +1,73 @@
|
||||
/**
|
||||
* meta-core API Client for writing metadata
|
||||
* Fails gracefully if meta-core is unavailable (logs warning, returns without throwing)
|
||||
*/
|
||||
|
||||
export class MetaCoreClient {
|
||||
constructor(private baseUrl: string) {}
|
||||
|
||||
private async safeFetch(url: string, options: RequestInit): Promise<Response | null> {
|
||||
try {
|
||||
return await fetch(url, { ...options, signal: AbortSignal.timeout(5000) });
|
||||
} catch (error) {
|
||||
console.warn(`[MetaCoreClient] Warning: meta-core unavailable at ${this.baseUrl}`);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
async setProperty(hashId: string, key: string, value: string): Promise<void> {
|
||||
const response = await this.safeFetch(`${this.baseUrl}/meta/${hashId}/${key}`, {
|
||||
method: 'PUT',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ value }),
|
||||
});
|
||||
if (response && !response.ok) {
|
||||
console.warn(`[MetaCoreClient] Failed to set property: ${response.status}`);
|
||||
}
|
||||
}
|
||||
|
||||
async getProperty(hashId: string, key: string): Promise<string | null> {
|
||||
const response = await this.safeFetch(`${this.baseUrl}/meta/${hashId}/${key}`, { method: 'GET' });
|
||||
if (!response || response.status === 404) return null;
|
||||
if (!response.ok) return null;
|
||||
const data = await response.json() as { value?: string };
|
||||
return data.value ?? null;
|
||||
}
|
||||
|
||||
async mergeMetadata(hashId: string, metadata: Record<string, string>): Promise<void> {
|
||||
const response = await this.safeFetch(`${this.baseUrl}/meta/${hashId}`, {
|
||||
method: 'PATCH',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(metadata),
|
||||
});
|
||||
if (response && !response.ok) {
|
||||
console.warn(`[MetaCoreClient] Failed to merge metadata: ${response.status}`);
|
||||
}
|
||||
}
|
||||
|
||||
async deleteProperty(hashId: string, key: string): Promise<void> {
|
||||
const response = await this.safeFetch(`${this.baseUrl}/meta/${hashId}/${key}`, { method: 'DELETE' });
|
||||
if (response && !response.ok && response.status !== 404) {
|
||||
console.warn(`[MetaCoreClient] Failed to delete property: ${response.status}`);
|
||||
}
|
||||
}
|
||||
|
||||
async addToSet(hashId: string, key: string, value: string): Promise<void> {
|
||||
const response = await this.safeFetch(`${this.baseUrl}/meta/${hashId}/_add/${key}`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ value }),
|
||||
});
|
||||
if (response && !response.ok) {
|
||||
console.warn(`[MetaCoreClient] Failed to add to set: ${response.status}`);
|
||||
}
|
||||
}
|
||||
|
||||
async getMetadata(hashId: string): Promise<Record<string, string>> {
|
||||
const response = await this.safeFetch(`${this.baseUrl}/meta/${hashId}`, { method: 'GET' });
|
||||
if (!response || response.status === 404) return {};
|
||||
if (!response.ok) return {};
|
||||
const data = await response.json() as { metadata?: Record<string, string> };
|
||||
return data.metadata ?? {};
|
||||
}
|
||||
}
|
||||
+167
@@ -0,0 +1,167 @@
|
||||
/**
|
||||
* File Info Plugin
|
||||
*
|
||||
* Extracts basic file information: type, MIME type, and size.
|
||||
* This is the first plugin in the processing chain.
|
||||
*/
|
||||
|
||||
import { stat } from 'fs/promises';
|
||||
import { basename, extname } from 'path';
|
||||
import { fileTypeFromFile } from 'file-type';
|
||||
import { lookup as mimeTypeLookup } from 'mime-types';
|
||||
import type { PluginManifest, ProcessRequest, CallbackPayload } from './types.js';
|
||||
import { MetaCoreClient } from './meta-core-client.js';
|
||||
|
||||
export const manifest: PluginManifest = {
|
||||
id: 'file-info',
|
||||
name: 'File Information',
|
||||
version: '1.0.0',
|
||||
description: 'Extracts basic file information (type, MIME, size)',
|
||||
author: 'MetaMesh',
|
||||
dependencies: [],
|
||||
priority: 10,
|
||||
color: '#607D8B',
|
||||
defaultQueue: 'fast',
|
||||
timeout: 30000,
|
||||
schema: {
|
||||
privateFilePath: {
|
||||
label: 'Original File Path',
|
||||
type: 'string',
|
||||
readonly: true,
|
||||
hint: 'Physical path on disk (used by Stremio addon and FUSE)',
|
||||
},
|
||||
fileType: {
|
||||
label: 'File Type',
|
||||
type: 'string',
|
||||
readonly: true,
|
||||
},
|
||||
mimeType: {
|
||||
label: 'MIME Type',
|
||||
type: 'string',
|
||||
readonly: true,
|
||||
},
|
||||
sizeByte: {
|
||||
label: 'Size (bytes)',
|
||||
type: 'number',
|
||||
readonly: true,
|
||||
},
|
||||
fileName: {
|
||||
label: 'File Name',
|
||||
type: 'string',
|
||||
readonly: true,
|
||||
},
|
||||
extension: {
|
||||
label: 'Extension',
|
||||
type: 'string',
|
||||
readonly: true,
|
||||
},
|
||||
},
|
||||
config: {},
|
||||
};
|
||||
|
||||
// File type detection based on extension (matching @metazla/filename-tools)
|
||||
// Note: Images are classified as 'document' not 'image' per original design
|
||||
const EXTENSION_MAPPINGS: Record<string, string> = {
|
||||
// Video
|
||||
'mp4': 'video', 'mkv': 'video', 'webm': 'video', 'avi': 'video',
|
||||
'mov': 'video', 'wmv': 'video', 'flv': 'video', 'm4v': 'video',
|
||||
'mpg': 'video', 'mpeg': 'video', '3gp': 'video', '3g2': 'video',
|
||||
'f4v': 'video', 'm2ts': 'video', 'mts': 'video', 'ts': 'video',
|
||||
'vob': 'video', 'ogm': 'video', 'divx': 'video', 'xvid': 'video',
|
||||
// Audio
|
||||
'mp3': 'audio', 'wav': 'audio', 'flac': 'audio', 'aac': 'audio',
|
||||
'ogg': 'audio', 'm4a': 'audio',
|
||||
// Document (includes images per original design)
|
||||
'pdf': 'document', 'doc': 'document', 'docx': 'document',
|
||||
'ppt': 'document', 'pptx': 'document', 'xls': 'document',
|
||||
'xlsx': 'document', 'txt': 'document', 'rtf': 'document',
|
||||
// Images are classified as 'document' per original library
|
||||
'jpg': 'document', 'jpeg': 'document', 'png': 'document',
|
||||
'gif': 'document', 'bmp': 'document', 'tif': 'document',
|
||||
'tiff': 'document', 'svg': 'document', 'webp': 'document',
|
||||
// Archive
|
||||
'zip': 'archive', 'rar': 'archive', '7z': 'archive',
|
||||
'tar': 'archive', 'gz': 'archive', 'bz2': 'archive', 'xz': 'archive',
|
||||
// Subtitles
|
||||
'srt': 'subtitle', 'sub': 'subtitle', 'sbv': 'subtitle', 'vtt': 'subtitle',
|
||||
// Torrent
|
||||
'torrent': 'torrent',
|
||||
};
|
||||
|
||||
function getFileTypeFromExtension(ext: string): string {
|
||||
if (!ext) return 'undefined';
|
||||
const lowerExt = ext.toLowerCase();
|
||||
return EXTENSION_MAPPINGS[lowerExt] || 'other';
|
||||
}
|
||||
|
||||
export async function process(
|
||||
request: ProcessRequest,
|
||||
sendCallback: (payload: CallbackPayload) => Promise<void>
|
||||
): Promise<void> {
|
||||
const startTime = Date.now();
|
||||
const metaCore = new MetaCoreClient(request.metaCoreUrl);
|
||||
|
||||
try {
|
||||
const { cid, filePath } = request;
|
||||
|
||||
// Get file stats
|
||||
const stats = await stat(filePath);
|
||||
|
||||
// Get file name and extension
|
||||
const fileName = basename(filePath);
|
||||
const extension = extname(filePath).slice(1).toLowerCase();
|
||||
|
||||
// Determine file type
|
||||
let fileType = getFileTypeFromExtension(extension);
|
||||
|
||||
// Try to get MIME type from file magic bytes
|
||||
let mimeType: string | undefined;
|
||||
try {
|
||||
const detected = await fileTypeFromFile(filePath);
|
||||
if (detected) {
|
||||
mimeType = detected.mime;
|
||||
}
|
||||
} catch {
|
||||
// Fallback to extension-based lookup
|
||||
}
|
||||
|
||||
// Fallback MIME type lookup by extension
|
||||
if (!mimeType) {
|
||||
mimeType = mimeTypeLookup(extension) || 'application/octet-stream';
|
||||
}
|
||||
|
||||
// Prepare metadata
|
||||
const metadata = {
|
||||
fileType,
|
||||
mimeType,
|
||||
sizeByte: String(stats.size),
|
||||
fileName,
|
||||
extension,
|
||||
filePath, // Store the file path for other plugins/services
|
||||
};
|
||||
|
||||
// Write metadata to meta-core
|
||||
await metaCore.mergeMetadata(cid, metadata);
|
||||
|
||||
const duration = Date.now() - startTime;
|
||||
console.log(`[file-info] Processed ${fileName} in ${duration}ms`);
|
||||
|
||||
await sendCallback({
|
||||
taskId: request.taskId,
|
||||
status: 'completed',
|
||||
duration,
|
||||
metadata, // Include metadata in callback as fallback
|
||||
});
|
||||
} catch (error) {
|
||||
const duration = Date.now() - startTime;
|
||||
const errorMessage = error instanceof Error ? error.message : String(error);
|
||||
console.error(`[file-info] Error:`, errorMessage);
|
||||
|
||||
await sendCallback({
|
||||
taskId: request.taskId,
|
||||
status: 'failed',
|
||||
duration,
|
||||
error: errorMessage,
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
/**
|
||||
* MetaMesh Plugin Types
|
||||
*/
|
||||
|
||||
export interface PluginManifest {
|
||||
id: string;
|
||||
name: string;
|
||||
version: string;
|
||||
description?: string;
|
||||
author?: string;
|
||||
dependencies?: string[];
|
||||
schema?: Record<string, SchemaField>;
|
||||
config?: Record<string, ConfigField>;
|
||||
priority?: number;
|
||||
color?: string;
|
||||
defaultQueue?: 'fast' | 'background';
|
||||
timeout?: number;
|
||||
}
|
||||
|
||||
export interface SchemaField {
|
||||
label: string;
|
||||
type?: 'string' | 'number' | 'boolean' | 'select' | 'text' | 'array' | 'json' | 'cid';
|
||||
readonly?: boolean;
|
||||
hint?: string;
|
||||
}
|
||||
|
||||
export interface ConfigField {
|
||||
type: 'string' | 'number' | 'boolean' | 'select';
|
||||
label?: string;
|
||||
required?: boolean;
|
||||
default?: unknown;
|
||||
secret?: boolean;
|
||||
}
|
||||
|
||||
export interface HealthResponse {
|
||||
status: 'healthy' | 'unhealthy';
|
||||
ready: boolean;
|
||||
version: string;
|
||||
}
|
||||
|
||||
export interface ProcessRequest {
|
||||
taskId: string;
|
||||
cid: string;
|
||||
filePath: string;
|
||||
callbackUrl: string;
|
||||
metaCoreUrl: string;
|
||||
existingMeta?: Record<string, string>;
|
||||
}
|
||||
|
||||
export interface ProcessResponse {
|
||||
status: 'accepted' | 'rejected';
|
||||
error?: string;
|
||||
}
|
||||
|
||||
export interface CallbackPayload {
|
||||
taskId: string;
|
||||
status: 'completed' | 'failed' | 'skipped';
|
||||
duration: number;
|
||||
error?: string;
|
||||
reason?: string;
|
||||
/** Metadata to write (fallback if meta-core unavailable) */
|
||||
metadata?: Record<string, string>;
|
||||
}
|
||||
|
||||
export interface ConfigureRequest {
|
||||
config: Record<string, unknown>;
|
||||
}
|
||||
|
||||
export interface ConfigureResponse {
|
||||
status: 'ok' | 'error';
|
||||
error?: string;
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "ES2022",
|
||||
"module": "NodeNext",
|
||||
"moduleResolution": "NodeNext",
|
||||
"lib": ["ES2022"],
|
||||
"outDir": "./dist",
|
||||
"rootDir": "./src",
|
||||
"strict": true,
|
||||
"esModuleInterop": true,
|
||||
"skipLibCheck": true,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"declaration": true,
|
||||
"sourceMap": true
|
||||
},
|
||||
"include": ["src/**/*"],
|
||||
"exclude": ["node_modules", "dist"]
|
||||
}
|
||||
Reference in New Issue
Block a user