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

mendersoftware / gui / 1015445845

25 Sep 2023 09:43AM UTC coverage: 82.537% (-17.4%) from 99.964%
1015445845

Pull #4028

gitlab-ci

mzedel
chore: aligned release retrieval with v2 api models

Signed-off-by: Manuel Zedel <manuel.zedel@northern.tech>
Pull Request #4028: MEN-6455

4355 of 6315 branches covered (0.0%)

184 of 206 new or added lines in 19 files covered. (89.32%)

1724 existing lines in 164 files now uncovered.

8323 of 10084 relevant lines covered (82.54%)

208.49 hits per line

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

62.5
/src/js/components/devices/troubleshoot/filetransfer.js
1
// Copyright 2021 Northern.tech AS
2
//
3
//    Licensed under the Apache License, Version 2.0 (the "License");
4
//    you may not use this file except in compliance with the License.
5
//    You may obtain a copy of the License at
6
//
7
//        http://www.apache.org/licenses/LICENSE-2.0
8
//
9
//    Unless required by applicable law or agreed to in writing, software
10
//    distributed under the License is distributed on an "AS IS" BASIS,
11
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
//    See the License for the specific language governing permissions and
13
//    limitations under the License.
14
import React, { useEffect, useState } from 'react';
15

16
import { FileCopy as CopyPasteIcon } from '@mui/icons-material';
17
import { Button, IconButton, Tab, Tabs, TextField, Tooltip } from '@mui/material';
18
import { makeStyles } from 'tss-react/mui';
19

20
import { canAccess } from '../../../constants/appConstants';
21
import FileUpload from '../../common/forms/fileupload';
22
import InfoText from '../../common/infotext';
23

24
const tabs = [
13✔
25
  { key: 'upload', canAccess: ({ userCapabilities: { canTroubleshoot, canWriteDevices } }) => canTroubleshoot && canWriteDevices },
1✔
26
  { key: 'download', canAccess }
27
];
28

29
const maxWidth = 400;
13✔
30

31
const useStyles = makeStyles()(theme => ({
13✔
32
  column: { maxWidth },
33
  inputWrapper: { display: 'grid', gridTemplateColumns: `${maxWidth}px max-content` },
34
  tab: { alignItems: 'flex-start' },
35
  fileDestination: { marginTop: theme.spacing(2) }
36
}));
37

