def capture_image(img_code, delete_img=False): #get img data from img_code and then capture and send it to amazon s3 if img_code != '': img = db.CaptureObj.find_one({'hashcode': img_code}) if img: #capture image imgcapture = Captureimg() filename = imgcapture.capture_img(img.url, img_code) if filename: #update aws path img.is_captured = 1 img.captured_at = datetime.now() #check if config set to upload s3 then upload, if not using url direct link if app.config['UPLOAD_TO_S3'] == True: #push to amazon s3 s3helper = S3Helper() upload = s3helper.upload(filename, ''.join([str(img_code), '.png']), app.config['S3_UPLOAD_DIRECTORY']) if upload: img.uploaded_to_aws = 1 img.aws_path = unicode(''.join([ app.config['S3_LOCATION'], app.config['S3_BUCKET'], '/', app.config['S3_UPLOAD_DIRECTORY'], '/', str(img_code), '.png' ])) else: #set img.aws_path = local path img.aws_path = None img.local_storage = unicode(''.join([ app.config['BASE_URL'], app.config['LOCAL_STORAGE'], img_code, '.png' ])) #save img object img.save() #if delete image = true then delete image from local path if delete_img == True and app.config['UPLOAD_TO_S3'] == True: try: os.remove(filename) except Exception, e: pass else: return False else: return False
def capture_image(img_code, delete_img = False): #get img data from img_code and then capture and send it to amazon s3 if img_code != '': img = db.CaptureObj.find_one({'hashcode':img_code}) if img: #capture image imgcapture = Captureimg() filename = imgcapture.capture_img(img.url, img_code) if filename: #update aws path img.is_captured = 1 img.captured_at = datetime.now() #check if config set to upload s3 then upload, if not using url direct link if app.config['UPLOAD_TO_S3'] == True: #push to amazon s3 s3helper = S3Helper() upload = s3helper.upload(filename, ''.join([str(img_code), '.png']), app.config['S3_UPLOAD_DIRECTORY']) if upload: img.uploaded_to_aws = 1 img.aws_path = unicode(''.join([app.config['S3_LOCATION'], app.config['S3_BUCKET'], '/', app.config['S3_UPLOAD_DIRECTORY'], '/', str(img_code), '.png'])) else: #set img.aws_path = local path img.aws_path = None img.local_storage = unicode(''.join([app.config['BASE_URL'], app.config['LOCAL_STORAGE'], img_code,'.png'])) #save img object img.save() #if delete image = true then delete image from local path if delete_img == True and app.config['UPLOAD_TO_S3'] == True: try: os.remove(filename) except Exception, e: pass else: return False else: return False
def capturedong(): current_user = db.User.find_one({'token_key':request.form['token']}) if current_user: if request.method == 'POST' and 'url' in request.form: url = request.form['url'] if url != '': hashed = uuid4() imgcapture = Captureimg() filename = ''.join([str(hashed), '.png']) local_storage = imgcapture.capture_img(url, hashed) if filename and filename != '': #create new capture image obj new_obj = db.CaptureObj() new_obj.hashcode = unicode(hashed) new_obj.url = unicode(url) new_obj.is_captured = 1 new_obj.status = 1 new_obj.filename = unicode(filename) new_obj.local_storage = unicode(local_storage) new_obj.created_by = str(current_user._id) new_obj.captured_at = datetime.now() #push to amazon s3 s3helper = S3Helper() upload = s3helper.upload(filename, ''.join([str(hashed), '.png']), app.config['S3_UPLOAD_DIRECTORY']) if upload: new_obj.aws_path = unicode(''.join([app.config['S3_LOCATION'], app.config['S3_BUCKET'], '/', app.config['S3_UPLOAD_DIRECTORY'], '/', str(hashed), '.png'])) print new_obj #save obj after push to amazon s3 new_obj.save() return RestHelper().build_response(200, 200, {'img_key': new_obj.hashcode, 'aws_url':new_obj.aws_path}, 'Success :p') else: return RestHelper().build_response(500, 500, {}, 'Image not captured, damn') else: return RestHelper().build_response(412, 412, {}, 'Url required!') else: return RestHelper().build_response(403, 403, {}, 'Unauthorized!')