Skip to content

Commit

Permalink
Merge pull request #515 from oat-sa/feat/REL-1723/update-flysystem
Browse files Browse the repository at this point in the history
Update flysystem usage after upgrade
  • Loading branch information
augustas authored Nov 28, 2024
2 parents e08369b + c675d70 commit a4cde2e
Show file tree
Hide file tree
Showing 14 changed files with 98 additions and 62 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/continuous-integration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ jobs:
fail-fast: false
matrix:
operating-system: [ ubuntu-latest ]
php-version: [ '7.4', '8.0', '8.1' ]
php-version: [ '8.1', '8.2', '8.3' ]
include:
- php-version: '8.1'
- php-version: '8.3'
coverage: true

steps:
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
"require": {
"oat-sa/oatbox-extension-installer": "~1.1||dev-master",
"oat-sa/lib-generis-search": "^2.1.2",
"oat-sa/generis": ">=15.39.0",
"oat-sa/tao-core": ">=54.23.0",
"oat-sa/generis": ">=16.0.0",
"oat-sa/tao-core": ">=54.26.0",
"oat-sa/extension-tao-item": ">=12.4.0",
"oat-sa/extension-tao-itemqti": ">=30.22.0",
"oat-sa/extension-tao-test": ">=16.3.0",
Expand Down
10 changes: 5 additions & 5 deletions model/export/service/SharedStimulusCSSExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
namespace oat\taoMediaManager\model\export\service;

use core_kernel_classes_Resource;
use League\Flysystem\FilesystemInterface;
use oat\oatbox\filesystem\FilesystemInterface;
use oat\oatbox\filesystem\FileSystemService;
use oat\oatbox\service\ConfigurableService;
use oat\taoMediaManager\model\fileManagement\FlySystemManagement;
Expand All @@ -44,20 +44,20 @@ public function pack(core_kernel_classes_Resource $mediaResource, string $link,
$fs = $this->getFileSystem();
$cssPath = dirname($link) . DIRECTORY_SEPARATOR . StoreService::CSS_DIR_NAME;

if (!$fs->has($cssPath)) {
if (!$fs->directoryExists($cssPath)) {
return;
}

$files = $fs->listContents($cssPath);
$files = $fs->listContents($cssPath)->toArray();
if (!count($files)) {
return;
}

$zip->addEmptyDir(self::CSS_ZIP_DIR_NAME);

foreach ($files as $file) {
$content = $fs->read($cssPath . DIRECTORY_SEPARATOR . $file['basename']);
$zip->addFromString(self::CSS_ZIP_DIR_NAME . DIRECTORY_SEPARATOR . $file['basename'], $content);
$content = $fs->read($cssPath . DIRECTORY_SEPARATOR . basename($file['path']));
$zip->addFromString(self::CSS_ZIP_DIR_NAME . DIRECTORY_SEPARATOR . basename($file['path']), $content);
}
}

Expand Down
33 changes: 24 additions & 9 deletions model/fileManagement/FlySystemManagement.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@
namespace oat\taoMediaManager\model\fileManagement;

use oat\oatbox\filesystem\File;
use oat\oatbox\filesystem\FilesystemException;
use oat\oatbox\filesystem\FilesystemInterface;
use oat\oatbox\service\ConfigurableService;
use League\Flysystem\Filesystem;
use Slim\Http\Stream;
use Psr\Http\Message\StreamInterface;
use oat\oatbox\filesystem\FileSystemService;
Expand All @@ -37,7 +38,7 @@ class FlySystemManagement extends ConfigurableService implements FileManagement
* @param string|File $fileSource
* @param string $label
* @return string
* @throws \League\Flysystem\FileExistsException
* @throws FilesystemException
*/
public function storeFile($fileSource, $label)
{
Expand All @@ -54,12 +55,23 @@ public function storeFile($fileSource, $label)

public function deleteDirectory(string $directoryPath): bool
{
return $this->getFilesystem()->deleteDir($directoryPath);
try {
$this->getFilesystem()->deleteDirectory($directoryPath);
return true;
} catch (FilesystemException $e) {
$this->logWarning($e->getMessage());
return false;
}
}

public function getFileSize($link)
{
return $this->getFilesystem()->getSize($link);
try {
return $this->getFilesystem()->fileSize($link);
} catch (FilesystemException $e) {
$this->logWarning($e->getMessage());
return null;
}
}

/**
Expand Down Expand Up @@ -89,13 +101,16 @@ public function retrieveFile($link)
*/
public function deleteFile($link)
{
return $this->getFilesystem()->delete($link);
try {
$this->getFilesystem()->delete($link);
return true;
} catch (FilesystemException $e) {
$this->logWarning($e->getMessage());
return false;
}
}

/**
* @return Filesystem
*/
protected function getFilesystem()
protected function getFilesystem(): FilesystemInterface
{
$fs = $this->getServiceLocator()->get(FileSystemService::SERVICE_ID);
return $fs->getFileSystem($this->getOption(self::OPTION_FS));
Expand Down
30 changes: 17 additions & 13 deletions model/sharedStimulus/css/repository/StylesheetRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,10 @@

namespace oat\taoMediaManager\model\sharedStimulus\css\repository;

use core_kernel_classes_Resource;
use oat\generis\model\data\Ontology;
use League\Flysystem\FilesystemInterface;
use oat\oatbox\filesystem\FilesystemException;
use oat\oatbox\filesystem\FilesystemInterface;
use oat\oatbox\service\ConfigurableService;
use oat\taoMediaManager\model\MediaService;
use League\Flysystem\FileNotFoundException;
use oat\oatbox\filesystem\FileSystemService;
use oat\taoMediaManager\model\fileManagement\FlySystemManagement;
use oat\taoMediaManager\model\fileManagement\FileSourceUnserializer;
Expand All @@ -48,35 +46,41 @@ public function getPath(string $uri): string
return dirname((string) $link);
}

public function listContents(string $path): array
public function listContents(string $path): iterable
{
return $this->getFileSystem()->listContents($path);
}

/**
* @throws FileNotFoundException
* @throws FilesystemException
*/
public function read(string $path): string
{
return $this->getFileSystem()->read($path);
}

public function put(string $path, string $contents): bool
/**
* @throws FilesystemException
*/
public function write(string $path, string $contents): void
{
return $this->getFileSystem()->put($path, $contents);
$this->getFileSystem()->write($path, $contents);
}

public function putStream(string $path, $streamResource): bool
/**
* @throws FilesystemException
*/
public function writeStream(string $path, $streamResource): void
{
return $this->getFileSystem()->putStream($path, $streamResource);
$this->getFileSystem()->writeStream($path, $streamResource);
}

/**
* @throws FileNotFoundException
* @throws FilesystemException
*/
public function delete(string $path): bool
public function delete(string $path): void
{
return $this->getFileSystem()->delete($path);
$this->getFileSystem()->delete($path);
}

private function getFileSystem(): FilesystemInterface
Expand Down
12 changes: 6 additions & 6 deletions model/sharedStimulus/css/service/ListStylesheetsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,23 @@ public function getList(ListStylesheets $listStylesheetsDTO): array
$path = $stylesheetRepository->getPath($listStylesheetsDTO->getUri());
$list = $stylesheetRepository->listContents(
$path . DIRECTORY_SEPARATOR . StylesheetRepository::STYLESHEETS_DIRECTORY
);
)->toArray();
/**
* here sorting files by creation date so that in case of css .selector collisions
* the rules will be applied from the last stylesheet added to the passage
*/
usort($list, function ($a, $b) {
return ($a['timestamp'] < $b['timestamp']) ? -1 : 1;
return ($a['lastModified'] < $b['lastModified']) ? -1 : 1;
});

$data = [];
foreach ($list as $file) {
$data[] = [
'name' => $file['basename'],
'uri' => DIRECTORY_SEPARATOR . $file['basename'],
'name' => basename($file['path']),
'uri' => DIRECTORY_SEPARATOR . basename($file['path']),
'mime' => 'text/css',
'filePath' => DIRECTORY_SEPARATOR . $file['basename'],
'size' => $file['size']
'filePath' => DIRECTORY_SEPARATOR . basename($file['path']),
'size' => $file['fileSize']
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

namespace oat\taoMediaManager\model\sharedStimulus\css\service;

use League\Flysystem\FileNotFoundException;
use oat\oatbox\filesystem\FilesystemException;
use oat\oatbox\service\ConfigurableService;
use oat\taoMediaManager\model\sharedStimulus\css\dto\LoadStylesheet;
use oat\taoMediaManager\model\sharedStimulus\css\repository\StylesheetRepository;
Expand All @@ -45,7 +45,7 @@ public function load(LoadStylesheet $loadStylesheetDTO): array
);

return $this->cssToArray($content);
} catch (FileNotFoundException $e) {
} catch (FilesystemException $e) {
$this->logDebug(
sprintf(
'Passage %s does not contain stylesheet %s. An empty array will be returned.',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
namespace oat\taoMediaManager\model\sharedStimulus\css\service;

use Exception;
use League\Flysystem\FileNotFoundException;
use oat\oatbox\filesystem\FilesystemException;
use oat\oatbox\service\ConfigurableService;
use oat\taoMediaManager\model\sharedStimulus\css\dto\SaveStylesheetClasses;
use oat\taoMediaManager\model\sharedStimulus\css\repository\StylesheetRepository;
Expand Down Expand Up @@ -52,7 +52,7 @@ public function save(SaveStylesheetClasses $saveStylesheetClassesDTO): void
}

$content = $this->getCssContentFromArray($cssClassesArray);
$this->getStylesheetRepository()->put(
$this->getStylesheetRepository()->write(
$path . DIRECTORY_SEPARATOR . $saveStylesheetClassesDTO->getStylesheetUri(),
$content
);
Expand All @@ -62,7 +62,7 @@ private function removeStoredStylesheet(string $path): void
{
try {
$this->getStylesheetRepository()->delete($path);
} catch (FileNotFoundException $exception) {
} catch (FilesystemException $exception) {
$this->logDebug(sprintf('Stylesheet %s to delete was not found when trying to clear styles', $path));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function save(UploadedStylesheet $uploadedStylesheetDTO): array

$tmpResource = $uploadedStylesheetDTO->getFileResource();
$size = filesize($uploadedStylesheetDTO->getTmpFileLink());
$this->getStylesheetRepository()->putStream($link, $tmpResource);
$this->getStylesheetRepository()->writeStream($link, $tmpResource);

if (is_resource($tmpResource)) {
fclose($tmpResource);
Expand Down
3 changes: 1 addition & 2 deletions model/sharedStimulus/factory/CommandFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

namespace oat\taoMediaManager\model\sharedStimulus\factory;

use League\Flysystem\FilesystemInterface;
use oat\generis\model\fileReference\FileReferenceSerializer;
use oat\oatbox\filesystem\Directory;
use oat\oatbox\filesystem\FileSystemService;
Expand All @@ -38,7 +37,7 @@ class CommandFactory extends ConfigurableService
{
public const DEFAULT_DIRECTORY = 'sharedStimulusUploads';

/** @var FilesystemInterface */
/** @var Directory */
private $directory;

public function makeCreateCommandByRequest(ServerRequestInterface $request): CreateCommand
Expand Down
5 changes: 3 additions & 2 deletions model/sharedStimulus/service/PatchService.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@
use core_kernel_classes_Resource as Resource;
use core_kernel_persistence_Exception;
use InvalidArgumentException;
use League\Flysystem\FilesystemInterface;
use LogicException;
use oat\generis\model\fileReference\FileReferenceSerializer;
use oat\generis\model\OntologyAwareTrait;
use oat\oatbox\filesystem\File;
use oat\oatbox\filesystem\FileSystem;
use oat\oatbox\filesystem\FilesystemInterface;
use oat\oatbox\filesystem\FileSystemService;
use oat\oatbox\service\ConfigurableService;
use oat\tao\model\media\TaoMediaException;
Expand Down Expand Up @@ -82,7 +83,7 @@ public function patch(PatchCommand $command): SharedStimulus
);
$sharedStimulusStoredSourceFile = $this->getFileSourceUnserializer()->unserialize((string)$link);

$this->getFileSystem()->putStream($sharedStimulusStoredSourceFile, $file->readStream());
$this->getFileSystem()->writeStream($sharedStimulusStoredSourceFile, $file->readStream());

$content = $file->read();
$resource->editPropertyValues(
Expand Down
11 changes: 6 additions & 5 deletions model/sharedStimulus/service/StoreService.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@

namespace oat\taoMediaManager\model\sharedStimulus\service;

use League\Flysystem\FilesystemInterface;
use oat\oatbox\filesystem\File;
use oat\oatbox\filesystem\FileSystem;
use oat\oatbox\filesystem\FilesystemInterface;
use oat\oatbox\filesystem\FileSystemService;
use oat\oatbox\service\ConfigurableService;
use oat\taoMediaManager\model\fileManagement\FlySystemManagement;
Expand Down Expand Up @@ -69,15 +70,15 @@ public function storeStream(
$fs = $this->getFileSystem();

$dirname = $this->getUniqueName($stimulusFilename);
$fs->createDir($dirname);
$fs->createDirectory($dirname);

$fs->putStream(
$fs->writeStream(
$dirname . DIRECTORY_SEPARATOR . $stimulusFilename,
$stimulusXmlStream
);

if (count($cssFiles)) {
$fs->createDir($dirname . DIRECTORY_SEPARATOR . self::CSS_DIR_NAME);
$fs->createDirectory($dirname . DIRECTORY_SEPARATOR . self::CSS_DIR_NAME);
foreach ($cssFiles as $file) {
if (!file_exists($file)) {
$this->getLogger()->notice(sprintf("file %s does not exist", $file));
Expand All @@ -89,7 +90,7 @@ public function storeStream(
continue;
}

$fs->putStream(
$fs->writeStream(
$dirname . DIRECTORY_SEPARATOR . self::CSS_DIR_NAME . DIRECTORY_SEPARATOR . basename($file),
fopen($file, 'r')
);
Expand Down
Loading

0 comments on commit a4cde2e

Please sign in to comment.