2022-06-08 19:51:55 +02:00
|
|
|
import tempfile
|
|
|
|
import os
|
2022-06-05 18:53:31 +02:00
|
|
|
from flask import (
|
|
|
|
Blueprint, flash, g, redirect, render_template, request, session, url_for
|
|
|
|
)
|
2022-06-08 19:51:55 +02:00
|
|
|
from werkzeug.utils import secure_filename
|
2022-06-05 18:53:31 +02:00
|
|
|
from sqlalchemy import create_engine
|
|
|
|
from sqlalchemy.orm import sessionmaker
|
|
|
|
from auth import login_required
|
|
|
|
|
|
|
|
from config import CONFIG
|
|
|
|
from create_db import User, Card
|
|
|
|
|
|
|
|
import nextcloud_client
|
|
|
|
|
2022-06-08 19:51:55 +02:00
|
|
|
|
2022-06-05 18:53:31 +02:00
|
|
|
bp = Blueprint('upload', __name__, url_prefix='/upload')
|
|
|
|
|
|
|
|
engine = create_engine(CONFIG['DB_CONNECTION'])
|
|
|
|
dbsessionmaker = sessionmaker(bind=engine)
|
|
|
|
dbsession = dbsessionmaker()
|
|
|
|
|
2022-06-05 21:18:24 +02:00
|
|
|
|
|
|
|
nc = nextcloud_client.Client('https://obzorje.kompot.si')
|
|
|
|
nc.login('gia', 'gia0000!')
|
|
|
|
|
|
|
|
|
2022-06-08 19:51:55 +02:00
|
|
|
@bp.route("/", methods=["GET", "POST"])
|
2022-06-05 18:53:31 +02:00
|
|
|
def index():
|
2022-06-07 18:58:57 +02:00
|
|
|
username = session["username"]
|
|
|
|
return render_template("menu/upload.html", username=username)
|
2022-06-05 18:53:31 +02:00
|
|
|
|
|
|
|
|
2022-06-08 19:51:55 +02:00
|
|
|
@bp.route('/uploader', methods = ('GET', 'POST'))
|
2022-06-05 18:53:31 +02:00
|
|
|
def upload_file():
|
2022-06-07 18:58:57 +02:00
|
|
|
user_id = session["user_id"]
|
2022-06-08 19:51:55 +02:00
|
|
|
username = session["username"]
|
2022-06-05 21:18:24 +02:00
|
|
|
message = ""
|
2022-06-05 18:53:31 +02:00
|
|
|
if request.method == 'POST':
|
|
|
|
#flask gets the file
|
2022-06-08 19:51:55 +02:00
|
|
|
upload = request.files["file"]
|
|
|
|
filename = secure_filename(upload.filename)
|
|
|
|
|
|
|
|
script_path = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
temp_path = script_path + "\\temp\\"
|
2022-06-05 21:18:24 +02:00
|
|
|
|
2022-06-08 19:51:55 +02:00
|
|
|
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)
|
|
|
|
|
2022-06-05 18:53:31 +02:00
|
|
|
#get public link
|
2022-06-08 19:51:55 +02:00
|
|
|
link = nc.share_file_with_link("/GIA CLOUD/" + filename)
|
|
|
|
print(link)
|
2022-06-05 18:53:31 +02:00
|
|
|
|
|
|
|
#update db
|
|
|
|
#predpostavlja title, ir, owner_id, item_location
|
2022-06-05 21:18:24 +02:00
|
|
|
#card = Card(title=name, interest_rate=-1.0, owner_id=1, item_location=link, )
|
2022-06-05 18:53:31 +02:00
|
|
|
#dbsession.add(card)
|
|
|
|
#dbsession.commit()
|
2022-06-08 19:51:55 +02:00
|
|
|
return render_template("menu/upload.html", message=message, user_id=user_id, username=username)
|