Skip to content

Commit 4f3ed07

Browse files
authored
beautify bash
reformat bash script
1 parent 717bce2 commit 4f3ed07

File tree

1 file changed

+52
-15
lines changed

1 file changed

+52
-15
lines changed

routes/upload.sh.ts

+52-15
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,60 @@
11
// routes/upload.sh.ts
22
import { Handlers } from '$fresh/server.ts'
33

4-
const script = `#!/bin/bash
5-
if [ "$#" -ne 1 ]; then
4+
// TODO: move this into a bash file within the root directory
5+
const script = `#!/usr/bin/env bash
6+
set -euo pipefail
7+
8+
usage() {
69
echo "Usage: $0 <filename>"
710
exit 1
8-
fi
9-
10-
FILENAME=$(basename "$1")
11-
KEY=$(openssl rand -hex 32)
12-
IV=$(openssl rand -hex 16)
13-
ENCODED_FILENAME=$(echo -n "$FILENAME" | xxd -plain | tr -d '\n' | sed 's/\\(.\\{2\\}\\)/%\\1/g')
14-
15-
# Use key as hash for storage
16-
openssl enc -aes-256-cbc -in "$1" -K "$KEY" -iv "$IV" | \\
17-
curl -s -X PUT "https://cryptsend.thingylabs.io/\${KEY}.enc" \\
18-
-H "Content-Type: application/octet-stream" \\
19-
--data-binary @- > /dev/null && \\
20-
echo "https://cryptsend.thingylabs.io/d/#\${KEY}\${IV}\${ENCODED_FILENAME}"
11+
}
12+
13+
prerequisites() {
14+
command -v openssl @>/dev/null || {
15+
echo "openssl is required!"
16+
exit 2
17+
}
18+
}
19+
20+
file() {
21+
# one argument required
22+
[ "\${#}" -eq 1 ] || usage()
23+
24+
local file="${1}"
25+
26+
# needs to be a file
27+
[ -f "\${file}" ] || usage()
28+
29+
# todo: check if it is a directory
30+
# if so zip after confirmation
31+
32+
echo "\${file}"
33+
}
34+
35+
upload() {
36+
local file="\${1}"
37+
local filename="$(basename "\${file}")"
38+
local key="$(openssl rand -hex 32)"
39+
local iv="$(openssl rand -hex 16)"
40+
local encoded_filename
41+
42+
encoded_filename="$(
43+
echo -n "\${filename}" | xxd -plain | tr -d '\n' | sed 's/\\(.\\{2\\}\\)/%\\1/g')
44+
)"
45+
46+
# Use key as hash for storage
47+
openssl \\
48+
enc -aes-256-cbc -in "\${file}" -K "\${key}" -iv "\${iv}" |
49+
curl -s -X PUT "https://cryptsend.thingylabs.io/\${key}.enc" \\
50+
-H "Content-Type: application/octet-stream" \\
51+
--data-binary @- > /dev/null
52+
53+
echo "https://cryptsend.thingylabs.io/d/#\${key}\${iv}\${encoded_filename}"
54+
}
55+
56+
prerequisites
57+
upload "$(file)"
2158
`
2259

2360
export const handler: Handlers = {

0 commit comments

Comments
 (0)