diff --git a/src/midcom/datamanager/extension/transformer/attachmentTransformer.php b/src/midcom/datamanager/extension/transformer/attachmentTransformer.php index 647495498..8126a40a2 100644 --- a/src/midcom/datamanager/extension/transformer/attachmentTransformer.php +++ b/src/midcom/datamanager/extension/transformer/attachmentTransformer.php @@ -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(); @@ -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']; @@ -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([ + '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 : []; + } }