Skip to content

Commit

Permalink
Fixing the CORS issues with 2.10
Browse files Browse the repository at this point in the history
  • Loading branch information
tino097 committed Sep 19, 2024
1 parent b8ee3f0 commit bba6f17
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions ckanext/s3filestore/views/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import mimetypes

import flask

import requests
from botocore.exceptions import ClientError

from ckantoolkit import config as ckan_config
Expand Down Expand Up @@ -74,7 +74,9 @@ def resource_download(package_type, id, resource_id, filename=None):
'attachment; filename=' + filename,
}
url = upload.get_signed_url_to_key(key_path, params)
return redirect(url)
response = s3_download(url)

return response

except ClientError as ex:
if ex.response['Error']['Code'] in ['NoSuchKey', '404']:
Expand All @@ -99,6 +101,19 @@ def resource_download(package_type, id, resource_id, filename=None):
return redirect(rsc[u'url'])


def s3_download(url):
s3_response = requests.get(url)
response = flask.make_response(s3_response.content)
response.headers['Content-Type'] = s3_response.headers.get('Content-Type')
response.headers['Content-Disposition'] = s3_response.headers.get('Content-Disposition')
# Set CORS headers
response.headers['Access-Control-Allow-Origin'] = '*'
response.headers['Access-Control-Allow-Methods'] = 'GET, OPTIONS'
response.headers['Access-Control-Allow-Headers'] = '*'

return response


def filesystem_resource_download(package_type, id, resource_id, filename=None):
"""
A fallback view action to download resources from the
Expand Down

0 comments on commit bba6f17

Please sign in to comment.