Skip to content

Commit

Permalink
CDN file fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
danog committed Oct 21, 2023
1 parent ae20f7f commit 096053b
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/API.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ final class API extends AbstractAPI
*
* @var string
*/
public const RELEASE = '8.0.0-beta154';
public const RELEASE = '8.0.0-beta155';
/**
* We're not logged in.
*
Expand Down
10 changes: 4 additions & 6 deletions src/MTProtoTools/Files.php
Original file line number Diff line number Diff line change
Expand Up @@ -1100,7 +1100,6 @@ public function downloadToCallable(mixed $messageMedia, callable $callable, ?cal
$cdn = false;
$params[0]['previous_promise'] = true;
$start = microtime(true);
$old_dc = null;
$size = $this->downloadPart($messageMedia, $cdn, $datacenter, $old_dc, $ige, $cb, $initParam = array_shift($params), $callable, $seekable, $cancellation);
if ($initParam['part_end_at'] - $initParam['part_start_at'] !== $size) {
// Premature end for undefined length files
Expand Down Expand Up @@ -1203,7 +1202,6 @@ private function downloadPart(array &$messageMedia, bool &$cdn, int &$datacenter
$messageMedia['file_token'] = $res['file_token'];
$messageMedia['cdn_key'] = $res['encryption_key'];
$messageMedia['cdn_iv'] = $res['encryption_iv'];
$old_dc = $datacenter;
$datacenter = ($this->isTestMode() ? 10_000 : 0) + $res['dc_id'];
if (!$this->datacenter->has($datacenter)) {
$this->config['expires'] = -1;
Expand All @@ -1216,7 +1214,7 @@ private function downloadPart(array &$messageMedia, bool &$cdn, int &$datacenter
$this->config['expires'] = 0;
$this->getConfig();
try {
$this->addCdnHashes($messageMedia['file_token'], $this->methodCallAsyncRead('upload.reuploadCdnFile', ['file_token' => $messageMedia['file_token'], 'request_token' => $res['request_token'], 'cancellation' => $cancellation], $datacenter));
$this->addCdnHashes($messageMedia['file_token'], $this->methodCallAsyncRead('upload.reuploadCdnFile', ['file_token' => $messageMedia['file_token'], 'request_token' => $res['request_token'], 'cancellation' => $cancellation], $this->authorized_dc));
} catch (RPCErrorException $e) {
switch ($e->rpc) {
case 'FILE_TOKEN_INVALID':
Expand Down Expand Up @@ -1250,7 +1248,7 @@ private function downloadPart(array &$messageMedia, bool &$cdn, int &$datacenter
if (isset($messageMedia['cdn_key'])) {
$ivec = substr($messageMedia['cdn_iv'], 0, 12).pack('N', $offset['offset'] >> 4);
$res['bytes'] = Crypt::ctrEncrypt($res['bytes'], $messageMedia['cdn_key'], $ivec);
$this->checkCdnHash($messageMedia['file_token'], $offset['offset'], $res['bytes'], $old_dc, $cancellation);
$this->checkCdnHash($messageMedia['file_token'], $offset['offset'], $res['bytes'], $cancellation);
}
if (isset($messageMedia['key'])) {
$res['bytes'] = $ige->decrypt($res['bytes']);
Expand Down Expand Up @@ -1281,11 +1279,11 @@ private function addCdnHashes(string $file, array $hashes): void
$this->cdn_hashes[$file][$hash['offset']] = ['limit' => $hash['limit'], 'hash' => (string) $hash['hash']];
}
}
private function checkCdnHash(string $file, int $offset, string $data, int &$datacenter, ?Cancellation $cancellation): void
private function checkCdnHash(string $file, int $offset, string $data, ?Cancellation $cancellation): void
{
while (\strlen($data)) {
if (!isset($this->cdn_hashes[$file][$offset])) {
$this->addCdnHashes($file, $this->methodCallAsyncRead('upload.getCdnFileHashes', ['file_token' => $file, 'offset' => $offset, 'cancellation' => $cancellation], $datacenter));
$this->addCdnHashes($file, $this->methodCallAsyncRead('upload.getCdnFileHashes', ['file_token' => $file, 'offset' => $offset, 'cancellation' => $cancellation], $this->authorized_dc));
}
if (!isset($this->cdn_hashes[$file][$offset])) {
throw new Exception('Could not fetch CDN hashes for offset '.$offset);
Expand Down

0 comments on commit 096053b

Please sign in to comment.