Fix some commands.

This commit is contained in:
James Cole
2025-08-16 15:04:15 +02:00
parent e7d9dc57d8
commit 24098f35bb
4 changed files with 48 additions and 34 deletions

View File

@@ -29,12 +29,15 @@ use FireflyIII\Models\Attachment;
use Illuminate\Console\Command;
use Illuminate\Contracts\Encryption\DecryptException;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use function Safe\tempnam;
use Safe\Exceptions\FileinfoException;
use Safe\Exceptions\FilesystemException;
use Safe\Exceptions\StringsException;
use function Safe\file_put_contents;
use function Safe\md5_file;
use function Safe\mime_content_type;
use function Safe\tempnam;
class ScansAttachments extends Command
{
@@ -42,10 +45,13 @@ class ScansAttachments extends Command
protected $description = 'Rescan all attachments and re-set the correct MD5 hash and mime.';
protected $signature = 'firefly-iii:scan-attachments';
protected $signature = 'firefly-iii:scan-attachments';
/**
* Execute the console command.
* @throws FilesystemException
* @throws StringsException
* @throws FileinfoException
*/
public function handle(): int
{
@@ -57,7 +63,7 @@ class ScansAttachments extends Command
$fileName = $attachment->fileName();
$encryptedContent = $disk->get($fileName);
if (null === $encryptedContent) {
app('log')->error(sprintf('No content for attachment #%d under filename "%s"', $attachment->id, $fileName));
Log::error(sprintf('No content for attachment #%d under filename "%s"', $attachment->id, $fileName));
continue;
}
@@ -65,18 +71,13 @@ class ScansAttachments extends Command
try {
$decryptedContent = Crypt::decrypt($encryptedContent); // verified
} catch (DecryptException $e) {
app('log')->error(sprintf('Could not decrypt data of attachment #%d: %s', $attachment->id, $e->getMessage()));
Log::error(sprintf('Could not decrypt data of attachment #%d: %s', $attachment->id, $e->getMessage()));
$decryptedContent = $encryptedContent;
}
$tempFileName = tempnam(sys_get_temp_dir(), 'FireflyIII');
if (false === $tempFileName) {
app('log')->error(sprintf('Could not create temporary file for attachment #%d', $attachment->id));
exit(1);
}
$tempFileName = tempnam(sys_get_temp_dir(), 'FireflyIII');
file_put_contents($tempFileName, $decryptedContent);
$attachment->md5 = (string) md5_file($tempFileName);
$attachment->mime = (string) mime_content_type($tempFileName);
$attachment->md5 = (string)md5_file($tempFileName);
$attachment->mime = (string)mime_content_type($tempFileName);
$attachment->save();
$this->friendlyInfo(sprintf('Fixed attachment #%d', $attachment->id));
}