improoooove
This commit is contained in:
parent
8c5bd53ba0
commit
b51b314d74
23
share.sh
23
share.sh
|
@ -1,15 +1,20 @@
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
# Share data to a running Shrlok instance.
|
# Share data to a running Shrlok instance.
|
||||||
|
|
||||||
|
socket_path="/run/shrlok/shr.sock"
|
||||||
|
|
||||||
usage() {
|
usage() {
|
||||||
echo "Usage: $0 [-s SOCKET_PATH] [] TYPE FILE"
|
echo "Usage: $0 [-s SOCKET_PATH] [-o key1=value1,key2=value2,…] TYPE FILE"
|
||||||
|
echo " -s override shrlok socket path (default: $socket_path)"
|
||||||
|
echo " -o comma separated "k=v" pairs to add to the shrlok header"
|
||||||
}
|
}
|
||||||
|
|
||||||
socket_path="/run/shrlok/shr.sock"
|
header_args=
|
||||||
while getopts "hs:" OPTION; do
|
while getopts "hs:o:" OPTION; do
|
||||||
case $OPTION in
|
case $OPTION in
|
||||||
h) usage; exit 0 ;;
|
h) usage; exit 0 ;;
|
||||||
s) socket_path="$OPTARG" ;;
|
s) socket_path="$OPTARG" ;;
|
||||||
|
o) readarray -d "," -t header_args <<< "$OPTARG" ;;
|
||||||
*) usage; exit 1 ;;
|
*) usage; exit 1 ;;
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
|
@ -24,10 +29,20 @@ if [ ! -S "$socket_path" ]; then
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
header_args_str=""
|
||||||
|
if [[ -n "$header_args" ]]; then
|
||||||
|
for item in "${header_args[@]}"; do
|
||||||
|
readarray -d "=" -t kv <<< "$item"
|
||||||
|
key="${kv[0]}"
|
||||||
|
val="$(echo "${kv[1]}" | xargs)" # strip trailing LFs
|
||||||
|
header_args_str+=",\"$key\":\"$val\""
|
||||||
|
done
|
||||||
|
fi
|
||||||
|
|
||||||
# The first thing sent through the socket is the total packet length, as ASCII
|
# The first thing sent through the socket is the total packet length, as ASCII
|
||||||
# digits for the convenience of scripts. Build the header before hand to get its
|
# digits for the convenience of scripts. Build the header before hand to get its
|
||||||
# size, then get the file length and push everything to the socket with socat.
|
# size, then get the file length and push everything to the socket with socat.
|
||||||
header="{\"type\":\"$share_type\"}"
|
header="{\"type\":\"$share_type\"$header_args_str}"
|
||||||
header_length="${#header}"
|
header_length="${#header}"
|
||||||
file_length="$(wc -c "$input_file" | cut -d' ' -f1)"
|
file_length="$(wc -c "$input_file" | cut -d' ' -f1)"
|
||||||
full_length="$(( $header_length + 1 + $file_length ))"
|
full_length="$(( $header_length + 1 + $file_length ))"
|
||||||
|
|
|
@ -66,9 +66,13 @@ class Handler(socketserver.StreamRequestHandler):
|
||||||
|
|
||||||
file_name = None
|
file_name = None
|
||||||
if header.get("type") == "txt":
|
if header.get("type") == "txt":
|
||||||
file_name = write_text(data, title=header.get("title"))
|
file_name = write_text(data, title=header.get("title", ""))
|
||||||
elif header.get("type") == "raw":
|
elif header.get("type") == "raw":
|
||||||
file_name = write_content(data)
|
file_name = write_content(
|
||||||
|
data,
|
||||||
|
name=header.get("name", ""),
|
||||||
|
extension=header.get("ext", ""),
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
print("Unknown type.")
|
print("Unknown type.")
|
||||||
if file_name is None:
|
if file_name is None:
|
||||||
|
@ -100,26 +104,36 @@ class Handler(socketserver.StreamRequestHandler):
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
||||||
def write_text(data: bytes, title=None):
|
def write_text(data: bytes, title: str = "", name: str = ""):
|
||||||
content = "<pre>{}</pre>".format(data.decode(errors="replace"))
|
content = "<pre>{}</pre>".format(data.decode(errors="replace"))
|
||||||
html = HTML_TEMPLATE.format(title=title or "", content=content).encode()
|
html = HTML_TEMPLATE.format(title=title or "", content=content).encode()
|
||||||
return write_content(html)
|
return write_content(html, extension="html")
|
||||||
|
|
||||||
|
|
||||||
def write_content(data: bytes):
|
def write_content(data: bytes, name: str = "", extension: str = ""):
|
||||||
|
print("name", name, "extension", extension)
|
||||||
path = str(Path(ARGS.root) / "txt")
|
path = str(Path(ARGS.root) / "txt")
|
||||||
if not os.path.isdir(path):
|
if not os.path.isdir(path):
|
||||||
print(f"Missing '{path}' folder.")
|
print(f"Missing '{path}' folder.")
|
||||||
return None
|
return None
|
||||||
with tempfile.NamedTemporaryFile(
|
with tempfile.NamedTemporaryFile(
|
||||||
mode="wb",
|
mode="wb",
|
||||||
dir=path,
|
dir=path,
|
||||||
suffix=".html",
|
suffix=f".{extension}" if extension else None,
|
||||||
delete=False
|
delete=False,
|
||||||
) as output_file:
|
) as output_file:
|
||||||
output_file.write(data)
|
output_file.write(data)
|
||||||
os.chmod(output_file.name, 0o644)
|
os.chmod(output_file.name, 0o644)
|
||||||
return output_file.name
|
file_name = output_file.name
|
||||||
|
if name:
|
||||||
|
new_file_name = os.path.join(os.path.dirname(file_name), name)
|
||||||
|
try:
|
||||||
|
os.rename(file_name, name)
|
||||||
|
file_name = new_file_name
|
||||||
|
except OSError as exc:
|
||||||
|
print(f"Failed to give required name to the file: {exc}")
|
||||||
|
return None
|
||||||
|
return file_name
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|
Reference in a new issue