Simplified the attachment collector.

This commit is contained in:
James Cole
2016-02-23 06:54:51 +01:00
parent 155801ab2b
commit 0c9c0f2032
2 changed files with 136 additions and 85 deletions

View File

@@ -36,6 +36,22 @@ class Attachment extends Model
protected $fillable = ['attachable_id', 'attachable_type', 'user_id', 'md5', 'filename', 'mime', 'title', 'notes', 'description', 'size', 'uploaded'];
/**
* @param Attachment $value
*
* @return Attachment
*/
public static function routeBinder(Attachment $value)
{
if (Auth::check()) {
if ($value->user_id == Auth::user()->id) {
return $value;
}
}
throw new NotFoundHttpException;
}
/**
* Get all of the owning imageable models.
*/
@@ -45,14 +61,30 @@ class Attachment extends Model
}
/**
* @codeCoverageIgnore
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
* Returns the expected filename for this attachment.
*
* @return string
*/
public function user()
public function fileName(): string
{
return $this->belongsTo('FireflyIII\User');
return sprintf('at-%s.data', strval($this->id));
}
/**
* @codeCoverageIgnore
*
* @param $value
*
* @return null|string
*/
public function getDescriptionAttribute($value)
{
if (is_null($value)) {
return null;
}
return Crypt::decrypt($value);
}
/**
* @codeCoverageIgnore
@@ -70,14 +102,6 @@ class Attachment extends Model
return Crypt::decrypt($value);
}
/**
* @param string $value
*/
public function setFilenameAttribute($value)
{
$this->attributes['filename'] = Crypt::encrypt($value);
}
/**
* @codeCoverageIgnore
*
@@ -95,11 +119,19 @@ class Attachment extends Model
}
/**
* @param string $value
* @codeCoverageIgnore
*
* @param $value
*
* @return null|string
*/
public function setMimeAttribute($value)
public function getNotesAttribute($value)
{
$this->attributes['mime'] = Crypt::encrypt($value);
if (is_null($value)) {
return null;
}
return Crypt::decrypt($value);
}
/**
@@ -118,30 +150,6 @@ class Attachment extends Model
return Crypt::decrypt($value);
}
/**
* @param string $value
*/
public function setTitleAttribute($value)
{
$this->attributes['title'] = Crypt::encrypt($value);
}
/**
* @codeCoverageIgnore
*
* @param $value
*
* @return null|string
*/
public function getDescriptionAttribute($value)
{
if (is_null($value)) {
return null;
}
return Crypt::decrypt($value);
}
/**
* @param string $value
*/
@@ -151,19 +159,19 @@ class Attachment extends Model
}
/**
* @codeCoverageIgnore
*
* @param $value
*
* @return null|string
* @param string $value
*/
public function getNotesAttribute($value)
public function setFilenameAttribute($value)
{
if (is_null($value)) {
return null;
}
$this->attributes['filename'] = Crypt::encrypt($value);
}
return Crypt::decrypt($value);
/**
* @param string $value
*/
public function setMimeAttribute($value)
{
$this->attributes['mime'] = Crypt::encrypt($value);
}
/**
@@ -175,19 +183,20 @@ class Attachment extends Model
}
/**
* @param Attachment $value
*
* @return Attachment
* @param string $value
*/
public static function routeBinder(Attachment $value)
public function setTitleAttribute($value)
{
if (Auth::check()) {
$this->attributes['title'] = Crypt::encrypt($value);
}
if ($value->user_id == Auth::user()->id) {
return $value;
}
}
throw new NotFoundHttpException;
/**
* @codeCoverageIgnore
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function user()
{
return $this->belongsTo('FireflyIII\User');
}
}