"""Image Location Model. Model representing the location of a remote image. """ import hashlib from oto import response from assets import config def get_backend_image_filename(entity_id, image_type, image_format): """Get the backend (S3) path where an image asset is located. This is slightly different from the path relative to the CDN URL because the CloudFront distribution is rooted at an "images" prefix in S3. Args: image_type: (String): Type of entity (e.g. "product" or "artist"). image_format: (String): Whether this is for a thumbnail or cover. entity_id (Integer): Unique identifier of the product or artist. Returns: Response: the path for the image. """ return response.Response(message='images/{}'.format( get_image_filename(entity_id, image_type, image_format))) def get_image_filename(entity_id, image_type, image_format): """Get image filename based on well-formed path structure. Args: image_type: (String): Type of entity (e.g. "product" or "artist"). image_format: (String): Whether this is for a thumbnail or cover. entity_id (Integer): Unique identifier of the product or artist. Returns: Response: the path for the image. """ product_hash = hashlib.md5() product_hash.update(str(entity_id).encode('utf8')) return '{image_type}/{image_format}/{md5_hash}.jpg'.format( image_type=image_type, image_format=image_format, md5_hash=str(product_hash.hexdigest())) def get_image_location_by_filename(filename): """Get image location for a specified filename. Gets the location for an image from the configured CDN. Args: filename: (String): The path and filename for the image. Returns: Response: the location for the file on the cdn. """ return response.Response('{cdn}/{filename}'.format( cdn=config.CDN_URL, filename=filename))