def post_image(): data = request.get_json() or {} if 'image_url' not in data or 'image_filename' not in data: return bad_request('image json needs to be in data ') image = Image() image.from_dict(data) db.session.add(image) db.session.commit() response = jsonify(image.to_dict()) response.status_code = 201 response.headers['Location'] = url_for('get_image', filename=image.image_filename) return response
def post(self): """ add photo """ file = request.files['file'] if file and self.allowed_file(file.filename): filename = secure_filename(file.filename) if not os.path.exists(current_app.config['UPLOAD_FOLDER']): os.makedirs(current_app.config['UPLOAD_FOLDER']) file.save( os.path.join(current_app.config['UPLOAD_FOLDER'], filename)) img_url = url_for('api.photo', filename=filename) img = Image(url=img_url) db.session.add(img) db.session.commit() return img.to_dict() else: raise Conflict
def image_post(): """ Creates a new image from data on post request """ data = request.get_json(force=True) uuid = upload_image_aws(data) image = Image( title=data['title'], grid=data['grid'], gifImgUrl='https://pixel-showdown.s3.amazonaws.com/app-content/'+uuid+'.gif', apngImgUrl='https://pixel-showdown.s3.amazonaws.com/app-content/'+uuid+'.png', userId=data['userId'], competitionId=data['competitionId'], ) db.session.add(image) db.session.commit() return image.to_dict()
def api_file_upload(): if "file" not in request.files: return error_response(["File must be provided"]) file: FileStorage = request.files["file"] if not file or file.filename == "": return error_response(["File must be provided"]) if file.mimetype not in {"image/jpeg", "image/gif"}: return error_response( ["Invalid image format. Must be a .jpg, .jpeg or .gif file."]) user_id = int(current_user.get_id()) filename_len = 10 filename = "".join( random.choices(string.ascii_letters + string.digits, k=filename_len)) ts = calendar.timegm(time.gmtime()) filename = "%s-%d-%s.jpg" % (user_id, ts, filename) fs_path = os.path.join(app.config["UPLOAD_DIR"], filename) file.save(fs_path) file_hash = hash_file(fs_path) image = Image( name=filename, original_name=file.filename, fs_path=fs_path, hash=file_hash, mime_type=file.mimetype, uploader_id=user_id, ) db.session.add(image) db.session.commit() return success_response(image.to_dict())
def upload_images(): images = {} idx = 0 if len(request.files) > 0: file_list = request.files.getlist('images') for file in file_list: if not check_file(file.filename): return {'errors': ['Unaccepted file extension, must be JPG, PNG, or JPEG']}, 400 for file in file_list: image = Image() image.user_id=current_user.id image.type_id=request.form.get("type_id") image.type=request.form.get("type") file_url = upload_file_to_s3(file, Config.S3_BUCKET) image.image_url = file_url db.session.add(image) db.session.commit() images[idx] = image.to_dict() idx += 1 return images, 201 else: return {"errors":"no files"}, 400