forked from geosolutions-it/MapStore2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix geosolutions-it#10506 Adding possibility to fetch legends images …
…using Bearer token (geosolutions-it#10507)
- Loading branch information
Showing
3 changed files
with
119 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/** | ||
* Copyright 2024, GeoSolutions Sas. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import React, { useEffect, useState } from 'react'; | ||
import axios from 'axios'; | ||
|
||
import { getAuthenticationMethod } from '../../utils/SecurityUtils'; | ||
|
||
|
||
const SecureImage = ({ | ||
alt, | ||
src, | ||
...props | ||
}) => { | ||
const [imageSrc, setImageSrc] = useState(''); | ||
|
||
// Function to validate the image once it loads | ||
const validateImg = (imgElement) => { | ||
// Implement your validation logic here | ||
// For example, check image dimensions, aspect ratio, etc. | ||
if (imgElement.naturalWidth === 0 || imgElement.naturalHeight === 0) { | ||
console.error('Image validation failed: Image is not valid.'); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
const authMethod = getAuthenticationMethod(src); | ||
|
||
if (authMethod === "bearer") { | ||
axios.get(src, { | ||
responseType: 'blob' | ||
}) | ||
.then((response) => { | ||
const imageUrl = URL.createObjectURL(response.data); | ||
setImageSrc(imageUrl); | ||
}) | ||
.catch((error) => { | ||
console.error('Error fetching image:', error); | ||
}); | ||
} else { | ||
setImageSrc(src); | ||
} | ||
|
||
// Clean up the URL object when the component unmounts | ||
return () => { | ||
if (imageSrc) { | ||
URL.revokeObjectURL(imageSrc); | ||
} | ||
}; | ||
}, [src]); | ||
|
||
return ( | ||
<img | ||
alt={alt} | ||
onError={props.onImgError} | ||
onLoad={(e) => validateImg(e.target)} | ||
src={imageSrc} | ||
style={props.style} | ||
{...props} | ||
/> | ||
); | ||
}; | ||
|
||
export default SecureImage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters