Untitled

mail@pastecode.io avatar
unknown
plain_text
7 days ago
4.8 kB
11
Indexable
Never
#[FiltersAnd(new FilterCommand('url'))]
public function handleUrls(PrivateMessage&Incoming&Message&IsNotEdited $message): void {
    $words = explode(' ', $message->message);
    $secondWord = $words[1];
    //$photo = $words[2];
    $urlx = $secondWord;
    $parse_url = parse_url($urlx, PHP_URL_PATH);
    $parse_del = preg_replace("/\/+/", "", $parse_url);

    try {
        $sent = $message->reply('Preparing...');
$url = $secondWord;
$PhotoTemp = $this->downloadToDir($message->media, '/tmp/');

//$message->reply("Photo: $photo ,URL: $url");
        $client = HttpClientBuilder::buildDefault();

        $request = new Request($url);
        $request->setTcpConnectTimeout(1000000);
        $request->setTlsHandshakeTimeout(1000000);
        $request->setTransferTimeout(10000000);
        $request->setBodySizeLimit(100000 * 1024 * 1024); // 100 MB

        $startTime = microtime(true);

        try {
            $response = $client->request($request);
            $body = $response->getBody();
            $payload = new Payload($body);

            $contentLength = $response->getHeader('content-length');
            $downloadedBytes = 0;

            $lastUpdate = 0;
            $fullzise = $contentLength / (1024 * 1024);

            if ($fullzise < 50) {
                $updateInterval = 5 * 1024 * 1024;
            } elseif ($fullzise >= 50 && $fullzise < 100) {
                $updateInterval = 20 * 1024 * 1024;
            } elseif ($fullzise >= 100 && $fullzise < 500) {
                $updateInterval = 20 * 1024 * 1024;
            } else {
                $updateInterval = 100 * 1024 * 1024;
            }

            while (null !== $chunk = $payload->read()) {
                $downloadedBytes += strlen($chunk);
                $elapsedTime = microtime(true) - $startTime;
                $downloadSpeed = $downloadedBytes / $elapsedTime;

                $downloadedMB = $downloadedBytes / (1024 * 1024);
                $elapsedSeconds = $elapsedTime;
                $downloadSpeedMBps = $downloadSpeed / (1024 * 1024);

                if ($downloadedBytes - $lastUpdate >= $updateInterval) {
                    $sent->editText("Download: " . number_format($downloadedMB, 2) . " MB\nTime: " . number_format($elapsedSeconds, 2) . " s\nSpeed: " . number_format($downloadSpeedMBps, 2) . " MB/s\nCompleting download: " . number_format($downloadedMB, 2) . " MBs Of ". number_format($fullzise, 1) . " MB");
                    $lastUpdate = $downloadedBytes;
                }
            }
            $headers = $response->getHeaders();
            $date = $headers['date'][0] ?? 'Date header not found';
            $lastModified = $headers['last-modified'][0] ?? 'Last-Modified header not found';

            $caption = "Response Information:\n";
            $caption .= "Headers: \nDate: " . $date . "\n";
            $caption .= "last-modified: ".$lastModified."\n";
            $caption .= "Download size: " . number_format($fullzise, 1) . " MB" . "\n";

            $extension = pathinfo($url, PATHINFO_EXTENSION);
            if (empty($extension)) {
                $extension = 'apk'; // Default extension
            }
$sendMessageTypingActionn = ['_' => 'sendMessageTypingAction'];
$this->messages->setTyping(['peer' => $message->chatId, 'action' =>$sendMessageTypingActionn]);
$this->sleep(1);
 $doc_by_user = $this->sendDocument(
                peer: $message->chatId,
                replyToMsgId: $message->replyToMsgId,
   file: new RemoteUrl($url),
   //  thumb: new RemoteUrl($photo),
    thumb: new LocalFile($PhotoTemp),
   caption: 'O',
   parseMode: ParseMode::HTML,
   fileName: $parse_del . '.' . $extension,
            );
 $forward_by_bot = $this->messages->forwardMessages(
    to_peer: -1002383942583,
    from_peer: $message->chatId,
    id: [$doc_by_user->id]);

if (!unlink($PhotoTemp)) {
            throw new Exception('Failed to delete photo.');
            }
        $message->reply("Pic $PhotoTemp has been delete.");


      } catch (Exception $e) {
            $sent->editText('Error: ' . $e->getMessage());
}
 
            
$this->sleep(2);
 $sent->delete();
    } catch (Throwable $e) {
        if (!str_contains($e->getMessage(), 'Could not connect to URI') && !($e instanceof UriException) && !str_contains($e->getMessage(), 'URI')) {
            $this->report((string) $e);
            $this->logger((string) $e, Logger::FATAL_ERROR);
        }
        if ($e instanceof RPCErrorException && $e->rpc === 'FILE_PARTS_INVALID') {
            $this->report(json_encode($file));
        }
        try {
            $sent->editText('Error: ' . $e->getMessage());
        } catch (Throwable $e) {
            $this->logger((string) $e, Logger::FATAL_ERROR);
        }
    }
}
Leave a Comment