You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
1.7 KiB

  1. <?php
  2. namespace App\Models;
  3. use App\Models\Model;
  4. use Illuminate\Support\Facades\Storage;
  5. class File extends Model
  6. {
  7. protected $table = 'files';
  8. protected $fillable = [
  9. 'user_id', 'business_id', 'project_id', 'disk', 'original_name', 'name',
  10. 'extension', 'mime', 'group','size', 'description'
  11. ];
  12. protected $casts = [];
  13. public function rules()
  14. {
  15. return [
  16. 'user_id' => 'required',
  17. 'business_id' => 'required',
  18. 'project_id' => 'required',
  19. 'original_name' => 'required',
  20. 'name' => 'required',
  21. 'extension' => 'required',
  22. 'size' => 'required',
  23. 'description' => 'nullable',
  24. ];
  25. }
  26. public function user()
  27. {
  28. return $this->belongsTo(User::class, 'user_id','id','id',__FUNCTION__);
  29. }
  30. public function business()
  31. {
  32. return $this->belongsTo(Business::class, 'business_id', 'id', 'id', __FUNCTION__);
  33. }
  34. public function project()
  35. {
  36. return $this->belongsTo(Project::class, 'project_id', 'id', 'id', __FUNCTION__);
  37. }
  38. public function updateRelations()
  39. {
  40. }
  41. public function reportActivity()
  42. {
  43. }
  44. public function getPath()
  45. {
  46. return $this->business->id . \DIRECTORY_SEPARATOR . $this->project->id . DIRECTORY_SEPARATOR . $this->name;
  47. }
  48. public function getTemporaryLink()
  49. {
  50. return Storage::disk('s3')->temporaryUrl(
  51. $this->getPath(),
  52. \Carbon\Carbon::now()->addMinutes(15),
  53. [
  54. 'Content-Type' => $this->mime,
  55. 'Content-Disposition' => $this->original_name,
  56. ]
  57. );
  58. }
  59. }