H5P NodeJS Library
    Preparing search index...

    Implementations need to implement the IContentStorage interface and pass it to the constructor of H5PEditor. It is used to persist content data (semantic data, images, videos etc.) permanently. See the FileContentStorage sample implementation in the examples directory for more details.

    interface IContentStorage {
        addContent(
            metadata: IContentMetadata,
            content: any,
            user: IUser,
            contentId?: string,
        ): Promise<string>;
        addFile(
            contentId: string,
            filename: string,
            readStream: Stream,
            user?: IUser,
        ): Promise<void>;
        contentExists(contentId: string): Promise<boolean>;
        deleteContent(contentId: string, user?: IUser): Promise<void>;
        deleteFile(
            contentId: string,
            filename: string,
            user?: IUser,
        ): Promise<void>;
        fileExists(contentId: string, filename: string): Promise<boolean>;
        getFileStats(
            contentId: string,
            file: string,
            user: IUser,
        ): Promise<IFileStats>;
        getFileStream(
            contentId: string,
            file: string,
            user: IUser,
            rangeStart?: number,
            rangeEnd?: number,
        ): Promise<Readable>;
        getMetadata(contentId: string, user?: IUser): Promise<IContentMetadata>;
        getParameters(contentId: string, user?: IUser): Promise<any>;
        getUsage(
            library: ILibraryName,
        ): Promise<{ asDependency: number; asMainLibrary: number }>;
        listContent(user?: IUser): Promise<string[]>;
        listFiles(contentId: string, user: IUser): Promise<string[]>;
        sanitizeFilename(filename: string): string;
    }

    Implemented by

    Index

    Methods

    • Creates a content object in the repository. Content files (like images) are added to it later with addFile(...). Throws an error if something went wrong. In this case the calling method will remove all traces of the content and all changes are reverted.

      Parameters

      • metadata: IContentMetadata

        The content metadata of the content (= h5p.json)

      • content: any

        the content object (= content/content.json)

      • user: IUser

        The user who owns this object.

      • OptionalcontentId: string

        (optional) The content id to use

      Returns Promise<string>

      The newly assigned content id

    • Adds a content file to an existing content object. The content object has to be created with addContent(...) first.

      Parameters

      • contentId: string

        The id of the content to add the file to

      • filename: string

        The filename; can be a path including subdirectories (e.g. 'images/xyz.png')

      • readStream: Stream

        A readable stream that contains the data

      • Optionaluser: IUser

        (optional) The user who owns this object

      Returns Promise<void>

    • Checks if a piece of content exists in storage.

      Parameters

      • contentId: string

        the content id to check

      Returns Promise<boolean>

      true if the piece of content exists

    • Deletes a content object and all its dependent files from the repository. Throws errors if something goes wrong.

      Parameters

      • contentId: string

        The content id to delete.

      • Optionaluser: IUser

        The user who wants to delete the content

      Returns Promise<void>

    • Deletes a file from a content object.

      Parameters

      • contentId: string

        the content object the file is attached to

      • filename: string

        the file to delete; can be a path including subdirectories (e.g. 'images/xyz.png')

      • Optionaluser: IUser

      Returns Promise<void>

    • Checks if a file exists.

      Parameters

      • contentId: string

        The id of the content to add the file to

      • filename: string

        the filename of the file to get; can be a path including subdirectories (e.g. 'images/xyz.png')

      Returns Promise<boolean>

      true if the file exists

    • Returns information about a content file (e.g. image or video) inside a piece of content.

      Parameters

      • contentId: string

        the id of the content object that the file is attached to

      • file: string

        the filename of the file to get information about

      • user: IUser

        the user who wants to retrieve the content file

      Returns Promise<IFileStats>

    • Returns a readable stream of a content file (e.g. image or video) inside a piece of content

      Parameters

      • contentId: string

        the id of the content object that the file is attached to

      • file: string

        the filename of the file to get; can be a path including subdirectories (e.g. 'images/xyz.png')

      • user: IUser

        the user who wants to retrieve the content file

      • OptionalrangeStart: number

        (optional) the position in bytes at which the stream should start

      • OptionalrangeEnd: number

        (optional) the position in bytes at which the stream should end

      Returns Promise<Readable>

      the stream (that can be used to send the file to the user)

    • Returns the content metadata (=h5p.json) for a content id

      Parameters

      • contentId: string

        the content id for which to retrieve the metadata

      • Optionaluser: IUser

        (optional) the user who wants to access the metadata. If undefined, access must be granted.

      Returns Promise<IContentMetadata>

      the metadata

    • Returns the content object (=content.json) for a content id

      Parameters

      • contentId: string

        the content id for which to retrieve the metadata

      • Optionaluser: IUser

        (optional) the user who wants to access the metadata. If undefined, access must be granted.

      Returns Promise<any>

      the content object

    • Calculates how often a library is in use.

      Parameters

      • library: ILibraryName

        the library for which to calculate usage.

      Returns Promise<{ asDependency: number; asMainLibrary: number }>

      asDependency: how often the library is used as subcontent in content; asMainLibrary: how often the library is used as a main library

    • Lists the content objects in the system (if no user is specified) or owned by the user.

      Parameters

      • Optionaluser: IUser

        (optional) the user who owns the content

      Returns Promise<string[]>

      a list of contentIds

    • Gets the filenames of files added to the content with addFile(...) (e.g. images, videos or other files)

      Parameters

      • contentId: string

        the piece of content

      • user: IUser

        the user who wants to access the piece of content

      Returns Promise<string[]>

      a list of files that are used in the piece of content, e.g. ['images/image1.png', 'videos/video2.mp4', 'file.xyz']

    • Removes invalid characters from filenames and enforces other filename rules required by the storage implementation (e.g. filename length restrictions). Note: file names will be appended with a 9 character long unique id, so you must make sure to shorten long filenames accordingly! If you do not implement this method, there will be no sanitization!

      Parameters

      • filename: string

        the filename to sanitize; this can be a relative path (e.g. "images/image1.png")

      Returns string

      the clean filename