Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

beautify bash #1

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 1 addition & 18 deletions routes/upload.sh.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,7 @@
// routes/upload.sh.ts
import { Handlers } from '$fresh/server.ts'

const script = `#!/bin/bash
if [ "$#" -ne 1 ]; then
echo "Usage: $0 <filename>"
exit 1
fi

FILENAME=$(basename "$1")
KEY=$(openssl rand -hex 32)
IV=$(openssl rand -hex 16)
ENCODED_FILENAME=$(echo -n "$FILENAME" | xxd -plain | tr -d '\n' | sed 's/\\(.\\{2\\}\\)/%\\1/g')

# Use key as hash for storage
openssl enc -aes-256-cbc -in "$1" -K "$KEY" -iv "$IV" | \\
curl -s -X PUT "https://cryptsend.thingylabs.io/\${KEY}.enc" \\
-H "Content-Type: application/octet-stream" \\
--data-binary @- > /dev/null && \\
echo "https://cryptsend.thingylabs.io/d/#\${KEY}\${IV}\${ENCODED_FILENAME}"
`
const script = Deno.readTextFile("../upload.sh")

export const handler: Handlers = {
GET() {
Expand Down
102 changes: 102 additions & 0 deletions upload.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#!/usr/bin/env bash
set -euo pipefail

# max file size of 100MB
MAX_SIZE="100000000"

# arguments
declare -a ARGS
ARGS[0]="${0}"

usage() {
local err="${1:-1}"
local opt="${2:-}"
case "${err}" in
1)
echo "Usage: ${ARGS[0]} <file | directory>"
;;
2)
echo "${opt} is required!"
;;
3)
echo "File to big! $(numfmt --to=iec "${opt}") of max $(numfmt --to=iec "${MAX_SIZE}")"
;;
esac
exit "${err}"
}

prerequisites() {
# one argument required
[[ "${#}" -eq 1 ]] || usage

local -r item="${1}"
# needs to be a file
[[ -f "${item}" || -d "${item}" ]] || usage
ARGS[1]="${item}"

command -v openssl @>/dev/null || usage 2 "openssl"
command -v curl @>/dev/null || usage 2 "curl"
command -v sed @>/dev/null || usage 2 "sed"
command -v tar @>/dev/null || usage 2 "tar"
}

confirm() {
local text="${1}"

read -p "${text} [yY] " -n 1 -r
echo
[[ "${REPLY,,}" = "y" ]] || return 1
}

file() {
local item="${ARGS[1]}"

# if so zip after confirmation
if [[ -d "${item}" ]]; then
local tarfile
tarfile="$(mktemp)"
TRAP 'rm -f "${tarfile}"' EXIT

# todo: we can add an -q flag to not promt this
confirm "Zip and upload folder '$(basename "${item}")'?" || exit 0
tar -czvf "${tarfile}" "${item}"
item="${tarfile}"
fi

# check max size
local size
size="$(
# linux || osx || bsd || yolo
stat -c"%s" "${item}" ||
stat -s "${item}" ||
stat -f"%z" "${item}" ||
echo "0"
)"
( size < MAX_SIZE ) || usage 3 "${size}"

echo "${item}"
}

upload() {
local file="${1}"
local filename encoded_filename key iv

filename="$(basename "${file}")"
key="$(openssl rand -hex 32)"
iv="$(openssl rand -hex 16)"
encoded_filename="$(
echo -n "${filename}" | xxd -plain | tr -d '\n' | sed 's/\\(.\\{2\\}\\)/%\\1/g')
)"

# Use key as hash for storage
openssl \\
enc -aes-256-cbc -in "${file}" -K "${key}" -iv "${iv}" \
| curl -s -X PUT "https://cryptsend.thingylabs.io/${key}.enc" \
-H "Content-Type: application/octet-stream" \
--data-binary @- > /dev/null

echo "https://cryptsend.thingylabs.io/d/#${key}${iv}${encoded_filename}"
}

prerequisites "${@}"
upload "$(file)"
Loading