From 338b70e334011dbd16bba306a930fac2d028562b Mon Sep 17 00:00:00 2001 From: ghost Date: Wed, 6 Sep 2023 13:37:05 +0300 Subject: [PATCH] implement direct download option --- src/public/download.php | 78 +++++++++++++++++++++++++---------------- 1 file changed, 47 insertions(+), 31 deletions(-) diff --git a/src/public/download.php b/src/public/download.php index 7a11103..c6c534a 100644 --- a/src/public/download.php +++ b/src/public/download.php @@ -65,7 +65,11 @@ $response = (object) $db->addMagnetDownload($magnet->magnetId, $userId, time()); // Build magnet link - $link = []; + $link = (object) + [ + 'magnet' => [], + 'direct' => [], + ]; /// Exact Topic $xt = []; @@ -91,10 +95,10 @@ $response = (object) } } - $link[] = sprintf('magnet:?%s', implode('&', $xt)); + $link->magnet[] = sprintf('magnet:?%s', implode('&', $xt)); /// Display Name - $link[] = sprintf('dn=%s', urlencode($magnet->dn)); + $link->magnet[] = sprintf('dn=%s', urlencode($magnet->dn)); // Keyword Topic $kt = []; @@ -104,7 +108,7 @@ $response = (object) $kt[] = urlencode($db->getKeywordTopic($result->keywordTopicId)->value); } - $link[] = sprintf('kt=%s', implode('+', $kt)); + $link->magnet[] = sprintf('kt=%s', implode('+', $kt)); /// Address Tracker foreach ($db->findAddressTrackerByMagnetId($magnet->magnetId) as $result) @@ -116,26 +120,24 @@ $response = (object) $port = $db->getPort($addressTracker->portId); $uri = $db->getUri($addressTracker->uriId); - $url = sprintf('tr=%s', urlencode($port->value ? sprintf('%s://%s:%s%s', $scheme->value, - $host->value, - $port->value, - $uri->value) : sprintf('%s://%s%s', $scheme->value, - $host->value, - $uri->value))); - // Yggdrasil host only if (!preg_match(YGGDRASIL_HOST_REGEX, str_replace(['[',']'], false, $host->value))) { continue; } - $link[] = $url; + $link->magnet[] = sprintf('tr=%s', urlencode($port->value ? sprintf('%s://%s:%s%s', $scheme->value, + $host->value, + $port->value, + $uri->value) : sprintf('%s://%s%s', $scheme->value, + $host->value, + $uri->value))); } // Append trackers.json foreach (json_decode(file_get_contents(__DIR__ . '/../config/trackers.json')) as $tracker) { - $link[] = sprintf('tr=%s', urlencode($tracker->announce)); + $link->magnet[] = sprintf('tr=%s', urlencode($tracker->announce)); } /// Acceptable Source @@ -148,19 +150,24 @@ $response = (object) $port = $db->getPort($acceptableSource->portId); $uri = $db->getUri($acceptableSource->uriId); - $url = sprintf('as=%s', urlencode($port->value ? sprintf('%s://%s:%s%s', $scheme->value, - $host->value, - $port->value, - $uri->value) : sprintf('%s://%s%s', $scheme->value, - $host->value, - $uri->value))); // Yggdrasil host only if (!preg_match(YGGDRASIL_HOST_REGEX, str_replace(['[',']'], false, $host->value))) { continue; } - $link[] = $url; + $link->magnet[] = sprintf('as=%s', urlencode($port->value ? sprintf('%s://%s:%s%s', $scheme->value, + $host->value, + $port->value, + $uri->value) : sprintf('%s://%s%s', $scheme->value, + $host->value, + $uri->value))); + $link->direct[] = $port->value ? sprintf('%s://%s:%s%s', $scheme->value, + $host->value, + $port->value, + $uri->value) : sprintf('%s://%s%s', $scheme->value, + $host->value, + $uri->value); } /// Exact Source @@ -173,20 +180,18 @@ $response = (object) $port = $db->getPort($eXactSource->portId); $uri = $db->getUri($eXactSource->uriId); - $url = sprintf('xs=%s', urlencode($port->value ? sprintf('%s://%s:%s%s', $scheme->value, - $host->value, - $port->value, - $uri->value) : sprintf('%s://%s%s', $scheme->value, - $host->value, - $uri->value))); - // Yggdrasil host only if (!preg_match(YGGDRASIL_HOST_REGEX, str_replace(['[',']'], false, $host->value))) { continue; } - $link[] = $url; + $link->magnet[] = sprintf('xs=%s', urlencode($port->value ? sprintf('%s://%s:%s%s', $scheme->value, + $host->value, + $port->value, + $uri->value) : sprintf('%s://%s%s', $scheme->value, + $host->value, + $uri->value))); } // Return html @@ -197,7 +202,10 @@ $response = (object) ); $response->html->h1 = htmlentities($magnet->metaTitle); - $response->html->link->magnet = implode('&', array_unique($link)); // @TODO implement .bittorrent and separated v1/v2 magnet links + + // @TODO implement .bittorrent, separated v1/v2 magnet links + $response->html->link->magnet = implode('&', array_unique($link->magnet)); + $response->html->link->direct = $link->direct; } ?> @@ -208,7 +216,7 @@ $response = (object) - <?php echo $response->data->title ?> + <?php echo $response->html->title ?> @@ -237,11 +245,19 @@ $response = (object)
- + + html->link->direct as $direct) { ?> + + + + + + +