simple repo browser added

This commit is contained in:
Matthias Seidl 2020-11-20 15:21:37 +01:00
parent f945c858da
commit 31a98e2817
5 changed files with 76 additions and 37 deletions

27
app.py
View File

@ -65,15 +65,21 @@ def logout():
@app.route('/') @app.route('/')
def home(): def home():
user = session.get('user') 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(): def test():
path = request.args.get('path') path = request.args.get('path')
if not path: if not path:
return 'no path given' 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() jresp = resp.json()
if 'errors' in jresp: if 'errors' in jresp:
error = {} error = {}
@ -81,7 +87,8 @@ def test():
return render_template('error.html', error=error) return render_template('error.html', error=error)
content = base64.b64decode(jresp['content']).decode('UTF-8') content = base64.b64decode(jresp['content']).decode('UTF-8')
sha = jresp['sha'] 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']) @app.route('/update', methods=['POST'])
def update(): def update():
@ -97,11 +104,21 @@ def update():
sample['content'] = content.decode('UTF-8') sample['content'] = content.decode('UTF-8')
sample['sha'] = sha 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) print(resp)
return str(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__": if __name__ == "__main__":
gitea = oauth.create_client('gitea') gitea = oauth.create_client('gitea')
app.run(debug=True) app.run(debug=True)

View File

@ -7,28 +7,28 @@ let progressBar = document.getElementById('progress-bar')
dropArea.addEventListener(eventName, preventDefaults, false) dropArea.addEventListener(eventName, preventDefaults, false)
}) })
function preventDefaults (e) { function preventDefaults (e) {
e.preventDefault() e.preventDefault()
e.stopPropagation() e.stopPropagation()
} }
;['dragenter', 'dragover'].forEach(eventName => { ;['dragenter', 'dragover'].forEach(eventName => {
dropArea.addEventListener(eventName, highlight, false) dropArea.addEventListener(eventName, highlight, false)
}) })
;['dragleave', 'drop'].forEach(eventName => { ;['dragleave', 'drop'].forEach(eventName => {
dropArea.addEventListener(eventName, unhighlight, false) dropArea.addEventListener(eventName, unhighlight, false)
}) })
function highlight(e) { function highlight(e) {
dropArea.classList.add('highlight') dropArea.classList.add('highlight')
} }
function unhighlight(e) { function unhighlight(e) {
dropArea.classList.remove('highlight') dropArea.classList.remove('highlight')
} }
dropArea.addEventListener('drop', handleDrop, false) dropArea.addEventListener('drop', handleDrop, false)
function handleDrop(e) { function handleDrop(e) {
let dt = e.dataTransfer let dt = e.dataTransfer
@ -42,9 +42,10 @@ function handleFiles(files) {
initializeProgress(files.length) initializeProgress(files.length)
files.forEach(uploadFile) files.forEach(uploadFile)
files.forEach(previewFile) files.forEach(previewFile)
} }
function uploadFile(file) {
let url = 'YOUR URL HERE' function uploadFile(file) {
let url = '/upload'
let formData = new FormData() let formData = new FormData()
formData.append('file', file) formData.append('file', file)
@ -55,9 +56,9 @@ function handleFiles(files) {
}) })
.then(progressDone) // <- Add `progressDone` call here .then(progressDone) // <- Add `progressDone` call here
.catch(() => { /* Error. Inform the user */ }) .catch(() => { /* Error. Inform the user */ })
} }
function previewFile(file) { function previewFile(file) {
let reader = new FileReader() let reader = new FileReader()
reader.readAsDataURL(file) reader.readAsDataURL(file)
reader.onloadend = function() { reader.onloadend = function() {
@ -71,15 +72,15 @@ function handleFiles(files) {
caption.textContent = file.name caption.textContent = file.name
document.getElementById(file.name).appendChild(caption) document.getElementById(file.name).appendChild(caption)
} }
} }
function initializeProgress(numfiles) { function initializeProgress(numfiles) {
progressBar.value = 0 progressBar.value = 0
filesDone = 0 filesDone = 0
filesToDo = numfiles filesToDo = numfiles
} }
function progressDone() { function progressDone() {
filesDone++ filesDone++
progressBar.value = filesDone / filesToDo * 100 progressBar.value = filesDone / filesToDo * 100
} }

View File

@ -4,9 +4,7 @@
<div class="row"> <div class="row">
<div class="col-md"> <div class="col-md">
<form action='/update' method='POST'> <form action='/update' method='POST'>
<textarea rows = "30" cols = "120" name = "content" id="content"> <textarea rows = "30" cols = "120" name = "content" id="content">{% if filecontent %}{{ filecontent }}
{% if filecontent %}
{{ filecontent }}
{% endif %} {% endif %}
</textarea><br> </textarea><br>
<input type='hidden' id='sha' name='sha' value="{{ sha }}"> <input type='hidden' id='sha' name='sha' value="{{ sha }}">
@ -17,13 +15,13 @@
<div class='col-md'> <div class='col-md'>
<div id="drop-area"> <div id="drop-area">
<form class="my-form"> <form class="my-form">
<p>Upload multiple files with the file dialog or by dragging and dropping images onto the dashed region</p> <p>Upload multiple files with the file dialog or by dragging and dropping images onto the dashed region. Files are automatically uploaded to git!</p>
<input type="file" id="fileElem" multiple accept="image/*" onchange="handleFiles(this.files)"> <input type="file" id="fileElem" multiple accept="image/*" onchange="handleFiles(this.files)">
<label class="button" for="fileElem">Select some files</label> <label class="button" for="fileElem">Select some files</label>
</form> </form>
<progress id="progress-bar" max=100 value=0></progress> <progress id="progress-bar" max=100 value=0></progress>
<div id="gallery"></div>
</div> </div>
<div id="gallery"></div>
</div> </div>
</div> </div>
</div> </div>

View File

@ -1,13 +1,16 @@
{% extends "layout.html" %} {% extends "layout.html" %}
{% block content %} {% block content %}
{% if user %} {% if user %}
<div> <ul>
Logged in as {% for file in files %}
<span><img src='{{ user.avatar_url }}'></span> {% if file.type == 'file' %}
<span>{{ user.username }}</span> <li><a href="/edit?path={{ file.path }}">{{ file.name }}</a></li>
</div> {% else %}
<a href="/test">test</a> <li><a href="/?path={{ file.path }}">{{ file.name }}/</a></li>
<a href="/logout">logout</a> {% endif %}
{% endfor %}
</ul>
{% else %} {% else %}
<a href="/login">login</a> <a href="/login">login</a>
{% endif %} {% endif %}

View File

@ -13,10 +13,30 @@
{% endblock %} {% endblock %}
</head> </head>
<body> <body>
<div id="header">
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="#">rst CMS</a>
<div class="navbar-collapse collapse justify-content-between">
<ul class="navbar-nav mr-auto">
<span class="navbar-text">
a no bs CMS
</span>
</ul>
<ul class="navbar-nav">
{% if user %}
<span class="navbar-text">
Logged in as: {{ user.username }} <span><img width='50px' src='{{ user.avatar_url }}'></span> <a class="nav-link" href="/logout">Logout</a>
</span>
{% endif %}
</ul>
</div>
</nav>
</div>
<div id="content">{% block content %}{% endblock %}</div> <div id="content">{% block content %}{% endblock %}</div>
<div id="footer"> <div id="footer">
{% block footer %} {% block footer %}
&copy; Copyright 2020 by <a href="https://seidlm.at/">Matthias Seidl</a>. powered by <a href=''>gitea</a> &amp; <a href=''>flask</a> / themed with <a href="">bootstrap</a> &copy; Copyright 2020 by <a href="https://seidlm.at/">Matthias Seidl</a>.
{% endblock %} {% endblock %}
</div> </div>
</body> </body>