Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 8 additions & 11 deletions src/Log/LoggerHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\AbstractProcessingHandler;
use Monolog\Logger;
use Monolog\LogRecord;
use SergiX44\Nutgram\Nutgram;

class LoggerHandler extends AbstractProcessingHandler
Expand All @@ -27,20 +28,16 @@ protected function getDefaultFormatter(): FormatterInterface
return new LineFormatter("%message% %context% %extra%\n");
}

protected function write(array $record): void
protected function write(LogRecord $record): void
{
$oldSplitConfig = config('nutgram.config.split_long_messages', false);
config(['nutgram.config.split_long_messages' => true]);

$this->bot->sendMessage($this->formatText($record), [
'chat_id' => $this->chatId,
'parse_mode' => 'html',
]);

config(['nutgram.config.split_long_messages' => $oldSplitConfig]);
$this->bot->sendChunkedMessage(
text: $this->formatText($record),
chat_id: $this->chatId,
parse_mode: 'html',
);
}

protected function formatText(array $record): string
protected function formatText(LogRecord $record): string
{
return sprintf(
"<b>%s %s</b> (%s):\n<pre>%s</pre>",
Expand Down
17 changes: 12 additions & 5 deletions src/Mixins/MixinUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Http\Client\ClientInterface;
use RuntimeException;
use SergiX44\Nutgram\Telegram\Types\Media\File;

class MixinUtils
Expand All @@ -24,22 +25,28 @@ class MixinUtils
*/
public static function saveFileToDisk(File $file, string $path, ?string $disk = null, array $clientOpt = []): bool
{
$bot = $file->getBot();

if ($bot === null) {
throw new RuntimeException('Bot instance not found.');
}

$storage = Storage::disk($disk);

if (Str::endsWith($path, ['/', '\\'])) {
$path .= basename($file->file_path ?? $file->file_id);
}

if ($file->getConfig()->is_local ?? false) {
return $storage->put($path, $file->downloadUrl($file));
if ($bot->getConfig()->is_local ?? false) {
return $storage->put($path, $bot->downloadUrl($file));
}

//create temp file
$tmpFile = tempnam(sys_get_temp_dir(), uniqid(strftime('%G-%m-%d'), true));
$tmpFile = tempnam(sys_get_temp_dir(), uniqid(time(), true));

//download file to temp file
$http = $file->getContainer()->get(ClientInterface::class);
$http->get($file->downloadUrl($file), array_merge(['sink' => $tmpFile], $clientOpt));
$http = $bot->getContainer()->get(ClientInterface::class);
$http->get($bot->downloadUrl($file), array_merge(['sink' => $tmpFile], $clientOpt));

//save temp file to disk
return $storage->putFileAs('/', new LaravelFile($tmpFile), $path);
Expand Down