mirror of
				https://github.com/firefly-iii/firefly-iii.git
				synced 2025-10-31 18:54:58 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			251 lines
		
	
	
		
			6.1 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			251 lines
		
	
	
		
			6.1 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?php
 | |
| /**
 | |
|  * Attachment.php
 | |
|  * Copyright (c) 2017 thegrumpydictator@gmail.com
 | |
|  *
 | |
|  * This file is part of Firefly III.
 | |
|  *
 | |
|  * 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
 | |
|  * along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
 | |
|  */
 | |
| declare(strict_types=1);
 | |
| 
 | |
| namespace FireflyIII\Models;
 | |
| 
 | |
| use Carbon\Carbon;
 | |
| use Crypt;
 | |
| use FireflyIII\User;
 | |
| use Illuminate\Database\Eloquent\Model;
 | |
| use Illuminate\Database\Eloquent\Relations\BelongsTo;
 | |
| use Illuminate\Database\Eloquent\Relations\MorphMany;
 | |
| use Illuminate\Database\Eloquent\Relations\MorphTo;
 | |
| use Illuminate\Database\Eloquent\SoftDeletes;
 | |
| use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
 | |
| 
 | |
| /**
 | |
|  * Class Attachment.
 | |
|  *
 | |
|  * @property int    $id
 | |
|  * @property Carbon $created_at
 | |
|  * @property Carbon $updated_at
 | |
|  * @property string $attachable_type
 | |
|  * @property string $md5
 | |
|  * @property string $filename
 | |
|  * @property string $title
 | |
|  * @property string $description
 | |
|  * @property string $notes
 | |
|  * @property string $mime
 | |
|  * @property int    $size
 | |
|  * @property User   $user
 | |
|  * @property bool   $uploaded
 | |
|  * @property bool   file_exists
 | |
|  */
 | |
| class Attachment extends Model
 | |
