diff --git a/app.py b/app.py index 95b13e9..947634a 100644 --- a/app.py +++ b/app.py @@ -65,15 +65,21 @@ def logout(): @app.route('/') def home(): user = session.get('user') - return render_template('home.html', user=user) + files = {} + path = request.args.get('path') + if not path: + path = '/' + resp = oauth.gitea.get(urllib.parse.quote('{}/contents/{}'.format(app.config.get('REPO_URL'), path))) + files = resp.json() + return render_template('home.html', user=user, files=files) -@app.route('/test') +@app.route('/edit') def test(): path = request.args.get('path') if not path: return 'no path given' - resp = oauth.gitea.get(urllib.parse.quote('repos/seidl/rstcms/contents/{}'.format(path))) + resp = oauth.gitea.get(urllib.parse.quote('{}/contents/{}'.format(app.config.get('REPO_URL'), path))) jresp = resp.json() if 'errors' in jresp: error = {} @@ -81,7 +87,8 @@ def test(): return render_template('error.html', error=error) content = base64.b64decode(jresp['content']).decode('UTF-8') sha = jresp['sha'] - return render_template('test.html',filecontent=content,sha=sha,path=path) + user = session.get('user') + return render_template('edit.html',filecontent=content,sha=sha,path=path, user=user) @app.route('/update', methods=['POST']) def update(): @@ -97,11 +104,21 @@ def update(): sample['content'] = content.decode('UTF-8') sample['sha'] = sha - resp = oauth.gitea.request('PUT', 'repos/seidl/rstcms/contents/{}'.format(path), data=json.dumps(sample), headers={'Content-type': 'application/json'}) + resp = oauth.gitea.request('PUT', urllib.parse.quote('{}/contents/{}'.format(app.config.get('REPO_URL'), path)), data=json.dumps(sample), headers={'Content-type': 'application/json'}) print(resp) return str(resp) +@app.route('/upload', methods=['POST']) +def upload(): + file = request.files + image_string = base64.b64encode(file['file'].read()) + payload = {"content": "", "message": "WEB API add file"} + payload['content'] = image_string.decode('UTF-8') + resp = oauth.gitea.request('POST', urllib.parse.quote('{}/contents/{}'.format(app.config.get('REPO_URL'), 'static/test.png')), data=json.dumps(payload), headers={'Content-type': 'application/json'}) + print(resp.json()) + return 'OK' + if __name__ == "__main__": gitea = oauth.create_client('gitea') app.run(debug=True) diff --git a/static/upload.js b/static/upload.js index 79fdca4..bb73fc1 100644 --- a/static/upload.js +++ b/static/upload.js @@ -7,28 +7,28 @@ let progressBar = document.getElementById('progress-bar') dropArea.addEventListener(eventName, preventDefaults, false) }) - function preventDefaults (e) { +function preventDefaults (e) { e.preventDefault() e.stopPropagation() - } +} - ;['dragenter', 'dragover'].forEach(eventName => { +;['dragenter', 'dragover'].forEach(eventName => { dropArea.addEventListener(eventName, highlight, false) }) - ;['dragleave', 'drop'].forEach(eventName => { +;['dragleave', 'drop'].forEach(eventName => { dropArea.addEventListener(eventName, unhighlight, false) }) - function highlight(e) { +function highlight(e) { dropArea.classList.add('highlight') - } +} - function unhighlight(e) { +function unhighlight(e) { dropArea.classList.remove('highlight') - } +} - dropArea.addEventListener('drop', handleDrop, false) +dropArea.addEventListener('drop', handleDrop, false) function handleDrop(e) { let dt = e.dataTransfer @@ -42,9 +42,10 @@ function handleFiles(files) { initializeProgress(files.length) files.forEach(uploadFile) files.forEach(previewFile) - } - function uploadFile(file) { - let url = 'YOUR URL HERE' +} + +function uploadFile(file) { + let url = '/upload' let formData = new FormData() formData.append('file', file) @@ -55,9 +56,9 @@ function handleFiles(files) { }) .then(progressDone) // <- Add `progressDone` call here .catch(() => { /* Error. Inform the user */ }) - } +} - function previewFile(file) { +function previewFile(file) { let reader = new FileReader() reader.readAsDataURL(file) reader.onloadend = function() { @@ -71,15 +72,15 @@ function handleFiles(files) { caption.textContent = file.name document.getElementById(file.name).appendChild(caption) } - } +} - function initializeProgress(numfiles) { +function initializeProgress(numfiles) { progressBar.value = 0 filesDone = 0 filesToDo = numfiles - } +} - function progressDone() { +function progressDone() { filesDone++ progressBar.value = filesDone / filesToDo * 100 - } +} diff --git a/templates/test.html b/templates/edit.html similarity index 85% rename from templates/test.html rename to templates/edit.html index 471d466..8c33335 100644 --- a/templates/test.html +++ b/templates/edit.html @@ -4,9 +4,7 @@
-
@@ -17,13 +15,13 @@
-

Upload multiple files with the file dialog or by dragging and dropping images onto the dashed region

+

Upload multiple files with the file dialog or by dragging and dropping images onto the dashed region. Files are automatically uploaded to git!

-
+
diff --git a/templates/home.html b/templates/home.html index f6cf89e..3add2e8 100644 --- a/templates/home.html +++ b/templates/home.html @@ -1,13 +1,16 @@ {% extends "layout.html" %} {% block content %} {% if user %} -
- Logged in as - - {{ user.username }} -
-test -logout + + {% else %} login {% endif %} diff --git a/templates/layout.html b/templates/layout.html index fd6d6bd..1cdd693 100644 --- a/templates/layout.html +++ b/templates/layout.html @@ -13,10 +13,30 @@ {% endblock %} +
{% block content %}{% endblock %}