forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds PCAP/Attachment download capability (#120)
* adding file/pcap download capability * updating packages * updating dev version of shared package * updating shared package dev version * update typescript, fix import * fixing broken ts build * sorting file names * update nm-web-shared version
- Loading branch information
Showing
19 changed files
with
1,112 additions
and
76 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import React, { useState, useEffect } from 'react'; | ||
import { createUseStyles } from 'react-jss'; | ||
import { EuiButtonIcon } from '@elastic/eui'; | ||
import { toastNotifications } from 'ui/notify'; | ||
import { startAttachmentDownload } from '@logrhythm/nm-web-shared/services/session_files'; | ||
import FileDownloadModal from './file_download/file_download_modal'; | ||
|
||
const useStyles = createUseStyles({ | ||
buttonWrapper: { | ||
cursor: 'pointer', | ||
}, | ||
}); | ||
|
||
export interface AttachDownloadProps { | ||
session: string; | ||
fileName: string; | ||
} | ||
|
||
const AttachDownload = (props: AttachDownloadProps) => { | ||
const { session, fileName } = props; | ||
|
||
const classes = useStyles(); | ||
|
||
const [downloadId, setDownloadId] = useState(''); | ||
const [fileNames, setFileNames] = useState<string[]>([]); | ||
|
||
useEffect( | ||
() => { | ||
const newFileNames = new Set<string>(); | ||
|
||
fileName | ||
.split(',') | ||
.map(f => f.trim()) | ||
.forEach(file => { | ||
let fileNameToAdd: string = file; | ||
|
||
const prefix: number = 1; | ||
while (newFileNames.has(fileNameToAdd)) { | ||
fileNameToAdd = `${prefix}_${file}`; | ||
} | ||
|
||
newFileNames.add(fileNameToAdd); | ||
}); | ||
|
||
setFileNames(Array.from(newFileNames)); | ||
}, | ||
[fileName] | ||
); | ||
|
||
const handleStartDownload = async () => { | ||
try { | ||
const startDownloadRes = await startAttachmentDownload(session, fileNames); | ||
|
||
setDownloadId(startDownloadRes.data.downloadID); | ||
} catch (err) { | ||
console.error( // eslint-disable-line | ||
`An error occurred creating an attachment download for session ${session}`, | ||
err | ||
); | ||
toastNotifications.addDanger('An error initiating a download for the attachment(s).'); | ||
return; | ||
} | ||
}; | ||
|
||
return ( | ||
<> | ||
<div className={classes.buttonWrapper}> | ||
<EuiButtonIcon | ||
disabled={fileNames.length === 0} | ||
iconType="importAction" | ||
color="primary" | ||
onClick={handleStartDownload} | ||
aria-label="Download Attachment" | ||
/> | ||
</div> | ||
<FileDownloadModal | ||
downloadId={downloadId} | ||
fileType="reconstruction" | ||
onClose={() => setDownloadId('')} | ||
/> | ||
</> | ||
); | ||
}; | ||
|
||
export default AttachDownload; // eslint-disable-line |
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,105 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import React, { useEffect, useState } from 'react'; | ||
import { createUseStyles } from 'react-jss'; | ||
import { EuiButtonIcon, EuiCheckbox } from '@elastic/eui'; | ||
import SelectedCaptureSessions from '@logrhythm/nm-web-shared/services/selected_capture_sessions'; | ||
import { startPcapDownload } from '@logrhythm/nm-web-shared/services/session_files'; | ||
import { toastNotifications } from 'ui/notify'; | ||
import FileDownloadModal from './file_download/file_download_modal'; | ||
|
||
const useStyles = createUseStyles({ | ||
contentWrapper: { | ||
display: 'flex', | ||
alignItems: 'center', | ||
}, | ||
buttonWrapper: { | ||
cursor: 'pointer', | ||
}, | ||
}); | ||
|
||
export interface AttachDownloadProps { | ||
session: string; | ||
} | ||
|
||
const CaptureDownload = (props: AttachDownloadProps) => { | ||
const { session } = props; | ||
|
||
const classes = useStyles(); | ||
|
||
const [downloadId, setDownloadId] = useState(''); | ||
const [isSelected, setIsSelected] = useState(false); | ||
|
||
useEffect( | ||
() => { | ||
return SelectedCaptureSessions.subscribe(session, setIsSelected); | ||
}, | ||
[session] | ||
); | ||
|
||
const handleStartDownload = async () => { | ||
try { | ||
const sessions = !SelectedCaptureSessions.isEmpty() | ||
? SelectedCaptureSessions.getAll() | ||
: [session]; | ||
|
||
const startDownloadRes = await startPcapDownload(sessions); | ||
|
||
setDownloadId(startDownloadRes.data.downloadID); | ||
SelectedCaptureSessions.reset(); | ||
} catch (err) { | ||
console.error(`An error occurred creating a PCAP download for session ${session}`, err); // eslint-disable-line | ||
toastNotifications.addDanger('An error initiating a download for the PCAP(s).'); | ||
return; | ||
} | ||
}; | ||
|
||
return ( | ||
<> | ||
<div className={classes.contentWrapper}> | ||
<div className={classes.buttonWrapper}> | ||
<EuiButtonIcon | ||
iconType="importAction" | ||
color="primary" | ||
onClick={handleStartDownload} | ||
aria-label="Download PCAPs" | ||
/> | ||
</div> | ||
<EuiCheckbox | ||
id={`download_session_${session}`} | ||
checked={isSelected} | ||
onChange={e => { | ||
const action = e.target.checked | ||
? SelectedCaptureSessions.add | ||
: SelectedCaptureSessions.remove; | ||
action(session); | ||
}} | ||
/> | ||
</div> | ||
<FileDownloadModal | ||
downloadId={downloadId} | ||
fileType="pcap" | ||
onClose={() => setDownloadId('')} | ||
/> | ||
</> | ||
); | ||
}; | ||
|
||
export default CaptureDownload; // eslint-disable-line |
Oops, something went wrong.