Bläddra i källkod

Fix Items functions

svalavuo 1 dag sedan
förälder
incheckning
d7fad5dfce
4 ändrade filer med 116 tillägg och 2 borttagningar
  1. 3 0
      Dockerfile
  2. 34 2
      backend/api/items.php
  3. 12 0
      build.sh
  4. 67 0
      fix_image_urls.php

+ 3 - 0
Dockerfile

@@ -57,6 +57,9 @@ COPY --from=frontend-builder /app/frontend/dist ./frontend/dist
 # Copy test script for environment testing
 COPY test-env.php ./
 
+# Copy image URL fix script
+COPY fix_image_urls.php ./
+
 # Create uploads and attachments directories and set permissions
 RUN mkdir -p uploads attachments && \
     chown -R www-data:www-data /var/www/html/uploads /var/www/html/attachments && \

+ 34 - 2
backend/api/items.php

@@ -25,12 +25,28 @@ switch($request_method) {
             $item->readOne();
             
             if($item->name != null) {
+                // Fix picture URL if it contains incorrect path
+                $picture_url = $item->picture;
+                if ($picture_url && strpos($picture_url, '/var/www/html/') !== false) {
+                    // Remove /var/www/html/ from the path
+                    $picture_url = str_replace('/var/www/html/', '', $picture_url);
+                }
+                if ($picture_url && strpos($picture_url, '/api/') !== false) {
+                    // Remove /api/ from the beginning if present
+                    $picture_url = str_replace('/api/', '', $picture_url);
+                }
+                // Ensure proper URL format
+                if ($picture_url && !preg_match('/^https?:\/\//', $picture_url)) {
+                    $baseUrl = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]";
+                    $picture_url = $baseUrl . '/' . ltrim($picture_url, '/');
+                }
+                
                 $item_arr = array(
                     "id" => $item->id,
                     "name" => $item->name,
                     "description" => $item->description,
                     "serial_number" => $item->serial_number,
-                    "picture" => $item->picture,
+                    "picture" => $picture_url,
                     "quantity" => $item->quantity,
                     "price" => $item->price,
                     "date_of_purchase" => $item->date_of_purchase,
@@ -55,12 +71,28 @@ switch($request_method) {
                 while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
                     extract($row);
                     
+                    // Fix picture URL if it contains incorrect path
+                    $picture_url = $picture;
+                    if ($picture && strpos($picture, '/var/www/html/') !== false) {
+                        // Remove /var/www/html/ from the path
+                        $picture_url = str_replace('/var/www/html/', '', $picture);
+                    }
+                    if ($picture && strpos($picture, '/api/') !== false) {
+                        // Remove /api/ from the beginning if present
+                        $picture_url = str_replace('/api/', '', $picture);
+                    }
+                    // Ensure proper URL format
+                    if ($picture_url && !preg_match('/^https?:\/\//', $picture_url)) {
+                        $baseUrl = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]";
+                        $picture_url = $baseUrl . '/' . ltrim($picture_url, '/');
+                    }
+                    
                     $item_item = array(
                         "id" => $id,
                         "name" => $name,
                         "description" => $description,
                         "serial_number" => $serial_number,
-                        "picture" => $picture,
+                        "picture" => $picture_url,
                         "quantity" => $quantity,
                         "price" => $price,
                         "date_of_purchase" => $date_of_purchase,

+ 12 - 0
build.sh

@@ -133,6 +133,18 @@ echo "   🗄️  Database User: $DB_USER"
 echo "   🔴 Redis Port: $REDIS_PORT ($([ "$REDIS_RUNNING" = true ] && echo "EXTERNAL" || echo "DOCKER"))"
 echo ""
 
+# Test environment variables and database connection inside container
+echo "🧪 Testing environment variables and database connection..."
+docker exec inventory-app php /var/www/html/test-env.php
+
+# Fix incorrect image URLs in database
+echo "🔧 Fixing incorrect image URLs in database..."
+docker exec inventory-app php /var/www/html/fix_image_urls.php
+
+# Run database migration script
+echo "🔄 Running database migration script..."
+docker-compose run --rm inventory-app php /var/www/html/migrate.php
+
 # Test environment variables and database connectivity
 echo "🔍 Testing environment variables and database connectivity..."
 echo "ℹ️  Note: If you see Docker Compose threading errors, they can be safely ignored"

+ 67 - 0
fix_image_urls.php

@@ -0,0 +1,67 @@
+<?php
+/**
+ * Database migration script to fix incorrect image URLs in items table
+ * This script will update all image URLs that contain incorrect paths
+ */
+
+require_once 'backend/config/database.php';
+
+echo "Starting image URL migration...\n";
+
+try {
+    $database = new Database();
+    $db = $database->getConnection();
+    
+    echo "Connected to database successfully.\n";
+    
+    // First, let's see how many items have incorrect URLs
+    $check_query = "SELECT id, picture FROM items WHERE picture LIKE '%/var/www/html/%' OR picture LIKE '%/api/%'";
+    $check_stmt = $db->prepare($check_query);
+    $check_stmt->execute();
+    
+    $incorrect_urls = $check_stmt->fetchAll(PDO::FETCH_ASSOC);
+    $total_items = count($incorrect_urls);
+    
+    echo "Found {$total_items} items with incorrect image URLs.\n";
+    
+    if ($total_items === 0) {
+        echo "No items need fixing. Migration complete.\n";
+        exit(0);
+    }
+    
+    // Update each incorrect URL
+    $update_query = "UPDATE items SET picture = ? WHERE id = ?";
+    $update_stmt = $db->prepare($update_query);
+    
+    $fixed_count = 0;
+    
+    foreach ($incorrect_urls as $item) {
+        $old_url = $item['picture'];
+        $new_url = $old_url;
+        
+        // Remove /var/www/html/ from the path
+        if (strpos($new_url, '/var/www/html/') !== false) {
+            $new_url = str_replace('/var/www/html/', '', $new_url);
+        }
+        
+        // Remove /api/ from the beginning if present
+        if (strpos($new_url, '/api/') !== false) {
+            $new_url = str_replace('/api/', '', $new_url);
+        }
+        
+        // Only update if the URL actually changed
+        if ($new_url !== $old_url) {
+            echo "Fixing item ID {$item['id']}: {$old_url} -> {$new_url}\n";
+            $update_stmt->execute([$new_url, $item['id']]);
+            $fixed_count++;
+        }
+    }
+    
+    echo "Successfully fixed {$fixed_count} image URLs.\n";
+    echo "Migration completed successfully!\n";
+    
+} catch (Exception $e) {
+    echo "Error during migration: " . $e->getMessage() . "\n";
+    exit(1);
+}
+?>