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

geosolutions-it / MapStore2 / 10454888618

19 Aug 2024 02:04PM UTC coverage: 76.784% (-0.009%) from 76.793%
10454888618

push

github

web-flow
Fix #10506 Adding possibility to fetch legends images using Bearer token (#10507)

31062 of 48573 branches covered (63.95%)

15 of 22 new or added lines in 2 files covered. (68.18%)

5 existing lines in 1 file now uncovered.

38775 of 50499 relevant lines covered (76.78%)

33.54 hits per line

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

61.11
/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 { getAuthenticationMethod } from '../../utils/SecurityUtils';
13

14

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

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

31
    useEffect(() => {
75✔
32
        const authMethod = getAuthenticationMethod(src);
36✔
33

34
        if (authMethod === "bearer") {
36!
NEW
35
            axios.get(src, {
×
36
                responseType: 'blob'
37
            })
38
                .then((response) => {
NEW
39
                    const imageUrl = URL.createObjectURL(response.data);
×
NEW
40
                    setImageSrc(imageUrl);
×
41
                })
42
                .catch((error) => {
NEW
43
                    console.error('Error fetching image:', error);
×
44
                });
45
        } else {
46
            setImageSrc(src);
36✔
47
        }
48

49
        // Clean up the URL object when the component unmounts
50
        return () => {
36✔
51
            if (imageSrc) {
36✔
52
                URL.revokeObjectURL(imageSrc);
5✔
53
            }
54
        };
55
    }, [src]);
56

57
    return (
75✔
58
        <img
59
            alt={alt}
60
            onError={props.onImgError}
NEW
61
            onLoad={(e) => validateImg(e.target)}
×
62
            src={imageSrc}
63
            style={props.style}
64
            {...props}
65
        />
66
    );
67
};
68

69
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