NexusDB compatible with binary field

This commit is contained in:
xiaomlove
2026-06-14 16:24:18 +07:00
parent fab32bf63d
commit c50867a71a
3 changed files with 31 additions and 5 deletions
+1 -1
View File
@@ -31,7 +31,7 @@ class BaseRepository
return $perPage;
}
protected function handleAnonymous($username, $user, User $authenticator, Torrent $torrent = null)
protected function handleAnonymous($username, $user, User $authenticator, ?Torrent $torrent = null)
{
if (!$user) {
return "";
+22
View File
@@ -568,4 +568,26 @@ class NexusDB
}
}
public static function binaryField(string $field): string
{
if (self::isMysql()) {
return sprintf("%s = :%s", $field, $field);
} elseif (self::isPgsql()) {
return sprintf("%s = decode(:%s, 'hex')", $field, $field);
} else {
throw new \RuntimeException('Not supported database.');
}
}
public static function binaryFieldBindValue($value): string
{
if (self::isMysql()) {
return $value;
} elseif (self::isPgsql()) {
return bin2hex($value);
} else {
throw new \RuntimeException('Not supported database.');
}
}
}
+8 -4
View File
@@ -173,10 +173,12 @@ elseif ($az['showclienterror'] == 'yes'){
// check torrent based on info_hash
$tsField = \Nexus\Database\NexusDB::unixTimestampField('added');
$checkTorrentSql = "SELECT torrents.id, size, owner, sp_state, seeders, leechers, times_completed, $tsField AS ts, added, banned, hr, approval_status, price, categories.mode FROM torrents left join categories on torrents.category = categories.id WHERE info_hash = decode(:info_hash, 'hex') limit 1";
$infoHashField = \Nexus\Database\NexusDB::binaryField('info_hash');
$infoHashFieldBindValue = \Nexus\Database\NexusDB::binaryFieldBindValue($info_hash);
$checkTorrentSql = "SELECT torrents.id, size, owner, sp_state, seeders, leechers, times_completed, $tsField AS ts, added, banned, hr, approval_status, price, categories.mode FROM torrents left join categories on torrents.category = categories.id WHERE $infoHashField limit 1";
if (!$torrent = $Cache->get_value('torrent_hash_'.$info_hash.'_content')){
$res = mysql_prepare($checkTorrentSql);
$res->execute(['info_hash' => bin2hex($info_hash)]);
$res->execute(['info_hash' => $infoHashFieldBindValue]);
$torrent = mysql_fetch_array($res);
$Cache->cache_value('torrent_hash_'.$info_hash.'_content', $torrent, 350);
}
@@ -290,12 +292,14 @@ if (isset($event) && $event == "stopped") {
}
}
}
$selfwhere = "torrent = $torrentid AND peer_id = decode(:peer_id, 'hex') AND userid = $userid";
$peerIdField = \Nexus\Database\NexusDB::binaryField('peer_id');
$peerIdFieldBindValue = \Nexus\Database\NexusDB::binaryFieldBindValue($peer_id);
$selfwhere = "torrent = $torrentid AND $peerIdField AND userid = $userid";
//no found in the above random selection
if (!isset($self))
{
$res = mysql_prepare("SELECT $fields FROM peers WHERE $selfwhere LIMIT 1");
$res->execute(['peer_id' => bin2hex($peer_id)]);
$res->execute(['peer_id' => $peerIdFieldBindValue]);
$row = mysql_fetch_assoc($res);
if ($row)
{