-
Notifications
You must be signed in to change notification settings - Fork 367
/
Copy pathupload-folder.sh
executable file
·135 lines (116 loc) · 3.05 KB
/
upload-folder.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/bin/bash
print_usage() {
echo "Usage: $0 [OPTIONS] FOLDER_PATH BUCKET_NAME GNFD_CMD_FILEPATH"
echo ""
echo "Upload files from a folder to a bucket using gnfd-cmd."
echo ""
echo "Positional Arguments:"
echo " FOLDER_PATH Path to the folder containing files to upload"
echo " BUCKET_NAME Name of the bucket to upload files to"
echo " GNFD_CMD_FILEPATH Path to the gnfd-cmd executable"
echo ""
echo "Options:"
echo " -h, --help Show this help message and exit"
echo " -v, --verbose Enable verbose mode, echoing execution of each file and progress"
}
if [[ "$1" == "-h" || "$1" == "--help" ]]; then
print_usage
exit 0
fi
verbose=false
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-v|--verbose)
verbose=true
shift
;;
*)
break
;;
esac
done
if [ "$#" -ne 3 ]; then
echo "Error: Invalid number of arguments."
echo ""
print_usage
exit 1
fi
FOLDER_PATH="$1"
BUCKET_NAME="$2"
GNFD_CMD_FILEPATH="$3"
# Function to get the content type based on file extension
get_content_type() {
case "$1" in
*.html)
echo "text/html"
;;
*.css)
echo "text/css"
;;
*.js)
echo "application/javascript"
;;
*.jpg|*.jpeg)
echo "image/jpeg"
;;
*.png)
echo "image/png"
;;
# Add more file extensions and their corresponding content types if needed
*)
echo "application/octet-stream"
;;
esac
}
# Function to calculate the total number of files
count_files() {
local folder="$1"
local count=0
# Iterate over files in the current folder
for file in "$folder"/*; do
if [ -f "$file" ]; then
count=$((count + 1))
elif [ -d "$file" ]; then
# Recursively count files in subfolders
count_subfolder=$(count_files "$file")
count=$((count + count_subfolder))
fi
done
echo "$count"
}
# Function to process files recursively
process_files() {
local folder="$1"
local count="$2"
local processed=0
# Iterate over files in the current folder
for file in "$folder"/*; do
if [ -f "$file" ]; then
filename=$(basename "$file")
content_type=$(get_content_type "$filename")
full_file_path=$(realpath "$file")
gnfd_cmd="$GNFD_CMD_FILEPATH object put --visibility=public-read --contentType=$content_type $full_file_path gnfd://$BUCKET_NAME/$filename"
if [ "$verbose" = true ]; then
echo "Executing command: $gnfd_cmd"
fi
# Uncomment the line below to execute the command
eval "$gnfd_cmd"
processed=$((processed + 1))
if [ "$verbose" = true ]; then
progress=$((processed * 100 / count))
echo "Progress: $progress% ($processed/$count)"
fi
elif [ -d "$file" ]; then
# Recursively process subfolders
process_files "$file" "$count"
fi
done
}
# Start processing files
if [ "$verbose" = true ]; then
echo "Verbose mode enabled."
total_files=$(count_files "$FOLDER_PATH")
echo "Total files to process: $total_files"
fi
process_files "$FOLDER_PATH" "$total_files"