display script API on the page

This commit is contained in:
ville rantanen
2018-06-28 22:52:47 +03:00
parent d52ba606ab
commit 84b28ac748
5 changed files with 94 additions and 9 deletions

View File

@@ -19,6 +19,7 @@ app = Flask(__name__)
app.config.from_object(__name__) app.config.from_object(__name__)
# Read config from json ! # Read config from json !
config_values = json.load(open(os.getenv('FLEES_CONFIG'),'rt')) config_values = json.load(open(os.getenv('FLEES_CONFIG'),'rt'))
app.config['PUBLIC_URL'] = config_values['public_url']
app.config['SITE_NAME'] = config_values['site_name'] app.config['SITE_NAME'] = config_values['site_name']
app.config['UPLOAD_FOLDER'] = config_values['data_folder'] app.config['UPLOAD_FOLDER'] = config_values['data_folder']
app.config['SHARES_FILE'] = config_values['shares_file'] app.config['SHARES_FILE'] = config_values['shares_file']
@@ -81,6 +82,8 @@ def authenticate(name):
if request.method == 'POST': if request.method == 'POST':
user_password = request.form['password'].encode('utf-8') user_password = request.form['password'].encode('utf-8')
session[name] = password_hash(user_password, app.secret_key) session[name] = password_hash(user_password, app.secret_key)
if name + 'Token' in session:
del session[name + 'Token']
return redirect(url_for('list_view',name=name)) return redirect(url_for('list_view',name=name))
@@ -262,6 +265,7 @@ def list_view(name, token = None):
return share return share
if token != None and 'pass_hash' in share: if token != None and 'pass_hash' in share:
session[name] = share['pass_hash'] session[name] = share['pass_hash']
session[name + 'Token'] = token
return redirect(url_for('list_view',name=name)) return redirect(url_for('list_view',name=name))
files = [] files = []
@@ -275,6 +279,11 @@ def list_view(name, token = None):
allow_direct = get_or_none('direct_links', share) if get_or_none('pass_hash', share) else False allow_direct = get_or_none('direct_links', share) if get_or_none('pass_hash', share) else False
upload = get_or_none('upload', share) upload = get_or_none('upload', share)
overwrite = get_or_none('overwrite', share) overwrite = get_or_none('overwrite', share)
if name + 'Token' in session:
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')]
if not upload: if not upload:
overwrite = False overwrite = False
return render_template( return render_template(
@@ -287,7 +296,8 @@ def list_view(name, token = None):
overwrite = overwrite, overwrite = overwrite,
direct = allow_direct, direct = allow_direct,
expire = get_or_none('expire', share), expire = get_or_none('expire', share),
description = get_or_none('description', share, "") description = get_or_none('description', share, ""),
script_api = script_api
) )
@@ -295,6 +305,8 @@ def list_view(name, token = None):
def logout(name): def logout(name):
if name in session: if name in session:
del session[name] del session[name]
if name + 'Token' in session:
del session[name + 'Token']
return render_template( return render_template(
"logout.html", "logout.html",
name = name name = name
@@ -600,9 +612,11 @@ def get_share(name, require_auth = True, token = None):
return (False, 'Share has expired') return (False, 'Share has expired')
authenticated = "no-pass" authenticated = "no-pass"
if not token == None: if not token == None:
if has_token(token, share):
require_auth = False require_auth = False
if has_token(token, share):
authenticated = "token" authenticated = "token"
else:
authenticated = False
if require_auth: if require_auth:
if 'pass_hash' in share: if 'pass_hash' in share:
authenticated = False authenticated = False

View File

@@ -8,6 +8,7 @@ body {
background-color: #FDF2E9; background-color: #FDF2E9;
min-height: 600px; min-height: 600px;
font: 14px/1.3 'Segoe UI',Arial, sans-serif; font: 14px/1.3 'Segoe UI',Arial, sans-serif;
margin: 8px;
} }
a:visited { a:visited {
@@ -79,7 +80,7 @@ tr:nth-child(odd) {
margin-left: 2em; margin-left: 2em;
margin-bottom: 1em; margin-bottom: 1em;
} }
#list_menu { #list_menu, #list_script {
float:right; float:right;
padding: 8px; padding: 8px;
border: 4px solid #148F77; border: 4px solid #148F77;
@@ -91,7 +92,7 @@ tr:nth-child(odd) {
border-bottom-left-radius: 15px; border-bottom-left-radius: 15px;
} }
#list_menu ul { #list_menu ul, #list_script ul {
margin-top: 0px; margin-top: 0px;
margin-bottom: 0px; margin-bottom: 0px;
} }
@@ -117,6 +118,7 @@ tr:nth-child(odd) {
margin-top: 24px; margin-top: 24px;
cursor: pointer; cursor: pointer;
text-align: right; text-align: right;
text-decoration: underline;
} }
#list_info { #list_info {
display: none; display: none;
@@ -129,10 +131,38 @@ tr:nth-child(odd) {
margin-bottom: 33px; margin-bottom: 33px;
} }
#list_script {
display: none;
z-index: 99;
position: absolute;
padding-top: 2em;
padding-bottom: 2em;
right: 8px;
}
#list_script_toggle {
cursor: pointer;
text-decoration: underline;
}
.direct { .direct {
margin-right: 1em; margin-right: 1em;
} }
.upper_corner {
position: absolute;
top: 0.5em;
right: 0.5em;
}
.code {
background-color: #FDF2E9;
font-family: monospace;
margin-right: 2em;
margin-bottom: 1em;
word-wrap: break-word;
}
/* index */ /* index */
#index_title { #index_title {

