MikaelTradera commited on
Commit
12b2337
·
verified ·
1 Parent(s): 33e21bc

Update dlmodels.sh

Browse files
Files changed (1) hide show
  1. dlmodels.sh +40 -31
dlmodels.sh CHANGED
@@ -8,64 +8,73 @@ fi
8
 
9
  # Set the input file from the first argument
10
  INPUT_FILE="$1"
 
11
 
12
- # Function to reliably get the remote file size
13
- get_remote_size() {
 
 
 
 
 
 
14
  local url="$1"
15
- # Attempt to retrieve Content-Length header
16
- local remote_size=$(curl -sI "$url" | grep -i "Content-Length" | awk '{print $2}' | tr -d '\r')
17
 
18
- # If Content-Length is not available, try downloading a small byte range
19
- if [[ -z "$remote_size" || "$remote_size" -lt 2 ]]; then
20
- remote_size=$(curl -s --range 0-1 "$url" | wc -c)
 
 
 
21
  fi
22
-
23
- # Return the size, or empty if it can't be determined
24
- echo "$remote_size"
25
  }
26
 
27
- # Function to check file existence and download integrity
28
  download_file() {
29
  local filepath="$1"
30
  local url="$2"
31
 
32
  echo -e "\nChecking file: $filepath"
33
 
34
- # If file already exists, verify integrity
35
  if [[ -f "$filepath" ]]; then
36
- # Get remote file size using get_remote_size function
37
- remote_size=$(get_remote_size "$url")
38
- local_size=$(stat -c%s "$filepath")
39
-
40
- # Debug output to confirm size checks
41
- echo "Remote size: ${remote_size:-Unknown}, Local size: $local_size for file $filepath"
42
 
43
- # Skip file if Content-Length is not available or remote size can't be determined
44
- if [[ -z "$remote_size" || "$remote_size" -lt 2 ]]; then
45
- echo -e "[SKIP] Unable to verify size for $filepath. Assuming complete."
46
  return 0
47
  fi
 
48
 
49
- # If the file sizes match, skip re-downloading
50
- if [[ "$remote_size" == "$local_size" ]]; then
51
- echo -e "[SKIP] $filepath already exists and is complete."
 
 
 
 
52
  return 0
53
- else
54
- echo -e "[RE-DOWNLOAD] $filepath is incomplete or corrupted. Re-downloading..."
55
- rm -f "$filepath" # Remove the corrupted file before re-downloading
56
  fi
57
  else
58
- echo -e "[DOWNLOAD] Starting download of $filepath"
59
  fi
60
 
61
- # Download the file using wget, with progress shown in a clean format
 
62
  wget -c --progress=dot -O "$filepath" "$url" 2>&1 | \
63
  grep --line-buffered "%" | \
64
  sed -u -e "s,\.,,g" | \
65
  awk '{printf("\r[DOWNLOADING] %s - %s", "'$filepath'", $2)}'
66
 
67
- # Confirm download completion
68
  echo -e "\n[COMPLETE] Downloaded $filepath"
 
 
 
 
69
  }
70
 
71
  # Read and process each line in the specified input file
@@ -74,7 +83,7 @@ while IFS= read -r line; do
74
  filepath=$(echo "$line" | awk '{print $1}')
75
  url=$(echo "$line" | awk '{print $2}')
76
 
77
- # Call the download function for each file in the list
78
  download_file "$filepath" "$url" &
79
  done < "$INPUT_FILE"
80
 
 
8
 
9
  # Set the input file from the first argument
10
  INPUT_FILE="$1"
11
+ CHECKSUM_FILE=".checksums"
12
 
13
+ # Function to generate SHA-256 checksum for a given file
14
+ generate_checksum() {
15
+ local filepath="$1"
16
+ sha256sum "$filepath" | awk '{print $1}'
17
+ }
18
+
19
+ # Function to retrieve the remote checksum if available
20
+ get_remote_checksum() {
21
  local url="$1"
22
+ local checksum_url="${url}.sha256" # Assuming the checksum URL is <file_url>.sha256
 
23
 
24
+ # Attempt to fetch the remote checksum
25
+ remote_checksum=$(curl -s "$checksum_url")
26
+ if [[ -z "$remote_checksum" ]]; then
27
+ echo "no_remote_checksum"
28
+ else
29
+ echo "$remote_checksum"
30
  fi
 
 
 
31
  }
32
 
33
+ # Function to download the file and verify its integrity
34
  download_file() {
35
  local filepath="$1"
36
  local url="$2"
37
 
38
  echo -e "\nChecking file: $filepath"
39
 
40
+ # Check if file exists locally and get its checksum
41
  if [[ -f "$filepath" ]]; then
42
+ local_checksum=$(generate_checksum "$filepath")
43
+ stored_checksum=$(grep "$filepath" "$CHECKSUM_FILE" | awk '{print $1}')
 
 
 
 
44
 
45
+ # Check if local file's checksum matches stored checksum
46
+ if [[ "$local_checksum" == "$stored_checksum" ]]; then
47
+ echo -e "[SKIP] $filepath already exists and matches the stored checksum."
48
  return 0
49
  fi
50
+ fi
51
 
52
+ # Check remote checksum if available
53
+ remote_checksum=$(get_remote_checksum "$url")
54
+ if [[ "$remote_checksum" != "no_remote_checksum" ]]; then
55
+ echo "Remote checksum found: $remote_checksum"
56
+ # Compare with local file's checksum if it exists
57
+ if [[ -f "$filepath" && "$remote_checksum" == "$local_checksum" ]]; then
58
+ echo -e "[SKIP] $filepath already matches the remote checksum."
59
  return 0
 
 
 
60
  fi
61
  else
62
+ echo "[INFO] No remote checksum available for $url. Proceeding with download."
63
  fi
64
 
65
+ # Download the file with wget, showing progress
66
+ echo -e "[DOWNLOAD] Downloading $filepath..."
67
  wget -c --progress=dot -O "$filepath" "$url" 2>&1 | \
68
  grep --line-buffered "%" | \
69
  sed -u -e "s,\.,,g" | \
70
  awk '{printf("\r[DOWNLOADING] %s - %s", "'$filepath'", $2)}'
71
 
72
+ # Confirm completion
73
  echo -e "\n[COMPLETE] Downloaded $filepath"
74
+
75
+ # Generate and store checksum after download
76
+ new_checksum=$(generate_checksum "$filepath")
77
+ echo "$new_checksum $filepath" >> "$CHECKSUM_FILE"
78
  }
79
 
80
  # Read and process each line in the specified input file
 
83
  filepath=$(echo "$line" | awk '{print $1}')
84
  url=$(echo "$line" | awk '{print $2}')
85
 
86
+ # Call the download function for each file
87
  download_file "$filepath" "$url" &
88
  done < "$INPUT_FILE"
89