Skip to content

Commit

Permalink
h5p export (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
qunabu authored Nov 15, 2021
1 parent 6f585de commit 38e97fa
Show file tree
Hide file tree
Showing 6 changed files with 557 additions and 522 deletions.
47 changes: 19 additions & 28 deletions src/HeadlessH5PServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,30 @@

namespace EscolaLms\HeadlessH5P;

use H5PCore;
use H5peditor;
use H5PEditorEndpoints;
use H5PFrameworkInterface;
use H5PFileStorage;
use H5PStorage;
use H5PValidator;
use EditorStorage;
use EditorAjaxRepository;
use H5peditorStorage;
use H5PEditorAjaxInterface;
use H5PContentValidator;

use Illuminate\Support\ServiceProvider;
use EscolaLms\HeadlessH5P\Commands\H5PSeedCommand;
use EscolaLms\HeadlessH5P\Commands\StorageH5PLinkCommand;
use EscolaLms\HeadlessH5P\Services\HeadlessH5PService;
use EscolaLms\HeadlessH5P\Services\Contracts\HeadlessH5PServiceContract;
use EscolaLms\HeadlessH5P\Repositories\Contracts\H5PContentRepositoryContract;
use EscolaLms\HeadlessH5P\Repositories\H5PRepository;
use EscolaLms\HeadlessH5P\Repositories\H5PFileStorageRepository;
use EscolaLms\HeadlessH5P\Repositories\H5PContentRepository;
use EscolaLms\HeadlessH5P\Repositories\H5PEditorAjaxRepository;
use EscolaLms\HeadlessH5P\Repositories\H5PEditorStorageRepository;
use EscolaLms\HeadlessH5P\Repositories\H5PContentRepository;
use EscolaLms\HeadlessH5P\Repositories\H5PFileStorageRepository;
use EscolaLms\HeadlessH5P\Repositories\H5PRepository;
use EscolaLms\HeadlessH5P\Services\Contracts\HeadlessH5PServiceContract;
use EscolaLms\HeadlessH5P\Services\HeadlessH5PService;
use H5PContentValidator;
use H5PCore;
use H5peditor;
use H5PStorage;
use H5PValidator;
use Illuminate\Support\ServiceProvider;

/**
* SWAGGER_VERSION
*/

* SWAGGER_VERSION.
*/
class HeadlessH5PServiceProvider extends ServiceProvider
{
public $singletons = [
H5PContentRepositoryContract::class => H5PContentRepository::class
H5PContentRepositoryContract::class => H5PContentRepository::class,
];

public function register(): void
Expand All @@ -48,7 +39,7 @@ private function bindH5P(): void
$this->app->singleton(HeadlessH5PServiceContract::class, function ($app) {
$repository = new H5PRepository();
$fileStorage = new H5PFileStorageRepository(storage_path('app/h5p'));
$core = new H5PCore($repository, $fileStorage, url('h5p'));
$core = new H5PCore($repository, $fileStorage, url('h5p'), config('hh5p.language'), true);
$core->aggregateAssets = false;
$validator = new H5PValidator($repository, $core);
$storage = new H5PStorage($repository, $core);
Expand All @@ -74,11 +65,11 @@ private function bindH5P(): void

public function boot(): void
{
$this->loadRoutesFrom(__DIR__ . '/routes.php');
$this->loadRoutesFrom(__DIR__.'/routes.php');
//$this->loadFactoriesFrom(__DIR__ . '/../database/factories');
$this->loadMigrationsFrom(__DIR__ . '/../database/migrations');
$this->loadTranslationsFrom(__DIR__ . '/../resources/lang', 'h5p');
$this->mergeConfigFrom(__DIR__ . '/../config/hh5p.php', 'hh5p');
$this->loadMigrationsFrom(__DIR__.'/../database/migrations');
$this->loadTranslationsFrom(__DIR__.'/../resources/lang', 'h5p');
$this->mergeConfigFrom(__DIR__.'/../config/hh5p.php', 'hh5p');
// Load configs
}
}
40 changes: 25 additions & 15 deletions src/Http/Controllers/ContentApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@
namespace EscolaLms\HeadlessH5P\Http\Controllers;

//use App\Http\Controllers\Controller;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use EscolaLms\HeadlessH5P\Http\Controllers\Swagger\ContentApiSwagger;
use EscolaLms\HeadlessH5P\Services\HeadlessH5PService;
use EscolaLms\HeadlessH5P\Services\Contracts\HeadlessH5PServiceContract;

use EscolaLms\HeadlessH5P\Http\Requests\ContentStoreRequest;
use EscolaLms\HeadlessH5P\Http\Requests\LibraryStoreRequest;
use Illuminate\Routing\Controller;
use EscolaLms\HeadlessH5P\Repositories\Contracts\H5PContentRepositoryContract;
use EscolaLms\HeadlessH5P\Services\Contracts\HeadlessH5PServiceContract;
use Exception;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

class ContentApiController extends Controller implements ContentApiSwagger
{
Expand All @@ -29,7 +27,8 @@ public function __construct(HeadlessH5PServiceContract $hh5pService, H5PContentR
public function index(Request $request): JsonResponse
{
$columns = ['title', 'id', 'library_id'];
$list = $request->get('per_page') !== null && $request->get('per_page') == 0 ? $this->contentRepository->unpaginatedList($columns) : $this->contentRepository->list($request->get('per_page'), $columns);
$list = $request->get('per_page') !== null && $request->get('per_page') == 0 ? $this->contentRepository->unpaginatedList($columns) : $this->contentRepository->list($request->get('per_page'), $columns);

return response()->json($list, 200);
}

Expand All @@ -39,12 +38,12 @@ public function update(ContentStoreRequest $request, int $id): JsonResponse
$contentId = $this->contentRepository->edit($id, $request->get('title'), $request->get('library'), $request->get('params'), $request->get('nonce'));
} catch (Exception $error) {
return response()->json([
'error' => $error->getMessage()
'error' => $error->getMessage(),
], 422);
}

return response()->json([
'id' => $contentId
'id' => $contentId,
], 200);
}

Expand All @@ -54,12 +53,12 @@ public function store(ContentStoreRequest $request): JsonResponse
$contentId = $this->contentRepository->create($request->get('title'), $request->get('library'), $request->get('params'), $request->get('nonce'));
} catch (Exception $error) {
return response()->json([
'error' => $error->getMessage()
'error' => $error->getMessage(),
], 422);
}

return response()->json([
'id' => $contentId
'id' => $contentId,
], 200);
}

Expand All @@ -69,12 +68,12 @@ public function destroy(Request $request, int $id): JsonResponse
$contentId = $this->contentRepository->delete($id);
} catch (Exception $error) {
return response()->json([
'error' => $error->getMessage()
'error' => $error->getMessage(),
], 422);
}