View File

@@ -27,6 +27,12 @@ function infoToggle() {
} }
function scriptToggle() {
var el = document.getElementById("list_script");
el.style.display = el.style.display === 'block' ? 'none' : 'block';
}
function UploadFile(file,file_no,files_total) { function UploadFile(file,file_no,files_total) {
if (uploadTurn != file_no) { if (uploadTurn != file_no) {
// Wait for our turn to upload. check every 0.5s // Wait for our turn to upload. check every 0.5s

View File

@@ -50,6 +50,7 @@
{% endif %} {% endif %}
{% 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('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><span id=list_script_toggle onclick="scriptToggle()">Command line info</span>
<li><a href="{{ url_for('logout',name=name) }}">Logout</a> <li><a href="{{ url_for('logout',name=name) }}">Logout</a>
</div> </div>
</div> </div>
@@ -79,4 +80,14 @@
</tbody> </tbody>
</table> </table>
</div> </div>
<div id=list_script>
<span id=list_script_toggle class=upper_corner onclick="scriptToggle()">Close X</span>
<div>Command line tools for using the share:</div>
<ul>
{% for entry in script_api %}
<li> {{ entry.doc }} </li>
<div class="code"><span> {{ entry.cmd }} </span></div>
{% endfor %}
</ul>
</div>
{% endblock %} {% endblock %}

View File

@@ -69,6 +69,29 @@ def get_or_none(key,d,none = None):
return none return none
def get_script_url(public_url, share, end_point, token = "[TOKEN]"):
cmd = None
doc = None
if get_or_none("direct_links", share) and end_point == "download":
end_point = "direct"
url = "%s/script/%s/%s/%s"%(
public_url,
end_point,
share['name'],
token
)
if end_point in ( "download", "direct"):
cmd = 'curl -s %s | bash /dev/stdin [-f]'%( url, )
doc = 'Download all files in the share.'
if end_point == "client":
cmd = 'python <( curl -s %s )'%( url, )
doc = 'Console client to download and upload files.'
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.'
return {'cmd': cmd, 'doc': doc}
def is_path_safe(path): def is_path_safe(path):
if path.startswith("."): if path.startswith("."):
return False return False
@@ -112,6 +135,7 @@ def iter_folder_files(path, recursive = True):
def path2url(path): def path2url(path):
return pathname2url(path) return pathname2url(path)
def safe_name(s): def safe_name(s):
return safe_string(s, "-_") return safe_string(s, "-_")