Skip to content

Commit

Permalink
S3 (#203)
Browse files Browse the repository at this point in the history
  • Loading branch information
daVitekPL authored Apr 15, 2024
1 parent fbde2d5 commit 60b4b52
Show file tree
Hide file tree
Showing 17 changed files with 598 additions and 78 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"type": "package",
"require": {
"php": ">=7.4",
"laravel/framework": ">=8.0",
"laravel/framework": ">=9.0",
"h5p/h5p-core": "1.24.*|dev-master",
"h5p/h5p-editor": "1.24.*|dev-master",
"escolalms/core": "^1",
Expand All @@ -13,7 +13,7 @@
},
"require-dev": {
"phpunit/phpunit": "^9.0",
"orchestra/testbench": "^6.0",
"orchestra/testbench": "^7.0",
"laravel/legacy-factories": "^1.0.4",
"guzzlehttp/guzzle": "^7"
},
Expand Down
4 changes: 2 additions & 2 deletions config/hh5p.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

'guzzle' => [],

'h5p_storage_path' => 'app/h5p',
'h5p_content_storage_path' => 'app/h5p/content/',
'h5p_storage_path' => 'h5p',
'h5p_content_storage_path' => 'h5p/content/',
'h5p_library_url' => 'h5p/libraries'
];
41 changes: 41 additions & 0 deletions src/Commands/StorageH5PCopyStorageCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace EscolaLms\HeadlessH5P\Commands;

use EscolaLms\HeadlessH5P\Helpers\Helpers;
use EscolaLms\HeadlessH5P\Repositories\H5PFileStorageRepository;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;

class StorageH5PCopyStorageCommand extends Command
{
/**
* The console command signature.
*
* @var string
*/
protected $signature = 'h5p:storage-copy';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Copy local H5P storage to s3 and after delete files from local storage';

/**
* Execute the console command.
*
* @return void
*/
public function handle()
{
$link = Storage::path('h5p');
$target = storage_path('app/h5p');

app(H5PFileStorageRepository::class, ['path' => env('AWS_URL')])->copyVendorFiles($target, $link);
Helpers::deleteFileTreeLocal($target);

$this->info("The files [$target] have been copied to [$link].");
}
}
21 changes: 7 additions & 14 deletions src/Commands/StorageH5PLinkCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace EscolaLms\HeadlessH5P\Commands;

use EscolaLms\HeadlessH5P\Repositories\H5PFileStorageRepository;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;

class StorageH5PLinkCommand extends Command
{
Expand All @@ -18,7 +20,7 @@ class StorageH5PLinkCommand extends Command
*
* @var string
*/
protected $description = 'Create the symbolic links for H%p configured for the application';
protected $description = 'Create the symbolic links for H5P configured for the application';

/**
* Execute the console command.
Expand All @@ -32,20 +34,12 @@ public function handle()
$links = $this->links();

foreach ($links as $link => $target) {
if (file_exists($link)) {
if (Storage::fileExists($link)) {
$this->error("The [$link] link already exists.");
continue;
}

if (is_link($link)) {
$this->laravel->make('files')->delete($link);
}

if ($relative) {
$this->laravel->make('files')->relativeLink($target, $link);
} else {
$this->laravel->make('files')->link($target, $link);
}
app(H5PFileStorageRepository::class, ['path' => env('AWS_URL')])->copyVendorFiles($target, $link);

$this->info("The [$link] link has been connected to [$target].");
}
Expand All @@ -61,9 +55,8 @@ public function handle()
protected function links()
{
return[
public_path('h5p') => storage_path('app/h5p'),
public_path('h5p-core') => base_path().'/vendor/h5p/h5p-core',
public_path('h5p-editor') => base_path().'/vendor/h5p/h5p-editor',
Storage::path('h5p-core') => base_path().'/vendor/h5p/h5p-core',
Storage::path('h5p-editor') => base_path().'/vendor/h5p/h5p-editor',
];
}
}
10 changes: 6 additions & 4 deletions src/HeadlessH5PServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace EscolaLms\HeadlessH5P;

use EscolaLms\HeadlessH5P\Commands\H5PSeedCommand;
use EscolaLms\HeadlessH5P\Commands\StorageH5PCopyStorageCommand;
use EscolaLms\HeadlessH5P\Commands\StorageH5PLinkCommand;
use EscolaLms\HeadlessH5P\Enums\ConfigEnum;
use EscolaLms\HeadlessH5P\Providers\SettingsServiceProvider;
Expand All @@ -15,12 +16,13 @@
use EscolaLms\HeadlessH5P\Repositories\H5PLibraryLanguageRepository;
use EscolaLms\HeadlessH5P\Repositories\H5PRepository;
use EscolaLms\HeadlessH5P\Services\Contracts\HeadlessH5PServiceContract;
use EscolaLms\HeadlessH5P\Services\H5PCoreService;
use EscolaLms\HeadlessH5P\Services\HeadlessH5PService;
use H5PContentValidator;
use H5PCore;
use H5peditor;
use H5PStorage;
use H5PValidator;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\ServiceProvider;

/**
Expand All @@ -35,7 +37,7 @@ class HeadlessH5PServiceProvider extends ServiceProvider

public function register(): void
{
$this->commands([H5PSeedCommand::class, StorageH5PLinkCommand::class]);
$this->commands([H5PSeedCommand::class, StorageH5PLinkCommand::class, StorageH5PCopyStorageCommand::class]);
$this->bindH5P();
$this->app->register(AuthServiceProvider::class);
$this->app->register(SettingsServiceProvider::class);
Expand All @@ -46,8 +48,8 @@ private function bindH5P(): void
$this->app->singleton(HeadlessH5PServiceContract::class, function ($app) {
$languageRepository = new H5PLibraryLanguageRepository();
$repository = new H5PRepository($languageRepository);
$fileStorage = new H5PFileStorageRepository(storage_path('app/h5p'));
$core = new H5PCore($repository, $fileStorage, url('h5p'), config('hh5p.language'), config('hh5p.h5p_export'));
$fileStorage = new H5PFileStorageRepository(config('filesystems.default') === 's3' ? Storage::path('/') . '/h5p' : storage_path('app/h5p'));
$core = new H5PCoreService($repository, $fileStorage, Storage::url('h5p'), config('hh5p.language'), config('hh5p.h5p_export'));
$core->aggregateAssets = true;
$validator = new H5PValidator($repository, $core);
$storage = new H5PStorage($repository, $core);
Expand Down
30 changes: 28 additions & 2 deletions src/Helpers/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace EscolaLms\HeadlessH5P\Helpers;

use Illuminate\Support\Facades\Storage;

class Helpers
{
/**
Expand Down Expand Up @@ -44,16 +46,39 @@ public static function fixCaseKeysArray($keys, $array)
* Indicates if the directory existed.
*/
public static function deleteFileTree($dir)
{
if (!Storage::directoryExists($dir)) {
return false;
}
if (is_link($dir)) {
// Do not traverse and delete linked content, simply unlink.
unlink($dir);
return;
}

foreach (Storage::directories($dir) as $directory) {
self::deleteFileTree($directory);
}
foreach (Storage::files($dir) as $file) {
Storage::delete($file);
}

return Storage::deleteDirectory($dir);
}

public static function deleteFileTreeLocal($dir)
{
if (!is_dir($dir)) {
return false;
}

if (is_link($dir)) {
// Do not traverse and delete linked content, simply unlink.
unlink($dir);
return;
}
$files = array_diff(scandir($dir), array('.','..'));

$files = array_diff(scandir($dir), array('.', '..'));
foreach ($files as $file) {
$filepath = "$dir/$file";
// Note that links may resolve as directories
Expand All @@ -62,9 +87,10 @@ public static function deleteFileTree($dir)
unlink($filepath);
} else {
// Traverse subdir and delete files
self::deleteFileTree($filepath);
self::deleteFileTreeLocal($filepath);
}
}

return rmdir($dir);
}
}
10 changes: 4 additions & 6 deletions src/Http/Controllers/ContentApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
use EscolaLms\HeadlessH5P\Services\Contracts\HeadlessH5PServiceContract;
use Exception;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Storage;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\StreamedResponse;

