summaryrefslogtreecommitdiff
path: root/app/Models/File.php
blob: 887333a96259a7bd1b636372b21a175c5eaa8e6f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Storage;

class File extends Model
{
    use HasFactory, SoftDeletes;

    protected $fillable = [
        'filename_og',
        'filename_stored',
        'storage_path',
        'disk',
        'mime_type',
        'extension',
        'size',
        'hash',
        'ip_address',
        'user_agent',
        'is_chunked',
        'expires_at',
        'is_expired',
    ];

    protected $casts = [
        'size' => 'integer',
        'download_count' => 'integer',
        'is_chunked' => 'boolean',
        'is_expired' => 'boolean',
        'expires_at' => 'datetime',
        'last_downloaded_at' => 'datetime',
    ];

    // Relationships
    public function activityLogs()
    {
        return $this->morphMany(ActivityLog::class, 'loggable');
    }

    public function downloads()
    {
        return $this->activityLogs()->where('event_type', 'download');
    }

    // Accessors
    public function getFormattedSizeAttribute()
    {
        $bytes = $this->size;
        $units = ['B', 'KB', 'MB', 'GB', 'TB'];

        for ($i = 0; $bytes > 1024 && $i < count($units) - 1; $i++) {
            $bytes /= 1024;
        }

        return round($bytes, 2) . ' ' . $units[$i];
    }

    public function getDownloadUrlAttribute()
    {
        return route('files.download', $this);
    }

    public function getFullPathAttribute()
    {
        return Storage::disk($this->disk)->path($this->storage_path);
    }

    // Methods
    public function logDownload($ipAddress, $userAgent = null)
    {
        // Create activity log entry
        $this->activityLogs()->create([
            'event_type' => 'download',
            'ip_address' => $ipAddress,
            'user_agent' => $userAgent,
        ]);

        // Update cached counters
        $this->increment('download_count');
        $this->update(['last_downloaded_at' => now()]);
    }

    public function setExpiration($days = null)
    {
        $days = $days ?? config('fileupload.expiration_days');

        if (config('fileupload.expiration_enabled')) {
            $this->expires_at = now()->addDays((int) $days);
            $this->save();
        }
    }

    public function isExpired()
    {
        return $this->is_expired ||
               ($this->expires_at && $this->expires_at->isPast());
    }

    // Scopes
    public function scopeNotExpired($query)
    {
        return $query->where(function ($q) {
            $q->where('is_expired', false)
              ->where(function ($q2) {
                  $q2->whereNull('expires_at')
                     ->orWhere('expires_at', '>', now());
              });
        });
    }

    public function scopeByIp($query, string $ip)
    {
        return $query->where('ip_address', $ip);
    }
}