def upload_file(filename=None):
    if request.method == 'GET':
        return send_from_directory(current_app.config['UPLOAD_FOLDER'], filename)

    if request.method == 'POST':
        if 'file' not in request.files:
            return jsonify({"error":"not files present"}), 422
        
        file = request.files['file']
        #Condition for not repeat files by name
        find_file = os.path.join(current_app.config['UPLOAD_FOLDER'], file.filename)
        find_file_true = os.path.exists(find_file)

        if file.filename == '':
            return jsonify({"error":"Please select a file"}), 422
        if find_file_true:
            return jsonify({"error":"Filename already exist"}), 422
        if file and allowed_file(file.filename, ALLOWED_EXTENSIONS):
            filename = secure_filename(file.filename)
            file.save(os.path.join(current_app.config['UPLOAD_FOLDER'], filename))
            return jsonify({"success":"file uploaded"}), 200
        else:
            return jsonify({"error":"file not allowed"}), 400
        
    if request.method == 'DELETE':
        file = os.path.join(current_app.config['UPLOAD_FOLDER'], filename)
        ex = os.path.exists(file)
        if not ex:
            return jsonify({"error":"file not exist"}), 422   
        else:
            os.remove(file)
            return jsonify({"success":"file removed"}), 200
Exemplo n.º 2
0
def upload_file(filename=None):
    if request.method == 'GET':
        return send_from_directory(app.config['UPLOAD_FOLDER'], filename)

    if request.method == 'POST':
        if 'file' not in request.files:
            return jsonify({"error": "not files present"}), 422

        file = request.files['file']
        if file.filename == '':
            return jsonify({"msg": "Please select a file"}), 422
        if file and allowed_file(file.filename, ALLOWED_EXTENSIONS):
            filename = secure_filename(file.filename)
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
            return jsonify({"msg": "file uploaded"}), 200
        else:
            return jsonify({"msg": "file not allowed"}), 400
Exemplo n.º 3
0
def upload(id):
    if request.method=="PUT":
        if "photo" not in request.files:
            return {"msg":"file not found"}, 204
        restaurant = Restaurantuser.query.get(id)
        if not restaurant:
            return jsonify({'msg':'Restaurant not found'}), 404
        file=request.files["photo"]
        if file.filename=="":
            return {"msg": "no selected file"}, 204
        if file and allowed_file(file.filename,ALLOWED_EXTENSIONS_IMAGES):
            filename=secure_filename(file.filename)
            if os.path.join(os.path.join(current_app.config["UPLOAD_FOLDER"],"img\\logos"),restaurant.logo)is not None and restaurant.logo != "empty.png":
                if os.path.exists(os.path.join(os.path.join(current_app.config["UPLOAD_FOLDER"],"img\\logos"),restaurant.logo)):
                    os.remove(os.path.join(os.path.join(current_app.config["UPLOAD_FOLDER"],"img\\logos"),restaurant.logo))
            extension = filename.split(".")[-1]
            now=datetime.datetime.today()
            file.save(os.path.join(os.path.join(current_app.config["UPLOAD_FOLDER"],"img\\logos"),str(restaurant.id)+now.strftime("%H-%M-%S-%f")+"."+extension))
            restaurant.logo=str(restaurant.id)+now.strftime("%H-%M-%S-%f")+"."+extension
            db.session.commit()
            return {"msg":"ok"}, 200

    return {"msg":"i dont know how did you got this"}