Skip to content

Commit

Permalink
Add support for info images
Browse files Browse the repository at this point in the history
  • Loading branch information
timbeccue committed May 21, 2021
1 parent 04a993e commit 8691ef6
Show file tree
Hide file tree
Showing 10 changed files with 11,036 additions and 83 deletions.
2 changes: 2 additions & 0 deletions api/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ def get_image_pkg(self):
"jpg_medium_exists": self.jpg_medium_exists,
"jpg_small_exists": self.jpg_small_exists,

"s3_directory": "data",

"username": self.username,
"user_id": self.user_id,
}
Expand Down
31 changes: 29 additions & 2 deletions api/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
log = logging.getLogger()
log.setLevel(logging.INFO)

info_images_table = dynamodb_r.Table(os.getenv('INFO_IMAGES_TABLE'))

def dummy_requires_auth(event, context):
""" No purpose other than testing auth functionality """
log.info(json.dumps(event, indent=2))
Expand Down Expand Up @@ -64,7 +66,24 @@ def upload(event, context):
"""
log.info(json.dumps(event, indent=2))
body = _get_body(event)
key = 'data/' + body['object_name']

# retrieve and validate the s3_directory
s3_directory = body.get('s3_directory', 'data')
if s3_directory not in ['data', 'info-images', 'allsky', 'test']:
error_msg = "s3_directory must be either 'data', 'info-images', or 'allsky'."
log.warning(error_msg)
return http_response(HTTPStatus.FORBIDDEN, error_msg)

# get upload metadata
metadata = body.get('metadata', None)
if metadata is not None:
metadata = json.dumps(json.loads(metadata), cls=DecimalEncoder)

# TODO:
# if applicable: add metadata to database
#

key = f"{s3_directory}/{body['object_name']}"
s3 = boto3.client('s3', REGION, config=Config(signature_version='s3v4'))
url = json.dumps(s3.generate_presigned_post(
Bucket = BUCKET_NAME,
Expand All @@ -78,7 +97,15 @@ def upload(event, context):
def download(event, context):
log.info(json.dumps(event, indent=2))
body = _get_body(event)
key = "data/" + body['object_name']

# retrieve and validate the s3_directory
s3_directory = body.get('s3_directory', 'data')
if s3_directory not in ['data', 'info-images', 'allsky', 'test']:
error_msg = "s3_directory must be either 'data', 'info-images', or 'allsky'."
log.warning(error_msg)
return http_response(HTTPStatus.FORBIDDEN, error_msg)

key = f"{s3_directory}/{body['object_name']}"
params = {
"Bucket": BUCKET_NAME,
"Key": key,
Expand Down
2 changes: 1 addition & 1 deletion api/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
BUCKET_NAME = os.environ['S3_BUCKET_NAME']
REGION = os.environ['REGION']
S3_PUT_TTL = 300
S3_GET_TTL = 3600
S3_GET_TTL = 86400 * 5 # 5 days before s3 image links expire

dynamodb_r = boto3.resource('dynamodb', REGION)
ssm_c = boto3.client('ssm', region_name=REGION)
Expand Down
48 changes: 48 additions & 0 deletions api/info_images.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

import json
import boto3
import os

from api.helpers import _get_secret, http_response, get_s3_file_url


ddb = boto3.resource('dynamodb', os.getenv('REGION'))
info_images_table = ddb.Table(os.getenv('INFO_IMAGES_TABLE'))

def get_info_image_package(event, context):
site = event['pathParameters']['site']
info_images_table.get_item(Key={"pk": site})
info_image_query = info_images_table.get_item(Key = { "pk": site })
try:
info_image = info_image_query['Item']

info_image_package = {
**info_image,
"site": info_image["pk"],
"base_filename": info_image["base_filename"],
#TODO: check for file existence by checking if the file path exists. Then remove the explicit existance keys from info-images database.
"jpg_medium_exists": info_image.get("jpg_medium_exists", False),
"jpg_small_exists": info_image.get("jpg_small_exists", False),
"fits_01_exists": info_image.get("fits_01_exists", False),
"fits_10_exists": info_image.get("fits_10_exists", False),
"s3_directory": "info-images",
}

if info_image_package["jpg_medium_exists"]:
jpg_medium_url = get_s3_file_url(info_image["jpg_10_file_path"])
info_image_package["jpg_url"] = jpg_medium_url
if info_image_package["jpg_small_exists"]:
jpg_small_url = get_s3_file_url(info_image["jpg_11_file_path"])
info_image_package["jpg_small_url"] = jpg_small_url
if info_image_package["fits_01_exists"]:
fits_01_url = get_s3_file_url(info_image["fits_01_file_path"])
info_image_package["fits_01_url"] = fits_01_url
if info_image_package["fits_10_exists"]:
fits_10_url = get_s3_file_url(info_image["fits_10_file_path"])
info_image_package["fits_10_url"] = fits_10_url


print(info_image_package)
return http_response(200, info_image_package)
except KeyError:
return http_response(404, f'No info image for site {site}.')
2 changes: 0 additions & 2 deletions api/tests/unit/test_events.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import pytest

import skyfield
from skyfield import api, almanac
from skyfield.nutationlib import iau2000b
from skyfield.api import utc

import datetime, time
Expand Down
Loading

0 comments on commit 8691ef6

Please sign in to comment.