Member Junction
    Preparing search index...

    Server-side file storage engine providing high-level operations for uploading, downloading, and managing files in MJ Storage.

    Follows the containment pattern (like AIEngine wraps AIEngineBase):

    Client-side code should use FileStorageEngineBase from @memberjunction/core-entities for metadata-only access (accounts, providers, lookups).

    Usage:

    import { FileStorageEngine } from '@memberjunction/storage';

    const engine = FileStorageEngine.Instance;
    await engine.Config(false, contextUser);

    // Upload a file
    const result = await engine.UploadFile({
    content: Buffer.from(base64Data, 'base64'),
    fileName: 'report.pdf',
    mimeType: 'application/pdf',
    contextUser
    });

    // Get a driver for direct operations
    const driver = await engine.GetDriver(accountId, contextUser);
    const objects = await driver.ListObjects('/');

    Hierarchy (View Summary)

    Index

    Constructors

    Accessors

    • get GlobalKey(): string

      Returns string

    Methods

    • Configures the engine by loading the underlying metadata cache and any server-specific state. Safe to call multiple times — uses cached data unless forceRefresh is true. Concurrent callers share a single loading promise to avoid redundant work.

      Parameters

      Returns Promise<void>

    • Returns an authenticated storage driver for a given account.

      Checks the pre-initialized driver cache first (populated during Config()). If the account wasn't cached (e.g., it failed during Config or was added after), falls back to on-demand initialization.

      This handles:

      • Looking up the account and provider from cached metadata
      • Decrypting credentials via the Credential Engine
      • Setting up OAuth token refresh callbacks for providers like Box

      Parameters

      • accountId: string

        The FileStorageAccount ID to get a driver for

      • contextUser: UserInfo

        User context for credential decryption (used for on-demand init)

      Returns Promise<FileStorageBase>

      An initialized, ready-to-use FileStorageBase driver

      Error if the account is not found or driver initialization fails

    • The Global Object Store is a place to store global objects that need to be shared across the application. Depending on the execution environment, this could be the window object in a browser, or the global object in a node environment, or something else in other contexts. The key here is that in some cases static variables are not truly shared because it is possible that a given class might have copies of its code in multiple paths in a deployed application. This approach ensures that no matter how many code copies might exist, there is only one instance of the object in question by using the Global Object Store.

      Returns GlobalObjectStore

    • Initializes storage drivers for all active accounts and caches them. Called automatically during Config(). Can also be called independently to re-initialize drivers without reloading metadata (e.g., after credential rotation). Accounts that fail to initialize are logged and skipped — they will fall back to on-demand initialization when GetDriver() is called.

      Returns Promise<void>

    • Resolves a storage account to use for file operations.

      Resolution logic:

      1. If accountId is provided, returns that specific account
      2. Otherwise, returns the first active account
      3. If no active accounts exist, returns the first account regardless of active status

      Parameters

      • OptionalaccountId: string

        Optional explicit account ID

      Returns StorageAccountWithProvider

      The resolved account with provider, or null if no accounts are configured

    • Uploads a file to MJ Storage and creates an MJ: Files entity record.

      This is the primary high-level method for storing files. It handles:

      1. Resolving which storage account to use
      2. Initializing an authenticated driver
      3. Uploading the file content
      4. Creating the MJ: Files database record

      Parameters

      • options: UploadFileOptions

        Upload options (content, fileName, mimeType, contextUser, etc.)

      Returns Promise<UploadFileResult>

      Upload result containing the file ID, storage path, and account/provider used

      Error if no storage accounts are configured or the upload/save fails

      const result = await FileStorageEngine.Instance.UploadFile({
      content: Buffer.from(base64Data, 'base64'),
      fileName: 'report.pdf',
      mimeType: 'application/pdf',
      contextUser,
      storageAccountId: resolvedAccountId // optional
      });
      console.log('Created file:', result.FileID);
    • Returns the singleton instance of the class. If the instance does not exist, it is created and stored in the Global Object Store. If className is provided it will be used as part of the key in the Global Object Store, otherwise the actual class name will be used. NOTE: the class name used by default is the lowest level of the object hierarchy, so if you have a class that extends another class, the lowest level class name will be used.

      Type Parameters

      Parameters

      • this: new () => T
      • OptionalclassName: string

      Returns T