Blog / Others/ How to Convert Between Regular URLs and Thunder Download Links (thunder://) in PHP

How to Convert Between Regular URLs and Thunder Download Links (thunder://) in PHP

Converting Regular URLs to Thunder Download Links (thunder://)

To convert a standard HTTP/HTTPS/FTP download link into a Thunder-specific link, the core principle is to wrap the original URL with specific identifiers ("AA" and "ZZ"), perform Base64 encoding, and finally prepend the "thunder://" prefix.

<?php
function ThunderEncode($url) {
    // Fixed prefix and suffix for Thunder links
    $thunderPrefix = "AA";
    $thunderSuffix = "ZZ";
    $thunderTitle = "thunder://";
    // Concatenate: AA + Original URL + ZZ, then Base64 encode, finally add the Thunder protocol header
    $thunderUrl = $thunderTitle . base64_encode($thunderPrefix . $url . $thunderSuffix);
    return $thunderUrl;
}
?>

Usage Example:

<?php
echo ThunderEncode("https://example.com/file.zip");
// Outputs something like: thunder://QUFodHRwczovL2V4YW1wbGUuY29tL2ZpbGUuemlwWlo=
?>

Converting Thunder Addresses (thunder://) Back to Regular URLs

To restore a Thunder-specific link to its original download address, perform the inverse of the encoding process: remove the protocol header, Base64 decode, and strip the identifiers.

<?php
function ThunderDecode($thunderUrl) {
    $thunderPrefix = "AA";
    $thunderSuffix = "ZZ";
    $thunderTitle = "thunder://";
    // 1. Trim whitespace from both ends of the string
    $url = trim($thunderUrl);
    // 2. Remove the "thunder://" protocol header (10 characters)
    $url = substr($url, 10);
    // 3. Base64 decode the remaining part
    $url = base64_decode($url);
    // 4. Remove the leading "AA" (2 chars) and trailing "ZZ" (2 chars) from the decoded string
    $url = substr($url, 2, -2);
    return $url;
}
?>

Usage Example:

<?php
echo ThunderDecode("thunder://QUFodHRwczovL2V4YW1wbGUuY29tL2ZpbGUuemlwWlo=");
// Outputs: https://example.com/file.zip
?>

Important Notes and Improvements

  • Error Handling: The basic functions above lack error handling. In a real application, add checks for input format, Base64 decoding validity, and string length.
  • Encoding: Ensure your PHP environment's character encoding is set correctly to avoid issues when processing URLs containing non-ASCII characters.
  • Security: This conversion is only for address format processing and does not involve the download action itself. Ensure the URLs you convert are from trusted sources.
  • Code Optimization: Consider defining the fixed prefix, suffix, and protocol header as class constants or in a configuration file for better maintainability.

Technical Principle: The Thunder thunder:// protocol is a private protocol. It works by wrapping the original address with "AA" and "ZZ" as markers, then Base64 encoding the result. This method does not provide encryption or security; it is merely a format encapsulation to help the Thunder client identify and take over download tasks.

Post a Comment

Your email will not be published. Required fields are marked with *.