def post(): user_id = get_jwt_identity() user = User.get_or_none(User.id == user_id) if "image" not in request.files: return jsonify({ "message": "No image provided", "status": "failed" }) file = request.files["image"] if file.filename == "": return jsonify({"message":"Please select a file"}) if file and allowed_file(file.filename): file_path = upload_to_s3(file, user_id) new_image = Image(image_url=file_path, user=user_id) if new_image.save(): return jsonify({ "image_path": app.config.get("AWS_S3_DOMAIN") + file_path, "success": "image uploaded" }) else: return jsonify({"Error": "Failed to save"}) else: return jsonify({ "message": "Not supported file format or file doesn't exist" })
def post_user_image(): user_id = get_jwt_identity() user = User.get_or_none(User.id == user_id) if "profile_image" not in request.files: return jsonify({"messages": "No profile_image key in request.files"}) file = request.files["profile_image"] if file.filename == "": return jsonify({"messages": "Please select a file"}) if file: file_path = upload_to_s3(file, "users") user.profile_image_url = file_path if user.save(): return jsonify({ "messages": "Uploaded successfully", "profile_image_path": app.config.get("S3_LOCATION") + user.profile_image_url }) else: return jsonify({"messages": "Error occured during uploading"}) else: return jsonify({"messages": "No file selected"})
def create(): user_id = get_jwt_identity() user = User.get_or_none(User.id == user_id) if user: if 'image' not in request.files: return jsonify({ "message": "No image provided", "status": "failed" }) file = request.files['image'] if file.filename == '': return jsonify({"Error": "No File selected"}) if file and allowed_file(file.filename): file_path = upload_to_s3(file, user_id) user.image_path = file_path if user.save(): return jsonify({ "image_path": app.config.get("AWS_S3_DOMAIN") + user.image_path, "success": "image uploaded" }) else: print(user.errors) return jsonify({"Error": "Failed to upload image"}) else: return jsonify({ "message": "Not supported file format or file doesn't exist" })
def home(): post_id = get_post_id_for_session(session) if request.method == 'POST': file1 = request.files['file1'] file2 = request.files['file2'] if file1 and allowed_file(file1.filename): filename = secure_filename(file1.filename) full_path = os.path.join(app.config['UPLOAD_FOLDER'], filename) file1.save(full_path) key = upload_to_s3(app.config, full_path) #else: return render('home.html')
def create(): if "user_image" not in request.files: return "No user_image key in request.files" file = request.files["user_image"] if file.filename == "": return "Please select a file" if file and allowed_file(file.filename): file_path = upload_to_s3(file, current_user.id) new_image = Image(image_url=file_path, user=current_user.id) if new_image.save(): flash("Uploaded successfully", "success") return redirect( url_for("users.show", username=current_user.username)) else: return "Could not upload profile image, please try again." else: return redirect(url_for("images.new"))
def upload_profile(id): user = User.get_or_none(User.id == id) if user: if "profile_image" not in request.files: return "No user_file key in request.files" file = request.files["profile_image"] if file.filename == "": return "Please select a file" if file: file_path = upload_to_s3(file, user.id) user.image_path = file_path if user.save(): return redirect(url_for("users.show", username=user.username)) else: return "Could not upload profile image, please go back and try again!" else: return redirect(url_for("users.edit", id=id)) else: return "No such user!"
def upload_profile(id): user = User.get_or_none(User.id == id) if user: if "profile_image" not in request.files: return "No user_file key in request.files" file = request.files["profile_image"] if file.filename == "": return "Please select a file" if file and allowed_file(file.filename): file_path = upload_to_s3(file, user.id) user.image_path = file_path if user.save(): return redirect(url_for("users.show", username=user.username)) else: return "Could not upload profile image, please try again." else: return redirect(url_for("users.profile_image", id=id)) else: flash("User not found!", "danger") return redirect("/")
def upload_image(recipe_id): recipe = Recipe.get_or_none(Recipe.id == recipe_id) if "recipe_image" not in request.files: return "No recipe_image key in request.files" file = request.files["recipe_image"] if file.filename == "": return "Please select a file" if file: file_path = upload_to_s3(file, "recipe") recipe.image_url = file_path if recipe.save(): flash("Image Uploaded") return redirect(url_for("recipes.edit", recipe_id=recipe.id)) else: flash("An error occured") return redirect(url_for("recipes.edit", recipe_id=recipe.id)) else: flash("No file selected") return redirect(url_for("recipes.edit", recipe_id=recipe.id))
def upload_profile(id): user = User.get_or_none(User.id == id) if "profile_image" not in request.files: return "No profile_image key in request.files" file = request.files["profile_image"] if file.filename == "": return "Please select a file" if file: file_path = upload_to_s3(file, "users") user.profile_image_url = file_path if user.save(): flash("Image Uploaded") return redirect(url_for("users.show", id=user.id)) else: flash("An error occured") return redirect(url_for("users.edit", id=user.id)) else: flash("No file selected") return redirect(url_for("users.edit", id=user.id))
def create(): if "user_images" not in request.files: return "No user_images key in request.files" file = request.files["user_images"] if file.filename == "": return "Please select a file" if file: file_path = upload_to_s3(file) new_image = Image(image_url = file_path, user = current_user.id ) if new_image.save(): flash("Upload successfully") return redirect(url_for('users.show', username = current_user.username)) else: return "Could not upload image, please go back and try again" else: return redirect(url_for('images.new'))
class Source(object): db = None def __init__(self, source_type, name=''): if source_type == 'database' and name: db = Database(name) self.db = db def update_description(self, source_id, value): x = json.loads(value) sql = "UPDATE source SET description='{}' WHERE source_id={}".format( json.dumps(x), source_id) self.db.exec_sql(sql, True) return sql def update_image(self, image_id, value): kv = value.split('=') put = '{}="{}"'.format(kv[0], kv[1]) sql = 'UPDATE image SET {} WHERE image_id={}'.format(put, image_id) self.db.exec_sql(sql, True) def upload_image(self, image_id): ## test sql = 'UPDATE image SET status="S" WHERE image_id={}'.format(image_id) #upload_to_s3(aws_conf, file_name, object_name) self.db.exec_sql(sql, True) time.sleep(5) ## TODO: update state return 'image_id: {} uploaded'.format(image_id) def poll_source_status(self, source_id_str): source_list = source_id_str.split(',') res = {} for i in source_list: x = self.get_source(i, 'uploaded') res[i] = len(x['image_list']) ## TODO: update state return res def batch_upload(self, config, source_id): ''' 1) upload annotation(one time) 2) upload image file 2-1) upload to s3 2-2) update file_url to server ''' #res = self.get_source(source_id, 'un-upload') res = self.get_source(source_id, 'all') # 1) upload annotation rows = [] #for i in res['image_list']: #a = i[7] if i[7] else '{}' # rows.append(i) res['upload_progress'] = '0' deployment_id = '--' if des := res['source'][7]: deployment_id = json.loads(des).get('deployment_id', '') payload = { 'image_list': res['image_list'], 'key': '{}-{}'.format(config['Installation']['account_id'], res['source'][0]), 'deployment_id': deployment_id, } # update image status to "start upload" sql = "UPDATE image SET status='100' WHERE image_id IN ({})".format( ','.join([str(x[0]) for x in res['image_list']])) self.db.exec_sql(sql, True) resp = post_to_server( '{}{}'.format(config['Server']['host'], config['Server']['image_annotation_api']), payload) if resp['status_code'] != 200: return resp['text'] else: res['upload_progress'] = '1' server_image_map = json.loads(resp['text'])['saved_image_ids'] # 2) upload file count = 0 count_uploaded = 0 for i in res['image_list']: count += 1 file_name = i[1] img = ClamImage(file_name) server_image_id = server_image_map.get(str(i[0]), '') #object_name = '{}-{}.jpg'.format(server_image_id, i[6]) object_name = '{}.jpg'.format(server_image_id) is_uploaded = upload_to_s3(config['AWSConfig'], file_name, object_name) if is_uploaded: count_uploaded = 0 sql = 'UPDATE image SET status="200", server_image_id={} WHERE image_id={}'.format( server_image_id, i[0]) self.db.exec_sql(sql, True) payload = { 'file_url': object_name, 'pk': server_image_id, } resp = post_to_server( '{}{}'.format(config['Server']['host'], config['Server']['image_update_api']), payload) sql = 'UPDATE source SET status="200" WHERE source_id={}'.format( source_id) self.db.exec_sql(sql, True) res['upload_progress'] = '2({}/{})'.format(count, count_uploaded) return res