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

「いいね!」 3

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)

「いいね!」 8

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

興味深いですね。JavaScriptではこれを再現できません。あなたの最後のメソッドはbinary2hex変換ですが、私の理解では、base62デコードの結果はバイナリ表現ではありません。

JSの観点から、以下の資料が役立つことがわかりました。
https://www.npmjs.com/package/aybabtu

function fromBase62(s) {
  var digits = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  var result = 0;
  for (var i = 0; i < s.length; i++) {
    var p = digits.indexOf(s[i]);
    if (p < 0) {
      return NaN;
    }
    result += p * Math.pow(digits.length, s.length - i - 1);
  }
  return result;
}

残念ながら、入力文字列を使用した場合、bin2hexまたはdec2hexメソッドのどちらも役に立ちません。

興味のある方のために、この問題に関するcodesandboxを作成しました。私の入力文字列の最終値は1ですが、これは間違っています ;(

ありがとうございます。

binVal.toString(16) を使用して16進数の値を取得できるはずです。
ただし、中間結果が通常の整数に収まらないほど大きいと思います(!)。

「いいね!」 2

リチャード、ヒントをありがとう。しかし、そのアイデアを使っても、さらに進展はありません。

「生のビュー(ルート)で絶対的なアップロードリンクを有効にする」のような設定があると良いと思いませんか?私の理解では、アップロードURLの短縮は、単にコードを減らすためのDiscourseの最適化ですよね?Markdownとは関係ありません。私たちのケースでは、まさにそれを使います。

ディスコースチームの誰か、絶対URLを復元できるJSメソッドを提供してもらえませんか?
コミュニティにとって役立つでしょう、考えてみてください。

私はこれを達成できませんでした ;(

@RGJ Discourseチームの誰かを知っていて、そのヘルパー関数を提供してもらえますか?

いいえ…
Marketplace に投稿することをお勧めします。

「いいね!」 1

ユースケースについてもう少し詳しく教えていただけますか。もしかしたら、これが最善の解決策ではないかもしれません。

Base62から16進数に変換することはできますが、Discourseのデータベースへの何らかのインターフェースなしでは、アップロードURLを完全に再構築することはできません。アップロードの完全なパスは、アップロードのidに依存しており、これは短いURLからは決定できません。

@pfaffmanが言ったように、ユースケースを説明していただければ、さらに詳しい情報を提供できるかもしれません。

「いいね!」 4