seria/src/Torrents/TorrentFiles.php

50 lines
1.8 KiB
PHP

<?php
namespace Seria\Torrents;
use InvalidArgumentException;
use RuntimeException;
use Index\Data\DbStatementCache;
use Index\Data\IDbConnection;
class TorrentFiles {
private DbStatementCache $cache;
public function __construct(IDbConnection $dbConn) {
$this->cache = new DbStatementCache($dbConn);
}
public function getFiles(TorrentInfo|string $torrentInfo): array {
$stmt = $this->cache->get('SELECT file_id, torrent_id, file_length, file_path FROM ser_torrents_files WHERE torrent_id = ? ORDER BY file_id ASC');
$stmt->addParameter(1, $torrentInfo instanceof TorrentInfo ? $torrentInfo->getId() : $torrentInfo);
$stmt->execute();
$result = $stmt->getResult();
$files = [];
while($result->next())
$files[] = new TorrentFileInfo($result);
return $files;
}
public function createFile(
TorrentInfo|string $torrentInfo,
int $length,
array|string $path
): void {
$stmt = $this->cache->get('INSERT INTO ser_torrents_files (torrent_id, file_length, file_path) VALUES (?, ?, ?)');
$stmt->addParameter(1, $torrentInfo instanceof TorrentInfo ? $torrentInfo->getId() : $torrentInfo);
$stmt->addParameter(2, $length);
$stmt->addParameter(3, is_array($path) ? implode('/', $path) : $path);
$stmt->execute();
}
public function countTotalSize(TorrentInfo|string $torrentInfo): int {
$stmt = $this->cache->get('SELECT SUM(file_length) FROM ser_torrents_files WHERE torrent_id = ?');
$stmt->addParameter(1, $torrentInfo instanceof TorrentInfo ? $torrentInfo->getId() : $torrentInfo);
$stmt->execute();
$result = $stmt->getResult();
return $result->next() ? $result->getInteger(0) : 0;
}
}