DanielleNguyen commited on
Commit
2c12d00
·
verified ·
1 Parent(s): d817b4f

Update supabase-sync.sh

Browse files
Files changed (1) hide show
  1. supabase-sync.sh +8 -26
supabase-sync.sh CHANGED
@@ -10,11 +10,9 @@ SUPABASE_API="${SUPABASE_URL}/storage/v1"
10
  upload_file() {
11
  local filepath="$1"
12
  local filename=$(basename "$filepath")
13
- local object_path="auths/${filename}"
14
 
15
- # Try update first (upsert), if fails try create
16
  HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" \
17
- -X POST "${SUPABASE_API}/object/${BUCKET}/${object_path}" \
18
  -H "Authorization: Bearer ${SUPABASE_SERVICE_KEY}" \
19
  -H "Content-Type: application/json" \
20
  -H "x-upsert: true" \
@@ -39,14 +37,12 @@ upload_all() {
39
  download() {
40
  echo "[SYNC] Listing files in Supabase bucket..."
41
 
42
- # List files in the auths/ folder
43
  FILE_LIST=$(curl -s \
44
  -H "Authorization: Bearer ${SUPABASE_SERVICE_KEY}" \
45
- "${SUPABASE_API}/object/list/${BUCKET}" \
46
  -H "Content-Type: application/json" \
 
47
  -d '{"prefix":"auths/","limit":100}')
48
 
49
- # Check if we got a valid response
50
  if echo "$FILE_LIST" | jq -e '.[0].name' > /dev/null 2>&1; then
51
  FILE_COUNT=$(echo "$FILE_LIST" | jq length)
52
  echo "[SYNC] Found ${FILE_COUNT} files in Supabase"
@@ -69,12 +65,10 @@ download() {
69
  # Watch auth directory for changes and sync to Supabase
70
  watch_dir() {
71
  echo "[SYNC] Starting file watcher on ${AUTH_DIR}..."
72
-
73
- # Use inotifywait to watch for file changes
74
  inotifywait -m -r -e close_write,create,modify "${AUTH_DIR}" --format '%f' | while read filename; do
75
  if [[ "$filename" == *.json ]]; then
76
  echo "[SYNC] Detected change: ${filename}"
77
- sleep 2 # Wait for file write to complete
78
  if [ -f "${AUTH_DIR}/${filename}" ]; then
79
  upload_file "${AUTH_DIR}/${filename}"
80
  fi
@@ -82,22 +76,10 @@ watch_dir() {
82
  done
83
  }
84
 
85
- # Main command dispatcher
86
  case "$1" in
87
- download)
88
- download
89
- ;;
90
- upload)
91
- upload_file "$2"
92
- ;;
93
- upload-all)
94
- upload_all
95
- ;;
96
- watch)
97
- watch_dir
98
- ;;
99
- *)
100
- echo "Usage: $0 {download|upload <file>|upload-all|watch}"
101
- exit 1
102
- ;;
103
  esac
 
10
  upload_file() {
11
  local filepath="$1"
12
  local filename=$(basename "$filepath")
 
13
 
 
14
  HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" \
15
+ -X POST "${SUPABASE_API}/object/${BUCKET}/auths/${filename}" \
16
  -H "Authorization: Bearer ${SUPABASE_SERVICE_KEY}" \
17
  -H "Content-Type: application/json" \
18
  -H "x-upsert: true" \
 
37
  download() {
38
  echo "[SYNC] Listing files in Supabase bucket..."
39
 
 
40
  FILE_LIST=$(curl -s \
41
  -H "Authorization: Bearer ${SUPABASE_SERVICE_KEY}" \
 
42
  -H "Content-Type: application/json" \
43
+ "${SUPABASE_API}/object/list/${BUCKET}" \
44
  -d '{"prefix":"auths/","limit":100}')
45
 
 
46
  if echo "$FILE_LIST" | jq -e '.[0].name' > /dev/null 2>&1; then
47
  FILE_COUNT=$(echo "$FILE_LIST" | jq length)
48
  echo "[SYNC] Found ${FILE_COUNT} files in Supabase"
 
65
  # Watch auth directory for changes and sync to Supabase
66
  watch_dir() {
67
  echo "[SYNC] Starting file watcher on ${AUTH_DIR}..."
 
 
68
  inotifywait -m -r -e close_write,create,modify "${AUTH_DIR}" --format '%f' | while read filename; do
69
  if [[ "$filename" == *.json ]]; then
70
  echo "[SYNC] Detected change: ${filename}"
71
+ sleep 2
72
  if [ -f "${AUTH_DIR}/${filename}" ]; then
73
  upload_file "${AUTH_DIR}/${filename}"
74
  fi
 
76
  done
77
  }
78
 
 
79
  case "$1" in
80
+ download) download ;;
81
+ upload) upload_file "$2" ;;
82
+ upload-all) upload_all ;;
83
+ watch) watch_dir ;;
84
+ *) echo "Usage: $0 {download|upload <file>|upload-all|watch}"; exit 1 ;;
 
 
 
 
 
 
 
 
 
 
 
85
  esac