/src/Clouds/Cloud.php

https://github.com/aminyazdanpanah/PHP-FFmpeg-video-streaming · PHP · 64 lines · 32 code · 10 blank · 22 comment · 4 complexity · c658e509b91e3cc7e7099d1b84bcc73b MD5 · raw file

  1. <?php
  2. /**
  3. * This file is part of the PHP-FFmpeg-video-streaming package.
  4. *
  5. * (c) Amin Yazdanpanah <contact@aminyazdanpanah.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Streaming\Clouds;
  11. use Streaming\Exception\InvalidArgumentException;
  12. use Streaming\File;
  13. class Cloud
  14. {
  15. /**
  16. * @param array $clouds
  17. * @param string $tmp_dir
  18. */
  19. public static function uploadDirectory(array $clouds, string $tmp_dir): void
  20. {
  21. if (isset($clouds['cloud'])) {
  22. $clouds = [$clouds];
  23. }
  24. foreach ($clouds as $cloud) {
  25. static::transfer($cloud, __FUNCTION__, $tmp_dir);
  26. }
  27. }
  28. /**
  29. * @param array $cloud
  30. * @param string|null $save_to
  31. * @return array
  32. */
  33. public static function download(array $cloud, string $save_to = null): array
  34. {
  35. $prefix = $cloud['options']['Key'] ?? $cloud['options']['object_name'] ?? $cloud['options']['blob'] ?? uniqid('stream_', true);
  36. list($save_to, $is_tmp) = $save_to ? [$save_to, false] : [File::tmp($prefix), true];
  37. static::transfer($cloud, __FUNCTION__, $save_to);
  38. return [$save_to, $is_tmp];
  39. }
  40. /**
  41. * @param $cloud_c
  42. * @param $method
  43. * @param $path
  44. */
  45. private static function transfer(array $cloud_c, string $method, string $path): void
  46. {
  47. extract($cloud_c);
  48. if (isset($cloud) && $cloud instanceof CloudInterface) {
  49. call_user_func_array([$cloud, $method], [$path, $options ?? []]);
  50. } else {
  51. throw new InvalidArgumentException('The cloud key must be instance of the CloudInterface');
  52. }
  53. }
  54. }