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

93 lines
3.0 KiB
PHP
Raw Normal View History

<?php
2016-09-21 19:16:47 +02:00
/**
* ScanAttachments.php
2020-01-23 20:35:02 +01:00
* Copyright (c) 2020 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
*/
/** @noinspection PhpDynamicAsStaticMethodCallInspection */
2018-05-11 10:08:34 +02:00
declare(strict_types=1);
2016-09-21 19:16:47 +02:00
namespace FireflyIII\Console\Commands;
use Crypt;
use FireflyIII\Models\Attachment;
use Illuminate\Console\Command;
use Illuminate\Contracts\Encryption\DecryptException;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Log;
2016-09-21 19:16:47 +02:00
use Storage;
/**
2017-11-15 12:25:49 +01:00
* Class ScanAttachments.
*
* @codeCoverageIgnore
2016-09-21 19:16:47 +02:00
*/
class ScanAttachments extends Command
{
/**
* The console command description.
*
* @var string
*/
protected $description = 'Rescan all attachments and re-set the MD5 hash and mime.';
/**
* The name and signature of the console command.
*
* @var string
*/
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();
$decryptedContent = '';
2016-09-21 19:16:47 +02:00
try {
$encryptedContent = $disk->get($fileName);
2016-09-21 19:16:47 +02:00
} catch (FileNotFoundException $e) {
$this->error(sprintf('Could not find data for attachment #%d: %s', $attachment->id, $e->getMessage()));
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) {
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');
file_put_contents($tempFileName, $decryptedContent);
2019-06-07 18:13:54 +02:00
$md5 = md5_file($tempFileName);
$mime = mime_content_type($tempFileName);
2016-09-21 19:16:47 +02:00
$attachment->md5 = $md5;
$attachment->mime = $mime;
$attachment->save();
$this->line(sprintf('Fixed attachment #%d', $attachment->id));
}
2018-08-06 19:14:30 +02:00
2018-07-22 10:05:06 +02:00
return 0;
2016-09-21 19:16:47 +02:00
}
}