Looking up and generating upload short-urls

AI-generated summary

The discussion revolves around mapping Discourse upload short-urls (e.g., upload://fkO7C9M7SLRhXjEiFTOSUJIdCgJ.jpeg) to their full URLs (e.g., //127.0.0.1:4200/uploads/default/original/1X/abcafdf0e1d9262d515075420e4d940718796a5d.jpeg). david explains that both URLs contain the same SHA1 hash, with the short-url using a base62 encoding for brevity, while the full URL uses the original hex-encoded version. The process involves retrieving the SHA1 from either the short or long URL and querying the database via Upload.find_by(sha1: sha1). merefield clarifies that upload.short_url and upload.url can be used directly in Rails to generate the respective URLs, and thanks david for the explanation. The conversation references specific methods in the Discourse codebase, including Upload.sha1_from_long_url, Upload.sha1_from_short_url, and the short_url method.

Appears in Post:

![img-MAVMAbkLhcg3sooqMLUyKIhT](upload://>>>>>yygWIAKKT974aRWzlyDof2O82bL<<<<<.jpeg)

How can I map it to this (and vice versa):

"//127.0.0.1:4200/uploads/default/original/1X/abcafdf0e1d9262d515075420e4d940718796a5d.jpeg"

appears as url property of an Upload.

?

You would use this function:

and then lookup the SHA1 in the database to find the upload record.

Ah, so that’s the SHA1? doh!

Yeah, the both the short-url and the regular URL include the SHA1.

The difference is that the long URL uses the original hex-encoded version, which matches the one in the database. The upload:// “short url” is a base62 representation of the same hash, so that it uses fewer characters.

Sorry, right got it.

So there is a:

  • short-url in the Post
  • url - original long url to upload.
  • SHA1 generated during upload.

and you just gave me the way to find the url from the short-url

sorry, early part of the day, so if my goal is to create the short-url, I need to transcode the SHA1 to base62?

Where are you doing this? In the Rails app?

If so, then you can generate the URLs using upload.short_url and upload.url.

To find an upload record in the first place, you can do:

sha1 = Upload.sha1_from_long_url(...)
# or
sha1 = Upload.sha1_from_short_url(...)

# then

upload = Upload.find_by(sha1: sha1)

Yes, Rails. I’m uploading from the back-end.

OMGoodness:

Upload.last.short_url

"upload://fkO7C9M7SLRhXjEiFTOSUJIdCgJ.jpeg" :tada:

Sorry David, I totally missed that!!

At least this Topic clarifies this all for anyone interested.

Thanks for all the info.