File size: 2,128 Bytes
f0743f4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | const { FileSources, FileContext } = require('librechat-data-provider');
/**
* Determines the appropriate file storage strategy based on file type and configuration.
*
* @param {AppConfig} appConfig - App configuration object containing fileStrategy and fileStrategies
* @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
* @returns {string} Storage strategy to use (e.g., FileSources.local, 's3', 'azure')
*
* @example
* // Legacy single strategy
* getFileStrategy({ fileStrategy: 's3' }) // Returns 's3'
*
* @example
* // Granular strategies
* getFileStrategy(
* {
* fileStrategy: 's3',
* fileStrategies: { avatar: FileSources.local, document: 's3' }
* },
* { isAvatar: true }
* ) // Returns FileSources.local
*/
function getFileStrategy(appConfig, { isAvatar = false, isImage = false, context = null } = {}) {
// Fallback to legacy single strategy if no granular config
if (!appConfig?.fileStrategies) {
return appConfig.fileStrategy || FileSources.local; // Default to FileSources.local if undefined
}
const strategies = appConfig.fileStrategies;
const defaultStrategy = strategies.default || appConfig.fileStrategy || FileSources.local;
// Priority order for strategy selection:
// 1. Specific file type strategy
// 2. Default strategy from fileStrategies
// 3. Legacy fileStrategy
// 4. FileSources.local as final fallback
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;
}
return selectedStrategy || FileSources.local; // Final fallback to FileSources.local
}
module.exports = { getFileStrategy };
|