38
export const FileTransfer = ({ deviceId, downloadPath, file, onDownload, onUpload, setFile, setDownloadPath, setUploadPath, uploadPath, userCapabilities }) => {
13✔
39
  const { classes } = useStyles();
2✔
40
  const [currentTab, setCurrentTab] = useState(tabs[0].key);
2✔
41
  const [isValidDestination, setIsValidDestination] = useState(true);
2✔
42
  const [availableTabs, setAvailableTabs] = useState(tabs);
2✔
43

44
  useEffect(() => {
2✔
45
    let destination = currentTab === 'download' ? downloadPath : uploadPath;
1!
46
    const isValid = destination.length ? /^(?:\/|[a-z]+:\/\/)/.test(destination) : true;
1!
47
    setIsValidDestination(isValid);
1✔
48
  }, [currentTab, downloadPath, uploadPath]);
49

50
  useEffect(() => {
2✔
51
    const availableTabs = tabs.reduce((accu, item) => {
1✔
52
      if (item.canAccess({ userCapabilities })) {
2!
53
        accu.push(item);
2✔
54
      }
55
      return accu;
2✔
56
    }, []);
57
    setAvailableTabs(availableTabs);
1✔
58
    setCurrentTab(availableTabs[0].key);
1✔
59
    // eslint-disable-next-line react-hooks/exhaustive-deps
60
  }, [JSON.stringify(userCapabilities)]);
61

62
  const onPasteDownloadClick = async () => {
2✔
UNCOV
63
    const path = await navigator.clipboard.readText();
×
UNCOV
64
    setDownloadPath(path);
×
65
  };
66

67
  const onPasteUploadClick = async () => {
2✔
UNCOV
68
    const path = await navigator.clipboard.readText();
×
UNCOV
69
    setUploadPath(path);
×
70
  };
71

72
  const onFileSelect = selectedFile => {
2✔
73
    let path;
UNCOV
74
    if (selectedFile) {
×
UNCOV
75
      path = `${uploadPath}/${selectedFile.name}`;
×
76
    } else {
UNCOV
77
      path = file && uploadPath.includes(file.name) ? uploadPath.substring(0, uploadPath.lastIndexOf('/')) : uploadPath;
×
78
    }
UNCOV
79
    setUploadPath(path);
×
UNCOV
80
    setFile(selectedFile);
×
81
  };
82

83
  return (
2✔
84
    <div className="tab-container with-sub-panels" style={{ minHeight: '95%' }}>
UNCOV
85
      <Tabs orientation="vertical" className="leftFixed" onChange={(e, item) => setCurrentTab(item)} value={currentTab}>
×
86
        {availableTabs.map(({ key }) => (
87
          <Tab className={`${classes.tab} capitalized`} key={key} label={key} value={key} />
4✔
88
        ))}
89
      </Tabs>
90
      <div className="rightFluid padding-right">
91
        {currentTab === 'upload' ? (
2!
92
          <>
93
            <InfoText className={classes.column}>Upload a file to the device</InfoText>
94
            <FileUpload
95
              enableContentReading={false}
96
              fileNameSelection={file?.name}
UNCOV
97
              onFileChange={() => undefined}
×
98
              onFileSelect={onFileSelect}
99
              placeholder={
100
                <>
101
                  Drag here or <a>browse</a> to upload a file
102
                </>
103
              }
104
              style={{ maxWidth }}
105
            />
106
            <div className={classes.inputWrapper}>
107
              <TextField
108
                autoFocus={true}
109
                error={!isValidDestination}
110
                fullWidth
111
                helperText={!isValidDestination && <div className="warning">Destination has to be an absolute path</div>}
2!
112
                inputProps={{ style: { marginTop: 16 } }}
113
                InputLabelProps={{ shrink: true }}
114
                label="Destination directory on the device where the file will be transferred"
UNCOV
115
                onChange={e => setUploadPath(e.target.value)}
×
116
                placeholder="Example: /opt/installed-by-single-file"
117
                value={uploadPath}
118
              />
119
              <Tooltip title="Paste" placement="top">
120
                <IconButton style={{ alignSelf: 'flex-end' }} onClick={onPasteUploadClick} size="large">
121
                  <CopyPasteIcon />
122
                </IconButton>
123
              </Tooltip>
124
            </div>
125
            <div className={`flexbox margin-top ${classes.column}`} style={{ justifyContent: 'flex-end' }}>
126
              <Button
127
                variant="contained"
128
                color="primary"
129
                disabled={!(file && uploadPath && isValidDestination)}
2!
UNCOV
130
                onClick={() => onUpload(deviceId, uploadPath, file)}
×
131
              >
132
                Upload
133
              </Button>
134
            </div>
135
          </>
136
        ) : (
137
          <>
138
            <InfoText>Download a file from the device</InfoText>
139
            <div className={classes.inputWrapper}>
140
              <TextField
141
                autoFocus={true}
142
                className={classes.column}
143
                error={!isValidDestination}
144
                fullWidth
145
                helperText={!isValidDestination && <div className="warning">Destination has to be an absolute path</div>}
×
146
                inputProps={{ className: classes.fileDestination }}
147
                InputLabelProps={{ shrink: true }}
148
                label="Path to the file on the device"
UNCOV
149
                onChange={e => setDownloadPath(e.target.value)}
×
150
                placeholder="Example: /home/mender/"
151
                value={downloadPath}
152
              />
153
              <Tooltip title="Paste" placement="top">
154
                <IconButton style={{ alignSelf: 'flex-end' }} onClick={onPasteDownloadClick} size="large">
155
                  <CopyPasteIcon />
156
                </IconButton>
157
              </Tooltip>
158
            </div>
159
            <div className={`flexbox margin-top ${classes.column}`} style={{ justifyContent: 'flex-end' }}>
160
              <Button
161
                variant="contained"
162
                color="primary"
163
                disabled={!(downloadPath && isValidDestination)}
×
UNCOV
164
                onClick={() => onDownload(downloadPath)}
×
165
                style={{ alignSelf: 'flex-end' }}
166
              >
167
                Download
168
              </Button>
169
            </div>
170
          </>
171
        )}
172
      </div>
173
    </div>
174
  );
175
};
176

177
export default FileTransfer;
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

© 2025 Coveralls, Inc