. */ 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); } }