edit txt files. keyboard shortcut for share tools

This commit is contained in:
ville rantanen
2018-07-20 21:29:11 +03:00
parent b53418b53f
commit 7d123db05f
7 changed files with 158 additions and 5 deletions

View File

@@ -86,6 +86,12 @@ Operation is one of download, direct_download, zip_download, or upload
Upload file from the browser
- @app.route('/upload_join/<name>/<token>', methods=['POST'])
Commence joining of a splitted upload file
- @app.route('/paste/<name>/<token>', methods=['POST'])
Upload a string from command line
- @app.route('/paste', methods=['POST'])
Upload a string from the browser
- @app.route('/editor', methods=['POST','GET'])
Open text editor.
- @app.route('/files/<name>/<token>', methods=['GET'])
Return plain text list of files in the share
- @app.route('/list/<name>/<token>', methods=['GET'])

View File

@@ -14,7 +14,7 @@ from utils.utils import *
from utils.crypt import *
__FLEES_VERSION__ = "20180702.0"
__FLEES_VERSION__ = "20180720.0"
app = Flask(__name__)
app.config.from_object(__name__)
# Read config from json !
@@ -227,6 +227,85 @@ def upload_url():
return "File uploaded\n", 200
@app.route('/editor', methods = ['GET','POST'])
@app.route('/editor/<name>', methods = ['GET','POST'])
def editor(name = None):
filename = 'paste.txt'
if request.method == 'POST':
name = request.form['editor_name']
filename = request.form['editor_filename']
(ok,share) = get_share(name)
if not ok:
notify({
"share": name,
"operation": "unauthorized_editor"
})
return share
pathname = os.path.join(
share['path'],
filename
)
content = ""
if os.path.isfile(pathname):
if pathname.endswith(".txt"):
content = open(pathname, 'rt').read(10 * 1024)
return render_template(
'editor.html',
name = name,
filename = filename,
content = content
)
@app.route('/paste/<name>/<token>', methods=['POST'])
@app.route('/paste/<name>', methods=['POST'])
def paste(name = None, token = None):
if request.method == 'POST':
file = request.form['filename']
paste = request.form['paste']
if name == None:
name = request.form['name']
(ok,share) = get_share(name, token = token)
if not ok:
notify({
"share": name,
"operation": "unauthorized_paste"
})
return share
if not get_or_none('upload', share) == True:
return "Upload not allowed\n",400
if file:
filename = os.path.join(
share['path'],
secure_filename(
file
)
)
if get_or_none('overwrite', share) == False:
if os.path.exists(filename):
file_versionize(filename)
#~ return "Overwrite forbidden", 403
print_debug("Saving " + filename)
with open(filename, 'wt') as fp:
fp.write(paste)
fp.close()
set_rights(filename)
notify({
"recipient": get_or_none('recipient', share),
"share": name,
"filename": filename,
"operation": "paste"
})
if 'from_gui' in request.form:
if request.form['from_gui'] == "true":
return redirect(url_for('list_view',name=name))
return "File uploaded\n", 200
else:
return "Use the 'file' variable to paste\n",400
@app.route('/file/list/<name>/<token>', methods=['GET'])
def file_list(name, token):

View File

@@ -219,6 +219,21 @@ tr:nth-child(odd) {
word-wrap: break-word;
}
.edit_form {
display: inline-block;
}
.edit_form_submit {
display: inline-block;
margin-left: 1em;
background-color: inherit;
border: none;
color: var(--text-color);
text-align: center;
text-decoration: underline;
cursor: pointer;
}
/* index */
#index_title {

View File

@@ -15,6 +15,7 @@ function index_form_enter(event) {
};
}
function index_form_submit() {
window.location = window.location + "/list/" +
document.getElementById("index_form_name").value;
@@ -29,8 +30,10 @@ function infoToggle() {
function scriptToggle() {
var el = document.getElementById("list_script");
if (el) {
el.style.display = el.style.display === 'block' ? 'none' : 'block';
}
}
function UploadFile(file,file_no,files_total) {
@@ -104,6 +107,7 @@ function FileSelectHandler(e) {
// Variable to stop parallel uploads
var uploadTurn = -1;
function UploadURL() {
var URL = document.getElementById("list_url_upload_text").value;
var xhr = new XMLHttpRequest();
@@ -150,3 +154,20 @@ function UploadURL() {
function changeTitle(newTitle) {
document.title = "Flees - " + newTitle;
}
function back() {
window.history.back();
}
function keyboardEntry(ev){
var kC=ev.keyCode;
var k=String.fromCharCode(ev.keyCode);
if ( document.activeElement === document.getElementById("list_url_upload_text")) {
return
}
if (/T/.test(k)) {
infoToggle();
}
}
document.onkeyup = keyboardEntry;

View File

@@ -0,0 +1,21 @@
{% extends "layout.html" %}
{% block body %}
<div id=index_menu>
Add text file to {{ name|safe }}
<div id=editor_form>
<form action={{ url_for('paste',name=name) }} method=post enctype=multipart/form-data>
<input type=hidden name=from_gui value="true" />
<input id="paste_filename" type=text name=filename
title="File name to write"
value="{{ filename | safe }}" onclick="clear_text(this.id,'paste.txt')">
<br>
<input type=submit value=Submit>
<input type=submit value=Cancel onclick="back()">
<br>
<textarea rows="25" cols="80" name="paste" id="paste_paste" autofocus>{{ content | safe }}</textarea>
</form>
</div>
</div>
<script type="text/javascript">changeTitle("{{ name | safe }}");</script>
{% endblock %}

View File

@@ -18,7 +18,7 @@
<input type=submit value=Upload disabled>
</div>
{% endif %}
<div id=list_info_toggle_container><span id=list_info_toggle onclick="infoToggle()">Share tools</span></div>
<div id=list_info_toggle_container><span id=list_info_toggle onclick="infoToggle()" title="Open menu, keyboard: t">Share tools</span></div>
<div id=list_info>
<div id=list_url_upload>
<form id="url_upload_form" action={{ url_for('upload_url') }} method=post>
@@ -54,6 +54,7 @@
{% 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><span id=list_script_toggle onclick="scriptToggle()" title="Show command line usage information">Command line info</span>
<li><a href="{{ url_for('editor', name = name) }}">Paste or write a file</a>
<li><a href="{{ url_for('index') }}">Return to index</a>
<li><a href="{{ url_for('logout',name=name) }}">Logout</a>
</div>
@@ -77,6 +78,15 @@
<a href="{{ url_for('download_direct', name = name, token = entry.token, filename = entry.name ) }}" title="Direct share link" class=direct>&#x2756;</a>
{% endif %}
<a href="{{ url_for('download_gui', name = name, filename = entry.url) }}">{{ entry.name }}</a>
{% if upload %}
{% if entry.editable %}
<form class="edit_form" action="{{ url_for('editor') }}" method=post>
<input class="editor_name" type=hidden name=editor_name value="{{ name | safe }}" />
<input class="editor_filename" type=hidden name=editor_filename value="{{ entry.name | safe }}" />
<input class="edit_form_submit" type=submit value="&#9998;" title="Edit text file">
</form>
{% endif %}
{% endif %}
<td class=td_right>{{ entry.size|safe }}
<td>{{ entry.mtime|safe }}
</tr>

View File

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