Lime Web Components API Documentation - v7.4.0
    Preparing search index...

    Interface LimeFile

    Represents a file attached to a LimeObject

    LimeFile provides metadata and URLs for files that are stored as attachments on Lime objects. This interface is typically used when working with file properties on LimeObjects, allowing components to display file information and provide download or viewing capabilities.

    Files can be accessed in different ways depending on the use case - for direct download, inline viewing, editing, or PDF conversion.

    import type { LimeFile } from '@limetech/lime-web-components';
    import {
    PlatformServiceName,
    createLogger,
    } from '@limetech/lime-web-components';

    const logger = createLogger('my-component');
    const repository = platform.get(PlatformServiceName.LimeObjectRepository);
    const deal = repository.getObject('deal', 1234);

    // Every file sits on its own file property, so there is no list to read
    const attachments = ['contract', 'quote', 'drawing']
    .map((property) => deal?.getFile(property))
    .filter((file): file is LimeFile => !!file);

    for (const file of attachments) {
    logger.info(`${file.filename} is ${file.size} bytes`, {
    download: file.getUrl('download'),
    });
    }
    interface LimeFile {
        extension: string;
        filename: string;
        id?: number;
        size: number;
        getUrl(type: LimeFileUrlType): string | undefined;
    }
    Index

    Properties

    Methods

    Properties

    extension: string

    The file extension without the leading dot

    The extension is extracted from the filename and provided separately for convenience when filtering or categorizing files.

    import type { LimeFile } from '@limetech/lime-web-components';
    import { PlatformServiceName } from '@limetech/lime-web-components';

    const IMAGE_EXTENSIONS = new Set(['jpg', 'jpeg', 'png', 'gif']);

    function isImage(file: LimeFile): boolean {
    return IMAGE_EXTENSIONS.has(file.extension.toLowerCase());
    }

    const repository = platform.get(PlatformServiceName.LimeObjectRepository);
    const attachment = repository.getObject('deal', 1234)?.getFile('drawing');

    const imageUrl =
    attachment && isImage(attachment) ? attachment.getUrl('view') : undefined;
    filename: string

    The name of the file including its extension

    This is the original filename as it was uploaded or created, including the file extension.

    import {
    PlatformServiceName,
    createLogger,
    } from '@limetech/lime-web-components';

    const logger = createLogger('my-component');
    const repository = platform.get(PlatformServiceName.LimeObjectRepository);
    const contract = repository.getObject('deal', 1234)?.getFile('contract');

    if (contract) {
    logger.info(contract.filename); // "annual-report.pdf"
    }
    id?: number

    The unique identifier of the file in the Lime database

    import { PlatformServiceName } from '@limetech/lime-web-components';

    const repository = platform.get(PlatformServiceName.LimeObjectRepository);
    const contract = repository.getObject('deal', 1234)?.getFile('contract');

    // The id is optional, so fall back to the filename when it is missing
    const key = contract?.id ?? contract?.filename;
    size: number

    The size of the file in bytes

    Use this to display file size information to users or to implement file size validation.

    import type { LimeFile } from '@limetech/lime-web-components';
    import {
    PlatformServiceName,
    createLogger,
    } from '@limetech/lime-web-components';

    const logger = createLogger('my-component');

    function formatSize(file: LimeFile): string {
    const megabytes = file.size / (1024 * 1024);

    return `${megabytes.toFixed(2)} MB`;
    }

    const repository = platform.get(PlatformServiceName.LimeObjectRepository);
    const contract = repository.getObject('deal', 1234)?.getFile('contract');

    if (contract) {
    logger.info(`File size: ${formatSize(contract)}`);
    }
    import {
    PlatformServiceName,
    createLogger,
    } from '@limetech/lime-web-components';

    const MAX_SIZE = 10 * 1024 * 1024;

    const logger = createLogger('my-component');
    const repository = platform.get(PlatformServiceName.LimeObjectRepository);
    const contract = repository.getObject('deal', 1234)?.getFile('contract');

    if (contract && contract.size > MAX_SIZE) {
    logger.error('File too large', undefined, {
    filename: contract.filename,
    size: contract.size,
    max: MAX_SIZE,
    });
    }

    Methods

    • Get a URL for accessing the file in different ways

      Different URL types are available depending on how you want to access the file. Some URL types may return undefined if not supported for the specific file type.

      Parameters

      Returns string | undefined

      The URL as a string, or undefined if the URL type is not available for this file.

      import type { LimeFile } from '@limetech/lime-web-components';
      import { PlatformServiceName } from '@limetech/lime-web-components';

      // A pdf url only exists when conversion is available for the file type
      function getPreviewUrl(file: LimeFile): string | undefined {
      return file.getUrl('pdf') ?? file.getUrl('view');
      }

      const repository = platform.get(PlatformServiceName.LimeObjectRepository);
      const contract = repository.getObject('deal', 1234)?.getFile('contract');
      const previewUrl = contract && getPreviewUrl(contract);

      if (previewUrl) {
      window.open(previewUrl);
      }