-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.sh
More file actions
executable file
·95 lines (81 loc) · 2.83 KB
/
index.sh
File metadata and controls
executable file
·95 lines (81 loc) · 2.83 KB
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
#!/bin/bash
# Function to calculate Base64-encoded SHA256 hash
calculate_sha256() {
if command -v shasum > /dev/null; then
cat | shasum -a 256 | cut -d ' ' -f 1 | xxd -r -p | base64
elif command -v sha256sum > /dev/null; then
cat | sha256sum | cut -d ' ' -f 1 | xxd -r -p | base64
else
echo "Error: Neither shasum nor sha256sum is available." >&2
exit 1
fi
}
# URL encoding function
urlencode() {
local string="$1"
local length="${#string}"
local encoded=""
local pos c o
for (( pos=0; pos<length; pos++ )); do
c="${string:$pos:1}"
case "$c" in
[-_.~a-zA-Z0-9])
encoded+="$c"
;;
*)
printf -v o '%%%02x' "'$c"
encoded+="$o"
;;
esac
done
echo "$encoded"
}
# Get the base directory (can be overridden by setting BRAIDFS_BASE_DIR)
BASE_DIR="${BRAIDFS_BASE_DIR:-${HOME}/http}"
# Get the configuration file to read the port
CONFIG_FILE="${BASE_DIR}/.braidfs/config"
if [ -f "$CONFIG_FILE" ]; then
PORT=$(grep -o '"port":[^,}]*' "$CONFIG_FILE" | sed 's/"port"://; s/ //g')
if [ -z "$PORT" ]; then
PORT=45678 # Default port if not found in config
fi
else
PORT=45678 # Default port if config file doesn't exist
fi
# Check if the first argument is "editing"
if [ "$1" = "editing" ]; then
FILENAME="$2"
# Convert to absolute path if needed
if [[ ! "$FILENAME" = /* ]]; then
FILENAME="$(pwd)/$FILENAME"
fi
# Calculate SHA256 hash directly from stdin
HASH=$(calculate_sha256)
# Make HTTP request
RESPONSE=$(curl -s -f "http://localhost:${PORT}/.braidfs/get_version/$(urlencode "$FILENAME")/$(urlencode "$HASH")")
CURL_EXIT_CODE=$?
echo "$RESPONSE"
exit $CURL_EXIT_CODE
# Check if the first argument is "edited"
elif [ "$1" = "edited" ]; then
FILENAME="$2"
# Convert to absolute path if needed
if [[ ! "$FILENAME" = /* ]]; then
FILENAME="$(pwd)/$FILENAME"
fi
PARENT_VERSION="$3"
# Make HTTP request (getting body from stdin)
curl -s -X PUT --data-binary @- "http://localhost:${PORT}/.braidfs/set_version/$(urlencode "$FILENAME")/$(urlencode "$PARENT_VERSION")"
# For all other commands, pass through to the Node.js script
else
# Resolve the actual path of the script, even if it's a symlink
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # Resolve $SOURCE until it's no longer a symlink
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
SOURCE="$(readlink "$SOURCE")"
# If $SOURCE was a relative symlink, resolve it relative to the symlink's directory
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE"
done
SCRIPT_DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
node "$SCRIPT_DIR/index.js" "$@"
fi