File download fixes (closes #113, #111)

This commit is contained in:
Daniil Gentili 2017-05-16 23:38:25 +01:00
parent be6bc0cf41
commit 9fa4bdc15b
2 changed files with 19 additions and 11 deletions

View File

@ -49,7 +49,7 @@ while (true) {
$MadelineProto->messages->sendMessage(['peer' => '@danogentili', 'message' => $e->getCode().': '.$e->getMessage().PHP_EOL.$e->getTraceAsString()]); $MadelineProto->messages->sendMessage(['peer' => '@danogentili', 'message' => $e->getCode().': '.$e->getMessage().PHP_EOL.$e->getTraceAsString()]);
} }
try { try {
if (isset($update['update']['message']['media']) && $update['update']['message']['media'] == 'messageMediaPhoto' && $update['update']['message']['media'] == 'messageMediaDocument') { if (isset($update['update']['message']['media']) && ($update['update']['message']['media']['_'] == 'messageMediaPhoto' || $update['update']['message']['media']['_'] == 'messageMediaDocument')) {
$time = time(); $time = time();
$file = $MadelineProto->download_to_dir($update['update']['message']['media'], '/tmp'); $file = $MadelineProto->download_to_dir($update['update']['message']['media'], '/tmp');
$MadelineProto->messages->sendMessage(['peer' => $update['update']['message']['from_id'], 'message' => 'Downloaded to '.$file.' in '.(time() - $time).' seconds', 'reply_to_msg_id' => $update['update']['message']['id'], 'entities' => [['_' => 'messageEntityPre', 'offset' => 0, 'length' => strlen($res), 'language' => 'json']]]); $MadelineProto->messages->sendMessage(['peer' => $update['update']['message']['from_id'], 'message' => 'Downloaded to '.$file.' in '.(time() - $time).' seconds', 'reply_to_msg_id' => $update['update']['message']['id'], 'entities' => [['_' => 'messageEntityPre', 'offset' => 0, 'length' => strlen($res), 'language' => 'json']]]);

View File

@ -140,8 +140,13 @@ trait Files
} }
return $res; return $res;
case 'photo':
case 'messageMediaPhoto': case 'messageMediaPhoto':
$photo = end($message_media['photo']['sizes']); if ($message_media['_'] == 'photo') {
$photo = end($message_media['sizes']);
} else {
$photo = end($message_media['photo']['sizes']);
}
$res['name'] = $photo['location']['volume_id'].'_'.$photo['location']['local_id']; $res['name'] = $photo['location']['volume_id'].'_'.$photo['location']['local_id'];
$res['InputFileLocation'] = ['_' => 'inputFileLocation', 'volume_id' => $photo['location']['volume_id'], 'local_id' => $photo['location']['local_id'], 'secret' => $photo['location']['secret'], 'dc_id' => $photo['location']['dc_id']]; $res['InputFileLocation'] = ['_' => 'inputFileLocation', 'volume_id' => $photo['location']['volume_id'], 'local_id' => $photo['location']['local_id'], 'secret' => $photo['location']['secret'], 'dc_id' => $photo['location']['dc_id']];
@ -248,6 +253,7 @@ trait Files
fseek($stream, $offset); fseek($stream, $offset);
} }
$downloaded_size = 0; $downloaded_size = 0;
if ($end === -1 && isset($message_media['size'])) $end = $message_media['size'];
$size = $end - $offset; $size = $end - $offset;
$part_size = 512 * 1024; $part_size = 512 * 1024;
$percent = 0; $percent = 0;
@ -269,6 +275,7 @@ trait Files
if ($start_at = $offset % $part_size) { if ($start_at = $offset % $part_size) {
$offset -= $start_at; $offset -= $start_at;
} }
try { try {
$res = $cdn ? $this->method_call('upload.getCdnFile', ['file_token' => $message_media['file_token'], 'offset' => $offset, 'limit' => $part_size], ['heavy' => true, 'datacenter' => $datacenter]) : $this->method_call('upload.getFile', ['location' => $message_media['InputFileLocation'], 'offset' => $offset, 'limit' => $part_size], ['heavy' => true, 'datacenter' => &$datacenter]); $res = $cdn ? $this->method_call('upload.getCdnFile', ['file_token' => $message_media['file_token'], 'offset' => $offset, 'limit' => $part_size], ['heavy' => true, 'datacenter' => $datacenter]) : $this->method_call('upload.getFile', ['location' => $message_media['InputFileLocation'], 'offset' => $offset, 'limit' => $part_size], ['heavy' => true, 'datacenter' => &$datacenter]);
} catch (\danog\MadelineProto\RPCErrorException $e) { } catch (\danog\MadelineProto\RPCErrorException $e) {
@ -279,6 +286,12 @@ trait Files
throw $e; throw $e;
} }
} }
if ($res['type']['_'] === 'storage.fileUnknown' && $res['bytes'] === '') $datacenter = 1;
while ($res['type']['_'] === 'storage.fileUnknown' && $res['bytes'] === '') {
$res = $this->method_call('upload.getFile', ['location' => $message_media['InputFileLocation'], 'offset' => $offset, 'limit' => $part_size], ['heavy' => true, 'datacenter' => $datacenter]);
$datacenter++;
if (!isset($this->datacenter->sockets[$datacenter])) break;
}
if ($res['_'] === 'upload.fileCdnRedirect') { if ($res['_'] === 'upload.fileCdnRedirect') {
$cdn = true; $cdn = true;
$message_media['file_token'] = $res['file_token']; $message_media['file_token'] = $res['file_token'];
@ -293,14 +306,6 @@ trait Files
$this->method_call('upload.reuploadCdnFile', ['file_token' => $message_media['file_token'], 'request_token' => $res['request_token']], ['heavy' => true, 'datacenter' => $datacenter]); $this->method_call('upload.reuploadCdnFile', ['file_token' => $message_media['file_token'], 'request_token' => $res['request_token']], ['heavy' => true, 'datacenter' => $datacenter]);
continue; continue;
} }
while ($res['type']['_'] === 'storage.fileUnknown' && $res['bytes'] === '') {
$datacenter = 1;
$res = $this->method_call('upload.getFile', ['location' => $message_media['InputFileLocation'], 'offset' => $offset, 'limit' => $part_size], ['heavy' => true, 'datacenter' => $datacenter]);
$datacenter++;
}
if ($res['bytes'] === '') {
break;
}
if (isset($message_media['cdn_key'])) { if (isset($message_media['cdn_key'])) {
$res['bytes'] = $this->encrypt_ctr($res['bytes'], $message_media['cdn_key'], $message_media['cdn_iv'], $offset); $res['bytes'] = $this->encrypt_ctr($res['bytes'], $message_media['cdn_key'], $message_media['cdn_iv'], $offset);
} }
@ -314,6 +319,9 @@ trait Files
$res['bytes'] = substr($res['bytes'], 0, $size - $downloaded_size); $res['bytes'] = substr($res['bytes'], 0, $size - $downloaded_size);
$theend = true; $theend = true;
} }
if ($res['bytes'] === '') {
break;
}
$offset += strlen($res['bytes']); $offset += strlen($res['bytes']);
$downloaded_size += strlen($res['bytes']); $downloaded_size += strlen($res['bytes']);
\danog\MadelineProto\Logger::log([fwrite($stream, $res['bytes'])], \danog\MadelineProto\Logger::ULTRA_VERBOSE); \danog\MadelineProto\Logger::log([fwrite($stream, $res['bytes'])], \danog\MadelineProto\Logger::ULTRA_VERBOSE);
@ -322,7 +330,7 @@ trait Files
break; break;
} }
//\danog\MadelineProto\Logger::log([$offset, $size, ftell($stream)], \danog\MadelineProto\Logger::ULTRA_VERBOSE); //\danog\MadelineProto\Logger::log([$offset, $size, ftell($stream)], \danog\MadelineProto\Logger::ULTRA_VERBOSE);
$cb($percent = $downloaded_size * 100 / $size); if ($end !== -1) $cb($percent = $downloaded_size * 100 / $size);
} }
if ($end === -1) { if ($end === -1) {
$cb(100); $cb(100);