Files
firefly-iii/app/Console/Commands/System/ScanAttachments.php

84 lines
2.9 KiB
PHP
Raw Normal View History

<?php
2023-04-16 07:33:12 +02:00
/*
2016-09-21 19:16:47 +02:00
* ScanAttachments.php
2023-04-16 07:33:12 +02:00
* Copyright (c) 2023 james@firefly-iii.org
2016-09-21 19:16:47 +02:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
2017-10-21 08:40:00 +02:00
*
* This program is distributed in the hope that it will be useful,
2017-10-21 08:40:00 +02:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
2017-10-21 08:40:00 +02:00
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2016-09-21 19:16:47 +02:00
*/
2018-05-11 10:08:34 +02:00
declare(strict_types=1);
2023-04-16 07:33:12 +02:00
namespace FireflyIII\Console\Commands\System;
2016-09-21 19:16:47 +02:00
use Crypt;
use FireflyIII\Console\Commands\ShowsFriendlyMessages;
2016-09-21 19:16:47 +02:00
use FireflyIII\Models\Attachment;
use Illuminate\Console\Command;
use Illuminate\Contracts\Encryption\DecryptException;
use Storage;
/**
2017-11-15 12:25:49 +01:00
* Class ScanAttachments.
*
2016-09-21 19:16:47 +02:00
*/
class ScanAttachments extends Command
{
use ShowsFriendlyMessages;
2023-11-05 09:54:53 +01:00
2023-04-16 07:33:12 +02:00
protected $description = 'Rescan all attachments and re-set the correct MD5 hash and mime.';
2016-09-21 19:16:47 +02:00
2023-11-05 09:54:53 +01:00
protected $signature = 'firefly-iii:scan-attachments';
2016-09-21 19:16:47 +02:00
/**
* Execute the console command.
*/
2018-07-22 10:05:06 +02:00
public function handle(): int
2016-09-21 19:16:47 +02:00
{
$attachments = Attachment::get();
$disk = Storage::disk('upload');
/** @var Attachment $attachment */
foreach ($attachments as $attachment) {
$fileName = $attachment->fileName();
$encryptedContent = $disk->get($fileName);
if (null === $encryptedContent) {
2023-10-29 06:32:00 +01:00
app('log')->error(sprintf('No content for attachment #%d under filename "%s"', $attachment->id, $fileName));
2016-09-21 19:16:47 +02:00
continue;
}
try {
$decryptedContent = Crypt::decrypt($encryptedContent); // verified
2016-09-21 19:16:47 +02:00
} catch (DecryptException $e) {
2023-10-29 06:32:00 +01:00
app('log')->error(sprintf('Could not decrypt data of attachment #%d: %s', $attachment->id, $e->getMessage()));
$decryptedContent = $encryptedContent;
2016-09-21 19:16:47 +02:00
}
2019-06-07 18:13:54 +02:00
$tempFileName = tempnam(sys_get_temp_dir(), 'FireflyIII');
2023-12-10 06:45:59 +01:00
if (false === $tempFileName) {
2023-11-26 12:10:42 +01:00
app('log')->error(sprintf('Could not create temporary file for attachment #%d', $attachment->id));
exit(1);
}
file_put_contents($tempFileName, $decryptedContent);
2023-12-10 06:45:59 +01:00
$attachment->md5 = (string)md5_file($tempFileName);
$attachment->mime = (string)mime_content_type($tempFileName);
2016-09-21 19:16:47 +02:00
$attachment->save();
$this->friendlyInfo(sprintf('Fixed attachment #%d', $attachment->id));
2016-09-21 19:16:47 +02:00
}
2020-03-21 15:43:41 +01:00
2018-07-22 10:05:06 +02:00
return 0;
2016-09-21 19:16:47 +02:00
}
}