Activity Feeds V3 is in closed alpha — do not use it in production (just yet).

File Uploads

Stream allows you to easily upload files to our CDN and use them as attachments for activities.

This functionality defaults to using the Stream CDN. If you would like, you can easily change the logic to upload to your own CDN of choice.

How to upload a file or image

// To upload a file or image, you can use the `FeedUploader` exposed by `FeedsClient`.
// Just create a `FeedUploadPayload` and pass it to the `upload` function.
val file = File("theFilePath")
val payload = FeedUploadPayload(file, FileType.Image("jpeg"))
val result: Result<UploadedFile> = client.uploader.upload(
    payload = payload,
    // Optionally, you can provide a listener to track the upload progress.
    progress = { progress -> println("Upload progress ${progress}%") }
)
result.fold(
    onSuccess = { uploadedFile -> println("File uploaded: ${uploadedFile.fileUrl}") },
    onFailure = { error -> println("Failed to upload file: ${error.message}") }
)

// You can also automatically upload files as attachments when creating an activity
// by passing a list of payloads in the `attachmentUploads` field of the request
val request = FeedAddActivityRequest(
    type = "activity",
    text = "Look at my beautiful image!",
    feeds = listOf("<feed id>"),
    attachmentUploads = listOf(
        FeedUploadPayload(myFile, FileType.Other("jpeg")),
    )
)
feed.addActivity(
    request = request,
    attachmentUploadProgress = { payload, progress ->
        println("${payload.file.name} upload progress: ${progress}%")
    }
)

// It also works for comments!
val request = ActivityAddCommentRequest(
    comment = "Look at my beautiful image!",
    activityId = "<activity id>",
    attachmentUploads = listOf(
        FeedUploadPayload(myFile, FileType.Other("jpeg")),
    )

)
feed.addComment(
    request = request,
    attachmentUploadProgress = { payload, progress ->
        println("${payload.file.name} upload progress: ${progress}%")
    }
)

Deleting Files and Images

We expose two methods for deleting files and images, client.deleteImage and client.deleteFile

client.deleteImage(url = "remote-image-url")
client.deleteFile(url = "remote-file-url")

Requirements for Images

  • Stream supported image types are: image/bmp, image/gif, image/jpeg, image/png, image/webp, image/heic, image/heic-sequence, image/heif, image/heif-sequence, image/svg+xml.

  • You can set a more restrictive list for your application if needed.

  • The maximum file size is 100MB.

Requirements for Files

  • Stream will not block any file types from uploading, however, different clients may handle different types differently or not at all.

  • You can set a more restrictive list for your application if needed.

  • The maximum file size is 100MB.

How to Allow/Block file extensions

Stream will allow any file extension. If you want to be more restrictive for an application, this is can be set via API or by logging into your dashboard.

  • To update via the dashboard, login and go to the Chat Overview page >> Upload Configuration.

  • API updates are made using UpdateAppSettings.

Image resizing

You can automatically resize an image appending query parameters to a valid image link stored on the Stream CDN.

An image can only be resized if the total pixel count of the source image is 16.800.000 or less. Attempting to resize an image with more pixels will result in an API error. An image of 4000 by 4000 would be accepted, but an image of 4100 by 4100 would pass the upper treshold for resizing.

There are four supported params - all of them are optional and can be used interchangeably:

ParameterTypeValuesDescription
wnumberWidth
hnumberHeight
resizestringclip, crop, scale, fillThe resizing mode
cropstringcenter, top, bottom, left, rightThe cropping direction during resize

Resized images will count against your stored files quota.

The Stream CDN URL returned during the upload contains a signature that validates the access to the file it points to. Only the members of a channel a file was uploaded to can see the attachment and its unique, signed link. Links can only be accessed with a valid signature, which also protects against enumeration attacks.

Whenever messages containing your attachments are retrieved (i.e., when querying a channel), the attachment links will contain a new, fresh signature.

A single Stream CDN URL expires after 14 days, after which its signature will stop working and the link won’t be valid anymore. You can check when a link will expire by comparing the current time with the Unix timestamp in the Expires parameter of the link’s query: https://us-east.stream-io-cdn.com/0000/images/foo.png?…&Expires=1602666347&…

Using Your Own CDN

All our SDKs make it easy to use your own CDN for uploads. The code examples below show how to change where files are uploaded:

// Your custom implementation of `FeedUploader`
class CustomUploader : FeedUploader {
    override suspend fun upload(
        payload: FeedUploadPayload,
        progress: ((Double) -> Unit)?
    ): Result<UploadedFile> {
        return when (payload.type) {
            is FileType.Image -> {
                // Your code to handle image uploading
                // Don't forget to call progress(x) to report back the uploading progress
                uploadImage(payload.file, progress)
            }

            is FileType.Other -> {
                // Your code to handle file uploading
                // Don't forget to call progress(x) to report back the uploading progress
                uploadFile(payload.file, progress)
            }
        }
    }
}

// Assign your custom uploader to the `FeedsConfig` instance you use when creating `FeedsClient`
val config = FeedsConfig(customUploader = CustomUploader())
val client = FeedsClient(
    feedsConfig = config
    // Other arguments
)
© Getstream.io, Inc. All Rights Reserved.