too much stuff. uploader script from template. dropdown menu for tools

This commit is contained in:
Ville Rantanen
2018-03-15 12:14:32 +02:00
parent 6724ac2557
commit 86c855285c
6 changed files with 340 additions and 75 deletions

View File

@@ -2,45 +2,56 @@
{% block body %}
<div id=list_menu>
{% if upload %}
<div id=list_upload>
<div id=list_upload>
<form id="upload_form" action={{ url_for('upload') }} method=post enctype=multipart/form-data>
<input id="list_upload_name" type=hidden name=name value="{{ name|safe }}" />
<input type=hidden name=from_gui value="true" />
<input id="list_upload_select" type=file name=file
onchange="FileSelectHandler(event)" multiple="multiple"><br>
<input id="list_upload_button" type=submit value=Upload>
<div id="progress"></div>
<div id="progress" class="clear"></div>
</form>
</div>
</div>
{% else %}
<div id=list_upload>
<input type=file name=file disabled>
<input type=submit value=Upload disabled>
</div>
</div>
{% endif %}
<div id=list_info>
Share:
<ul>
<div id=list_info_toggle onclick="infoToggle()">Share tools</div>
<div id=list_info>
<div id=list_url_upload>
<form id="url_upload_form" action={{ url_for('upload_url') }} method=post>
<input id="list_upload_name" type=hidden name=name value="{{ name|safe }}" />
<input type=hidden name=from_gui value="true" />
<input id="list_url_upload_text" type=text name=url
value="https://..." onclick="clear_text(this.id,'https://...')"><br>
<input id="list_url_upload_button" type=submit value="Upload URL">
<div class="clear"></div>
</form>
</div>
<ul>
{% if public %}
<li>is <a href="{{ url_for('index') }}" title="Share is publicly visible in the index">public</a>
<li>is <a href="{{ url_for('index') }}" title="Share is publicly visible in the index">public</a>
{% else %}
<li>is <a href="{{ url_for('index') }}" title="Share not listed in the index unless you have logged in">unlisted</a>
<li>is <a href="{{ url_for('index') }}" title="Share not listed in the index unless you have logged in">unlisted</a>
{% endif %}
{% if expire %}
<li><span title="All share operations disabled after the date">expires {{ expire }}</span>
<li><span title="All share operations disabled after the date">expires {{ expire }}</span>
{% else %}
<li>never expires
<li>never expires
{% endif %}
{% if upload %}
{% if overwrite %}
<li><span title="Uploaded files with existing names are overwritten">uploads overwrite</span>
<li><span title="Uploaded files with existing names are overwritten">uploads overwrite</span>
{% else %}
<li><span title="Uploaded files with existing names are versioned">uploads versioned</span>
<li><span title="Uploaded files with existing names are versioned">uploads versioned</span>
{% endif %}
{% endif %}
<li><a href="{{ url_for('download_zip',name=name) }}" title="Download all the files as one ZIP file. Total size of files must be less than {{ g.max_zip_size }} Mb">Download as zip</a>
<li><a href="{{ url_for('logout',name=name) }}">Logout</a>
</div>
<li><a href="{{ url_for('download_zip',name=name) }}" title="Download all the files as one ZIP file. Total size of files must be less than {{ g.max_zip_size }} Mb">Download as zip</a>
<li><a href="{{ url_for('logout',name=name) }}">Logout</a>
</div>
</div>
<div id=list_left>
<div id=list_title><h1>{{ name }}</h1></div>

View File

@@ -0,0 +1,109 @@
#!/usr/bin/env python
import argparse,sys,os, subprocess
from subprocess import Popen, PIPE, STDOUT
from io import BytesIO
ROOTURL="{{ rooturl }}"
SHARE="{{ name }}"
TOKEN="{{ token }}"
def split_upload(path, opts):
if os.path.isdir(path):
tar = Popen(
[
'tar','c',path
],
stdout = PIPE
)
reader = tar.stdout
basename = os.path.basename(path.rstrip("/")) + ".tar"
elif os.path.isfile(path):
reader = open(path, 'rb')
basename = os.path.basename(path)
else:
print("Path %s doesnt exist"%( path, ))
return
try:
chunk = reader.read(opts.split * 1024 * 1024)
part = 0
while chunk != "":
chunk_name = ".%s.part.%03d"%(
basename,
part
)
print("%s part %d"%( basename, part ))
if not is_chunk_sent(chunk_name, opts):
p = Popen(
[
'curl','-f',
'-F','file=@-;filename=%s'%(chunk_name,),
'%supload/%s/%s'%(opts.rooturl, opts.share, opts.token)
],
stdout=PIPE,
stdin=PIPE,
stderr=PIPE
)
stdout_data, stderr_data = p.communicate(input=chunk)
print(stdout_data)
chunk = reader.read(opts.split * 1024 * 1024)
part += 1
finally:
reader.close()
join_chunks(basename,opts)
return
def is_chunk_sent(name, opts):
p = Popen(
[
'curl','-s',
'%sfile/size/%s/%s/%s'%(opts.rooturl, opts.share, opts.token, name)
],
stdout=PIPE,
stderr=PIPE
)
stdout_data, stderr_data = p.communicate()
return stdout_data == str(opts.split * 1024 * 1024)
def join_chunks(name,opts):
p = Popen(
[
'curl',
'-F','filename=%s'%(name,),
'%supload_join/%s/%s'%(opts.rooturl, opts.share, opts.token)
],
stdout=PIPE,
stderr=PIPE
)
stdout_data, stderr_data = p.communicate()
print(stdout_data)
def parse_options():
parser = argparse.ArgumentParser(description='Flees uploader')
parser.add_argument('-s', action="store", type=int, dest="split", default = 64,
help = "Split size in megabytes [%(default)s]"
)
parser.add_argument('--rooturl', action="store", dest="rooturl",
default = ROOTURL,
help = "Address of Flees server [%(default)s]"
)
parser.add_argument('--share', action="store", dest="share",
default = SHARE,
help = "Name of Flees share [%(default)s]"
)
parser.add_argument('--token', action="store", dest="token",
default = TOKEN,
help = "API token for the share [%(default)s]"
)
parser.add_argument('path', nargs='+')
return parser.parse_args()
if __name__ == "__main__":
opts = parse_options()
for path in opts.path:
split_upload(path,opts)