Update the FileSystemSchema and work on uploading the files.
This commit is contained in:
parent
ecb7cc395c
commit
7972d517ce
@ -9,6 +9,9 @@ use DI\ContainerBuilder;
|
||||
|
||||
use League\Config\Configuration;
|
||||
|
||||
use League\Flysystem\Filesystem;
|
||||
use League\Flysystem\FilesystemOperator;
|
||||
use League\Flysystem\Local\LocalFilesystemAdapter;
|
||||
use Monolog\Handler\StreamHandler;
|
||||
use Monolog\Level;
|
||||
use Monolog\Logger;
|
||||
@ -26,12 +29,14 @@ use Psr\Log\LoggerInterface;
|
||||
use Slim\App;
|
||||
use Slim\Factory\AppFactory;
|
||||
use Slim\Psr7\Factory\ResponseFactory;
|
||||
use Slovocast\Configuration\FileSystemSchema;
|
||||
use Slovocast\Middleware\SessionMiddleware;
|
||||
use Twig\Error\LoaderError;
|
||||
|
||||
use Slovocast\Configuration\DatabaseConnectionSchema;
|
||||
use Slovocast\Configuration\SessionSchema;
|
||||
use Slovocast\Configuration\SiteInformationSchema;
|
||||
use Slovocast\Configuration\FileSystemSchema;
|
||||
|
||||
use Slovocast\Domain\Repository\Channel\ChannelRepository;
|
||||
use Slovocast\Domain\Repository\Channel\ChannelRepositoryInterface;
|
||||
@ -73,6 +78,7 @@ class Bootstrap
|
||||
$config->addSchema('site', SiteInformationSchema::getSchema());
|
||||
$config->addSchema('database', DatabaseConnectionSchema::getSchema());
|
||||
$config->addSchema('session', SessionSchema::getSchema());
|
||||
$config->addSchema('filestytem', FileSystemSchema::getSchema());
|
||||
|
||||
$config->merge([
|
||||
'site' => [
|
||||
@ -91,6 +97,10 @@ class Bootstrap
|
||||
'username' => $_ENV['DB_USER'],
|
||||
'password' => $_ENV['DB_PASSWORD'],
|
||||
'port' => (int) $_ENV['DB_PORT'],
|
||||
],
|
||||
'filesystem' => [
|
||||
'type' => 'local',
|
||||
'location' => '/var/slovocast/data'
|
||||
]
|
||||
]);
|
||||
|
||||
@ -166,6 +176,16 @@ class Bootstrap
|
||||
);
|
||||
},
|
||||
|
||||
/**
|
||||
* FlySystem set up
|
||||
*/
|
||||
FilesystemOperator::class => function (ContainerInterface $container) {
|
||||
// get config
|
||||
$config = $container->get('config');
|
||||
$adapter = new LocalFilesystemAdapter($config('filesystem.location'));
|
||||
return new Filesystem($adapter);
|
||||
},
|
||||
|
||||
/**
|
||||
* Utility classes
|
||||
*/
|
||||
|
18
app/src/Configuration/FileSystemSchema.php
Normal file
18
app/src/Configuration/FileSystemSchema.php
Normal file
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace Slovocast\Configuration;
|
||||
|
||||
use Nette\Schema\Expect;
|
||||
use Nette\Schema\Schema;
|
||||
|
||||
class FileSystemSchema
|
||||
{
|
||||
public static function getSchema(): Schema
|
||||
{
|
||||
return Expect::structure([
|
||||
'type' => Expect::anyOf([ 'local', 's3' ])->default('local'),
|
||||
'location' => Expect::string()->default('/var/slovocast/data')->required(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
@ -2,7 +2,12 @@
|
||||
|
||||
namespace Slovocast\Handler\Channel\Episode;
|
||||
|
||||
use League\Flysystem\FilesystemException;
|
||||
use League\Flysystem\FilesystemOperator;
|
||||
use League\Flysystem\UnableToWriteFile;
|
||||
use Odan\Session\SessionInterface;
|
||||
use Psr\Http\Message\UploadedFileInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Slovocast\Handler\Handler;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Slovocast\Domain\Repository\Episode\EpisodeFileRepositoryInterface;
|
||||
@ -10,7 +15,10 @@ use Slovocast\Domain\Repository\Episode\EpisodeFileRepositoryInterface;
|
||||
class UploadEpisodeFileAction extends Handler
|
||||
{
|
||||
public function __construct(
|
||||
EpisodeFileRepositoryInterface $episodeFileRepository
|
||||
private EpisodeFileRepositoryInterface $episodeFileRepository,
|
||||
private FilesystemOperator $filesystem,
|
||||
private SessionInterface $session,
|
||||
private LoggerInterface $logger
|
||||
) { }
|
||||
|
||||
public function handle(): ResponseInterface
|
||||
@ -19,7 +27,18 @@ class UploadEpisodeFileAction extends Handler
|
||||
$files = $this->request->getUploadedFiles();
|
||||
|
||||
// move the files to storage
|
||||
try {
|
||||
foreach ($files as $file) {
|
||||
$this->filesystem->writeStream($file->getStream());
|
||||
}
|
||||
|
||||
return $this->json([ 'message' => 'Uploaded Episode file.' ]);
|
||||
return $this->json([ 'message' => 'Uploaded Episode file' ]);
|
||||
} catch (FilesystemException | UnableToWriteFile $e) {
|
||||
$this->logger->error($e->getMessage());
|
||||
}
|
||||
|
||||
$errorMessage = "Unable to upload file";
|
||||
$this->session->getFlash()->add('error', $errorMessage);
|
||||
return $this->json([ 'message' => $errorMessage ]);
|
||||
}
|
||||
}
|
||||
|
@ -1,21 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Slovocast\Infrastructure\Api\File;
|
||||
|
||||
interface FileHandlerInterface
|
||||
{
|
||||
/**
|
||||
* Persists a file in a specific location.
|
||||
*/
|
||||
public function save(string $path): bool;
|
||||
|
||||
/**
|
||||
* Opens a file as a string buffer
|
||||
*/
|
||||
public function open(string $path): string;
|
||||
|
||||
/**
|
||||
* Moves a file from one location to another
|
||||
*/
|
||||
public function move(string $source, string $destination): bool;
|
||||
}
|
Loading…
Reference in New Issue
Block a user