def post(self): token = get_token_from_header(request) try: app.logger.debug(dir(file_upload_parser)) form = file_upload_parser.parse_args() filename_orig = form['file'].filename extension = (filename_orig.rsplit('.', 1)[1]).lower() if extension.lower() not in ['jpg', 'jpeg', 'bmp', 'gif', 'png']: app.logger.error('ERROR:file format is not supported:{0}'.format(filename_orig)) return err_response('ERROR:file format is not supported:{0}'.format(filename_orig), 400) current_user = get_cognito_user(token) filename = secure_filename("{0}.{1}".format(uuid.uuid4(), extension)) filesize = save_s3(form['file'], filename, current_user['email']) user_id = current_user['user_id'] new_photo = create_photo_info(user_id, filename, filesize, form) solution_put_photo_info_ddb(user_id, new_photo) return m_response({"photo_id": filename}, 200) except Exception as e: app.logger.error('ERROR:file upload failed:user_id:{}'.format(get_cognito_user(token)['user_id'])) app.logger.error(e) return err_response(e, 500)
def post(self): form = file_upload_parser.parse_args() filename_orig = form['file'].filename token = get_token_from_header(request) extension = (filename_orig.rsplit('.', 1)[1]).lower() current_user = get_cognito_user(token) if extension.lower() not in ['jpg', 'jpeg', 'bmp', 'gif', 'png']: app.logger.error( 'File format is not supported:{0}'.format(filename_orig)) raise BadRequest( 'File format is not supported:{0}'.format(filename_orig)) try: filename = secure_filename("{0}.{1}".format( uuid.uuid4(), extension)) filesize = save_s3(form['file'], filename, current_user['email']) user_id = current_user['user_id'] solution_put_photo_info_ddb(user_id, filename, form, filesize) return make_response({'ok': True}, 200) except Exception as e: app.logger.error('ERROR:file upload failed:user_id:{}'.format( current_user['user_id'])) app.logger.error(e) raise InternalServerError('File upload failed: {0}'.format(e))
def delete(self, photo_id): """one photo delete""" token = get_token_from_header(request) user = get_cognito_user(token) try: photo = Photo.get(user['user_id'], photo_id) photo.delete() file_deleted = delete_s3(photo.filename, user['email']) if file_deleted: app.logger.debug( 'success:photo deleted: user_id:{}, photo_id:{}'.format( user['user_id'], photo_id)) return make_response( { 'ok': True, 'photos': { 'photo_id': photo_id } }, 200) else: raise FileNotFoundError except FileNotFoundError as e: raise InternalServerError(e) except Exception as e: raise InternalServerError(e)
def delete(self, photo_id): """one photo delete""" token = get_token_from_header(request) try: user = get_cognito_user(token) photo = Photo.get(user['user_id'], photo_id) photo.delete() file_deleted = delete_s3(photo.filename, user['email']) if file_deleted: app.logger.debug( "success:photo deleted: user_id:{}, photo_id:{}".format( user['user_id'], photo_id)) return m_response({'photo_id': photo_id}, 200) else: raise FileNotFoundError except FileNotFoundError as e: app.logger.error('ERROR:not exist photo_id:{}'.format(photo_id)) return err_response('ERROR:not exist photo_id:{}'.format(photo_id), 404) except Exception as e: app.logger.error( "ERROR:photo delete failed: photo_id:{}".format(photo_id)) app.logger.error(e) return err_response( "ERROR:photo delete failed: photo_id:{}".format(photo_id), 500)
def get(self, photo_id): token = get_token_from_header(request) try: mode = request.args.get('mode') user = get_cognito_user(token) email = user['email'] return presigned_url(photo_id, email, True if mode else False) except Exception as e: app.logger.error('ERROR:get photo failed:photo_id:{}'.format(photo_id)) app.logger.error(e) return 'http://placehold.it/400x300'
def get(self): """Get all photos as list""" token = get_token_from_header(request) try: user = get_cognito_user(token) photos = Photo.query(user['user_id']) data = {'photos': []} [data['photos'].append(with_presigned_url(user, photo)) for photo in photos] app.logger.debug('success:photos_list: {}'.format(data)) return make_response({'ok': True, 'photos': data['photos']}, 200) except Exception as e: app.logger.error('ERROR:photos list failed') app.logger.error(e) raise InternalServerError('Photos list retrieving failed')
def get(self): """Get all photos as list""" token = get_token_from_header(request) try: user = get_cognito_user(token) photos = Photo.query(user['user_id']) data = {'photos': []} [data['photos'].append(with_presigned_url(user, photo)) for photo in photos] app.logger.debug("success:photos_list:{}".format(data)) return m_response(data['photos'], 200) except Exception as e: app.logger.error("ERROR:photos list failed") app.logger.error(e) return err_response(e, 500)