class ContentApiController extends EscolaLmsBaseController implements ContentApiSwagger
{
Expand Down Expand Up @@ -125,15 +127,11 @@ public function upload(LibraryStoreRequest $request): JsonResponse
return $this->sendResponseForResource(ContentResource::make($content));
}

public function download(AdminContentReadRequest $request, $id): BinaryFileResponse
public function download(AdminContentReadRequest $request, $id): StreamedResponse
{
$filepath = $this->contentRepository->download($id);

return response()
->download($filepath, '', [
'Content-Type' => 'application/zip',
'Cache-Control' => 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0',
]);
return Storage::download($filepath);
}

public function deleteUnused(): JsonResponse
Expand Down
13 changes: 5 additions & 8 deletions src/Repositories/H5PContentRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;


Expand Down Expand Up @@ -107,19 +108,15 @@ public function edit(int $id, string $library, string $params, string $nonce): i

private function moveTmpFilesToContentFolders($nonce, $contentId): bool
{
$storage_path = storage_path(config('hh5p.h5p_storage_path'));
$storage_path = Storage::path(config('hh5p.h5p_storage_path'));

$files = H5PTempFile::where(['nonce' => $nonce])->get();

foreach ($files as $file) {
$old_path = $storage_path . $file->path;
if (strpos($file->path, '/editor') !== false) {
$new_path = $storage_path . str_replace('/editor', '/content/' . $contentId, $file->path);
$dir_path = dirname($new_path);
if (!is_dir($dir_path)) {
mkdir($dir_path, 0777, true);
}
rename($old_path, $new_path);
Storage::move($old_path, $new_path);
}

$file->delete();
Expand Down Expand Up @@ -178,7 +175,7 @@ public function delete(int $id): int
$content = H5PContent::findOrFail($id);
$content->delete();

$storage_path = storage_path(config('hh5p.h5p_content_storage_path') . $id);
$storage_path = config('hh5p.h5p_content_storage_path') . $id;

Helpers::deleteFileTree($storage_path);

Expand Down Expand Up @@ -228,7 +225,7 @@ public function download($id): string

$filename = $this->hh5pService->getRepository()->getDownloadFile($id);

return storage_path('app/h5p/exports/' . $filename);
return 'h5p/exports/' . $filename;
}

public function getLibraryById(int $id): H5PLibrary
Expand Down
5 changes: 3 additions & 2 deletions src/Repositories/H5PEditorStorageRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use EscolaLms\HeadlessH5P\Models\H5PContent;
use EscolaLms\HeadlessH5P\Models\H5PTempFile;
use EscolaLms\HeadlessH5P\Helpers\Helpers;
use Illuminate\Support\Facades\Storage;


/**
Expand Down Expand Up @@ -188,11 +189,11 @@ public static function markFileForCleanup($file, $nonce)
*/
public static function removeTemporarilySavedFiles($filePath)
{
if (is_dir($filePath)) {
if (Storage::directoryExists($filePath)) {
Helpers::deleteFileTree($filePath);
}
else {
unlink($filePath);
Storage::delete($filePath);
}
}
}
Loading

0 comments on commit 60b4b52

Please sign in to comment.