1
0

upload.sh 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #!/bin/bash
  2. # Set your Supabase project URL and API key
  3. SUPABASE_URL="https://v5.fares.cyou"
  4. SUPABASE_API_KEY="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyAgCiAgICAicm9sZSI6ICJzZXJ2aWNlX3JvbGUiLAogICAgImlzcyI6ICJzdXBhYmFzZS1kZW1vIiwKICAgICJpYXQiOiAxNjQxNzY5MjAwLAogICAgImV4cCI6IDE3OTk1MzU2MDAKfQ.DaYlNEoUrrEn2Ig7tqibS-PHK5vgusbcbo7X36XVt4Q"
  5. BUCKET_NAME="csv"
  6. #FOLDER="/ftphich"
  7. # Directory to watch
  8. WATCHED_DIR="/app/watched"
  9. # Files to monitor
  10. FILES_TO_MONITOR=("ExportPGRGPNmois.zip" "secondprgtype.zip" "exportPRG.zip" "exportPGRGPN.zip" "exportlicence.txt")
  11. # Declare an associative array to hold the last modified timestamps
  12. declare -A last_modified
  13. # Initialize the last modified timestamps
  14. #"for file in "${FILES_TO_MONITOR[@]}"; do
  15. # last_modified["$file"]=$(stat -c %Y "$WATCHED_DIR/$file" 2>/dev/null)
  16. #done
  17. # Function to upload file to Supabase
  18. upload_file() {
  19. local file="$1"
  20. local basename_file=$(basename "$file")
  21. #echo "Uploading $basename_file to Supabase storage..."
  22. # Use curl to upload the file
  23. response=$(curl --fail-with-body -X POST "${SUPABASE_URL}/upload" \
  24. -H "Content-Type: application/octet-stream" \
  25. -H "Authorization: Bearer ${SUPABASE_API_KEY}" \
  26. --data-binary @"$file" 2>/dev/null)
  27. response=$(curl -F "file=@$file;type=application/octet-stream" \
  28. -H "Authorization: Bearer ${SUPABASE_API_KEY}" \
  29. -X POST "${SUPABASE_URL}/upload" 2>/dev/null)
  30. if [ $? -eq 0 ]; then
  31. echo "File uploaded successfully: $response"
  32. # Check if the upload was successful
  33. if echo "$response" | grep -q '"Key":'; then
  34. #echo "$response"
  35. echo "$(date '+%d%b%y %H:%M:%S') +++ Successfully uploaded $basename_file."
  36. else
  37. echo "$(date '+%d%b%y %H:%M:%S') --- Failed to upload $basename_file. Response: $response"
  38. fi
  39. else
  40. echo "$(date '+%d%b%y %H:%M:%S') --- Failed to upload file. Response: $response"
  41. fi
  42. }
  43. # Polling loop
  44. while true; do
  45. for file in "${FILES_TO_MONITOR[@]}"; do
  46. full_path="$WATCHED_DIR/$file"
  47. # Get the current modified timestamp
  48. current_modified=$(stat -c %Y "$full_path" 2>/dev/null)
  49. #echo "file $file timestamp $current_modified"
  50. # Check if the file has been modified
  51. if [[ "$current_modified" != "${last_modified[$file]}" ]]; then
  52. formatted_lastdate=$(date -d @"${last_modified[$file]}" '+%d%b%y %H%M')
  53. formatted_curdate=$(date -d @"$current_modified" '+%d%b%y %H%M')
  54. echo "=================================================== $file"
  55. echo "$(date '+%d%b%y %H:%M:%S') ... file has been modified. last: $formatted_lastdate >>> current: $formatted_curdate"
  56. upload_file "$full_path"
  57. # Update the last modified timestamp
  58. last_modified["$file"]=$current_modified
  59. fi
  60. done
  61. # Sleep for a specified interval (e.g., 5 seconds)
  62. sleep 5
  63. done