Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix missing files #216

Merged
merged 6 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions src/Helpers/MargeFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
namespace EscolaLms\HeadlessH5P\Helpers;

use Exception;
use Illuminate\Http\File;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;

class MargeFiles
{
Expand Down Expand Up @@ -43,7 +46,7 @@ public function getHash(): string
{
$hash = [];
foreach ($this->arrayFiles as $file) {
$hash[] = hash_file('md5', $file);
$hash[] = hash('md5', Storage::get($this->getNameAfterPrefix($file)));
}

return md5(serialize($hash));
Expand Down Expand Up @@ -93,7 +96,11 @@ private function createFile(string $fileName): bool
fwrite($stream, $contents . PHP_EOL);
}
rewind($stream);
file_put_contents($fileName, $stream);
if (config('filesystems.default') === 's3') {
Storage::put($this->getNameAfterPrefix($fileName), $stream);
} else {
file_put_contents($fileName, $stream);
}
fclose($stream);

return true;
Expand All @@ -107,10 +114,20 @@ private function createFile(string $fileName): bool
*/
private function getContent(string $path): string
{
$folderPath = $this->getNameAfterPrefix($path);
if (Storage::exists($folderPath)) {
return Storage::get($folderPath);
}

if (file_exists($path)) {
return file_get_contents($path);
}

throw new Exception("File: '".$path."' do not exist");
}

private function getNameAfterPrefix(string $fileName): string
{
return env('AWS_URL', null) ? Str::after($fileName, env('AWS_URL')) : $fileName;
}
}
7 changes: 5 additions & 2 deletions src/Services/H5PExportService.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@ public function createExportFile($content) {

// Create new zip instance.
$zip = new ZipArchive();
$zip->open($zipPath, ZipArchive::CREATE | ZipArchive::OVERWRITE);
$zipName = Str::afterLast($zipPath, '/') . '.zip';
$zipName = storage_path('app/h5p/temp/' . $zipName);
$zip->open($zipName, ZipArchive::CREATE | ZipArchive::OVERWRITE);

// Add all the files from the tmp dir.
foreach ($files as $file) {
Expand All @@ -129,8 +131,9 @@ public function createExportFile($content) {

// Close zip and remove tmp dir
$zip->close();
Storage::putFileAs('h5p/temp', new File($zipPath), Str::afterLast($tmpFile, '/'));
Storage::putFileAs('h5p/temp', new File($zipName), Str::afterLast($tmpFile, '/'));
H5PCore::deleteFileTree($tmpPath);
\Illuminate\Support\Facades\File::delete($zipName);

$filename = $content['slug'] . '-' . $content['id'] . '.h5p';
try {
Expand Down
6 changes: 6 additions & 0 deletions src/Services/HeadlessH5PService.php
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,12 @@ private function getEditorLangScript(string $lang, string $h5pEditorDir): string
}

private function getH5pEditorDir(): array {
if (config('filesystems.default') === 's3') {
return [
env('AWS_URL') . '/h5p-editor',
env('AWS_URL') . '/h5p-core',
];
}
$h5pEditorDir = file_exists(__DIR__ . '/../../vendor/h5p/h5p-editor')
? __DIR__ . '/../../vendor/h5p/h5p-editor'
: __DIR__ . '/../../../../../vendor/h5p/h5p-editor';
Expand Down
Loading