Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into feature/h5p-spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
daVitekPL committed Jul 9, 2024
2 parents 7525d0b + 768d91e commit 33d5e6e
Show file tree
Hide file tree
Showing 31 changed files with 873 additions and 268 deletions.
39 changes: 39 additions & 0 deletions .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: Tests PHPStan in environments

on: [pull_request]

jobs:
php82-laravel-latest-phpstan-postgres:
runs-on: ubuntu-latest
container:
image: escolalms/php:8.2

services:
postgres:
image: postgres:12
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: test
TZ: Europe/Warsaw
ports:
- 5432:5432

steps:
- name: Instantiate package
uses: actions/checkout@v2

- name: Update composer
run: COMPOSER_ROOT_VERSION=0.9.9 composer update

- name: Setup environment
run: cp env/postgres/* .

- name: Clear config
run: vendor/bin/testbench config:clear

- name: Publish things
run: vendor/bin/testbench migrate:fresh

- name: Run tests
run: vendor/bin/phpstan analyse
7 changes: 4 additions & 3 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,9 +13,10 @@
},
"require-dev": {
"phpunit/phpunit": "^9.0",
"orchestra/testbench": "^6.0",
"orchestra/testbench": "^7.0",
"laravel/legacy-factories": "^1.0.4",
"guzzlehttp/guzzle": "^7"
"guzzlehttp/guzzle": "^7",
"nunomaduro/larastan": "^2.0"
},
"autoload": {
"psr-4": {
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'
];
10 changes: 10 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
includes:
- ./vendor/nunomaduro/larastan/extension.neon

parameters:

paths:
- src/

# The level 9 is the highest level
level: 3
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
32 changes: 29 additions & 3 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 @@ -43,17 +45,40 @@ public static function fixCaseKeysArray($keys, $array)
* @return boolean
* Indicates if the directory existed.
*/
public static function deleteFileTree($dir)
public static function deleteFileTree(string $dir): bool
{
if (!Storage::directoryExists($dir)) {
return false;
}
if (is_link($dir)) {
// Do not traverse and delete linked content, simply unlink.
unlink($dir);
return true;
}

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
24 changes: 12 additions & 12 deletions src/Http/Resources/ContentIndexResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ class ContentIndexResource extends JsonResource

public function toArray($request): array
{
$lib = H5PLibrary::find($this->library_id);
$lib = H5PLibrary::find($this->resource->library_id);

$fields = [
'id' => $this->id,
'uuid' => $this->uuid,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
'user_id' => $this->user_id,
'author' => $this->author,
'title' => $this->title,
'library_id' => $this->library_id,
'id' => $this->resource->id,
'uuid' => $this->resource->uuid,
'created_at' => $this->resource->created_at,
'updated_at' => $this->resource->updated_at,
'user_id' => $this->resource->user_id,
'author' => $this->resource->author,
'title' => $this->resource->title,
'library_id' => $this->resource->library_id,
'library' => isset($lib) ? (new LibraryIndexResource($lib))->toArray() : null,
'slug' => $this->slug,
'filtered' => $this->filtered,
'disable' => $this->disable,
'slug' => $this->resource->slug,
'filtered' => $this->resource->filtered,
'disable' => $this->resource->disable,
];

return self::apply($fields, $this);
Expand Down
30 changes: 15 additions & 15 deletions src/Http/Resources/ContentResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@ public function __construct(H5PContent $content)
public function toArray($request): array
{
return [
'id' => $this->id,
'uuid' => $this->uuid,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
'user_id' => $this->user_id,
'title' => $this->title,
'library_id' => $this->library_id,
'parameters' => $this->parameters,
'params' => $this->params,
'metadata' => $this->metadata,
'slug' => $this->slug,
'filtered' => $this->filtered,
'disable' => $this->disable,
'embed_type' => $this->embed_type,
'nonce' => $this->nonce,
'id' => $this->resource->id,
'uuid' => $this->resource->uuid,
'created_at' => $this->resource->created_at,
'updated_at' => $this->resource->updated_at,
'user_id' => $this->resource->user_id,
'title' => $this->resource->title,
'library_id' => $this->resource->library_id,
'parameters' => $this->resource->parameters,
'params' => $this->resource->params,
'metadata' => $this->resource->metadata,
'slug' => $this->resource->slug,
'filtered' => $this->resource->filtered,
'disable' => $this->resource->disable,
'embed_type' => $this->resource->embed_type,
'nonce' => $this->resource->nonce,
];
}
}
Loading

0 comments on commit 33d5e6e

Please sign in to comment.