StoryRotateMedia.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. namespace App\Jobs\StoryPipeline;
  3. use Illuminate\Support\Facades\Storage;
  4. use Illuminate\Support\Str;
  5. use App\Story;
  6. use Illuminate\Bus\Queueable;
  7. use Illuminate\Contracts\Queue\ShouldQueue;
  8. use Illuminate\Foundation\Bus\Dispatchable;
  9. use Illuminate\Queue\InteractsWithQueue;
  10. use Illuminate\Queue\SerializesModels;
  11. use App\Util\ActivityPub\Helpers;
  12. class StoryRotateMedia implements ShouldQueue
  13. {
  14. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  15. protected $story;
  16. /**
  17. * Create a new job instance.
  18. *
  19. * @return void
  20. */
  21. public function __construct(Story $story)
  22. {
  23. $this->story = $story;
  24. }
  25. /**
  26. * Execute the job.
  27. *
  28. * @return void
  29. */
  30. public function handle()
  31. {
  32. $story = $this->story;
  33. if($story->local == false) {
  34. return;
  35. }
  36. $paths = explode('/', $story->path);
  37. $name = array_pop($paths);
  38. $oldPath = $story->path;
  39. $ext = pathinfo($name, PATHINFO_EXTENSION);
  40. $new = Str::random(13) . '_' . Str::random(24) . '_' . Str::random(3) . '.' . $ext;
  41. array_push($paths, $new);
  42. $newPath = implode('/', $paths);
  43. if(Storage::exists($oldPath)) {
  44. Storage::copy($oldPath, $newPath);
  45. $story->path = $newPath;
  46. $story->bearcap_token = null;
  47. $story->save();
  48. Storage::delete($oldPath);
  49. }
  50. }
  51. }