From efadeefa98ed4374313289beb8c56d037c4069d5 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Thu, 5 Sep 2024 16:40:03 +0200 Subject: [PATCH] wip #7062 @3 --- .../API/FluidbookPublicationAPIController.php | 13 ++ .../API/FluidbookThemeAPIController.php | 168 ------------------ app/Jobs/FluidbookEasyCreation.php | 4 +- routes/api.php | 7 +- 4 files changed, 17 insertions(+), 175 deletions(-) delete mode 100644 app/Http/Controllers/API/FluidbookThemeAPIController.php diff --git a/app/Http/Controllers/API/FluidbookPublicationAPIController.php b/app/Http/Controllers/API/FluidbookPublicationAPIController.php index 5f7a86477..715760752 100644 --- a/app/Http/Controllers/API/FluidbookPublicationAPIController.php +++ b/app/Http/Controllers/API/FluidbookPublicationAPIController.php @@ -4,8 +4,10 @@ namespace App\Http\Controllers\API; use App\Fluidbook\Packager\Download; use App\Http\Controllers\Controller; +use App\Jobs\FluidbookEasyCreation; use App\Models\FluidbookPublication; use App\Models\User; +use Cubist\Util\Files\Files; class FluidbookPublicationAPIController extends Controller { @@ -33,4 +35,15 @@ class FluidbookPublicationAPIController extends Controller 'locale' => $fluidbook->locale, 'date' => (new \DateTime($fluidbook->updated_at))->getTimestamp()]; } + + public function create() + { + if (!can('fluidbook-publication:write')) { + abort(401); + } + + $job = new FluidbookEasyCreation(request('base'), Files::move_uploaded_file(request()->file('file')), request('title'), request('export'), backpack_user(), request('notify', false)); + return $job->handle(); + } + } diff --git a/app/Http/Controllers/API/FluidbookThemeAPIController.php b/app/Http/Controllers/API/FluidbookThemeAPIController.php deleted file mode 100644 index 3506997d3..000000000 --- a/app/Http/Controllers/API/FluidbookThemeAPIController.php +++ /dev/null @@ -1,168 +0,0 @@ - 'id', 'proprietaire' => 'owner', 'nom' => 'name']; - foreach ($data as $k => $v) { - if (isset($map[$k])) { - $k = $map[$k]; - } - if (!$theme->hasField($k)) { - continue; - } - $field = $theme->getField($k); - if ($field instanceof Checkbox) { - if ($v === 'true' || $v == 1) { - $v = 1; - } else if ($v == 0 || $v === 'false' || !$v) { - $v = 0; - } - } else if ($field instanceof Color) { - if ($v === '' && $field->getAttribute('allows_empty')) { - - } else { - $v = FluidbookTheme::_colorToWS3($v); - } - } else if ($field instanceof Files) { - continue; - } - $theme->setAttribute($k, $v); - } - return $theme; - } - - /** - * @param $theme FluidbookTheme - * @param $data array - * @return FluidbookTheme - */ - public static function _handleFiles($theme, $data) - { - // $oldRoot = '/home/extranet/www/fluidbook/themes/' . $theme->getAttribute('id') . '/'; - foreach ($theme->getFields() as $field) { - if (!($field instanceof Files)) { - continue; - } - $k = $field->getName(); - $v = $data[$k] ?? null; - - if (!$v) { - $theme->deleteMediaInField($k); - //continue; - } -// $path = $oldRoot . $v; -// if (!file_exists($path)) { -// $opath = str_replace('.svg', '.o.svg', $path); -// if (file_exists($opath)) { -// copy($opath, $path); -// } else { -// continue; -// } -// } -// -// $media = $theme->getMediaInField($k); -// $upload = false; -// -// if ($media->count() === 0) { -// $upload = true; -// } else { -// /** @var Media $m */ -// $m = $media->get(0); -// -// if ($m->getAttribute('file_name') !== $k) { -// $upload = true; -// } else { -// $mpath = $m->getPath(); -// if (filesize($path) !== filesize($mpath)) { -// $upload = true; -// } -// } -// } -// -// if (!$upload) { -// continue; -// } -// $s = storage_path('themes/' . $theme->getAttribute('id')); -// if (!file_exists($s)) { -// mkdir($s); -// } -// $f = $s . '/' . $v; -// copy($path, $f); -// $theme->addMediaToField($k, $f); - } - } - - - /** - * Update the specified resource in storage. - * - * @param \Illuminate\Http\Request $request - * @param int $id - * @return \Illuminate\Http\Response - */ - public function updatefromws2(Request $request, $id) - { - FluidbookTheme::$updateWS2SyncViewOnChange = true; - $theme = FluidbookTheme::find($id); - self::_prepareData($theme, $request->get('data')); - $theme->save(); - self::_handleFiles($theme, $request->get('data')); - } - - public function clonefromws2(Request $request, $id) - { - FluidbookTheme::$updateWS2SyncViewOnChange = true; - $theme = FluidbookTheme::find($id); - $newtheme = $theme->replicate(); - $newtheme->name .= ' (copy)'; - $newtheme->owner = Auth::id(); - $newtheme->push(); - - return $newtheme->id; - } - - public function renamefromws2(Request $request, $id) - { - FluidbookTheme::$updateWS2SyncViewOnChange = true; - $theme = FluidbookTheme::find($id); - $theme->name = $request->get('newname'); - $theme->save(); - } - - public function deletefromws2(Request $request, $id) - { - FluidbookTheme::$updateWS2SyncViewOnChange = true; - /** @var FluidbookTheme $theme */ - $theme = FluidbookTheme::find($id); - $theme->delete(); - } - - public function uploadfile(Request $request, $id, $fieldname) - { - FluidbookTheme::$updateWS2SyncViewOnChange = true; - /** @var FluidbookTheme $theme */ - $theme = FluidbookTheme::find($id); - $theme->replaceMediaInField($fieldname, $request->get('path'), true); - $theme->save(); - } -} diff --git a/app/Jobs/FluidbookEasyCreation.php b/app/Jobs/FluidbookEasyCreation.php index a78cdcf8f..ff4be11bb 100644 --- a/app/Jobs/FluidbookEasyCreation.php +++ b/app/Jobs/FluidbookEasyCreation.php @@ -74,6 +74,8 @@ class FluidbookEasyCreation extends \App\Jobs\Tools\Base $fluidbook->save(); $download = new Download($fluidbook, 'online', $install ? 'install' : 'download', $this->user); - $download->package($this->notify); + $url = $download->package($this->notify); + + return ['fluidbook' => $fluidbook, 'url' => $url]; } } diff --git a/routes/api.php b/routes/api.php index a5095dc17..2f535c910 100644 --- a/routes/api.php +++ b/routes/api.php @@ -19,12 +19,7 @@ Route::group([ ], function () { Route::get('fluidbook-publication/{id}/download/{version}', [\App\Http\Controllers\API\FluidbookPublicationAPIController::class, 'download']); Route::get('fluidbook-publication/{id}/metadata', [\App\Http\Controllers\API\FluidbookPublicationAPIController::class, 'metadata']); - - Route::post('fluidbook-theme/{id}/clone', [\App\Http\Controllers\API\FluidbookThemeAPIController::class, 'clonefromws2']); - Route::put('fluidbook-theme/{id}', [\App\Http\Controllers\API\FluidbookThemeAPIController::class, 'updatefromws2']); - Route::put('fluidbook-theme/{id}/rename', [\App\Http\Controllers\API\FluidbookThemeAPIController::class, 'renamefromws2']); - Route::put('fluidbook-theme/{id}/uploadfile/{fieldname}', [\App\Http\Controllers\API\FluidbookThemeAPIController::class, 'uploadfile']); - Route::delete('fluidbook-theme/{id}', [\App\Http\Controllers\API\FluidbookThemeAPIController::class, 'deletefromws2']); + Route::get('fluidbook-publication/create', [\App\Http\Controllers\API\FluidbookPublicationAPIController::class, 'create']); Route::post('cache/clear/{tag?}', [\App\Http\Controllers\API\CacheAPIController::class, 'clear']); }); -- 2.39.5