'integer', 'size' => 'integer', 'order' => 'integer', 'meta' => 'array', 'created_at' => 'datetime', 'updated_at' => 'datetime', 'deleted_at' => 'datetime', ]; } /** * 모델 부트 메서드 */ protected static function boot(): void { parent::boot(); static::creating(function (Attachment $attachment) { if (empty($attachment->hash)) { $attachment->hash = self::generateUniqueHash(); } }); } /** * 고유한 해시를 생성합니다. * * @return string 생성된 고유 해시 */ protected static function generateUniqueHash(): string { do { $hash = Str::random(12); } while (self::withTrashed()->where('hash', $hash)->exists()); return $hash; } /** * 게시판과의 관계를 정의합니다. */ public function board(): BelongsTo { return $this->belongsTo(Board::class, 'board_id'); } /** * 게시글과의 관계를 정의합니다. */ public function post(): BelongsTo { return $this->belongsTo(Post::class, 'post_id'); } /** * 업로더와의 관계를 정의합니다. */ public function creator(): BelongsTo { return $this->belongsTo(User::class, 'created_by'); } /** * 다운로드 URL 반환 (API 서빙 URL) * * @return string 다운로드 URL */ public function getDownloadUrlAttribute(): string { $boardSlug = $this->relationLoaded('board') ? $this->board->slug : Board::find($this->board_id)?->slug; return '/api/modules/sirsoft-board/boards/'.$boardSlug.'/attachment/'.$this->hash; } /** * 이미지 미리보기 URL 반환 * * @return string|null 미리보기 URL 또는 null */ public function getPreviewUrlAttribute(): ?string { if (! $this->is_image) { return null; } $boardSlug = $this->relationLoaded('board') ? $this->board->slug : Board::find($this->board_id)?->slug; return '/api/modules/sirsoft-board/boards/'.$boardSlug.'/attachment/'.$this->hash.'/preview'; } /** * 이미지 여부 확인 * * @return bool 이미지 여부 */ public function getIsImageAttribute(): bool { return str_starts_with($this->mime_type, 'image/'); } /** * 파일 크기 포맷팅 (KB, MB 등) * * @return string 포맷된 파일 크기 */ public function getSizeFormattedAttribute(): string { $bytes = $this->size; $units = ['B', 'KB', 'MB', 'GB']; $i = 0; while ($bytes >= 1024 && $i < count($units) - 1) { $bytes /= 1024; $i++; } return round($bytes, 2).' '.$units[$i]; } }