#!/bin/bash # # Fix Immich RAF files that were mislabeled as JPG # This script: # 1. Finds all JPG files that are actually Fujifilm RAF (RAW) files # 2. Renames them from .jpg to .raf on the filesystem # 3. Updates Immich's database to match # 4. Triggers thumbnail regeneration # # Run from Mac Mini or any machine with SSH access to PVE # set -e # Config SSH_PASS="GrilledCh33s3#" PVE_IP="10.10.10.120" SSH_OPTS="-o StrictHostKeyChecking=no" # Colors RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' echo "==========================================" echo " Immich RAF File Fixer" echo "==========================================" echo "" # Test connectivity echo "Testing connection to Saltbox..." if ! sshpass -p "$SSH_PASS" ssh $SSH_OPTS root@$PVE_IP 'qm status 101' &>/dev/null; then echo -e "${RED}Error: Cannot connect to PVE or Saltbox VM not running${NC}" exit 1 fi echo -e "${GREEN}Connected${NC}" echo "" # Step 1: Find mislabeled files echo "Step 1: Finding JPG files that are actually RAF..." echo "" MISLABELED_COUNT=$(sshpass -p "$SSH_PASS" ssh $SSH_OPTS root@$PVE_IP 'qm guest exec 101 -- bash -c "echo \"SELECT COUNT(*) FROM asset a JOIN asset_exif e ON a.id = e.\\\"assetId\\\" WHERE a.\\\"originalFileName\\\" ILIKE '"'"'%.jpg'"'"' AND e.\\\"fileSizeInByte\\\" > 35000000 AND e.make = '"'"'FUJIFILM'"'"';\" | docker exec -i immich-postgres psql -U hutson -d immich -t"' 2>/dev/null | grep -o '[0-9]*' | head -1) echo -e "Found ${YELLOW}${MISLABELED_COUNT}${NC} mislabeled files" echo "" if [ "$MISLABELED_COUNT" -eq 0 ]; then echo -e "${GREEN}No mislabeled files found. Nothing to fix!${NC}" exit 0 fi # Confirm before proceeding read -p "Proceed with fixing these files? (y/N) " -n 1 -r echo "" if [[ ! $REPLY =~ ^[Yy]$ ]]; then echo "Aborted." exit 0 fi echo "" echo "Step 2: Creating fix script on Saltbox..." # Create the fix script on Saltbox sshpass -p "$SSH_PASS" ssh $SSH_OPTS root@$PVE_IP 'qm guest exec 101 -- bash -c "cat > /tmp/fix-raf-files.sh << '"'"'SCRIPT'"'"' #!/bin/bash set -e echo "Getting list of mislabeled files..." # Get list of files to fix docker exec -i immich-postgres psql -U hutson -d immich -t -A -F\",\" -c " SELECT a.id, a.\"originalPath\", a.\"originalFileName\" FROM asset a JOIN asset_exif e ON a.id = e.\"assetId\" WHERE a.\"originalFileName\" ILIKE '"'"'"'"'"'"'"'"'%.jpg'"'"'"'"'"'"'"'"' AND e.\"fileSizeInByte\" > 35000000 AND e.make = '"'"'"'"'"'"'"'"'FUJIFILM'"'"'"'"'"'"'"'"' " > /tmp/files_to_fix.csv TOTAL=$(wc -l < /tmp/files_to_fix.csv) echo "Processing $TOTAL files..." COUNT=0 ERRORS=0 while IFS="," read -r asset_id old_path old_filename; do COUNT=$((COUNT + 1)) # Skip empty lines [ -z "$asset_id" ] && continue # Calculate new paths new_filename=$(echo "$old_filename" | sed "s/\.[jJ][pP][gG]$/.RAF/") new_path=$(echo "$old_path" | sed "s/\.[jJ][pP][gG]$/.raf/") echo "[$COUNT/$TOTAL] $old_filename -> $new_filename" # Rename file on filesystem (inside immich container) if docker exec immich test -f "$old_path"; then docker exec immich mv "$old_path" "$new_path" 2>/dev/null if [ $? -ne 0 ]; then echo " ERROR: Failed to rename file" ERRORS=$((ERRORS + 1)) continue fi else echo " WARNING: File not found at $old_path" ERRORS=$((ERRORS + 1)) continue fi # Update database docker exec -i immich-postgres psql -U hutson -d immich -c " UPDATE asset SET \"originalPath\" = '"'"'"'"'"'"'"'"'$new_path'"'"'"'"'"'"'"'"', \"originalFileName\" = '"'"'"'"'"'"'"'"'$new_filename'"'"'"'"'"'"'"'"' WHERE id = '"'"'"'"'"'"'"'"'$asset_id'"'"'"'"'"'"'"'"'::uuid; " > /dev/null 2>&1 if [ $? -ne 0 ]; then echo " ERROR: Failed to update database" # Try to rename back docker exec immich mv "$new_path" "$old_path" 2>/dev/null ERRORS=$((ERRORS + 1)) continue fi done < /tmp/files_to_fix.csv echo "" echo "==========================================" echo "Completed: $((COUNT - ERRORS)) fixed, $ERRORS errors" echo "==========================================" # Cleanup rm -f /tmp/files_to_fix.csv SCRIPT chmod +x /tmp/fix-raf-files.sh"' echo "" echo "Step 3: Running fix script (this may take a while)..." echo "" # Run the fix script sshpass -p "$SSH_PASS" ssh $SSH_OPTS root@$PVE_IP 'qm guest exec 101 -- bash -c "/tmp/fix-raf-files.sh"' 2>&1 | grep -o '"out-data"[^}]*' | sed 's/"out-data" *: *"//' | sed 's/\\n/\n/g' | sed 's/\\t/\t/g' | sed 's/"$//' echo "" echo "Step 4: Restarting Immich to pick up changes..." sshpass -p "$SSH_PASS" ssh $SSH_OPTS root@$PVE_IP 'qm guest exec 101 -- bash -c "docker restart immich"' > /dev/null 2>&1 echo -e "${GREEN}Done!${NC}" echo "" echo "Next steps:" echo "1. Go to Immich Admin -> Jobs -> Thumbnail Generation -> All -> Start" echo "2. This will regenerate thumbnails for all assets" echo ""