diff --git a/system/src/Grav/Framework/Flex/FlexForm.php b/system/src/Grav/Framework/Flex/FlexForm.php index 85504b1a6..879db72b6 100644 --- a/system/src/Grav/Framework/Flex/FlexForm.php +++ b/system/src/Grav/Framework/Flex/FlexForm.php @@ -207,9 +207,8 @@ class FlexForm implements FlexFormInterface $flash = $this->getFlash(); - // TODO: Figure out how to deal with newly uploaded files - //$files = array_merge_recursive($flash->getFilesByFields(), $request->getUploadedFiles()); - $files = $flash->getFilesByFields(); + $includeOriginal = (bool)($this->getBlueprint()->form()['images']['original'] ?? null); + $files = $flash->getFilesByFields($includeOriginal); $data = $request->getParsedBody(); $this->submit($data, $files); @@ -231,9 +230,8 @@ class FlexForm implements FlexFormInterface $flash = $this->getFlash(); - // TODO: Figure out how to deal with newly uploaded files - //$files = array_merge_recursive($flash->getFilesByFields(), $request->getUploadedFiles()); - $files = $flash->getFilesByFields(); + $includeOriginal = (bool)($this->getBlueprint()->form()['images']['original'] ?? null); + $files = $flash->getFilesByFields($includeOriginal); $body = $request->getParsedBody(); $this->files = $files ?? []; @@ -567,8 +565,7 @@ class FlexForm implements FlexFormInterface $value = json_decode($value, true); if ($value === null && json_last_error() !== JSON_ERROR_NONE) { unset($data[$key]); - // FIXME: add back - //$this->errors[] = "Badly encoded JSON data (for {$key}) was sent to the form"; + $this->errors[] = "Badly encoded JSON data (for {$key}) was sent to the form"; } } } diff --git a/system/src/Grav/Framework/Flex/FlexObject.php b/system/src/Grav/Framework/Flex/FlexObject.php index 57f6f57aa..2c27a9228 100644 --- a/system/src/Grav/Framework/Flex/FlexObject.php +++ b/system/src/Grav/Framework/Flex/FlexObject.php @@ -134,8 +134,8 @@ class FlexObject implements FlexObjectInterface, FlexAuthorizeInterface $this->setElements($data); } - if ($files && method_exists($this, 'updateMediaFiles')) { - $this->updateMediaFiles($files); + if ($files && method_exists($this, 'setUpdatedMediaFiles')) { + $this->setUpdatedMediaFiles($files); } return $this; diff --git a/system/src/Grav/Framework/Flex/Traits/FlexMediaTrait.php b/system/src/Grav/Framework/Flex/Traits/FlexMediaTrait.php index 872721683..5968ae7ea 100644 --- a/system/src/Grav/Framework/Flex/Traits/FlexMediaTrait.php +++ b/system/src/Grav/Framework/Flex/Traits/FlexMediaTrait.php @@ -27,6 +27,8 @@ trait FlexMediaTrait { use MediaTrait; + private $uploads; + public function checkUploadedMediaFile(UploadedFileInterface $uploadedFile) { $grav = Grav::instance(); @@ -80,12 +82,12 @@ trait FlexMediaTrait } } - public function uploadMediaFile(UploadedFileInterface $uploadedFile, string $filename = null, string $field = null) : void + public function uploadMediaFile(UploadedFileInterface $uploadedFile, string $filename = null) : void { $this->checkUploadedMediaFile($uploadedFile); if ($filename) { - $this->checkMediaFilename($filename); + $this->checkMediaFilename(basename($filename)); } else { $filename = $uploadedFile->getClientFilename(); } @@ -122,12 +124,16 @@ trait FlexMediaTrait $this->clearMediaCache(); } - public function deleteMediaFile(string $filename, string $field = null) : void + public function deleteMediaFile(string $filename) : void { $grav = Grav::instance(); $language = $grav['language']; - if (!Utils::checkFilename($filename)) { + $basename = basename($filename); + $dirname = dirname($filename); + $dirname = $dirname === '.' ? '' : '/' . $dirname; + + if (!Utils::checkFilename($basename)) { throw new RuntimeException($language->translate('PLUGIN_ADMIN.FILE_COULD_NOT_BE_DELETED') . ': Bad filename: ' . $filename, 400); } @@ -136,47 +142,71 @@ trait FlexMediaTrait /** @var UniformResourceLocator $locator */ $locator = $grav['locator']; - $targetPath = $media->path() . '/' . $filename; - if ($locator->isStream($targetPath)) { + $targetPath = $media->path() . '/' . $dirname; + $targetFile = $media->path() . '/' . $filename; + if ($locator->isStream($targetFile)) { $targetPath = $locator->findResource($targetPath, true, true); + $targetFile = $locator->findResource($targetFile, true, true); $locator->clearCache($targetPath); + $locator->clearCache($targetFile); } - $fileParts = pathinfo($filename); - $found = false; + $fileParts = pathinfo($basename); - if (file_exists($targetPath)) { - $found = true; + if (file_exists($targetFile)) { - $result = unlink($targetPath); + $result = unlink($targetFile); if (!$result) { throw new RuntimeException($language->translate('PLUGIN_ADMIN.FILE_COULD_NOT_BE_DELETED') . ': ' . $filename, 500); } } // Remove Extra Files - foreach (scandir($media->path(), SCANDIR_SORT_NONE) as $file) { - if (preg_match("/{$fileParts['filename']}@\d+x\.{$fileParts['extension']}(?:\.meta\.yaml)?$|{$filename}\.meta\.yaml$/", $file)) { - $targetPath = $media->path() . '/' . $file; - if ($locator->isStream($targetPath)) { - $targetPath = $locator->findResource($targetPath, true, true); - $locator->clearCache($targetPath); + foreach (scandir($targetPath, SCANDIR_SORT_NONE) as $file) { + $preg_name = preg_quote($fileParts['filename'], '`'); + $preg_ext =preg_quote($fileParts['extension'], '`'); + $preg_filename = preg_quote($basename, '`'); + echo $file .' - '; + if (preg_match("`({$preg_name}@\d+x\.{$preg_ext}(?:\.meta\.yaml)?$|{$preg_filename}\.meta\.yaml)$`", $file)) { + $testPath = $targetPath . '/' . $file; + if ($locator->isStream($testPath)) { + $testPath = $locator->findResource($testPath, true, true); + $locator->clearCache($testPath); } - $result = unlink($targetPath); - if (!$result) { - throw new RuntimeException($language->translate('PLUGIN_ADMIN.FILE_COULD_NOT_BE_DELETED') . ': ' . $filename, 500); + if (is_file($testPath)) { + $result = unlink($testPath); + if (!$result) { + throw new RuntimeException($language->translate('PLUGIN_ADMIN.FILE_COULD_NOT_BE_DELETED') . ': ' . $filename, 500); + } } - - $found = true; } } $this->clearMediaCache(); + } - if (!$found) { - throw new RuntimeException($language->translate('PLUGIN_ADMIN.FILE_NOT_FOUND') . ': ' . $filename, 500); + /** + * @param array|null $files + */ + protected function setUpdatedMediaFiles(?array $files): void + { + $list = []; + foreach ($files as $group) { + foreach ($group as $filename => $file) { + $list[$filename] = $file; + } } + + $this->uploads = $list; + } + + /** + * @return array|null + */ + protected function getUpdatedMediaFiles(): ?array + { + return $this->uploads; } } diff --git a/system/src/Grav/Framework/Form/FormFlash.php b/system/src/Grav/Framework/Form/FormFlash.php index f5f0b5008..bc4c43cd3 100644 --- a/system/src/Grav/Framework/Form/FormFlash.php +++ b/system/src/Grav/Framework/Form/FormFlash.php @@ -190,13 +190,14 @@ class FormFlash implements \JsonSerializable } /** + * @param bool $includeOriginal * @return array */ - public function getFilesByFields(): array + public function getFilesByFields($includeOriginal = false): array { $list = []; foreach ($this->files as $field => $values) { - if (strpos($field, '/')) { + if (!$includeOriginal && strpos($field, '/')) { continue; } $list[$field] = $this->getFilesByField($field);