Files are now stored as a hash from a uuid to preserve uniqueness

Server prints out the submitted data
This commit is contained in:
sqozz 2015-02-15 04:51:27 +01:00
parent 4ba2e1168f
commit 9ec244d9f3
4 changed files with 53 additions and 15 deletions

View file

@ -1,6 +1,6 @@
#!/usr/bin/python3 #!/usr/bin/python3
#/* vim:set ts=2 set noexpandtab */ #/* vim:set ts=2 set noexpandtab */
import json import json, uuid, hashlib
from flask import Flask, render_template, url_for, request from flask import Flask, render_template, url_for, request
from werkzeug import secure_filename from werkzeug import secure_filename
app = Flask(__name__) app = Flask(__name__)
@ -20,13 +20,11 @@ def create():
if request.method == "GET": if request.method == "GET":
return render_template("create.html", language="english", categories=settings["categories"], strings=strings, errors=None) return render_template("create.html", language="english", categories=settings["categories"], strings=strings, errors=None)
elif request.method == "POST": elif request.method == "POST":
uploadfile = request.files["torrentFile"] errors = createNewTorrent(request)
filename = secure_filename(uploadfile.filename) if errors == None:
# TODO: Create unique filename so that existing files doesn't get overwritten return "It's allright"
uploadfile.save("torrentFiles/" + filename) else:
# TODO: Process inputdate from the form and save it to the (until now) non-existing DB return render_template("create.html", language="english", categories=settings["categories"], strings=strings, errors=errors)
print(request.form["name"])
return "\o/"
@app.route("/search", methods=['GET']) @app.route("/search", methods=['GET'])
def search(): def search():
@ -51,6 +49,22 @@ def getLocalString(language, descriptor):
else: else:
return descriptor return descriptor
def createNewTorrent(reuqest):
uploadfile = request.files["torrentFile"]
filename = secure_filename(uploadfile.filename)
h = hashlib.sha256()
h.update((str(uuid.uuid4()) + filename).encode())
safeFilename = h.hexdigest()
uploadfile.save("torrentFiles/" + safeFilename)
print("=== CREATE NEW TORRENT FILE ===")
print( "Name: " + request.form["name"] )
print( "Torrent file: " + safeFilename )
print( "Category: " + request.form["category"] )
print( "Subcategory: " + request.form["subcategory"] )
print( "Description: " + request.form["description"] )
# TODO: Process inputdate from the form and save it to the (until now) non-existing DB
return ["Error1", "Error2"]
if __name__ == "__main__": if __name__ == "__main__":
init() init()

View file

@ -1,3 +1,19 @@
.category-column {
padding-right: 5px;
}
.subcategory-column {
padding-left: 5px;
}
.row.row-container {
margin-bottom: 0px;
}
.button-container {
padding-right: 20px;
padding-left: 20px;
}
/* Links */ /* Links */
a, a,

View file

@ -92,6 +92,8 @@ function setButtonToFilename(event) {
// Sets the text of a given dropdown-button to a given value // Sets the text of a given dropdown-button to a given value
function setDropdownButtonText(text, dropdownButton) { function setDropdownButtonText(text, dropdownButton) {
var dropdownTextSpan = dropdownButton.getElementsByClassName("text")[0] var dropdownTextSpan = dropdownButton.getElementsByClassName("text")[0]
dropdownHiddenValue = dropdownButton.getElementsByTagName("input")[0]
dropdownHiddenValue.setAttribute("value", getDescriptorByLocalString("english", text))
dropdownTextSpan.innerHTML = text dropdownTextSpan.innerHTML = text
} }

View file

@ -9,12 +9,14 @@ vim: ts=2 noexpandtab
<script src="{{ url_for("static", filename="js/create.js") }}"></script> <script src="{{ url_for("static", filename="js/create.js") }}"></script>
<div> <div>
<h2 class="headline">{{ getLocalString(language, "create_new_torrent") }}</h2> <h2 class="headline">{{ getLocalString(language, "create_new_torrent") }}</h2>
<!-- {% if errors %}
{% for error in errors %}
<div class="alert alert-danger alert-dismissible" role="alert"> <div class="alert alert-danger alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button> <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<strong>Warning!</strong> Better check yourself, you're not looking too good. <span class="message">{{ error }}</span>
</div> </div>
--> {% endfor %}
{% endif %}
<form action="/create" method="post" enctype="multipart/form-data"> <form action="/create" method="post" enctype="multipart/form-data">
<div class="row"> <div class="row">
@ -30,12 +32,13 @@ vim: ts=2 noexpandtab
<strong>{{ getLocalString(language, "category") }}</strong> <strong>{{ getLocalString(language, "category") }}</strong>
</div> </div>
<div class="col-md-8"> <div class="col-md-8">
<div class="row"> <div class="row row-container">
<div class="col-md-6"> <div class="col-md-6 category-column">
<div class="dropdown"> <div class="dropdown">
<button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-expanded="true"> <button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-expanded="true">
<span class="text">{{ getLocalString(language, "category") }}</span> <span class="text">{{ getLocalString(language, "category") }}</span>
<span class="caret"></span> <span class="caret"></span>
<input type="hidden" name="category">
</button> </button>
<ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu1"> <ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu1">
{% for category in sorted(categories.keys()) %} {% for category in sorted(categories.keys()) %}
@ -44,11 +47,12 @@ vim: ts=2 noexpandtab
</ul> </ul>
</div> </div>
</div> </div>
<div class="col-md-6"> <div class="col-md-6 subcategory-column">
<div class="dropdown"> <div class="dropdown">
<button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-expanded="true" disabled> <button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-expanded="true" disabled>
<span class="text">{{ getLocalString(language, "subcategory") }}</span> <span class="text">{{ getLocalString(language, "subcategory") }}</span>
<span class="caret"></span> <span class="caret"></span>
<input type="hidden" name="subcategory">
</button> </button>
<ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu1"> <ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu1">
</ul> </ul>
@ -97,7 +101,9 @@ vim: ts=2 noexpandtab
<textarea name="description" class="form-control" rows="10"></textarea> <textarea name="description" class="form-control" rows="10"></textarea>
</div> </div>
</div> </div>
<button type="submit" class="btn btn-default"><span class="glyphicon glyphicon-plus" aria-hidden="true"></span> {{ getLocalString(language, "create") }}!</button> <div class="button-container">
<button type="submit" class="btn btn-default"><span class="glyphicon glyphicon-plus" aria-hidden="true"></span> {{ getLocalString(language, "create") }}!</button>
</div>
</form> </form>
</div> </div>
{% endblock content%} {% endblock content%}