2018-04-28 12:20:07 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Controllers;
|
|
|
|
|
2019-01-10 22:22:19 +00:00
|
|
|
use App\Database\DB;
|
|
|
|
use App\Web\Session;
|
2018-11-12 17:56:12 +00:00
|
|
|
use League\Flysystem\FileNotFoundException;
|
2019-05-19 13:39:42 +00:00
|
|
|
use League\Flysystem\Filesystem;
|
2019-01-10 22:22:19 +00:00
|
|
|
use Monolog\Logger;
|
2018-11-11 16:02:50 +00:00
|
|
|
use Slim\Container;
|
2018-04-28 12:20:07 +00:00
|
|
|
|
2019-01-10 22:22:19 +00:00
|
|
|
/**
|
|
|
|
* @property Session|null session
|
|
|
|
* @property mixed|null view
|
|
|
|
* @property DB|null database
|
|
|
|
* @property Logger|null logger
|
2019-05-19 13:39:42 +00:00
|
|
|
* @property Filesystem|null storage
|
2019-01-10 22:22:19 +00:00
|
|
|
*/
|
2018-04-28 12:20:07 +00:00
|
|
|
abstract class Controller
|
|
|
|
{
|
|
|
|
|
2018-11-11 16:02:50 +00:00
|
|
|
/** @var Container */
|
|
|
|
protected $container;
|
2018-06-02 20:32:14 +00:00
|
|
|
|
2018-11-11 16:02:50 +00:00
|
|
|
public function __construct(Container $container)
|
|
|
|
{
|
|
|
|
$this->container = $container;
|
2018-04-28 12:20:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-11-11 16:02:50 +00:00
|
|
|
* @param $name
|
|
|
|
* @return mixed|null
|
|
|
|
* @throws \Interop\Container\Exception\ContainerException
|
2018-04-28 12:20:07 +00:00
|
|
|
*/
|
2018-11-11 16:02:50 +00:00
|
|
|
public function __get($name)
|
2018-04-28 12:20:07 +00:00
|
|
|
{
|
2018-11-11 16:02:50 +00:00
|
|
|
if ($this->container->has($name)) {
|
|
|
|
return $this->container->get($name);
|
2018-04-28 12:20:07 +00:00
|
|
|
}
|
2018-11-11 16:02:50 +00:00
|
|
|
return null;
|
2018-04-28 12:20:07 +00:00
|
|
|
}
|
|
|
|
|
2018-11-12 17:56:12 +00:00
|
|
|
/**
|
|
|
|
* @param $id
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
protected function getUsedSpaceByUser($id): int
|
|
|
|
{
|
|
|
|
$medias = $this->database->query('SELECT `uploads`.`storage_path` FROM `uploads` WHERE `user_id` = ?', $id)->fetchAll();
|
|
|
|
|
|
|
|
$totalSize = 0;
|
|
|
|
|
2019-05-19 13:39:42 +00:00
|
|
|
$filesystem = $this->storage;
|
2018-11-12 17:56:12 +00:00
|
|
|
foreach ($medias as $media) {
|
|
|
|
try {
|
|
|
|
$totalSize += $filesystem->getSize($media->storage_path);
|
|
|
|
} catch (FileNotFoundException $e) {
|
2019-05-12 13:52:26 +00:00
|
|
|
$this->logger->error('Error calculating file size', ['exception' => $e]);
|
2018-11-12 17:56:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $totalSize;
|
|
|
|
}
|
2018-04-28 12:20:07 +00:00
|
|
|
}
|