How can I add file size to the response for a file field?
You can achieve this by creating a custom process that retrieves the target file size and adds it to the response, then setting it as the post-processing for the endpoint.
To retrieve the file size, you can use the rcms_file_size Smarty plugin.
Set in Custom Process
Create a custom process by setting the following code from the Custom Process Editing screen.
The following code assumes that the slug
of the file field is set to files
in the details 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 process you added as the post-processing for the target endpoint.
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.
Related Documents
Support
If you have any other questions, please contact us or check out Our Slack Community.