mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2026-05-04 05:06:37 +00:00
chore: code cleanup.
This commit is contained in:
@@ -32,9 +32,9 @@ use Illuminate\Contracts\Encryption\EncryptException;
|
||||
use Illuminate\Contracts\Filesystem\Filesystem;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\MessageBag;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Symfony\Component\HttpFoundation\File\UploadedFile;
|
||||
|
||||
/**
|
||||
@@ -211,6 +211,39 @@ class AttachmentHelper implements AttachmentHelperInterface
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a model already has this file attached.
|
||||
*
|
||||
* @param UploadedFile $file
|
||||
* @param Model $model
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function hasFile(UploadedFile $file, Model $model): bool
|
||||
{
|
||||
$md5 = md5_file($file->getRealPath());
|
||||
$name = $file->getClientOriginalName();
|
||||
$class = get_class($model);
|
||||
$count = 0;
|
||||
// ignore lines about polymorphic calls.
|
||||
if ($model instanceof PiggyBank) {
|
||||
$count = $model->account->user->attachments()->where('md5', $md5)->where('attachable_id', $model->id)->where('attachable_type', $class)->count();
|
||||
}
|
||||
if ($model instanceof PiggyBank) {
|
||||
$count = $model->user->attachments()->where('md5', $md5)->where('attachable_id', $model->id)->where('attachable_type', $class)->count(
|
||||
); // @phpstan-ignore-line
|
||||
}
|
||||
$result = false;
|
||||
if ($count > 0) {
|
||||
$msg = (string)trans('validation.file_already_attached', ['name' => $name]);
|
||||
$this->errors->add('attachments', $msg);
|
||||
Log::error($msg);
|
||||
$result = true;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the upload of a file.
|
||||
*
|
||||
@@ -268,39 +301,6 @@ class AttachmentHelper implements AttachmentHelperInterface
|
||||
return $attachment;
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify if the file was uploaded correctly.
|
||||
*
|
||||
* @param UploadedFile $file
|
||||
* @param Model $model
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function validateUpload(UploadedFile $file, Model $model): bool
|
||||
{
|
||||
Log::debug('Now in validateUpload()');
|
||||
$result = true;
|
||||
if (!$this->validMime($file)) {
|
||||
$result = false;
|
||||
}
|
||||
if (0 === $file->getSize()) {
|
||||
Log::error('Cannot upload empty file.');
|
||||
$result = false;
|
||||
}
|
||||
|
||||
|
||||
// can't seem to reach this point.
|
||||
if (true === $result && !$this->validSize($file)) {
|
||||
$result = false;
|
||||
}
|
||||
|
||||
if (true === $result && $this->hasFile($file, $model)) {
|
||||
$result = false;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify if the mime of a file is valid.
|
||||
*
|
||||
@@ -353,33 +353,33 @@ class AttachmentHelper implements AttachmentHelperInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a model already has this file attached.
|
||||
* Verify if the file was uploaded correctly.
|
||||
*
|
||||
* @param UploadedFile $file
|
||||
* @param Model $model
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function hasFile(UploadedFile $file, Model $model): bool
|
||||
protected function validateUpload(UploadedFile $file, Model $model): bool
|
||||
{
|
||||
$md5 = md5_file($file->getRealPath());
|
||||
$name = $file->getClientOriginalName();
|
||||
$class = get_class($model);
|
||||
$count = 0;
|
||||
// ignore lines about polymorphic calls.
|
||||
if ($model instanceof PiggyBank) {
|
||||
$count = $model->account->user->attachments()->where('md5', $md5)->where('attachable_id', $model->id)->where('attachable_type', $class)->count();
|
||||
Log::debug('Now in validateUpload()');
|
||||
$result = true;
|
||||
if (!$this->validMime($file)) {
|
||||
$result = false;
|
||||
}
|
||||
if ($model instanceof PiggyBank) {
|
||||
$count = $model->user->attachments()->where('md5', $md5)->where('attachable_id', $model->id)->where('attachable_type', $class)->count(
|
||||
); // @phpstan-ignore-line
|
||||
if (0 === $file->getSize()) {
|
||||
Log::error('Cannot upload empty file.');
|
||||
$result = false;
|
||||
}
|
||||
$result = false;
|
||||
if ($count > 0) {
|
||||
$msg = (string)trans('validation.file_already_attached', ['name' => $name]);
|
||||
$this->errors->add('attachments', $msg);
|
||||
Log::error($msg);
|
||||
$result = true;
|
||||
|
||||
|
||||
// can't seem to reach this point.
|
||||
if (true === $result && !$this->validSize($file)) {
|
||||
$result = false;
|
||||
}
|
||||
|
||||
if (true === $result && $this->hasFile($file, $model)) {
|
||||
$result = false;
|
||||
}
|
||||
|
||||
return $result;
|
||||
|
||||
Reference in New Issue
Block a user