Upload.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php namespace app\common\models;
  2. use Illuminate\Database\Eloquent\Model;
  3. use zgldh\UploadManager\UploadManager;
  4. /**
  5. * Class Upload
  6. * @property string $name
  7. * @property string $description
  8. * @property string $disk
  9. * @property string $path
  10. * @property string $size
  11. * @property string $user_id
  12. * @package App
  13. */
  14. class Upload extends Model
  15. {
  16. protected $table = 'yz_uploads';
  17. public function user()
  18. {
  19. return $this->belongsTo('app\common\models\user\User', 'user_id', 'uid');
  20. }
  21. public function uploadable()
  22. {
  23. return $this->morphTo();
  24. }
  25. public function getUrlAttribute()
  26. {
  27. $manager = UploadManager::getInstance();
  28. $url = $manager->getUploadUrl($this->disk, $this->path);
  29. return $url;
  30. }
  31. public function deleteFile($autoSave = true)
  32. {
  33. if ($this->path) {
  34. $disk = \Storage::disk($this->disk);
  35. if ($disk->exists($this->path)) {
  36. $disk->delete($this->path);
  37. $this->path = '';
  38. if($autoSave)
  39. {
  40. $this->save();
  41. }
  42. }
  43. }
  44. }
  45. public function isInDisk($diskName)
  46. {
  47. return $this->disk == $diskName ? true : false;
  48. }
  49. public function moveToDisk($newDiskName)
  50. {
  51. if ($newDiskName == $this->disk) {
  52. return true;
  53. }
  54. $currentDisk = \Storage::disk($this->disk);
  55. $content = $currentDisk->get($this->path);
  56. $newDisk = \Storage::disk($newDiskName);
  57. $newDisk->put($this->path, $content);
  58. if ($newDisk->exists($this->path)) {
  59. $this->disk = $newDiskName;
  60. $this->save();
  61. $currentDisk->delete($this->path);
  62. return true;
  63. }
  64. return false;
  65. }
  66. }