matrix-media
v1.0.0
Published
Allows uploading files to matrix.org homeserver through media api.
Readme
MediaClient
MediaClient is a simple JavaScript class for uploading and downloading media files to/from a Matrix homeserver.
Installation
npm install matrix-media(Replace with your actual package name or usage)
Usage
const MediaClient = require('matrix-media');
const client = new MediaClient({ token: 'YOUR_ACCESS_TOKEN' });
// Upload a file buffer
const mxcUri = await client.upload(fileBuffer, 'filename.ext');
// mxcUri example: "mxc://matrix.org/abcdefg"
// Download a file from Matrix (returns Uint8Array)
const fileData = await client.downloadBuffer(mxcUri);API
constructor({ fetch = globalThis.fetch, token })
fetch— Optional: custom fetch implementation (default: global fetch)token— Required: Matrix access token for authorization
async upload(buffer, filename = "upload.bin") : Promise
buffer— Buffer | Uint8Array | ArrayBuffer — File content to uploadfilename— string (optional) — Filename to use on upload (default: "upload.bin")
Returns a Promise that resolves to the Matrix content URI (mxc://...).
The max upload size is ~50MB.
async downloadBuffer(mxcOrMediaUrl, timeout = 5000) : Promise
mxcOrMediaUrl— string — Matrix content URI (mxc://...) or direct download URLtimeout— number (optional) — Timeout in milliseconds (default: 5000)
Returns a Promise resolving to the file data as a Uint8Array.
