Controller.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. }