@@ -7,16 +7,15 @@ import GridList from "../../components/GridList";
77import Hash from "../../components/Hash" ;
88import CopyPasteMenu from "../../components/CopyPasteMenu" ;
99import TextField from "@material-ui/core/TextField" ;
10- import CryptoJs from "crypto-js/crypto-js" ;
11- import { FileDataReader } from "../../utils/FileDataReader" ;
12- import { CryptoCalculator } from "../../utils/CryptoCalculator" ;
10+ import FileDataReader from "../../utils/FileDataReader" ;
1311import BackButton from "../../components/BackButton" ;
1412import CsvExport from "../../components/CsvExport" ;
1513import { useHistory } from "react-router" ;
1614import { setActiveListItem } from "../../reducers/MainReducer/Actions" ;
1715import { MainContext } from "../../contexts/MainContextProvider" ;
1816import { CryptoContext } from "../../contexts/CryptoContextReducer" ;
1917import { setCurrentFile , setFileHashes } from "../../reducers/CryptoReducer/Actions" ;
18+ import Loadingbar from "../../components/Loadingbar" ;
2019
2120const useStyles = makeStyles ( theme => ( {
2221 heroContent : {
@@ -29,6 +28,7 @@ const useStyles = makeStyles(theme => ({
2928 } ,
3029 paper : {
3130 padding : theme . spacing ( 2 ) ,
31+ marginBottom : theme . spacing ( 1 ) ,
3232 display : 'flex' ,
3333 overflow : 'auto' ,
3434 flexDirection : 'column'
@@ -50,12 +50,6 @@ const File = () => {
5050
5151 const hashes = crypto . fileHashes ;
5252 const file = crypto . currentFile ;
53- const language = state . languages [ state . languageIndex ] ;
54-
55- const [ compare , setCompare ] = useState ( false ) ;
56- const [ compareHash , setCompareHash ] = useState ( "" ) ;
57-
58- const fileRef = useRef ( null ) ;
5953
6054 const md5 = crypto . md5 ;
6155 const sha1 = crypto . sha1 ;
@@ -66,6 +60,14 @@ const File = () => {
6660 const sha512 = crypto . sha512 ;
6761 const ripemd160 = crypto . ripemd160 ;
6862
63+ const language = state . languages [ state . languageIndex ] ;
64+
65+ const [ compare , setCompare ] = useState ( false ) ;
66+ const [ compareHash , setCompareHash ] = useState ( "" ) ;
67+ const [ loading , setLoading ] = useState ( false ) ;
68+
69+ const fileRef = useRef ( null ) ;
70+
6971 const history = useHistory ( ) ;
7072 const classes = useStyles ( ) ;
7173
@@ -105,21 +107,21 @@ const File = () => {
105107
106108 /**
107109 * Calculate the hashes of a specific file
108- * @returns {Promise<void> }
109110 */
110- const calculateHashes = async ( ) => {
111+ const calculateHashes = ( e ) => {
112+ if ( e ) e . preventDefault ( ) ;
111113 if ( ! file || file . length === 0 ) return ;
112114
113115 d2 ( setFileHashes ( null ) ) ;
116+ setLoading ( true ) ;
114117
115- const data = await FileDataReader ( file ) ;
116- if ( ! data || data . length === 0 ) return ;
117-
118- const encoded = CryptoJs . enc . Latin1 . parse ( data ) ;
119- let newHashes = CryptoCalculator ( encoded , md5 , sha1 , sha224 , sha256 , sha3 , sha384 , sha512 , ripemd160 ) ;
120-
121- if ( newHashes . length === 0 ) newHashes = null ;
122- d2 ( setFileHashes ( newHashes ) ) ;
118+ FileDataReader ( file , md5 , sha1 , sha224 , sha256 , sha3 , sha384 , sha512 , ripemd160 )
119+ . then ( data => {
120+ d2 ( setFileHashes ( data ) ) ;
121+ } )
122+ . finally ( ( ) => {
123+ setLoading ( false ) ;
124+ } ) ;
123125 } ;
124126
125127 /**
@@ -146,7 +148,7 @@ const File = () => {
146148 * Change the currently selected file
147149 * @param event The event that called this function
148150 */
149- const onFileChange = function ( event ) {
151+ const onFileChange = ( event ) => {
150152 d2 ( setCurrentFile ( event . target . files [ 0 ] ) ) ;
151153 fileRef . current . value = "" ;
152154 } ;
@@ -179,7 +181,9 @@ const File = () => {
179181 < Paper className = { classes . paper } >
180182 < TextField
181183 margin = "normal"
182- onClick = { ( ) => fileRef . current . click ( ) }
184+ onClick = { ( ) => {
185+ if ( ! loading ) fileRef . current . click ( )
186+ } }
183187 disabled
184188 id = "filled-disabled"
185189 value = { file && file . path ? file . path : "" }
@@ -190,7 +194,7 @@ const File = () => {
190194 < input ref = { fileRef } type = "file" onChange = { onFileChange }
191195 style = { { display : 'none' } } />
192196
193- < Button color = { "primary" } variant = { "contained" }
197+ < Button color = { "primary" } variant = { "contained" } disabled = { loading }
194198 onClick = { ( ) => fileRef . current . click ( ) } > { language . select } </ Button >
195199
196200 < FormControlLabel
@@ -206,6 +210,7 @@ const File = () => {
206210 />
207211 { compareField }
208212 </ Paper >
213+ { loading ? < Loadingbar /> : null }
209214 { hashes && hashes . length > 0 ? (
210215 < >
211216 < Button className = { classes . button } color = { "primary" } variant = { "contained" }
@@ -223,8 +228,8 @@ const File = () => {
223228
224229 ) : null }
225230 < Button className = { classes . button } color = { "primary" } variant = { "contained" }
226- disabled = { ! file || file . length === 0 }
227- style = { { float : 'right' } } onClick = { async ( ) => calculateHashes ( ) } >
231+ disabled = { ! file || file . length === 0 || loading }
232+ style = { { float : 'right' } } onClick = { calculateHashes } >
228233 { language . calculate }
229234 </ Button >
230235 { output }
0 commit comments