return response()->json([
'id' => $contentId
'id' => $contentId,
], 200);
}

Expand All @@ -84,7 +83,7 @@ public function show(Request $request, int $id): JsonResponse
$settings = $this->hh5pService->getContentSettings($id);
} catch (Exception $error) {
return response()->json([
'error' => $error->getMessage()
'error' => $error->getMessage(),
], 422);
}

Expand All @@ -100,7 +99,7 @@ public function upload(LibraryStoreRequest $request): JsonResponse
$content = $this->contentRepository->upload($request->file('h5p_file'));
} catch (Exception $error) {
return response()->json([
'error' => $error->getMessage()
'error' => $error->getMessage(),
], 422);
}

Expand All @@ -109,4 +108,15 @@ public function upload(LibraryStoreRequest $request): JsonResponse
200
);
}

public function download(Request $request, $id)
{
$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',
]);
}
}
59 changes: 36 additions & 23 deletions src/Repositories/H5PContentRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

namespace EscolaLms\HeadlessH5P\Repositories;

use EscolaLms\HeadlessH5P\Exceptions\H5PException;
use EscolaLms\HeadlessH5P\Helpers\Helpers;
use EscolaLms\HeadlessH5P\Models\H5PContent;
use EscolaLms\HeadlessH5P\Models\H5PLibrary;
use EscolaLms\HeadlessH5P\Models\H5PTempFile;
use EscolaLms\HeadlessH5P\Repositories\Contracts\H5PContentRepositoryContract;
use EscolaLms\HeadlessH5P\Services\Contracts\HeadlessH5PServiceContract;
use EscolaLms\HeadlessH5P\Exceptions\H5PException;
use Illuminate\Pagination\LengthAwarePaginator;
use EscolaLms\HeadlessH5P\Helpers\Helpers;
use Illuminate\Support\Collection;

