Controller.php 1.3 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. abstract class Controller
  8. {
  9. /** @var Container */
  10. protected $container;
  11. public function __construct(Container $container)
  12. {
  13. $this->container = $container;
  14. }
  15. /**
  16. * @param $name
  17. * @return mixed|null
  18. * @throws \Interop\Container\Exception\ContainerException
  19. */
  20. public function __get($name)
  21. {
  22. if ($this->container->has($name)) {
  23. return $this->container->get($name);
  24. }
  25. return null;
  26. }
  27. /**
  28. * Get a filesystem instance
  29. * @return Filesystem
  30. */
  31. protected function getStorage(): Filesystem
  32. {
  33. return new Filesystem(new Local($this->settings['storage_dir']));
  34. }
  35. /**
  36. * @param $id
  37. * @return int
  38. */
  39. protected function getUsedSpaceByUser($id): int
  40. {
  41. $medias = $this->database->query('SELECT `uploads`.`storage_path` FROM `uploads` WHERE `user_id` = ?', $id)->fetchAll();
  42. $totalSize = 0;
  43. $filesystem = $this->getStorage();
  44. foreach ($medias as $media) {
  45. try {
  46. $totalSize += $filesystem->getSize($media->storage_path);
  47. } catch (FileNotFoundException $e) {
  48. $this->logger->error('Error calculating file size', [$e->getTraceAsString()]);
  49. }
  50. }
  51. return $totalSize;
  52. }
  53. }