Getting the file size of attachments added to posts through the API

Hi,

I have been looking around the topic but couldn’t find it.

I’m looking for a way to retrieve the size of a file whenever a post contains an image (or other type of attachment).

The upload related api has no such ‘get metadata’ function. I can’t use the data explorer because that plugin is not installed.

ask.discourse is indicating that this is a feature gap

You’re correct in identifying a gap in the API. Currently, fetching specific upload attributes (like file size) directly via the REST API is not exposed unless it’s part of the upload response when the file is first uploaded.

Is this correct or am I looking at the wrong location?

2 Likes

Well, there is an UploadSerializer that includes filesize, but I don’t see a way to get it.

Uploads/attachments do include the filesize, but only in the description of the attachment.

What problem are you solving by getting an upload’s filesize?

2 Likes

Hi @pfaffman

I’m part of Exalate - we provide an integration solution allowing to build bridges between various systems. We are currently building an integration allowing to insert the discourse interactions with our own backend system. One of the requirements whenever we provide an attachment on the other side, is that a filesize is provided.

I solved the problem now by getting the file, without streaming it (I hope that it is ok for the discourse backend). The header contains the necessary information such as mimetype and filesize.

It would be nicer to have an explicit REST endpoint, but there is a workaround, so all good.

Francis

3 Likes

Clever solution!

If you can’t install the data explorer plugin, I’m afraid you’ve got the best available solution.

3 Likes