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('/')
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)

View File

@ -43,8 +43,9 @@ function handleFiles(files) {
files.forEach(uploadFile)
files.forEach(previewFile)
}
function uploadFile(file) {
let url = 'YOUR URL HERE'
let url = '/upload'
let formData = new FormData()
formData.append('file', file)

View File

@ -4,9 +4,7 @@
<div class="row">
<div class="col-md">
<form action='/update' method='POST'>
<textarea rows = "30" cols = "120" name = "content" id="content">
{% if filecontent %}
{{ filecontent }}
<textarea rows = "30" cols = "120" name = "content" id="content">{% if filecontent %}{{ filecontent }}
{% endif %}
</textarea><br>
<input type='hidden' id='sha' name='sha' value="{{ sha }}">
@ -17,13 +15,13 @@
<div class='col-md'>
<div id="drop-area">
<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)">
<label class="button" for="fileElem">Select some files</label>
</form>
<progress id="progress-bar" max=100 value=0></progress>
<div id="gallery"></div>
</div>
<div id="gallery"></div>
</div>
</div>
</div>

View File

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

View File

@ -13,10 +13,30 @@
{% endblock %}
</head>
<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="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 %}
</div>
</body>