• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

geosolutions-it / MapStore2 / 15184777770

22 May 2025 10:57AM UTC coverage: 76.935% (+0.001%) from 76.934%
15184777770

Pull #11133

github

web-flow
Merge c9a56fe63 into 94d9822b2
Pull Request #11133: Fix #10966 adjusted many requests to include headers

30989 of 48275 branches covered (64.19%)

47 of 54 new or added lines in 18 files covered. (87.04%)

3 existing lines in 3 files now uncovered.

38632 of 50214 relevant lines covered (76.93%)

36.11 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

60.61
/web/client/components/misc/SecureImage.jsx
1
/**
2
 * Copyright 2024, GeoSolutions Sas.
3
 * All rights reserved.
4
 *
5
 * This source code is licensed under the BSD-style license found in the
6
 * LICENSE file in the root directory of this source tree.
7
 */
8

9
import React, { useEffect, useState } from 'react';
10
import axios from 'axios';
11

12
import { getAuthKeyParameter, getAuthenticationMethod, getAuthorizationBasic, getToken } from '../../utils/SecurityUtils';
13
import { updateUrlParams } from '../../utils/URLUtils';
14

15

16
const SecureImage = ({
1✔
17
    alt,
18
    src,
19
    ...props
20
}) => {
21
    const [imageSrc, setImageSrc] = useState('');
74✔
22

23
    // Function to validate the image once it loads
24
    const validateImg = (imgElement) => {
74✔
25
        // Implement your validation logic here
26
        // For example, check image dimensions, aspect ratio, etc.
27
        if (imgElement.naturalWidth === 0 || imgElement.naturalHeight === 0) {
×
28
            console.error('Image validation failed: Image is not valid.');
×
29
        }
30
    };
31

32
    useEffect(() => {
74✔
33
        const authMethod = getAuthenticationMethod(src);
36✔
34

35
        const headers = getAuthorizationBasic(props?.layer?.security?.sourceId);
36✔
36
        if (authMethod === "bearer") {
36!
37
            axios.get(src, {
×
38
                responseType: 'blob'
39
            })
40
                .then((response) => {
41
                    const imageUrl = URL.createObjectURL(response.data);
×
42
                    setImageSrc(imageUrl);
×
43
                })
44
                .catch((error) => {
45
                    console.error('Error fetching image:', error);
×
46
                });
47
        } else if (authMethod === "authkey") {
36✔
48
            const authParam = getAuthKeyParameter(src);
3✔
49
            const token = getToken();
3✔
50
            if (authParam && token) {
3!
51
                const newSrc = updateUrlParams(src, {[authParam]: token});
×
52
                setImageSrc(newSrc);
×
53
            } else {
54
                setImageSrc(src);
3✔
55
            }
56

57
        } else if (props?.layer?.security?.sourceId) {
33!
NEW
58
            axios.get(src, {
×
59
                responseType: 'blob',
60
                headers
61
            })
62
                .then((response) => {
NEW
63
                    const imageUrl = URL.createObjectURL(response.data);
×
NEW
64
                    setImageSrc(imageUrl);
×
65
                })
66
                .catch((error) => {
NEW
67
                    console.error('Error fetching image:', error);
×
68
                });
69
        } else {
70
            setImageSrc(src);
33✔
71
        }
72

73
        // Clean up the URL object when the component unmounts
74
        return () => {
36✔
75
            if (imageSrc) {
36✔
76
                URL.revokeObjectURL(imageSrc);
6✔
77
            }
78
        };
79
    }, [src]);
80

81
    return (
74✔
82
        <img
83
            alt={alt}
84
            onError={(e) => {
85
                if (imageSrc) {
16!
86
                    props.onImgError(e);
16✔
87
                }
88
            }}
89
            onLoad={(e) => validateImg(e.target)}
×
90
            src={imageSrc}
91
            style={props.style}
92
            {...props}
93
        />
94
    );
95
};
96

97
export default SecureImage;
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc