FlexForm: Implemented delayed flash uploads

This commit is contained in:
Matias Griese
2018-12-07 20:41:37 +02:00
parent 257396aa06
commit 787bf8beeb
5 changed files with 21 additions and 15 deletions

View File

@@ -231,7 +231,7 @@ class FlexDirectory implements FlexAuthorizeInterface
* @param bool $isFullUpdate
* @return FlexObject
*/
public function update(array $data, string $key = null, bool $isFullUpdate = false) : FlexObject
public function update(array $data, string $key = null) : FlexObject
{
$object = null !== $key ? $this->getIndex()->get($key) : null;
@@ -248,7 +248,7 @@ class FlexDirectory implements FlexAuthorizeInterface
}
} else {
$oldKey = $object->getStorageKey();
$object->update($data, $isFullUpdate);
$object->update($data);
$newKey = $object->getStorageKey();
if ($oldKey !== $newKey) {

View File

@@ -205,10 +205,14 @@ class FlexForm implements FlexFormInterface
throw new \RuntimeException(sprintf('FlexForm: Bad HTTP method %s', $method));
}
$flash = $this->getFlash();
$data = $request->getParsedBody();
$files = $request->getUploadedFiles();
$files = array_merge_recursive($flash->getFilesByFields(), $request->getUploadedFiles());
$this->submit($data, $files);
$flash->delete();
} catch (\Exception $e) {
$this->errors[] = $e->getMessage();
}
@@ -449,15 +453,16 @@ class FlexForm implements FlexFormInterface
{
$this->validate();
/** @var FlexObject $object */
$object = clone $this->getObject();
$object->update($data);
if (method_exists($object, 'triggerEvent')) {
$object->triggerEvent('onSave');
if ($files && method_exists($object, 'upload')) {
$object->upload($files);
}
if (method_exists($object, 'upload')) {
$object->upload($files);
if (method_exists($object, 'triggerEvent')) {
$object->triggerEvent('onSave');
}
$object->save();
@@ -468,6 +473,9 @@ class FlexForm implements FlexFormInterface
protected function checkUploads(array $files): void
{
foreach ($files as $file) {
if (null === $file) {
continue;
}
if ($file instanceof UploadedFileInterface) {
$this->checkUpload($file);
} else {

View File

@@ -109,11 +109,10 @@ class FlexObject implements FlexObjectInterface, FlexAuthorizeInterface
/**
* @param array $data
* @param bool $isFullUpdate
* @return $this
* @throws ValidationException
*/
public function update(array $data, $isFullUpdate = false)
public function update(array $data)
{
// Validate and filter the incoming data.
$blueprint = $this->getFlexDirectory()->getBlueprint();
@@ -121,11 +120,9 @@ class FlexObject implements FlexObjectInterface, FlexAuthorizeInterface
// Filter updated data.
$this->filterElements($data);
if (!$isFullUpdate) {
// Partial update: merge data to the existing object.
$elements = $this->getElements();
$data = $blueprint->mergeData($elements, $data);
}
// Merge data to the existing object.
$elements = $this->getElements();
$data = $blueprint->mergeData($elements, $data);
// Validate and filter elements and throw an error if any issues were found.
$blueprint->validate($data + ['storage_key' => $this->getStorageKey(), 'timestamp' => $this->getTimestamp()]);

View File

@@ -115,6 +115,7 @@ trait FlexMediaTrait
$uploadedFile->moveTo($filepath);
} catch (\Exception $e) {
$language = $grav['language'];
throw new RuntimeException($language->translate('PLUGIN_ADMIN.FAILED_TO_MOVE_UPLOADED_FILE'), 400);
}

View File

@@ -55,7 +55,7 @@ class FormFlashFile implements UploadedFileInterface, \JsonSerializable
throw new \InvalidArgumentException('Invalid path provided for move operation; must be a non-empty string');
}
$this->moved = \rename($this->getTmpFile(), $targetPath);
$this->moved = \copy($this->getTmpFile(), $targetPath);
if (false === $this->moved) {
throw new \RuntimeException(\sprintf('Uploaded file could not be moved to %s', $targetPath));