2025-08-26 12:10:18 -04:00
|
|
|
const { FileSources, FileContext } = require('librechat-data-provider');
|
2025-07-25 16:50:18 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Determines the appropriate file storage strategy based on file type and configuration.
|
|
|
|
|
*
|
2025-08-26 12:10:18 -04:00
|
|
|
* @param {AppConfig} appConfig - App configuration object containing fileStrategy and fileStrategies
|
2025-07-25 16:50:18 +02:00
|
|
|
* @param {Object} options - File context options
|
|
|
|
|
* @param {boolean} options.isAvatar - Whether this is an avatar upload
|
|
|
|
|
* @param {boolean} options.isImage - Whether this is an image upload
|
|
|
|
|
* @param {string} options.context - File context from FileContext enum
|
2025-08-26 12:10:18 -04:00
|
|
|
* @returns {string} Storage strategy to use (e.g., FileSources.local, 's3', 'azure')
|
2025-07-25 16:50:18 +02:00
|
|
|
*
|
|
|
|
|
* @example
|
|
|
|
|
* // Legacy single strategy
|
|
|
|
|
* getFileStrategy({ fileStrategy: 's3' }) // Returns 's3'
|
|
|
|
|
*
|
|
|
|
|
* @example
|
|
|
|
|
* // Granular strategies
|
|
|
|
|
* getFileStrategy(
|
|
|
|
|
* {
|
|
|
|
|
* fileStrategy: 's3',
|
2025-08-26 12:10:18 -04:00
|
|
|
* fileStrategies: { avatar: FileSources.local, document: 's3' }
|
2025-07-25 16:50:18 +02:00
|
|
|
* },
|
|
|
|
|
* { isAvatar: true }
|
2025-08-26 12:10:18 -04:00
|
|
|
* ) // Returns FileSources.local
|
2025-07-25 16:50:18 +02:00
|
|
|
*/
|
2025-08-26 12:10:18 -04:00
|
|
|
function getFileStrategy(appConfig, { isAvatar = false, isImage = false, context = null } = {}) {
|
2025-07-25 16:50:18 +02:00
|
|
|
// Fallback to legacy single strategy if no granular config
|
2025-08-26 12:10:18 -04:00
|
|
|
if (!appConfig?.fileStrategies) {
|
|
|
|
|
return appConfig.fileStrategy || FileSources.local; // Default to FileSources.local if undefined
|
2025-07-25 16:50:18 +02:00
|
|
|
}
|
|
|
|
|
|
2025-08-26 12:10:18 -04:00
|
|
|
const strategies = appConfig.fileStrategies;
|
|
|
|
|
const defaultStrategy = strategies.default || appConfig.fileStrategy || FileSources.local;
|
2025-07-25 16:50:18 +02:00
|
|
|
|
|
|
|
|
// Priority order for strategy selection:
|
|
|
|
|
// 1. Specific file type strategy
|
|
|
|
|
// 2. Default strategy from fileStrategies
|
|
|
|
|
// 3. Legacy fileStrategy
|
2025-08-26 12:10:18 -04:00
|
|
|
// 4. FileSources.local as final fallback
|
2025-07-25 16:50:18 +02:00
|
|
|
|
|
|
|
|
let selectedStrategy;
|
|
|
|
|
|
|
|
|
|
if (isAvatar || context === FileContext.avatar) {
|
|
|
|
|
selectedStrategy = strategies.avatar || defaultStrategy;
|
|
|
|
|
} else if (isImage || context === FileContext.image_generation) {
|
|
|
|
|
selectedStrategy = strategies.image || defaultStrategy;
|
|
|
|
|
} else {
|
|
|
|
|
// All other files (documents, attachments, etc.)
|
|
|
|
|
selectedStrategy = strategies.document || defaultStrategy;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-26 12:10:18 -04:00
|
|
|
return selectedStrategy || FileSources.local; // Final fallback to FileSources.local
|
2025-07-25 16:50:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports = { getFileStrategy };
|