Converting short upload URLs to full URLs

I may be barking up the wrong tree here, so apologies if I am - but any pointers very much welcome!

There are some threads in our Discourse site which are displayed in part on our main website. Because the cooked version of a post contains all the HTML for the lightbox, which we don’t want on the main site, I’m working with the raw version of a post.

One thing that’s tripping me up is the file upload URLs. How can I convert an upload:// URL to a full URL? I’ve tried searching and come across SHA1 and Base62, but apart from that, no matter what I try, I can’t get the full URL.

As I said, I may looking at the wrong thing, or there’s (likely) to be an easier way to these things, so any advice welcome!

Thanks in advance

2 Likes

First base62 decode using the inverted character set, then hex encode the result.

In Python code it looks like this:

rebase = hex(base62.decode(base, base62.CHARSET_INVERTED))[2:].zfill(40)

5 Likes

Thanks for the swift reply @michaeld. Will give it a go later today :slight_smile:


For anyone interested and doing this in PHP, I used a composer library called base62 by tuupola.

This is the code I used:

<?php

$base62 = new Tuupola\Base62(["characters" => Tuupola\Base62::INVERTED]);

/** Set the original file name, excluding any file extensions */
$s = "r3AYqESanERjladb4vBB7VsMBm6";

/** Decode, convert to hex */
$decoded = $base62->decode($s);

/** Expected result: bda2c513e1da04f7b4e99230851ea2aafeb8cc4e */
echo bin2hex($decoded);
2 Likes