client brief - fil(s) upload working
This commit is contained in:
parent
769639b241
commit
6d43e3b8c7
5 changed files with 118 additions and 17 deletions
|
|
@ -21,6 +21,7 @@ return [
|
|||
'routes' => [
|
||||
require(__DIR__ . '/routes/logout.php'),
|
||||
require(__DIR__ . '/routes/toggle-favorite.php'),
|
||||
require(__DIR__ . '/routes/upload.php')
|
||||
],
|
||||
'hooks' => [
|
||||
'page.create:after' => require_once(__DIR__ . '/hooks/create-steps.php'),
|
||||
|
|
|
|||
53
public/site/config/routes/upload.php
Normal file
53
public/site/config/routes/upload.php
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
<?php
|
||||
|
||||
return [
|
||||
'pattern' => 'upload.json',
|
||||
'method' => 'POST',
|
||||
'action' => function () {
|
||||
if ($uploads = kirby()->request()->files()) {
|
||||
$pageUri = kirby()->request()->query()->get('pageUri');
|
||||
$page = page($pageUri);
|
||||
|
||||
$alerts = [];
|
||||
$success = '';
|
||||
foreach ($uploads->get('images') as $upload) {
|
||||
// check for duplicate
|
||||
$files = $page->files();
|
||||
$duplicates = $files->filter(function ($file) use ($upload) {
|
||||
// get original safename without prefix
|
||||
$pos = strpos($file->filename(), '_');
|
||||
$originalSafename = substr($file->filename(), $pos + 1);
|
||||
|
||||
return $originalSafename === F::safeName($upload['name']) &&
|
||||
$file->mime() === $upload['type'] &&
|
||||
$file->size() === $upload['size'];
|
||||
});
|
||||
|
||||
if ($duplicates->count() > 0) {
|
||||
$alerts[$upload['name']] = "The file already exists";
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
$name = crc32($upload['name'].microtime()). '_' . $upload['name'];
|
||||
$file = $page->createFile([
|
||||
'source' => $upload['tmp_name'],
|
||||
'filename' => $name,
|
||||
'template' => 'default',
|
||||
'content' => [
|
||||
'date' => date('Y-m-d h:m')
|
||||
]
|
||||
]);
|
||||
$success = 'Your file upload was successful';
|
||||
} catch (Exception $e) {
|
||||
$alerts[$upload['name']] = $e->getMessage();
|
||||
}
|
||||
}
|
||||
return compact('alerts', 'success');
|
||||
}
|
||||
|
||||
return [
|
||||
'error' => 'Aucun fichier reçu.',
|
||||
];
|
||||
}
|
||||
];
|
||||
Loading…
Add table
Add a link
Reference in a new issue