Messa
July 25, 2024, 9:16am
1
Hello.
I have a route set up for removing files - That work’s so far.
$file->delete();
But in the .txt file on the page, there are still references to the deleted files. I removed one out of three, but it still retains all of them.
Image:
- file://u2cR2PYzozMy6AVO
- file://d4UVic0ANq8xxhkl
- file://er8x3gQLHDPrhl8W // removed one
Do i need to load all Images and also update the page content? Or is there a smoother way?
Best regards
Yes, you need to update all files fields when you delete files that could be referenced anywhere.
You can also leave them there, when you call toFiles()
on the field, non-existing files will be excluded. When calling toFile()
, you will have to make sure a file object is returned before you call any file methods, but that is best practice anyway.
If deleting the removed files did not work, I need your code to help you.
Messa
July 25, 2024, 11:08am
3
I have something like that - The “Get images from page” part is where i stopped right now.
[
'pattern' => 'delete/(:any)/(:any)',
'method' => 'DELETE',
'auth' => false,
'action' => function ($projectid, $fileUuid) {
$alerts = [];
$page = site()->index($drafts = true)->filterBy('template', 'pages')->findBy('Projectid', $projectid);
try {
if ($file = $page->files()->find('file://' . $fileUuid)) {
// authenticate as almighty
kirby()->impersonate('kirby');
$file->delete();
// Get images from page
$pageFiles = $page->content()->Image()->toFiles();
if (!empty($pageFiles)) {
foreach ($pageFiles as $files) {
// REMOVE FILE OF PAGE.TXT
}
}
$page->update([
'image' => $newpageFiles,
]);
}
} catch (Exception $e) {
$alerts = $e->getMessage();
}
return compact('alerts', 'projectid', 'fileUuid', 'file');
}
]
I think if you call $pageFiles at this point, i.e. after already deleting the file, it should only contain existing files. So please check this. If so, you only need to update the page with these files.
Messa
July 25, 2024, 12:07pm
5
That removes the whole image content there is not an empty one.
Before:
Title: Test Frontend
----
Note:
----
Image:
- file://u2cR2PYzozMy6AVO
- file://d4UVic0ANq8xxhkl
----
Upload:
----
Projectid: test
----
After :
Title: Test Frontend
----
Note:
----
Upload:
----
Projectid: test
----
[
'pattern' => 'delete/(:any)/(:any)',
'method' => 'DELETE',
'auth' => false,
'action' => function ($projectid, $fileUuid) {
$alerts = [];
$success = '';
$page = site()->index($drafts = true)->filterBy('template', 'submission')->findBy('Projectid', $projectid);
try {
if ($file = $page->files()->find('file://' . $fileUuid)) {
// authenticate as almighty
kirby()->impersonate('kirby');
$file->delete();
$pageFiles = $page->content()->Image()->toFiles();
$page->update([
'Image' => $pageFiles,
]);
}
$success = 'Your file remove was successful';
} catch (Exception $e) {
$alerts = $e->getMessage();
}
return compact('alerts', 'success', 'projectid', 'fileUuid', 'pageFiles');
}
]
From the networktab I got that back.
|alerts|[]|
|success|"Your file remove was successful"|
|projectid|"test"|
|fileUuid|"KA374QOTvZ560wXE"|
|pageFiles|Object { data: {…} }|
|data|Object { "test-frontend/3935723974_bildschirmfoto-2024-07-24-um-14.17.08.png": {…}, "test-frontend/1147461372_bildschirmfoto-2024-07-24-um-12.45.52.png": {…} }|
|test-frontend/3935723974_bildschirmfoto-2024-07-24-um-14.17.08.png|Object { content: {}, translations: null }|
|test-frontend/1147461372_bildschirmfoto-2024-07-24-um-12.45.52.png|Object { content: {}, translations: null }|
You need to pass an array of files, not a collection
Messa
July 25, 2024, 1:01pm
7
Like that?
$pageFiles = $page->content()->Image()->toArray();
Messa
July 29, 2024, 11:30am
8
So that works →
if ($file = $page->files()->find('file://' . $fileUuid)) {
kirby()->impersonate('kirby');
$file->delete();
}
$pageFiles = $page->content()->Image()->toFiles()->toArray();
$page->update([
'image' => $pageFiles,
]);