function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
 {
     $url = $request->getAttribute('virtualUri', '');
     if (!str_beginsWith($url, "{$this->settings->fileBaseUrl()}/")) {
         return $next();
     }
     // Strip prefix from URL
     $url = substr($url, strlen($this->settings->fileBaseUrl()) + 1);
     $path = "{$this->settings->fileArchivePath()}/{$url}";
     if (!file_exists($path)) {
         return $this->responseFactory->make(404, "Not found: {$path}", 'text/plain');
     }
     $mime = FileUtil::getMimeType($path);
     // Serve image file.
     if (FileUtil::isImageType($mime)) {
         // Use image manipulation parameters extracted from the request.
         return $this->glideServer->getImageResponse($url, $request->getQueryParams());
     }
     // Server non-image file.
     return $this->responseFactory->makeStream(fopen($path, 'rb'), 200, ['Content-Type' => $mime, 'Content-Length' => (string) filesize($path), 'Cache-Control' => 'max-age=31536000, public', 'Expires' => date_create('+1 years')->format('D, d M Y H:i:s') . ' GMT']);
 }
 /**
  * Handle the case where a file has been uploaded for a field, possibly replacing another already set on the field.
  *
  * @param Model                 $model
  * @param string                $fieldName
  * @param UploadedFileInterface $file
  */
 private function newUpload(Model $model, $fieldName, UploadedFileInterface $file)
 {
     $filename = $file->getClientFilename();
     $ext = strtolower(str_segmentsLast($filename, '.'));
     $name = str_segmentsStripLast($filename, '.');
     $id = uniqid();
     $mime = FileUtil::getUploadedFileMimeType($file);
     $isImage = FileUtil::isImageType($mime);
     $fileModel = $model->files()->create(['id' => $id, 'name' => $name, 'ext' => $ext, 'mime' => $mime, 'image' => $isImage, 'group' => str_segmentsLast($fieldName, '.')]);
     // Save the uploaded file.
     $path = "{$this->fileArchivePath}/{$fileModel->path}";
     $dir = dirname($path);
     if (!file_exists($dir)) {
         mkdir($dir, 0777, true);
     }
     $file->moveTo($path);
     // Delete the previous file for this field, if one exists.
     $prevFilePath = $model->getOriginal($fieldName);
     if (exists($prevFilePath)) {
         $this->deleteFile($prevFilePath);
     }
     $model->{$fieldName} = $fileModel->path;
     $model->save();
 }