{tag_filesize} - Anyone know if there's a TreeplCMS Liquid equivalent?

Can anyone tell me if there is a TreeplCMS liquid equivalent for {tag_filesize} (or similar?) when used with the migrated Media Download PDF files? Have tried {{this[‘FileSize’]}} with no luck - is it possible to generate like BC MediaDownloads eg. Screenshot below

tag-filesize

Hi @Craig.
There’s nothing in the current Liquid data for file size.
I know you can do this with javascript - but it’s not very efficient as you basically have to load all files to get their sizes.

But I had a crazy idea that I had to test and surprisingly it’s working pretty well :slight_smile:

Basically I thought if you could pull in a file’s raw text content and count the characters you should be able to get a pretty good idea of it’s file size??? (assuming 1 character = 1 byte, approx.)
So I did this:

{% capture fileStr %}{% include '/images/treehouse-logo.svg' %}{% endcapture%}
({{fileStr | size}} Bytes)<br>
({{fileStr | size | divided_by: 1000}} KB)<br>
({{fileStr | size | divided_by: 1000000}} MB)

and it’s not too far off. But file size various depending on various factors anyway.
Perhaps the maths being used here could be more accurate too?

Disclosure: calculating file size like this is generally not accurate and varies depending on file type.
However, I tested a .jpg, .svg, .pdf and .tff file and all weren’t too far off.

I’d probably round these figures up using the | round filter and just report them as approximate sizes.

But yes, | fileSize would be a good backlog request item :slight_smile: but maybe this’ll help in the meantime.

2 Likes

And so using this with a module item it might look like this:

{% capture fileStr %}{% include this['fileField'] %}{% endcapture%}
  • fileField being the name of your file property in the module setup.
1 Like

Thanks Adam - love your crazy ideas - where would we be without you? I’ll give that a try.

Honestly, where do you even come up with this stuff. So creative. Great workaround.