Update the FileSystemSchema and work on uploading the files.

This commit is contained in:
Dave Smith-Hayes 2025-04-27 02:53:34 +00:00
parent ecb7cc395c
commit 7972d517ce
4 changed files with 59 additions and 23 deletions

View File

@ -9,6 +9,9 @@ use DI\ContainerBuilder;
use League\Config\Configuration; use League\Config\Configuration;
use League\Flysystem\Filesystem;
use League\Flysystem\FilesystemOperator;
use League\Flysystem\Local\LocalFilesystemAdapter;
use Monolog\Handler\StreamHandler; use Monolog\Handler\StreamHandler;
use Monolog\Level; use Monolog\Level;
use Monolog\Logger; use Monolog\Logger;
@ -26,12 +29,14 @@ use Psr\Log\LoggerInterface;
use Slim\App; use Slim\App;
use Slim\Factory\AppFactory; use Slim\Factory\AppFactory;
use Slim\Psr7\Factory\ResponseFactory; use Slim\Psr7\Factory\ResponseFactory;
use Slovocast\Configuration\FileSystemSchema;
use Slovocast\Middleware\SessionMiddleware; use Slovocast\Middleware\SessionMiddleware;
use Twig\Error\LoaderError; use Twig\Error\LoaderError;
use Slovocast\Configuration\DatabaseConnectionSchema; use Slovocast\Configuration\DatabaseConnectionSchema;
use Slovocast\Configuration\SessionSchema; use Slovocast\Configuration\SessionSchema;
use Slovocast\Configuration\SiteInformationSchema; use Slovocast\Configuration\SiteInformationSchema;
use Slovocast\Configuration\FileSystemSchema;
use Slovocast\Domain\Repository\Channel\ChannelRepository; use Slovocast\Domain\Repository\Channel\ChannelRepository;
use Slovocast\Domain\Repository\Channel\ChannelRepositoryInterface; use Slovocast\Domain\Repository\Channel\ChannelRepositoryInterface;
@ -73,6 +78,7 @@ class Bootstrap
$config->addSchema('site', SiteInformationSchema::getSchema()); $config->addSchema('site', SiteInformationSchema::getSchema());
$config->addSchema('database', DatabaseConnectionSchema::getSchema()); $config->addSchema('database', DatabaseConnectionSchema::getSchema());
$config->addSchema('session', SessionSchema::getSchema()); $config->addSchema('session', SessionSchema::getSchema());
$config->addSchema('filestytem', FileSystemSchema::getSchema());
$config->merge([ $config->merge([
'site' => [ 'site' => [
@ -91,6 +97,10 @@ class Bootstrap
'username' => $_ENV['DB_USER'], 'username' => $_ENV['DB_USER'],
'password' => $_ENV['DB_PASSWORD'], 'password' => $_ENV['DB_PASSWORD'],
'port' => (int) $_ENV['DB_PORT'], '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 * Utility classes
*/ */

View 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(),
]);
}
}

View File

@ -2,7 +2,12 @@
namespace Slovocast\Handler\Channel\Episode; 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\Http\Message\UploadedFileInterface;
use Psr\Log\LoggerInterface;
use Slovocast\Handler\Handler; use Slovocast\Handler\Handler;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
use Slovocast\Domain\Repository\Episode\EpisodeFileRepositoryInterface; use Slovocast\Domain\Repository\Episode\EpisodeFileRepositoryInterface;
@ -10,7 +15,10 @@ use Slovocast\Domain\Repository\Episode\EpisodeFileRepositoryInterface;
class UploadEpisodeFileAction extends Handler class UploadEpisodeFileAction extends Handler
{ {
public function __construct( public function __construct(
EpisodeFileRepositoryInterface $episodeFileRepository private EpisodeFileRepositoryInterface $episodeFileRepository,
private FilesystemOperator $filesystem,
private SessionInterface $session,
private LoggerInterface $logger
) { } ) { }
public function handle(): ResponseInterface public function handle(): ResponseInterface
@ -19,7 +27,18 @@ class UploadEpisodeFileAction extends Handler
$files = $this->request->getUploadedFiles(); $files = $this->request->getUploadedFiles();
// move the files to storage // 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 ]);
} }
} }

View File

@ -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;
}