Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add webp support #5

Merged
merged 6 commits into from
May 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Prism is an image transformation proxy for AWS S3. The source image is determine
![Prism Flow Diagram](flow.png)

### Example request:
http://prism-dev.tryprism.com -> This is the live server domain.

URL: `https://prism-dev.tryprism.com/images/test-1.jpg?h=200`
Image file: `images/test-1.jpg`
Parameters: `h=200`
Expand Down Expand Up @@ -113,9 +115,17 @@ The TEST_IMAGE setting is used to provide an image to be used for the test and h

### uWSGI Configuration

The Prism app runs under uWSGI. By default it runs with 2 processes and 2 threads per process. These settings can be overridden using the UWSGI_PROCESSES and UWSGI_THREADS environment variables. Similarly other options can be passed to uWSGI using UWSGI_* environment variables.
The Prism app runs under uWSGI. By default, it runs with 2 processes and 2 threads per process. These settings can be overridden using the UWSGI_PROCESSES and UWSGI_THREADS environment variables. Similarly, other options can be passed to uWSGI using UWSGI_* environment variables.


## Deployment
The Docker container runs a uwsgi process with a HTTP socket (port 8000) and a uwsgi socket (port 3001). For local development and testing connecting to the HTTP server is sufficient. For production use it is recommended to use Nginx in front of uwsgi. A sample Nginx configuration including caching setup is included here: [nginx-sample.conf](nginx-sample.conf)

To run docker container use following command:

`docker-compose -f docker-compose.yml -f docker-compose.development.yml up`

The `8000` port of the container is mapped to the `8001` port of the host. Use `localhost:8001` to access the app.

`http://localhost:8001/test`. This test url runs the same command both on your local and the live Prism server, and provides comparisons between local and live prism server image resizing operations.

29 changes: 14 additions & 15 deletions prism/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def get_customer(self, request):
def main(self, request):
path = request.path[1:]
_, extension = os.path.splitext(path.lower())
if extension not in ('.png', '.jpg', '.jpeg', '.gif'):
if extension not in ('.png', '.jpg', '.jpeg', '.gif', '.webp'):
raise NotFound()
try:
args = parse_args(path, request)
Expand Down Expand Up @@ -152,19 +152,18 @@ def info(path, args, customer):
return json_response(info)


def fetch_image(args, original_url, exists):
if args['with_info'] or args['force'] or not exists:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This if check is redundant.
See line 181.

try:
return core.fetch_image(original_url)
except HTTPError as e:
if e.response.status_code in (404, 403):
raise NotFound()
else:
raise
except core.EmptyOriginalFile as e:
raise BadRequest(e.message)
except core.InvalidImageError as e:
raise BadRequest(e.message)
def fetch_image(original_url):
try:
return core.fetch_image(original_url)
except HTTPError as e:
if e.response.status_code in (404, 403):
raise NotFound()
else:
raise
except core.EmptyOriginalFile as e:
raise BadRequest(e.message)
except core.InvalidImageError as e:
raise BadRequest(e.message)


def process(path, args, customer):
Expand All @@ -181,7 +180,7 @@ def process(path, args, customer):
exists = core.check_s3_object_exists(result_url)
if args['with_info'] or args['force'] or not exists:
clear_old_tmp_files()
im = fetch_image(args=args, original_url=original_url, exists=exists)
im = fetch_image(original_url=original_url)
f = core.resize(im.clone(), cmd, options)
bucket = dict(
id=customer.write_bucket_name,
Expand Down
2 changes: 1 addition & 1 deletion prism/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def check_s3_object_exists(url):

def upload_file(bucket_dict, file, new_filename):
"""
uploads file to s3 bucket under prism-thumbnails folder
uploads file to s3 bucket under prism-images folder
"""

logger.info("bucket_key_id: %s", bucket_dict['key_id'])
Expand Down
3 changes: 2 additions & 1 deletion prism/static/test.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
'cmd=resize&w=100',
'cmd=resize&h=100',
'cmd=resize&w=100&h=100',

'cmd=resize&w=100&h=100&preserve_ratio=1',
'cmd=resize&w=100&h=100&preserve_ratio=0',

Expand Down Expand Up @@ -56,6 +56,7 @@
var images = [
'/images/test-1.jpg',
'/images/test-bs.png',
'/images/hippo.webp',
]

var urls = [];
Expand Down