firefly-iii/app/Helpers/Attachments/AttachmentHelper.php

267 lines
7.6 KiB
PHP
Raw Normal View History

2015-07-18 02:49:29 -05:00
<?php
/**
* AttachmentHelper.php
2017-10-21 01:40:00 -05:00
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 01:40:00 -05:00
* This file is part of Firefly III.
*
2017-10-21 01:40:00 -05:00
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2017-12-17 07:41:58 -06:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
2015-07-18 02:49:29 -05:00
namespace FireflyIII\Helpers\Attachments;
use Crypt;
use FireflyIII\Models\Attachment;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
2015-07-18 02:49:29 -05:00
use Illuminate\Support\MessageBag;
2017-06-12 12:58:32 -05:00
use Log;
use Storage;
2016-02-16 11:39:03 -06:00
use Symfony\Component\HttpFoundation\File\UploadedFile;
2017-06-05 04:12:50 -05:00
2015-07-18 02:49:29 -05:00
/**
2017-11-15 05:25:49 -06:00
* Class AttachmentHelper.
2015-07-18 02:49:29 -05:00
*/
class AttachmentHelper implements AttachmentHelperInterface
{
2017-11-15 05:25:49 -06:00
/** @var Collection */
public $attachments;
2015-07-18 15:17:31 -05:00
/** @var MessageBag */
2015-07-18 02:49:29 -05:00
public $errors;
2015-07-18 15:17:31 -05:00
/** @var MessageBag */
2015-07-18 02:49:29 -05:00
public $messages;
2016-01-19 06:59:54 -06:00
/** @var array */
2017-01-14 12:43:33 -06:00
protected $allowedMimes = [];
2016-01-19 06:59:54 -06:00
/** @var int */
2017-01-14 12:43:33 -06:00
protected $maxUploadSize = 0;
2015-07-18 02:49:29 -05:00
/** @var \Illuminate\Contracts\Filesystem\Filesystem */
protected $uploadDisk;
2018-02-28 14:32:59 -06:00
2015-07-18 02:49:29 -05:00
/**
2018-02-28 14:32:59 -06:00
* AttachmentHelper constructor.
2015-07-18 02:49:29 -05:00
*/
public function __construct()
{
2017-01-14 12:43:33 -06:00
$this->maxUploadSize = intval(config('firefly.maxUploadSize'));
2017-02-16 23:42:36 -06:00
$this->allowedMimes = (array)config('firefly.allowedMimes');
2015-07-18 15:17:31 -05:00
$this->errors = new MessageBag;
$this->messages = new MessageBag;
$this->attachments = new Collection;
$this->uploadDisk = Storage::disk('upload');
2015-07-18 02:49:29 -05:00
}
2015-07-18 14:32:31 -05:00
/**
* @param Attachment $attachment
*
2015-07-18 15:17:31 -05:00
* @return string
2015-07-18 14:32:31 -05:00
*/
2016-02-18 00:21:48 -06:00
public function getAttachmentLocation(Attachment $attachment): string
2015-07-18 14:32:31 -05:00
{
2016-09-25 01:20:17 -05:00
$path = sprintf('%s%sat-%d.data', storage_path('upload'), DIRECTORY_SEPARATOR, intval($attachment->id));
2015-07-18 14:32:31 -05:00
return $path;
}
/**
* @return Collection
*/
public function getAttachments(): Collection
{
return $this->attachments;
}
2016-01-19 06:59:54 -06:00
/**
* @return MessageBag
*/
2016-02-18 00:21:48 -06:00
public function getErrors(): MessageBag
2016-01-19 06:59:54 -06:00
{
return $this->errors;
}
/**
* @return MessageBag
*/
2016-02-18 00:21:48 -06:00
public function getMessages(): MessageBag
2016-01-19 06:59:54 -06:00
{
return $this->messages;
}
2015-07-18 02:49:29 -05:00
/**
2016-12-28 06:02:56 -06:00
* @param Model $model
* @param array|null $files
2015-07-18 02:49:29 -05:00
*
* @return bool
*/
2017-07-23 12:06:24 -05:00
public function saveAttachmentsForModel(Model $model, ?array $files): bool
2015-07-18 02:49:29 -05:00
{
2017-11-26 02:54:09 -06:00
Log::debug(sprintf('Now in saveAttachmentsForModel for model %s', get_class($model)));
2015-07-25 11:33:19 -05:00
if (is_array($files)) {
2017-11-26 02:54:09 -06:00
Log::debug('$files is an array.');
2017-10-27 05:59:43 -05:00
/** @var UploadedFile $entry */
2016-12-28 06:02:56 -06:00
foreach ($files as $entry) {
2017-11-15 05:25:49 -06:00
if (null !== $entry) {
2016-12-28 06:02:56 -06:00
$this->processFile($entry, $model);
}
}
2017-10-27 05:59:43 -05:00
Log::debug('Done processing uploads.');
return true;
2015-07-18 02:49:29 -05:00
}
2017-10-27 05:59:43 -05:00
Log::debug('Array of files is not an array. Probably nothing uploaded. Will not store attachments.');
2015-07-18 02:49:29 -05:00
return true;
}
/**
* @param UploadedFile $file
* @param Model $model
*
* @return bool
*/
2016-02-18 00:21:48 -06:00
protected function hasFile(UploadedFile $file, Model $model): bool
2015-07-18 02:49:29 -05:00
{
$md5 = md5_file($file->getRealPath());
$name = $file->getClientOriginalName();
2015-07-18 02:49:29 -05:00
$class = get_class($model);
$count = $model->user->attachments()->where('md5', $md5)->where('attachable_id', $model->id)->where('attachable_type', $class)->count();
2015-07-18 02:49:29 -05:00
if ($count > 0) {
$msg = (string)trans('validation.file_already_attached', ['name' => $name]);
2015-07-18 15:17:31 -05:00
$this->errors->add('attachments', $msg);
2017-10-27 05:59:43 -05:00
Log::error($msg);
return true;
}
return false;
2015-07-18 02:49:29 -05:00
}
2015-07-18 15:17:31 -05:00
/**
* @param UploadedFile $file
* @param Model $model
*
2016-02-18 00:21:48 -06:00
* @return Attachment
2015-07-18 15:17:31 -05:00
*/
2016-02-18 00:21:48 -06:00
protected function processFile(UploadedFile $file, Model $model): Attachment
2015-07-18 15:17:31 -05:00
{
2017-11-26 02:54:09 -06:00
Log::debug('Now in processFile()');
2015-07-18 15:17:31 -05:00
$validation = $this->validateUpload($file, $model);
2017-11-15 05:25:49 -06:00
if (false === $validation) {
2016-02-18 00:21:48 -06:00
return new Attachment;
2015-07-18 15:17:31 -05:00
}
$attachment = new Attachment; // create Attachment object.
$attachment->user()->associate($model->user);
2015-07-18 02:49:29 -05:00
$attachment->attachable()->associate($model);
$attachment->md5 = md5_file($file->getRealPath());
2015-07-18 02:49:29 -05:00
$attachment->filename = $file->getClientOriginalName();
$attachment->mime = $file->getMimeType();
$attachment->size = $file->getSize();
$attachment->uploaded = 0;
$attachment->save();
2017-06-12 12:58:32 -05:00
Log::debug('Created attachment:', $attachment->toArray());
2015-07-18 02:49:29 -05:00
$fileObject = $file->openFile('r');
$fileObject->rewind();
$content = $fileObject->fread($file->getSize());
2015-07-18 02:49:29 -05:00
$encrypted = Crypt::encrypt($content);
2017-06-12 12:58:32 -05:00
Log::debug(sprintf('Full file length is %d and upload size is %d.', strlen($content), $file->getSize()));
Log::debug(sprintf('Encrypted content is %d', strlen($encrypted)));
2015-07-18 02:49:29 -05:00
// store it:
$this->uploadDisk->put($attachment->fileName(), $encrypted);
2015-07-18 15:17:31 -05:00
$attachment->uploaded = 1; // update attachment
2015-07-18 02:49:29 -05:00
$attachment->save();
$this->attachments->push($attachment);
2015-07-18 02:49:29 -05:00
2015-07-18 15:17:31 -05:00
$name = e($file->getClientOriginalName()); // add message:
$msg = (string)trans('validation.file_attached', ['name' => $name]);
2015-07-18 15:17:31 -05:00
$this->messages->add('attachments', $msg);
2015-07-18 02:49:29 -05:00
// return it.
return $attachment;
}
/**
* @param UploadedFile $file
*
* @return bool
*/
2016-02-18 00:21:48 -06:00
protected function validMime(UploadedFile $file): bool
2015-07-18 02:49:29 -05:00
{
2017-11-26 02:54:09 -06:00
Log::debug('Now in validMime()');
2015-07-18 15:17:31 -05:00
$mime = e($file->getMimeType());
$name = e($file->getClientOriginalName());
2017-11-26 02:54:09 -06:00
Log::debug(sprintf('Name is %, and mime is %s', $name, $mime));
Log::debug('Valid mimes are', $this->allowedMimes);
2015-07-18 02:49:29 -05:00
if (!in_array($mime, $this->allowedMimes)) {
$msg = (string)trans('validation.file_invalid_mime', ['name' => $name, 'mime' => $mime]);
2015-07-18 15:17:31 -05:00
$this->errors->add('attachments', $msg);
2017-10-27 05:59:43 -05:00
Log::error($msg);
2015-07-18 02:49:29 -05:00
return false;
}
return true;
}
/**
* @codeCoverageIgnore
2017-06-05 04:12:50 -05:00
*
* @param UploadedFile $file
*
* @return bool
*/
2016-02-18 00:21:48 -06:00
protected function validSize(UploadedFile $file): bool
2015-07-18 02:49:29 -05:00
{
$size = $file->getSize();
2015-07-18 15:17:31 -05:00
$name = e($file->getClientOriginalName());
2015-07-18 02:49:29 -05:00
if ($size > $this->maxUploadSize) {
$msg = (string)trans('validation.file_too_large', ['name' => $name]);
2015-07-18 15:17:31 -05:00
$this->errors->add('attachments', $msg);
2017-10-27 05:59:43 -05:00
Log::error($msg);
2015-07-18 02:49:29 -05:00
return false;
}
return true;
}
/**
2016-01-19 06:59:54 -06:00
* @param UploadedFile $file
* @param Model $model
*
* @return bool
2015-07-18 02:49:29 -05:00
*/
2016-02-18 00:21:48 -06:00
protected function validateUpload(UploadedFile $file, Model $model): bool
2015-07-18 02:49:29 -05:00
{
2017-11-26 02:54:09 -06:00
Log::debug('Now in validateUpload()');
2016-01-19 06:59:54 -06:00
if (!$this->validMime($file)) {
return false;
}
if (!$this->validSize($file)) {
return false; // @codeCoverageIgnore
2016-01-19 06:59:54 -06:00
}
if ($this->hasFile($file, $model)) {
return false;
}
2015-07-18 02:49:29 -05:00
2016-01-19 06:59:54 -06:00
return true;
2015-07-18 02:49:29 -05:00
}
2015-07-19 02:38:44 -05:00
}