class H5PContentRepository implements H5PContentRepositoryContract
Expand All @@ -21,7 +21,7 @@ public function __construct(HeadlessH5PServiceContract $hh5pService)
$this->hh5pService = $hh5pService;
}

public function create(string $title, string $library, string $params, string $nonce):int
public function create(string $title, string $library, string $params, string $nonce): int
{
$libNames = $this->hh5pService->getCore()->libraryFromString($library);

Expand All @@ -42,19 +42,19 @@ public function create(string $title, string $library, string $params, string $n
}

$content = $this->hh5pService->getCore()->saveContent([
'library_id'=> $libDb->id,
'title'=>$title,
'library'=>$library,
'parameters'=>$params,
'nonce'=>$nonce
'library_id' => $libDb->id,
'title' => $title,
'library' => $library,
'parameters' => $params,
'nonce' => $nonce,
]);

$this->moveTmpFilesToContentFolders($nonce, $content);

return $content;
}

public function edit(int $id, string $title, string $library, string $params, string $nonce):int
public function edit(int $id, string $title, string $library, string $params, string $nonce): int
{
$libNames = $this->hh5pService->getCore()->libraryFromString($library);

Expand All @@ -74,30 +74,28 @@ public function edit(int $id, string $title, string $library, string $params, st
throw new H5PException(H5PException::INVALID_PARAMETERS_JSON);
}

$content = H5PLibrary::where('id', $id)->first();
$content = H5PContent::where('id', $id)->first();

if ($content === null) {
throw new H5PException(H5PException::CONTENT_NOT_FOUND);
}

$id = $this->hh5pService->getCore()->saveContent([
'id'=>$id,
'library_id'=> $libDb->id,
'title'=>$title,
'library'=>$library,
'parameters'=>$params,
'id' => $id,
'library_id' => $libDb->id,
'title' => $title,
'library' => $library,
'parameters' => $params,
//'nonce'=>$nonce
], $id);


$this->moveTmpFilesToContentFolders($nonce, $id);

return $id;
}

private function moveTmpFilesToContentFolders($nonce, $contentId):bool
private function moveTmpFilesToContentFolders($nonce, $contentId): bool
{

// TODO: take this from config
$storage_path = storage_path('app/h5p');

Expand All @@ -120,7 +118,7 @@ private function moveTmpFilesToContentFolders($nonce, $contentId):bool
return true;
}

public function list($per_page = 15, array $columns = ['*']):LengthAwarePaginator
public function list($per_page = 15, array $columns = ['*']): LengthAwarePaginator
{
$paginator = H5PContent::with(
['library']
Expand All @@ -129,12 +127,14 @@ public function list($per_page = 15, array $columns = ['*']):LengthAwarePaginato
// Your code here
$content->library->makeHidden(['semantics']);
$content->library->setAppends([]);

return $content;
});

return $paginator;
}

public function unpaginatedList(array $columns = ['*']):Collection
public function unpaginatedList(array $columns = ['*']): Collection
{
$list = H5PContent::with(
['library']
Expand All @@ -143,12 +143,14 @@ public function unpaginatedList(array $columns = ['*']):Collection
// Your code here
$content->library->makeHidden(['semantics']);
$content->library->setAppends([]);

return $content;
});

return $list;
}

public function delete(int $id):int
public function delete(int $id): int
{
$content = H5PContent::findOrFail($id);
$content->delete();
Expand All @@ -161,12 +163,12 @@ public function delete(int $id):int
return $id;
}

public function show(int $id):H5PContent
public function show(int $id): H5PContent
{
return H5PContent::findOrFail($id);
}

public function upload($file, $content = null, $only_upgrade = null, $disable_h5p_security = false):H5PContent
public function upload($file, $content = null, $only_upgrade = null, $disable_h5p_security = false): H5PContent
{
if ($disable_h5p_security) {
// Make it possible to disable file extension check
Expand Down Expand Up @@ -195,4 +197,15 @@ public function upload($file, $content = null, $only_upgrade = null, $disable_h5

// The uploaded file was not a valid H5P package
}

public function download($id): string
{
$content = $this->hh5pService->getCore()->loadContent($id);
$content['filtered'] = '';
$this->hh5pService->getCore()->filterParameters($content);

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

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

0 comments on commit 38e97fa

Please sign in to comment.