| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBoards\App\Models; |
| 4 |
|
| 5 |
use FluentBoards\App\Models\Comment; |
| 6 |
use FluentBoards\App\Models\Task; |
| 7 |
use FluentBoards\App\Services\Constant; |
| 8 |
|
| 9 |
class CommentImage extends Attachment |
| 10 |
{ |
| 11 |
public static function boot() |
| 12 |
{ |
| 13 |
parent::boot(); |
| 14 |
|
| 15 |
static::creating(function ($model) { |
| 16 |
$uid = wp_generate_uuid4(); |
| 17 |
$model->file_hash = md5($uid . wp_rand(0, 1000)); |
| 18 |
}); |
| 19 |
} |
| 20 |
|
| 21 |
public function scopeWithComment($query) |
| 22 |
{ |
| 23 |
return $query->where('object_type', Constant::COMMENT_IMAGE)->with('comment', function ($query) { |
| 24 |
$query->select('id', 'board_id'); |
| 25 |
}); |
| 26 |
} |
| 27 |
|
| 28 |
public function comment() |
| 29 |
{ |
| 30 |
return $this->belongsTo(Comment::class, 'object_id', 'id'); |
| 31 |
} |
| 32 |
|
| 33 |
public function getSecureUrlAttribute() |
| 34 |
{ |
| 35 |
if ($this->attachment_type === 'url') { |
| 36 |
return $this->full_url; |
| 37 |
} |
| 38 |
return add_query_arg([ |
| 39 |
'fbs' => 1, |
| 40 |
'fbs_comment_image' => $this->file_hash, |
| 41 |
'secure_sign' => md5($this->id . gmdate('YmdH')) |
| 42 |
], site_url('/index.php')); |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
} |
| 47 |
|
| 48 |
|