client brief > my images : upload images working
This commit is contained in:
parent
663f841fff
commit
8c19269e48
24 changed files with 164 additions and 108 deletions
|
|
@ -4,19 +4,19 @@ return [
|
|||
'pattern' => 'upload-images.json',
|
||||
'method' => 'POST',
|
||||
'action' => function () {
|
||||
if ($uploads = kirby()->request()->files()) {
|
||||
if ($uploads = kirby()->request()->files()) {
|
||||
$pageUri = kirby()->request()->query()->get('pageUri');
|
||||
$page = page($pageUri);
|
||||
$page = page($pageUri);
|
||||
|
||||
$allFiles = $page->clientBriefImages()->toFiles()->pluck('uuid', ',');
|
||||
|
||||
$alerts = [];
|
||||
$success = '';
|
||||
$newFiles = [];
|
||||
$allFiles = [];
|
||||
|
||||
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);
|
||||
|
||||
|
|
@ -26,9 +26,14 @@ return [
|
|||
});
|
||||
|
||||
if ($duplicates->count() > 0) {
|
||||
$alerts[$upload['name']] = "The file already exists";
|
||||
$duplicate = $duplicates->first();
|
||||
|
||||
if (in_array($duplicate->uuid(), $allFiles)) {
|
||||
$allFiles[] = $duplicate->uuid();
|
||||
$alerts[$upload['name']] = "The file already exists, added to the field.";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
$name = crc32($upload['name'].microtime()). '_' . $upload['name'];
|
||||
$newFile = $page->createFile([
|
||||
|
|
@ -49,19 +54,24 @@ return [
|
|||
$alerts[$upload['name']] = $e->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
$images = array_map(function ($file) {
|
||||
return [
|
||||
'url' => $file->url(),
|
||||
'uuid' => $file->uuid()
|
||||
];
|
||||
}, $newFiles);
|
||||
|
||||
|
||||
$newPage = $page->update([
|
||||
'clientBriefImages' => $allFiles
|
||||
]);
|
||||
|
||||
return compact('images', 'alerts', 'success');
|
||||
$images = [];
|
||||
|
||||
foreach ($newPage->clientBriefImages()->toFiles() as $image) {
|
||||
$images[] = [
|
||||
'url' => $image->url(),
|
||||
'uuid' => (string) $image->uuid(),
|
||||
'tags' => $image->tags()->split(),
|
||||
'name' => $image->filename(),
|
||||
'description' => $image->description()->value(),
|
||||
];
|
||||
}
|
||||
|
||||
return compact('images', 'alerts');
|
||||
}
|
||||
|
||||
return [
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue