Controller.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. namespace App\Controllers;
  3. use League\Flysystem\Adapter\Local;
  4. use League\Flysystem\FileNotFoundException;
  5. use League\Flysystem\Filesystem;
  6. use Slim\Container;
  7. use Slim\Http\Response;
  8. abstract class Controller
  9. {
  10. /** @var Container */
  11. protected $container;
  12. public function __construct(Container $container)
  13. {
  14. $this->container = $container;
  15. }
  16. /**
  17. * @param $name
  18. * @return mixed|null
  19. * @throws \Interop\Container\Exception\ContainerException
  20. */
  21. public function __get($name)
  22. {
  23. if ($this->container->has($name)) {
  24. return $this->container->get($name);
  25. }
  26. return null;
  27. }
  28. /**
  29. * Get a filesystem instance
  30. * @return Filesystem
  31. */
  32. protected function getStorage(): Filesystem
  33. {
  34. return new Filesystem(new Local($this->settings['storage_dir']));
  35. }
  36. /**
  37. * @param $id
  38. * @return int
  39. */
  40. protected function getUsedSpaceByUser($id): int
  41. {
  42. $medias = $this->database->query('SELECT `uploads`.`storage_path` FROM `uploads` WHERE `user_id` = ?', $id)->fetchAll();
  43. $totalSize = 0;
  44. $filesystem = $this->getStorage();
  45. foreach ($medias as $media) {
  46. try {
  47. $totalSize += $filesystem->getSize($media->storage_path);
  48. } catch (FileNotFoundException $e) {
  49. }
  50. }
  51. return $totalSize;
  52. }
  53. /**
  54. * @param Response $response
  55. * @param string $path
  56. * @return Response
  57. */
  58. function redirectTo(Response $response, string $path): Response
  59. {
  60. return $response->withRedirect($this->settings['base_url'] . $path);
  61. }
  62. }