Skip to content

Commit

Permalink
Merge pull request #5684 from GeoNode/ISSUE_5682_210x
Browse files Browse the repository at this point in the history
[Backport to 2.10.x][Fixes #5682] Thumbnails broken for RemoteServices using a invalid WM…
  • Loading branch information
Alessio Fabiani authored Feb 13, 2020
2 parents b259f03 + 10e6349 commit 08b4ac1
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 31 deletions.
7 changes: 7 additions & 0 deletions geonode/base/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1168,6 +1168,13 @@ def has_thumbnail(self):
def save_thumbnail(self, filename, image):
upload_path = os.path.join('thumbs/', filename)
try:
# Check that the image is valid
from PIL import Image
from io import BytesIO
content_data = BytesIO(image)
im = Image.open(content_data)
im.verify() # verify that it is, in fact an image

for _thumb in glob.glob(storage.path('thumbs/%s*' % os.path.splitext(filename)[0])):
try:
os.remove(_thumb)
Expand Down
2 changes: 1 addition & 1 deletion geonode/layers/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ def test_layer_links(self):

links = Link.objects.filter(resource=lyr.resourcebase_ptr, link_type="image")
self.assertIsNotNone(links)
self.assertEqual(len(links), 8)
self.assertEqual(len(links), 9)

def test_get_valid_user(self):
# Verify it accepts an admin user
Expand Down
20 changes: 18 additions & 2 deletions geonode/layers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -964,8 +964,10 @@ def create_thumbnail(instance, thumbnail_remote_url, thumbnail_create_url=None,
if overwrite or not _thumb_exists:
BBOX_DIFFERENCE_THRESHOLD = 1e-5

is_remote = False
if not thumbnail_create_url:
thumbnail_create_url = thumbnail_remote_url
is_remote = True

if check_bbox:
# Check if the bbox is invalid
Expand Down Expand Up @@ -1004,6 +1006,21 @@ def create_thumbnail(instance, thumbnail_remote_url, thumbnail_create_url=None,

if ogc_client:
if check_ogc_backend(geoserver.BACKEND_PACKAGE):
headers = {}
if is_remote and thumbnail_remote_url:
try:
resp, image = ogc_client.request(
thumbnail_remote_url,
headers=headers,
timeout=600)
if 'ServiceException' in image or \
resp.status_code < 200 or resp.status_code > 299:
msg = 'Unable to obtain thumbnail: %s' % image
logger.error(msg)
# Replace error message with None.
image = None
except BaseException:
image = None
if image is None:
request_body = {
'width': width,
Expand Down Expand Up @@ -1061,7 +1078,6 @@ def create_thumbnail(instance, thumbnail_remote_url, thumbnail_create_url=None,
for _p in params.keys():
if _p.lower() not in thumbnail_create_url.lower():
thumbnail_create_url = thumbnail_create_url + '&%s=%s' % (_p, params[_p])
headers = {}
if check_ogc_backend(geoserver.BACKEND_PACKAGE):
_ogc_server_settings = settings.OGC_SERVER['default']
_user = _ogc_server_settings['USER'] if 'USER' in _ogc_server_settings else 'admin'
Expand All @@ -1079,7 +1095,6 @@ def create_thumbnail(instance, thumbnail_remote_url, thumbnail_create_url=None,

# Replace error message with None.
image = None

except BaseException as e:
logger.exception(e)

Expand All @@ -1091,6 +1106,7 @@ def create_thumbnail(instance, thumbnail_remote_url, thumbnail_create_url=None,
else:
msg = 'Unable to obtain thumbnail for: %s' % instance
logger.error(msg)
instance.save_thumbnail(thumbnail_name, image=None)


# this is the original implementation of create_gs_thumbnail()
Expand Down
59 changes: 31 additions & 28 deletions geonode/security/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,34 +286,37 @@ def purge_geofence_layer_rules(resource):
passwd = settings.OGC_SERVER['default']['PASSWORD']
headers = {'Content-type': 'application/json'}
workspace = get_layer_workspace(resource.layer)
r = requests.get(
"{}rest/geofence/rules.json?workspace={}&layer={}".format(
url, workspace, resource.layer.name),
headers=headers,
auth=HTTPBasicAuth(user, passwd),
timeout=10,
verify=False
)
if (r.status_code >= 200 and r.status_code < 300):
gs_rules = r.json()
r_ids = []
if gs_rules and gs_rules['rules']:
for r in gs_rules['rules']:
if r['layer'] and r['layer'] == resource.layer.name:
r_ids.append(r['id'])

# Delete GeoFence Rules associated to the Layer
# curl -X DELETE -u admin:geoserver http://<host>:<port>/geoserver/rest/geofence/rules/id/{r_id}
for i, r_id in enumerate(r_ids):
r = requests.delete(url + 'rest/geofence/rules/id/' + str(r_id),
headers=headers,
auth=HTTPBasicAuth(user, passwd))
if (r.status_code < 200 or r.status_code > 201):
msg = "Could not DELETE GeoServer Rule for Layer "
msg = msg + str(resource.layer.name)
e = Exception(msg)
logger.debug("Response [{}] : {}".format(r.status_code, r.text))
raise e
try:
r = requests.get(
"{}rest/geofence/rules.json?workspace={}&layer={}".format(
url, workspace, resource.layer.name),
headers=headers,
auth=HTTPBasicAuth(user, passwd),
timeout=10,
verify=False
)
if (r.status_code >= 200 and r.status_code < 300):
gs_rules = r.json()
r_ids = []
if gs_rules and gs_rules['rules']:
for r in gs_rules['rules']:
if r['layer'] and r['layer'] == resource.layer.name:
r_ids.append(r['id'])

# Delete GeoFence Rules associated to the Layer
# curl -X DELETE -u admin:geoserver http://<host>:<port>/geoserver/rest/geofence/rules/id/{r_id}
for i, r_id in enumerate(r_ids):
r = requests.delete(url + 'rest/geofence/rules/id/' + str(r_id),
headers=headers,
auth=HTTPBasicAuth(user, passwd))
if (r.status_code < 200 or r.status_code > 201):
msg = "Could not DELETE GeoServer Rule for Layer "
msg = msg + str(resource.layer.name)
e = Exception(msg)
logger.debug("Response [{}] : {}".format(r.status_code, r.text))
raise e
except BaseException as e:
logger.exception(e)


@on_ogc_backend(geoserver.BACKEND_PACKAGE)
Expand Down
1 change: 1 addition & 0 deletions geonode/services/serviceprocessors/arcgis.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ def _create_layer_thumbnail(self, geonode_layer):
"width": "200",
"height": "150",
"format": "image/png",
"styles": ""
}
kvp = "&".join("{}={}".format(*item) for item in params.items())
thumbnail_remote_url = "{}?{}".format(
Expand Down
1 change: 1 addition & 0 deletions geonode/services/serviceprocessors/wms.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ def _create_layer_thumbnail(self, geonode_layer):
"width": "200",
"height": "150",
"format": "image/png",
"styles": ""
}
kvp = "&".join("{}={}".format(*item) for item in params.items())
thumbnail_remote_url = "{}?{}".format(
Expand Down

0 comments on commit 08b4ac1

Please sign in to comment.