ThatGuySam / vumbnail

All things Vimeo and Thumbnails. Supports Vimeo and Youtube thumbnails.
https://vumbnail.com
39 stars 4 forks source link

Older video need to have zeros prefixed onto the ID #42

Closed ChrisMBarr closed 4 months ago

ChrisMBarr commented 1 year ago

Vimeo IDs less than 8 digits need zeros prefixed to get a valid thumbnail

I have a video at https://vimeo.com/961922 which is a few years old, so it's video ID is shorter than the common video IDs now. Using the ID 961922 fails to get an image: https://vumbnail.com/961922.jpg

But I took a guess and prefixed some zeros to make it 8 characters long and that works! https://vumbnail.com/00961922.jpg

Vimeo video IDs can be any length

(well at least 4 digits from what I can tell)

The one I have above is 6 digits, but here are some older ones (some 18 years old!) that have between 4 and 7 digits.

With my finding above, as expected: https://vumbnail.com/8612.jpg fails but https://vumbnail.com/00008612.jpg will work

My guess at a fix would be to focus on the isValidId method and this regex that expects an 8 digit minimum

ThatGuySam commented 5 months ago

Thanks for the tip!

This is really something I should fix internally but this work-around should help everyone in the mean time

ChrisMBarr commented 5 months ago

Actually, here's a pretty simple fix for it:

function isValidId ( maybeId ) {
    const zeroPadded = maybeId.padStart(8, '0') // anything less than 8 chars gets zeros added to the start
    const isCorrectLength = zeroPadded.length >= 8
    const isAlphanumeric = /^[a-zA-Z0-9_-]+$/i.test( zeroPadded )

    return isCorrectLength && isAlphanumeric
}

But given that, you probably don't need the isCorrectLength check any more since now it will always be at least 8 characters long. So probably just this is sufficient:

function isValidId ( maybeId ) {
    const zeroPadded = maybeId.padStart(8, '0') // anything less than 8 chars gets zeros added to the start
    const isAlphanumeric = /^[a-zA-Z0-9_-]+$/i.test( zeroPadded )
    return isAlphanumeric
}
ThatGuySam commented 5 months ago

Hmmm... that fixes the padding issue, but I'd rather fix the underlying issue that makes it have to add the 0s in the first place.

Maybe we could have 2 checks inside isValidId:

This will break YouTube thumbnails that only contain numbers, but that will be 1 in 737 Million according to ChatGPT, and we can handle that bridge when it starts to become a problem.

ThatGuySam commented 4 months ago

Alright, I added an additional check for Vimeo IDs that checks if every character before the first _ is a digit.

This should support shorter ids, including those you shared above, without adding zeroes(yay!).

https://github.com/ThatGuySam/vumbnail/blob/bf76cce5e89e9dc9e22752686ac869e5b1f631d1/helpers/url.ts#L92-L110

ThatGuySam commented 4 months ago

Let me know if this fixed the issue for you

ChrisMBarr commented 4 months ago

Cool, I'll give it a shot. I used this on a PR I submitted to a project forever ago and they never accepted yet. It has merge issues on it so I'll probably for those and remove the need for this patch now. here it is if your curious: https://github.com/MurhafSousli/ngx-gallery/pull/575/files#diff-59d3f3892ed1e6d41d8073a45aef6c2a6c4106f36d536b177626d6500eeb6c75R67-R75