From f0f288797a148a7922f248af4dbf2e5ac343a244 Mon Sep 17 00:00:00 2001 From: jekkos Date: Thu, 5 Mar 2026 12:42:38 +0000 Subject: [PATCH] Add migration to fix existing image filenames with spaces (#4372) This migration will: - Scan all items for filenames containing spaces - Rename both original and thumbnail files on the filesystem - Update database records with sanitized filenames - Only process files that actually exist on the filesystem --- .../20250521000000_FixImageFilenameSpaces.php | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 app/Database/Migrations/20250521000000_FixImageFilenameSpaces.php diff --git a/app/Database/Migrations/20250521000000_FixImageFilenameSpaces.php b/app/Database/Migrations/20250521000000_FixImageFilenameSpaces.php new file mode 100644 index 000000000..7f7eb39b2 --- /dev/null +++ b/app/Database/Migrations/20250521000000_FixImageFilenameSpaces.php @@ -0,0 +1,65 @@ +table('ospos_items'); + + // Get all items with pic_filename containing spaces + $query = $builder->like('pic_filename', ' ', 'both')->get(); + $items = $query->getResult(); + + foreach ($items as $item) { + $old_filename = $item->pic_filename; + $ext = pathinfo($old_filename, PATHINFO_EXTENSION); + $base_name = pathinfo($old_filename, PATHINFO_FILENAME); + + // Sanitize the filename by replacing spaces and special characters + $sanitized_name = preg_replace('/[^a-zA-Z0-9_\-\.]/', '_', $base_name); + $new_filename = $sanitized_name . '.' . $ext; + + // Rename the file on the filesystem + $old_path = FCPATH . 'uploads/item_pics/' . $old_filename; + $new_path = FCPATH . 'uploads/item_pics/' . $new_filename; + + if (file_exists($old_path)) { + // Rename the original file + if (rename($old_path, $new_path)) { + // Check if thumbnail exists and rename it too + $old_thumb = FCPATH . 'uploads/item_pics/' . $base_name . '_thumb.' . $ext; + $new_thumb = FCPATH . 'uploads/item_pics/' . $sanitized_name . '_thumb.' . $ext; + if (file_exists($old_thumb)) { + rename($old_thumb, $new_thumb); + } + + // Update database record + $builder->where('item_id', $item->item_id) + ->update(['pic_filename' => $new_filename]); + } + } + } + } + + /** + * Revert a migration. + * Note: This migration does not support rollback as the original filenames are lost + */ + public function down(): void + { + // This migration cannot be safely reversed as the original filenames are lost + // after sanitization. + } +} \ No newline at end of file