import os from flask import render_template, Blueprint, request, redirect, url_for from flask_cors import CORS, cross_origin from flask.helpers import make_response, send_file, send_from_directory from werkzeug.wrappers import Response import requests from PIL import Image from urllib.parse import urlparse import uuid #from backstage.utils.routes import update_manage_table from backstage.config import PORTAL_SERVER, BHOUSE_SERVER upload_app = Blueprint('upload', __name__) @upload_app.route('/backstage/upload', methods=['POST','GET']) def upload_post(): #bdata = request.stream.read() #aa = request.get_data() if request.method == 'POST': print(request.method) # check if the post request has the file part #print(request.files) """ if 'file' not in request.files: #flash('No file part') return redirect(request.url) """ file = request.files['image'] # If the user does not select a file, the browser submits an # empty file without a filename. """ if file.filename == '': #flash('No selected file') return redirect(request.url) """ if file: #filename = secure_filename(file.filename) #print(os.getcwd().) file.save(os.getcwd()+ "/backstage/upload/img/"+ file.filename) #return redirect(url_for('download_file', name=file.filename)) aa = {"success" : 1,"file": { "url" : "/backstage/upload/"+ file.filename, } } return aa if request.method == 'GET': print(request.method) #print(request.files) #print(request.form) #print(requests.post("/backstage/upload")) aa = {"success" : 1,"file": { "url" : "https://www.tesla.com/tesla_theme/assets/img/_vehicle_redesign/roadster_and_semi/roadster/hero.jpg", } } return aa @upload_app.route('/backstage/upload/', methods=['GET']) def upload_get(filename): #print(filename) #aa = {"success" : 1,"file": { "url" : "https://www.tesla.com/tesla_theme/assets/img/_vehicle_redesign/roadster_and_semi/roadster/hero.jpg", } } #return redirect(url_for('upload.upload_get',filename=filename), code=301) return send_from_directory(os.getcwd()+ "/backstage/upload/img" , filename) @upload_app.route('/backstage/getimage', methods=['POST','GET']) def get_image(): print(request.get_json()['url']) fname = str(uuid.uuid4()) + str(request.get_json()['url'])[str(request.get_json()['url']).rfind("."):] f = open(os.getcwd()+ "/backstage/upload/img/" + fname,'wb') f.write(requests.get(request.get_json()['url']).content) f.close() #image_object = Image.open(os.getcwd()+ "/backstage/upload/tmp") #send_file() #aa = {"success" : 1,"file": { "url" : "http://localhost:9000/backstage/upload/avatar1.jpg", } } #resp = make_response(open(os.getcwd()+ "/backstage/upload/" + fname, 'br').read(), 301) #resp.content_type = "image/jpeg" #resp.content_encoding = "Unicode" #return redirect(request.get_json()['url'], code=301) aa = {"success" : 1,"file": { "url" : "/backstage/upload/"+ fname , } } return aa