| {
 | |
|     use SoftDeletes;
 | |
| 
 | |
|     /**
 | |
|      * The attributes that should be casted to native types.
 | |
|      *
 | |
|      * @var array
 | |
|      */
 | |
|     protected $casts
 | |
|         = [
 | |
|             'created_at' => 'datetime',
 | |
|             'updated_at' => 'datetime',
 | |
|             'deleted_at' => 'datetime',
 | |
|             'uploaded'   => 'boolean',
 | |
|         ];
 | |
|     /** @var array Fields that can be filled */
 | |
|     protected $fillable = ['attachable_id', 'attachable_type', 'user_id', 'md5', 'filename', 'mime', 'title', 'description', 'size', 'uploaded'];
 | |
| 
 | |
|     /**
 | |
|      * Route binder. Converts the key in the URL to the specified object (or throw 404).
 | |
|      *
 | |
|      * @param string $value
 | |
|      *
 | |
|      * @return Attachment
 | |
|      * @throws NotFoundHttpException
 | |
|      */
 | |
|     public static function routeBinder(string $value): Attachment
 | |
|     {
 | |
|         if (auth()->check()) {
 | |
|             $attachmentId = (int)$value;
 | |
|             /** @var User $user */
 | |
|             $user = auth()->user();
 | |
|             /** @var Attachment $attachment */
 | |
|             $attachment = $user->attachments()->find($attachmentId);
 | |
|             if (null !== $attachment) {
 | |
|                 return $attachment;
 | |
|             }
 | |
|         }
 | |
|         throw new NotFoundHttpException;
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * Get all of the owning attachable models.
 | |
|      *
 | |
|      * @codeCoverageIgnore
 | |
|      *
 | |
|      * @return MorphTo
 | |
|      */
 | |
|     public function attachable(): MorphTo
 | |
|     {
 | |
|         return $this->morphTo();
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * Returns the expected filename for this attachment.
 | |
|      *
 | |
|      * @codeCoverageIgnore
 | |
|      * @return string
 | |
|      */
 | |
|     public function fileName(): string
 | |
|     {
 | |
|         return sprintf('at-%s.data', (string)$this->id);
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * @param $value
 | |
|      *
 | |
|      * @codeCoverageIgnore
 | |
|      * @return null|string
 | |
|      * @throws \Illuminate\Contracts\Encryption\DecryptException
 | |
|      */
 | |
|     public function getDescriptionAttribute($value): ?string
 | |
|     {
 | |
|         if (null === $value || '' === $value) {
 | |
|             return null;
 | |
|         }
 | |
| 
 | |
|         return Crypt::decrypt($value);
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * @param $value
 | |
|      *
 | |
|      * @codeCoverageIgnore
 | |
|      * @return null|string
 | |
|      * @throws \Illuminate\Contracts\Encryption\DecryptException
 | |
|      */
 | |
|     public function getFilenameAttribute($value): ?string
 | |
|     {
 | |
|         if (null === $value || '' === $value) {
 | |
|             return null;
 | |
|         }
 | |
| 
 | |
|         return Crypt::decrypt($value);
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * @param $value
 | |
|      *
 | |
|      * @codeCoverageIgnore
 | |
|      * @return null|string
 | |
|      * @throws \Illuminate\Contracts\Encryption\DecryptException
 | |
|      */
 | |
|     public function getMimeAttribute($value): ?string
 | |
|     {
 | |
|         if (null === $value || '' === $value) {
 | |
|             return null;
 | |
|         }
 | |
| 
 | |
|         return Crypt::decrypt($value);
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * @param $value
 | |
|      *
 | |
|      * @codeCoverageIgnore
 | |
|      * @return null|string
 | |
|      * @throws \Illuminate\Contracts\Encryption\DecryptException
 | |
|      */
 | |
|     public function getTitleAttribute($value): ?string
 | |
|     {
 | |
|         if (null === $value || '' === $value) {
 | |
|             return null;
 | |
|         }
 | |
| 
 | |
|         return Crypt::decrypt($value);
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * @codeCoverageIgnore
 | |
|      * Get all of the notes.
 | |
|      */
 | |
|     public function notes(): MorphMany
 | |
|     {
 | |
|         return $this->morphMany(Note::class, 'noteable');
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * @codeCoverageIgnore
 | |
|      *
 | |
|      * @param string|null $value
 | |
|      */
 | |
|     public function setDescriptionAttribute(string $value = null): void
 | |
|     {
 | |
|         if (null !== $value) {
 | |
|             $this->attributes['description'] = Crypt::encrypt($value);
 | |
|         }
 | |
| 
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * @codeCoverageIgnore
 | |
|      *
 | |
|      * @param string $value
 | |
|      *
 | |
|      * @throws \Illuminate\Contracts\Encryption\EncryptException
 | |
|      */
 | |
|     public function setFilenameAttribute(string $value): void
 | |
|     {
 | |
|         $this->attributes['filename'] = Crypt::encrypt($value);
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * @codeCoverageIgnore
 | |
|      *
 | |
|      * @param string $value
 | |
|      *
 | |
|      * @throws \Illuminate\Contracts\Encryption\EncryptException
 | |
|      */
 | |
|     public function setMimeAttribute(string $value): void
 | |
|     {
 | |
|         $this->attributes['mime'] = Crypt::encrypt($value);
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * @codeCoverageIgnore
 | |
|      *
 | |
|      * @param string $value
 | |
|      *
 | |
|      * @throws \Illuminate\Contracts\Encryption\EncryptException
 | |
|      */
 | |
|     public function setTitleAttribute(string $value = null): void
 | |
|     {
 | |
|         if (null !== $value) {
 | |
|             $this->attributes['title'] = Crypt::encrypt($value);
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * @codeCoverageIgnore
 | |
|      * @return BelongsTo
 | |
|      */
 | |
|     public function user(): BelongsTo
 | |
|     {
 | |
|         return $this->belongsTo(User::class);
 | |
|     }
 | |
| }
 |