Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public function transform(mixed $input) : mixed
$identifier = substr($input->location, strlen(midcom::get()->config->get('midcom_tempdir')) + 1);
$stats = stat($input->location);
$file = new UploadedFile($input->location, $input->name);
$this->store_tmpfile_metadata($input);
} else {
$identifier = $input->guid;
$stats = $input->stat();
Expand Down Expand Up @@ -101,9 +102,14 @@ public function reverseTransform(mixed $array) : mixed
} elseif (str_starts_with($array['identifier'] ?? '', 'tmpfile-')) {
$tmpfile = midcom::get()->config->get('midcom_tempdir') . '/' . $array['identifier'];
if (file_exists($tmpfile)) {
$metadata = $this->get_tmpfile_metadata($tmpfile);
$attachment = new midcom_db_attachment;
$attachment->name = $title ?: $array['identifier'];
$attachment->name = $metadata['name'] ?? ($title ?: $array['identifier']);
$attachment->mimetype = $metadata['mimetype'] ?? '';
$attachment->location = $tmpfile;
if (empty($title)) {
$title = $metadata['title'] ?? $attachment->name;
}
}
} elseif (!empty($array['object'])) {
$attachment = $array['object'];
Expand All @@ -119,4 +125,30 @@ public function reverseTransform(mixed $array) : mixed

return $attachment;
}

private function store_tmpfile_metadata(midcom_db_attachment $attachment) : void
{
if (!str_starts_with(basename($attachment->location), 'tmpfile-')) {
return;
}

// the attachment location is already inside midcom_tempdir at this point.
// store the metadata next to the tmpfile so it follows the same cleanup lifecycle.
file_put_contents($attachment->location . '.metadata', json_encode([

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we're going to store this on the file system, there would need to be some cleanup at some point, otherwise these files will accumulate forever.

Another option might be to save this kind of stuff in midcom (or maybe system) temp dir, which get cleaned periodically

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looking at the code some more, is att->location inside the tempdir at this point already? If so, maybe add a comment to that effect

'name' => $attachment->name,
'title' => $attachment->title,
'mimetype' => $attachment->mimetype,
]));
}

private function get_tmpfile_metadata(string $tmpfile) : array
{
$metadata_file = $tmpfile . '.metadata';
if (!file_exists($metadata_file)) {
return [];
}

$metadata = json_decode((string) file_get_contents($metadata_file), true);
return is_array($metadata) ? $metadata : [];
}
}