editor for client

This commit is contained in:
ville rantanen
2018-07-24 20:28:00 +03:00
parent c0ccc40d17
commit c004c75f75
3 changed files with 84 additions and 47 deletions

View File

@@ -14,7 +14,7 @@ from utils.utils import *
from utils.crypt import * from utils.crypt import *
__FLEES_VERSION__ = "20180721.1" __FLEES_VERSION__ = "20180724.0"
app = Flask(__name__) app = Flask(__name__)
app.config.from_object(__name__) app.config.from_object(__name__)
# Read config from json ! # Read config from json !
@@ -251,7 +251,7 @@ def editor(name = None):
content = "" content = ""
if os.path.isfile(pathname): if os.path.isfile(pathname):
if pathname.endswith(".txt"): if pathname.endswith(".txt"):
content = open(pathname, 'rt').read(10 * 1024) content = open(pathname, 'rt').read(65536)
return render_template( return render_template(
'editor.html', 'editor.html',

View File

@@ -4,6 +4,7 @@ from subprocess import call, Popen, PIPE, STDOUT
import readline import readline
import glob import glob
from io import StringIO from io import StringIO
import tempfile, hashlib
try: try:
from tabulate import tabulate from tabulate import tabulate
except ImportError: except ImportError:
@@ -71,11 +72,13 @@ class Completer(object):
return results[state] return results[state]
def download_file(file, opts): def download_file(file, opts, filename = False):
print("Download " + file['name']) print("Download " + file['name'])
if not filename:
filename = file['name']
cmd = [ cmd = [
'curl','--create-dirs', 'curl','--create-dirs',
'-o', file['name'], '-o', filename,
'%s%s/%s/%s/%s'%( '%s%s/%s/%s/%s'%(
opts.rooturl, opts.rooturl,
"download", "download",
@@ -96,23 +99,43 @@ def download_file(file, opts):
return return
def file_hash(file):
buf = 65536 # lets read stuff in 64kb chunks!
md5 = hashlib.md5()
with open(file, 'rb') as f:
while True:
data = f.read(buf)
if not data:
break
md5.update(data)
return md5.hexdigest()
def edit_file(file):
old_hash = file_hash(file)
editor = os.environ.get('EDITOR','vim')
call([editor, file])
new_hash = file_hash(file)
return new_hash != old_hash
def menu(opts): def menu(opts):
commands = [ commands = {
'Download', '1': {'name': 'Download', 'cmd': menu_download},
'Upload' '2': {'name': 'Upload', 'cmd': menu_upload},
] '3': {'name': 'Edit', 'cmd': menu_edit},
}
print_title("Main menu") print_title("Main menu")
for i,command in enumerate(commands): for command in sorted(commands):
print(" %d. %s"%( i+1, command, )) print(" %s. %s"%( command, commands[command]['name']))
comp = Completer(choices = ["1","2"]) comp = Completer(choices = commands.keys())
readline.set_completer(comp.complete) readline.set_completer(comp.complete)
print("\n[Empty to exit]") print("\n[Empty to exit]")
choice = user_input("Number of action: ").strip() choice = user_input("Number of action: ").strip()
if choice == "1": if choice in commands:
menu_download(opts) commands[choice]['cmd'](opts)
if choice == "2": else:
menu_upload(opts)
if choice == "":
sys.exit(0) sys.exit(0)
@@ -149,6 +172,40 @@ def menu_download(opts):
download_file(files[choice], opts) download_file(files[choice], opts)
def menu_edit(opts):
while True:
print_title("Edit file")
files = json.loads(run_command("file/details", opts))
file_table = []
for f in files:
if not f['editable']:
continue
file_table.append((
f['name'],
float(f['size'].replace(",","")),
f['mtime']
))
print(tabulate(file_table, headers = ("Name", "Size [Mb]", "Modified")))
name_list = [x['name'] for x in files]
comp = Completer(choices = name_list)
# we want to treat '/' as part of a word, so override the delimiters
readline.set_completer(comp.complete)
print("\n[Empty to return, new name to create new file]")
choice = user_input('Edit file: ').strip()
if choice == "":
return
tfp, tmp = tempfile.mkstemp(
suffix = ".txt"
)
if choice in name_list:
file = [f for f in files if f['name'] == choice][0]
download_file(file, opts, filename = tmp)
changed = edit_file(tmp)
if changed:
upload_named_file(tmp, opts, choice)
os.remove(tmp)
def menu_upload(opts): def menu_upload(opts):
while True: while True:
print_title("Upload") print_title("Upload")
@@ -268,41 +325,21 @@ def upload_file(file, opts):
return return
cmd1 = [ def upload_named_file(file, opts, filename):
'curl','-s', print("Upload " + filename)
'%s%s/%s/%s'%( p = Popen(
"curl -F 'file=@%s;filename=%s' %s%s/%s/%s"%(
file,
filename,
opts.rooturl, opts.rooturl,
"script/upload_split", "upload",
opts.share, opts.share,
opts.token, opts.token
), ),
]
cmd2 = [
'python', '-',
'-s', str(opts.split),
file
]
p1 = Popen(
cmd1,
stderr = PIPE,
stdout = PIPE, stdout = PIPE,
shell = True
) )
script, stderr = p1.communicate() read_output(p.stdout)
p2 = Popen(
cmd2,
stdin = PIPE,
stdout = PIPE,
stderr = PIPE,
bufsize = 1
)
p2.stdin.write(script)
p2.stdin.close()
for char in iter(lambda: p2.stderr.read(1), ''):
if not char:
break
sys.stderr.write(char)
sys.stderr.flush()
return return

View File

@@ -67,7 +67,7 @@ def file_stat(path, filename):
'mtime': file_date_human(s.st_mtime), 'mtime': file_date_human(s.st_mtime),
'name': filename, 'name': filename,
'url': path2url(filename), 'url': path2url(filename),
'editable': (s.st_size < 10240 and filename.endswith(".txt")) 'editable': (s.st_size < 65536 and filename.endswith(".txt"))
} }