diff --git a/upload.py b/upload.py index 5ed4e0e..d1a9b6c 100644 --- a/upload.py +++ b/upload.py @@ -1,7 +1,9 @@ -from email import message +import tempfile +import os from flask import ( Blueprint, flash, g, redirect, render_template, request, session, url_for ) +from werkzeug.utils import secure_filename from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker from auth import login_required @@ -11,6 +13,7 @@ from create_db import User, Card import nextcloud_client + bp = Blueprint('upload', __name__, url_prefix='/upload') engine = create_engine(CONFIG['DB_CONNECTION']) @@ -22,28 +25,39 @@ nc = nextcloud_client.Client('https://obzorje.kompot.si') nc.login('gia', 'gia0000!') -@bp.route("/", methods=("GET", "POST")) +@bp.route("/", methods=["GET", "POST"]) def index(): username = session["username"] return render_template("menu/upload.html", username=username) -@bp.route('/uploader', methods = ['GET', 'POST']) +@bp.route('/uploader', methods = ('GET', 'POST')) def upload_file(): user_id = session["user_id"] + username = session["username"] message = "" if request.method == 'POST': #flask gets the file - upload = request.files #??? + upload = request.files["file"] + filename = secure_filename(upload.filename) + + script_path = os.path.dirname(os.path.abspath(__file__)) + temp_path = script_path + "\\temp\\" - #upload to nc - #nc.put_file('testdir/remotefile.txt', 'localfile.txt') #not sure kako bi to + with tempfile.NamedTemporaryFile(dir=temp_path, delete=False) as fp: + fp.write(upload.stream.read()) + path = fp.name + fp.close() + nc.put_file("/GIA CLOUD/" + filename, "temp\\" + path.split("\\")[-1]) + os.remove(fp.name) + #get public link - #link = nc.share_file_with_link('testdir/remotefile.txt') + link = nc.share_file_with_link("/GIA CLOUD/" + filename) + print(link) #update db #predpostavlja title, ir, owner_id, item_location #card = Card(title=name, interest_rate=-1.0, owner_id=1, item_location=link, ) #dbsession.add(card) #dbsession.commit() - return render_template("menu/upload.html", message=message, user_id=user_id) \ No newline at end of file + return render_template("menu/upload.html", message=message, user_id=user_id, username=username) \ No newline at end of file