client brief pdf - upload : add file to field without uploading it if it already exist
This commit is contained in:
parent
e74ff559bf
commit
a14f1d210f
9 changed files with 132 additions and 86 deletions
|
|
@ -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
|
||||
Binary file not shown.
|
|
@ -0,0 +1,9 @@
|
|||
Date: 2024-10-14 01:15
|
||||
|
||||
----
|
||||
|
||||
Uuid: xFobejliB3pkAqLr
|
||||
|
||||
----
|
||||
|
||||
Template: document
|
||||
|
|
@ -10,7 +10,7 @@ Clientbriefpdf:
|
|||
|
||||
----
|
||||
|
||||
Description: description
|
||||
Description:
|
||||
|
||||
----
|
||||
|
||||
|
|
|
|||
|
|
@ -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'),
|
||||
|
|
|
|||
57
public/site/config/routes/upload-pdf.php
Normal file
57
public/site/config/routes/upload-pdf.php
Normal 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(),
|
||||
];
|
||||
}
|
||||
}
|
||||
];
|
||||
Loading…
Add table
Add a link
Reference in a new issue