From f4a23ad08c6be0cc1531fe8dca6ff40e8aacd6f5 Mon Sep 17 00:00:00 2001 From: Ville Rantanen Date: Mon, 20 Aug 2018 20:43:55 +0300 Subject: [PATCH] new pasteboard idea --- code/app.py | 76 ++++++++++++++++- code/flees-manager.py | 2 +- code/templates/flip | 180 +++++++++++++++++++++++++++++++++++++++ code/templates/upload.sh | 2 +- code/utils/utils.py | 3 + 5 files changed, 259 insertions(+), 4 deletions(-) create mode 100755 code/templates/flip diff --git a/code/app.py b/code/app.py index 942df4b..bf307ce 100644 --- a/code/app.py +++ b/code/app.py @@ -14,7 +14,7 @@ from utils.utils import * from utils.crypt import * -__FLEES_VERSION__ = "20180724.0" +__FLEES_VERSION__ = "20180820.0" app = Flask(__name__) app.config.from_object(__name__) # Read config from json ! @@ -333,6 +333,67 @@ def file_details(name, token): return jsonify(files), 200 +@app.route('/file/delete///', methods=['GET']) +def file_delete(name, token, filename): + (ok,share) = get_share(name, token = token) + if not ok: + return share + full_path = os.path.join( + share['path'], + secure_filename_hidden(filename) + ) + if not os.path.exists(full_path): + return "-1", 403 + allow_direct = get_or_none('direct_links', share) if get_or_none('pass_hash', share) else False + if not allow_direct: + return "Not allowed", 403 + upload = get_or_none('upload', share) + if not upload: + return "Not allowed", 403 + overwrite = get_or_none('overwrite', share) + if overwrite: + os.remove(full_path) + else: + file_versionize(full_path) + return "OK", 200 + + +#~ @app.route('/file/direct///', methods=['GET']) +#~ def file_direct(name, token, filename): + #~ (ok,share) = get_share(name, token = token) + #~ if not ok: + #~ return share + #~ full_path = os.path.join( + #~ share['path'], + #~ secure_filename_hidden(filename) + #~ ) + #~ if not os.path.exists(full_path): + #~ return "-1", 403 + #~ allow_direct = get_or_none('direct_links', share) if get_or_none('pass_hash', share) else False + #~ if not allow_direct: + #~ return "-1", 403 + #~ token = get_direct_token(share, filename) + #~ return token, 200 + + +@app.route('/file/ls//', methods=['GET']) +def file_ls(name, token): + (ok,share) = get_share(name, token = token) + if not ok: + return share + files = [] + for file in iter_folder_files(share['path'], version_folder = app.config['VERSION_FOLDER']): + status = file_stat(share['path'],file) + files.append( + "%s %8s %s"%( + status['mtime'], + status['size'], + status['name'], + ) + ) + return "\n".join(files), 200 + + @app.route('/file/size///', methods=['GET']) def file_size(name, token, filename): (ok,share) = get_share(name, token = token) @@ -378,7 +439,7 @@ def list_view(name, token = None): used_token = session[name + 'Token'] else: used_token = '[TOKEN]' - script_api = [get_script_url(app.config['PUBLIC_URL'], share, x, used_token) for x in ('client', 'download', 'upload_split')] + script_api = [get_script_url(app.config['PUBLIC_URL'], share, x, used_token) for x in ('client', 'download', 'upload_split', 'flip')] if not upload: overwrite = False return render_template( @@ -494,6 +555,17 @@ def script_client(name = None, token = None): rooturl = request.url_root ) +@app.route('/script/flip//', methods=['GET']) +@app.route('/script/flip', methods=['GET']) +def script_flip(name = "", token = ""): + return render_template( + "flip", + name = name, + token = token, + rooturl = request.url_root, + version = __FLEES_VERSION__ + ) + @app.route('/script/upload//', methods=['GET']) def script_upload(name = None, token = None): diff --git a/code/flees-manager.py b/code/flees-manager.py index 63a4cc9..1cb8080 100755 --- a/code/flees-manager.py +++ b/code/flees-manager.py @@ -286,7 +286,7 @@ def modify_share(shares, config, opts): if not token in share['tokens']: share['tokens'].append(token) - if opts.expire: + if not opts.expire == False: if opts.expire == "": # REMOVE EXPIRATION if 'expire' in share: diff --git a/code/templates/flip b/code/templates/flip new file mode 100755 index 0000000..244512c --- /dev/null +++ b/code/templates/flip @@ -0,0 +1,180 @@ +#!/bin/bash + +set -e +_help() { + SELF=$( basename "$0" ) + echo "Fleese Clipboard v. {{ version }} + + Usage: $SELF [command] [name] [filename] + + Commands: + (r)ead Display on screen / copies to file + (w)rite Save from stdin / from file / folder + (d)elete Delete an entry + (l)ist [default] List names of clipboards + + Name: Any string for the clipboard name. default: 0 + Filename: + When reading from clipboard: + File or folder to write the clipboard contents. If omitted: stdout + When writing to clipboard: + File or folder to read from. If omitted: stdin + + Shorthand: + echo my data | $SELF 1 # writes data with name: 1 + $SELF 1 | cat - # prints the contents of file: 1 + + Config: + set: FLEES_ROOTURL, FLEES_SHARE, FLEES_TOKEN +" + exit +} + + +_load_config() { + FLEES_SHARE="${FLEES_SHARE:-{{ name }}}" + FLEES_TOKEN="${FLEES_TOKEN:-{{ token }}}" + FLEES_ROOTURL="${FLEES_ROOTURL:-{{ rooturl }}}" +} + +_update_client() { + [[ -n "$FLEES_TOKEN" ]] && [[ -n "$FLEES_SHARE" ]] && { + sharetoken="/$FLEES_SHARE/$FLEES_TOKEN" + } + curl -o "$0" "$FLEES_ROOTURL/script/flip$sharetoken" + err=$? + chmod +x "$0" + exit $err +} + +_list() { + printf "%-16s %8s %s\n" Date Size Name + curl -s "$FLEES_ROOTURL/file/ls/$FLEES_SHARE/$FLEES_TOKEN" + printf "\n" +} + +_write() { + # no file mentioned, use stdin + [[ -z "$FILE" ]] && stream_in=1 + # stdin is open, use stdin + [ -t 0 ] || stream_in=1 + + [ -t 0 ] && [[ -z "$FILE" ]] && { + _msg File to read needed, or use stdin + exit 1 + } + + [[ "$stream_in" -eq 1 ]] && { + _write_stdin "$NAME" + } || { + [[ -d "$FILE" ]] && { + _write_folder "$NAME" "$FILE" + } || { + _write_file "$NAME" "$FILE" + } + } +} + +_write_folder() { # name, file + tar c "$2" | curl -F "file=@-;filename=$1" --progress-bar "${FLEES_ROOTURL}upload/${FLEES_SHARE}/${FLEES_TOKEN}" | cat - +} +_write_file() { # name, file + curl -F "file=@$2;filename=$1" --progress-bar "${FLEES_ROOTURL}upload/${FLEES_SHARE}/${FLEES_TOKEN}" | cat - +} +_write_stdin() { # name + cat - | curl -F "file=@-;filename=$1" "${FLEES_ROOTURL}upload/${FLEES_SHARE}/${FLEES_TOKEN}" +} + +_read() { + [[ -z "$FILE" ]] && stream_out=1 + [ -t 1 ] || stream_out=1 + + [[ "$stream_out" -eq 1 ]] && { + _read_stdout "$NAME" + } || { + _read_file "$NAME" "$FILE" + } +} + +_read_file() { # name, file + curl --progress "$FLEES_ROOTURL/download/$FLEES_SHARE/$FLEES_TOKEN/$1" > "$2" +} +_read_stdout() { # name + curl -s "$FLEES_ROOTURL/download/$FLEES_SHARE/$FLEES_TOKEN/$1" +} + +_delete() { # name + read -p "Sure to delete: $1 ? Break to exit " foo + curl -s "$FLEES_ROOTURL/file/delete/$FLEES_SHARE/$FLEES_TOKEN/$1" +} + +_msg() { + echo "$@" >&2 +} + +_get_name() { + [[ "$ARG1" = "$CMD" ]] && { + NAME="$ARG2" + } || { + NAME="$ARG1" + } + [[ -z "$NAME" ]] && [[ ! "$CMD" = delete ]] && NAME=0 + return 0 +} + +_get_file() { + [[ "$ARG1" = "$NAME" ]] && { + FILE="$ARG2" + } + [[ "$ARG2" = "$NAME" ]] && { + FILE="$ARG3" + } + return 0 +} + +_load_config +for (( i=1; i<=$#; i++ )); do + [[ "${!i}" = "-h" ]] && _help + [[ "${!i}" = "--help" ]] && _help +done + +ARG1="$1" +ARG2="$2" +ARG3="$3" +CMD=list +[[ "$1" = "r" || "$1" = "read" ]] && { CMD=read; ARG1=$CMD; } +[[ "$1" = "w" || "$1" = "write" ]] && { CMD=write; ARG1=$CMD; } +[[ "$1" = "d" || "$1" = "delete" || "$1" = "del" ]] && { CMD=delete; ARG1=$CMD; } +[[ "$1" = "l" || "$1" = "list" ]] && { CMD=list; ARG1=$CMD; } +[[ "$1" = "update" ]] && { _update_client; } +[[ "$1" = "h" || "$1" = "help" ]] && _help +[[ -n "$1" ]] && [[ -e "$STORAGE"/"$1" ]] && CMD=read +# if stdout redirected, default to read +[ -t 1 ] || CMD=read +# if stdin comes from stream, default to write +[ -t 0 ] || CMD=write + +[[ "$CMD" = list ]] && { + _list + exit $? +} + +_get_name +_get_file +[[ "$CMD" = read ]] && { + _read + exit $? +} +[[ "$CMD" = delete ]] && { + _delete "$NAME" + exit $? +} +[[ "$CMD" = write ]] && { + _write + exit $? +} +[[ "$CMD" = edit ]] && { + _edit + exit $? +} + diff --git a/code/templates/upload.sh b/code/templates/upload.sh index 25ae55e..3b8e5ea 100644 --- a/code/templates/upload.sh +++ b/code/templates/upload.sh @@ -10,7 +10,7 @@ SHARE="{{ name }}" TOKEN="{{ token }}" send_file() { - $CAT "$file_name" | curl -F "file=@-;filename=${base_name}" ${ROOTURL}upload/${SHARE}/${TOKEN} + $CAT "$file_name" | curl -F "file=@-;filename=${base_name}" "${ROOTURL}upload/${SHARE}/${TOKEN}" } send_folder() { which pv &> /dev/null && printf -v dusize -- "--size %dk" $( du -s -k "$file_name" | cut -f1 ) diff --git a/code/utils/utils.py b/code/utils/utils.py index 085d6c8..3eb9dae 100644 --- a/code/utils/utils.py +++ b/code/utils/utils.py @@ -121,6 +121,9 @@ def get_script_url(public_url, share, end_point, token = "[TOKEN]"): if end_point == "upload_split": cmd = 'curl -s %s | python - [-s split_size_in_Mb] file_to_upload.ext [second.file.ext]'%( url, ) doc = 'Upload files to the share. -s to set splitting size.' + if end_point == "flip": + cmd = 'curl -s %s > flip && ./flip'%( url, ) + doc = 'Use the share as a command line clipboard' return {'cmd': cmd, 'doc': doc}