/**
  * Remove a physical file and the respective database file record.
  * ><p>Non-existing records or physical files are ignored.
  *
  * @param string $filePath A folder1/folder1/UID.ext path.
  * @throws \Exception If the file could not be deleted.
  */
 private function deleteFile($filePath)
 {
     $id = str_segmentsLast($filePath, '/');
     $id = str_segmentsStripLast($id, '.');
     $file = File::find($id);
     if ($file) {
         $file->delete();
     }
 }
 function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
 {
     if ($request->getMethod() != 'POST') {
         return $response->withStatus(405);
     }
     // Method not allowed
     $files = $request->getUploadedFiles();
     if ($files) {
         try {
             /** @var UploadedFileInterface $file */
             $file = array_pop($files);
             $fileRec = new File();
             $params = $request->getQueryParams();
             $fileRec->owner_id = get($params, 'owner_id');
             $fileRec->owner_type = get($params, 'owner_type');
             $ownerType = last(explode('\\', $fileRec->owner_type));
             $uid = uniqid();
             $name = $file->getClientFilename();
             $l = explode('.', $name);
             $ext = strtolower(array_pop($l));
             $fileRec->name = implode('.', $l);
             $fileRec->path = self::SOURCE_PATH . "/{$ownerType}/{$fileRec->owner_id}/{$uid}.{$ext}";
             $fileRec->image = $ext == 'jpg' || $ext == 'png' || $ext == 'gif' || $ext == 'tiff';
             $fileRec->id = $uid;
             $fileRec->ext = $ext;
             $base = $this->kernelSettings->baseDirectory . '/';
             $path = $base . dirname($fileRec->path);
             @mkdir($path, 0777, true);
             $file->moveTo($base . $fileRec->path);
             $fileRec->save();
             $response->getBody()->write($fileRec->path);
             return $response;
         } catch (\Exception $e) {
             echo $e->__toString();
             exit(500);
         }
     }
     return $response->withStatus(400);
     // Bad request
 }
 static function startUp(KernelInterface $kernel, ModuleInfo $moduleInfo)
 {
     $kernel->onConfigure(function (MatisseSettings $matisseSettings, ModelControllerInterface $modelController, InjectorInterface $injector, ContentServerSettings $contentServerSettings, AssetsService $assetsService) use($moduleInfo) {
         $matisseSettings->registerMacros($moduleInfo)->registerComponents(['Button' => C\Button::class, 'Checkbox' => C\Checkbox::class, 'DataGrid' => C\DataGrid::class, 'Dropzone' => C\Dropzone::class, 'Field' => C\Field::class, 'FileUpload' => C\FileUpload::class, 'HtmlEditor' => C\HtmlEditor::class, 'Image' => C\Image::class, 'ImageField' => C\ImageField::class, 'Input' => C\Input::class, 'Label' => C\Label::class, 'Link' => C\Link::class, 'MainMenu' => C\MainMenu::class, 'NavigationPath' => C\NavigationPath::class, 'Paginator' => C\Paginator::class, 'RadioButton' => C\RadioButton::class, 'Select' => C\Select::class, 'Switch' => C\Switch_::class, 'Tab' => C\Tab::class, 'TabPage' => C\TabPage::class, 'Tabs' => C\Tabs::class]);
         $assetsService->registerAssets($moduleInfo->name, ['dist/components.css']);
         $modelController->registerExtension($injector->makeFactory(ImageFieldHandler::class));
         File::deleting(function (File $model) use($contentServerSettings, $injector) {
             if (exists($model->path)) {
                 $path = "{$contentServerSettings->fileArchivePath()}/{$model->path}";
                 if (file_exists($path)) {
                     unlink($path);
                 }
                 $glideServer = $injector->make(Server::class);
                 $glideServer->deleteCache($model->path);
             }
         });
     });
 }