def get(self, photo_id): """ Return image for thumbnail and original photo. :param photo_id: target photo id :queryparam mode: None(original) or thumbnail :return: image url for authenticated user """ try: mode = request.args.get('mode') user = get_jwt_identity() email = user['email'] path = os.path.join(app.config['UPLOAD_FOLDER'], email_normalize(email)) full_path = Path(path) photo = Photo.get(user['user_id'], range_key=photo_id) if photo.id == photo_id: if mode == 'thumbnail': full_path = full_path / 'thumbnails' / photo.filename else: full_path = full_path / photo.filename with full_path.open('rb') as f: contents = f.read() resp = make_response(contents) app.logger.debug('filepath: {}'.format(str(full_path))) resp.content_type = 'image/jpeg' return resp 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 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 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 solution_delete_photo_from_ddb(user, photo_id): app.logger.info('RUNNING TODO#4 SOLUTION CODE:') app.logger.info('Delete a photo from photos list, and update!') app.logger.info('Follow the steps in the lab guide to replace this method with your own implementation.') photo = Photo.get(user['user_id'], photo_id) photo.delete() filename = photo.filename return filename