UNPKG

@aj-archipelago/cortex

Version:

Cortex is a GraphQL API for AI. It provides a simple, extensible interface for using AI services from OpenAI, Azure and others.

61 lines (50 loc) 1.83 kB
import { ConversionService } from "./ConversionService.js"; import { getFileStoreMap, setFileStoreMap } from "../redis.js"; import { urlExists } from "../helper.js"; import { gcsUrlExists, uploadChunkToGCS, gcs } from "../blobHandler.js"; import { downloadFile } from "../fileChunker.js"; import { StorageFactory } from "./storage/StorageFactory.js"; import { moveFileToPublicFolder } from "../localFileHandler.js"; import { v4 as uuidv4 } from "uuid"; export class FileConversionService extends ConversionService { constructor(context, useAzure = true) { super(context); this.useAzure = useAzure; this.storageFactory = StorageFactory.getInstance(); } async _getFileStoreMap(key) { return getFileStoreMap(key); } async _setFileStoreMap(key, value) { return setFileStoreMap(key, value); } async _urlExists(url) { return urlExists(url); } async _gcsUrlExists(url) { return gcsUrlExists(url); } async _downloadFile(url, destination) { return downloadFile(url, destination); } async _saveConvertedFile(filePath, requestId, filename = null) { // Generate a fallback requestId if none supplied (e.g. during checkHash calls) const reqId = requestId || uuidv4(); let fileUrl; if (this.useAzure) { // Container parameter is ignored - always uses default container from env var const provider = await this.storageFactory.getAzureProvider(); const result = await provider.uploadFile({}, filePath, reqId, null, filename); fileUrl = result.url; } else { fileUrl = await moveFileToPublicFolder(filePath, reqId); } return { url: fileUrl }; } async _uploadChunkToGCS(filePath, requestId, filename = null) { return uploadChunkToGCS(filePath, requestId, filename); } _isGCSConfigured() { return !!gcs; } }