Skip to main content

How can I add file size to the response for a file field?

There are two methods: using an Convert List or setting up custom post-processing.
Please refer to the following instructions for configuration.

Using the Convert List

Configure in the Post-Processing of the Endpoint

Add the following settings to the post-processing of the Topics::list endpoint.

info

The following settings assume that the Topics::list endpoint has the slug files set in the file field.
Please adjust them according to your own configuration.

ItemSetting
Execution contentConvert List
OperationCopy
Fieldlist.files.url
New Fieldfile_size
ProcessFileSize

Image from Gyazo

Using the Custom function

Create a custom function by setting the following code from the Custom function editor screen.

info

The following code assumes that the slug of the file field is set to files in the Topics::list endpoint.
Please adjust it according to your settings.

{foreach from=$json.list key=key item=details}
{if isset($details.files) && $details.files.url_org}
{assign var='file_url' value=$details.files.url_org}
{assign var='file_size' value=$file_url|rcms_file_size}
{assign_array_set var="json.list.$key.files" key="file_size" value=$file_size from=$json.list.$key.files}
{/if}
{/foreach}

{assign var='processed_json' value=$json}

Set as Post-Processing for the Endpoint

Set the custom function you added as the post-processing for the target endpoint.

Image from Gyazo

Operation Verification

When you check the endpoint response using Swagger UI, you can see that the file_size field has been added to the files object in the response as shown below.

Image from Gyazo

caution

Please note that the fields "File (upload to GCS)" and "File (upload to S3)" are not supported.


Support

If you have any other questions, please contact us or check out Our Slack Community.