Skip to content

Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active

1.3 KB
Raw Blame
import { logger } from '@librechat/data-schemas';
import { FileSources } from 'librechat-data-provider';
import type { SaveURLResult } from './types';
import { extractKeyFromS3Url, getStorageMetadataForKey } from './s3/crud';

type StorageMetadataInput = {
  filepath?: string | null;
  source?: string | null;
  storageKey?: string | null;
  storageRegion?: string | null;
};

export function getStorageMetadata({
  filepath,
  source,
  storageKey,
  storageRegion,
}: StorageMetadataInput): Pick<SaveURLResult, 'storageKey' | 'storageRegion'> {
  if (source !== FileSources.s3 && source !== FileSources.cloudfront) {
    return {};
  }

  let key = storageKey ?? '';
  if (!key && filepath) {
    try {
      key = extractKeyFromS3Url(filepath);
    } catch (error) {
      logger.warn('[getStorageMetadata] Unable to extract storage key from filepath', error);
      return {};
    }
  }
  if (!key) {
    return {};
  }

  const metadata = getStorageMetadataForKey(key);
  if (!metadata.storageKey) {
    return {};
  }

  if (storageRegion && metadata.storageRegion && storageRegion !== metadata.storageRegion) {
    logger.warn(
      `[getStorageMetadata] storageRegion "${storageRegion}" does not match key region "${metadata.storageRegion}".`,
    );
  }

  return {
    ...metadata,
    ...(storageRegion ? { storageRegion } : {}),
  };
}