client brief pdf - upload : add file to field without uploading it if it already exist

This commit is contained in:
isUnknown 2024-10-14 15:23:06 +02:00
parent e74ff559bf
commit a14f1d210f
9 changed files with 132 additions and 86 deletions

View file

@ -22,4 +22,4 @@ Uuid: 6yh1yt2Sk45Y2sOl
----
Clientbriefimages: - file://ihuGLrw5vll0R4j2
Clientbriefpdf: home/927346290_au-commencement-du-college-chloe-avait-donc-fait-la-connaissance-dun-garcon.-il-etait-plus-vieux.pdf

View file

@ -10,7 +10,7 @@ Clientbriefpdf:
----
Description: description
Description:
----

View file

@ -25,6 +25,7 @@ return [
require(__DIR__ . '/routes/save-page.php'),
require(__DIR__ . '/routes/save-file.php'),
require(__DIR__ . '/routes/remove-file.php'),
require(__DIR__ . '/routes/upload-pdf.php'),
],
'hooks' => [
'page.create:after' => require_once(__DIR__ . '/hooks/create-steps.php'),

View file

@ -0,0 +1,57 @@
<?php
return [
'pattern' => 'upload-pdf.json',
'method' => 'POST',
'action' => function () {
$kirby = kirby();
$request = $kirby->request();
$files = $request->files();
$data = $request->data();
$upload = $files->get('file') ?? null;
$pageUri = $request->query()->get('pageUri');
$page = page($pageUri);
if (!$page) {
return [
'error' => 'Invalid page.',
];
}
try {
$name = crc32($upload['name'] . microtime()) . '_' . $upload['name'];
$existingFileId = $page->clientBriefPdf()->id() ?? null;
if ($existingFileId) {
$existingFile = $kirby->file($existingFileId);
if ($existingFile) {
$existingFile->delete();
}
}
$newFile = $page->createFile([
'source' => $upload['tmp_name'],
'filename' => $name,
'template' => 'document',
'content' => [
'date' => date('Y-m-d h:i')
]
]);
$newPage = $page->update([
"clientBriefPdf" => $newFile->id()
]);
return $newPage->toArray();
} catch (Exception $e) {
return [
'error' => $e->getMessage(),
];
}
}
];