1+ import React from 'react' ;
2+ import '../Css/Recieve.css' ;
3+ import firebase from 'firebase/app' ;
4+ import 'firebase/storage' ;
5+ import '../init-firebase' ;
6+ 7+ class Recieve extends React . Component {
8+ constructor ( ) {
9+ super ( ) ;
10+ this . state = {
11+ PROGRESS :0 ,
12+ FILES :[ ]
13+ }
14+ }
15+ download = ( ) => {
16+ let files = this . state . FILES
17+ let len = files . length ;
18+ let step = ( 100 / len ) . toFixed ( 10 )
19+ this . setState ( { PROGRESS : parseFloat ( 0 ) } )
20+ if ( len > 0 ) {
21+ let i ;
22+ for ( i = 0 ; i < len ; i ++ ) {
23+ const storageRef = firebase . storage ( ) . ref ( ) ;
24+ var starsRef = storageRef . child ( files [ i ] ) ;
25+ starsRef . getDownloadURL ( )
26+ . then ( ( url ) => {
27+ let oldval = this . state . PROGRESS
28+ this . setState ( { PROGRESS : parseFloat ( oldval ) + parseFloat ( step ) } )
29+ document . getElementById ( 'input' ) . value = '' ;
30+ console . log ( url )
31+ // let res = url.split("?");
32+ // let res1 = res[0].split(".");
33+ // let num = i
34+ // let filename = 'file'+String(num+1)+'.'+String(res1[res1.length-1])
35+ let element = document . createElement ( 'a' ) ;
36+ element . setAttribute ( 'href' , url ) ;
37+ element . setAttribute ( 'target' , "_blank" ) ;
38+ // element.setAttribute('download', filename);
39+ document . body . appendChild ( element ) ;
40+ element . click ( ) ;
41+ document . body . removeChild ( element ) ;
42+ 43+ } ) . catch ( ( error ) => {
44+ console . log ( error )
45+ document . getElementById ( "alertRecieve" ) . style . display = "block"
46+ document . getElementById ( "alertRecieve" ) . innerHTML = "Unable to Download File"
47+ setTimeout ( function ( ) {
48+ document . getElementById ( "alertRecieve" ) . style . display = "none" ;
49+ } , 3000 ) ;
50+ } ) ;
51+ }
52+ }
53+ }
54+ checkFiles = ( ) => {
55+ let text = document . getElementById ( 'input' ) . value
56+ if ( text . length > 0 ) {
57+ console . log ( 'true' )
58+ const storageRef = firebase . storage ( ) . ref ( ) ;
59+ var listRef = storageRef . child ( text + "/" ) ;
60+ listRef . listAll ( ) . then ( ( res ) => {
61+ res . items . forEach ( ( itemRef ) => {
62+ this . state . FILES . push ( itemRef . location . path )
63+ } ) ;
64+ } )
65+ . then ( ( ) => {
66+ if ( this . state . FILES . length > 0 ) {
67+ this . download ( )
68+ }
69+ else {
70+ document . getElementById ( "alertRecieve" ) . style . display = "block"
71+ document . getElementById ( "alertRecieve" ) . innerHTML = "Either the name you Entered is wrong or the item has expired"
72+ setTimeout ( function ( ) {
73+ document . getElementById ( "alertRecieve" ) . style . display = "none" ;
74+ } , 4000 ) ;
75+ }
76+ } )
77+ . catch ( function ( error ) {
78+ document . getElementById ( "alertRecieve" ) . style . display = "block"
79+ document . getElementById ( "alertRecieve" ) . innerHTML = "We faced some error while searching for your files please try again!!"
80+ setTimeout ( function ( ) {
81+ document . getElementById ( "alertRecieve" ) . style . display = "none" ;
82+ } , 4000 ) ;
83+ } ) ;
84+ }
85+ else {
86+ document . getElementById ( "alertRecieve" ) . style . display = "block"
87+ document . getElementById ( "alertRecieve" ) . innerHTML = "Enter a valid Name"
88+ setTimeout ( function ( ) {
89+ document . getElementById ( "alertRecieve" ) . style . display = "none" ;
90+ } , 1500 ) ;
91+ }
92+ }
93+ inputHighlight = ( ) => {
94+ let input = document . getElementById ( "input" ) ;
95+ input . style . border = "none" ;
96+ input . style . borderBottom = "1px solid #6125ac" ;
97+ }
98+ render ( ) {
99+ return (
100+ < div className = "Recieve" >
101+ < div className = "imgPanel" >
102+ < img src = { require ( "./image/recieve.jpg" ) } width = { "600px" } alt = "sharing file" className = "recieveImage" />
103+ </ div >
104+ < div className = "ContentPanel" >
105+ < div className = "contentPanelElements" >
106+ < img src = { require ( './image/download.png' ) } width = '200px' alt = "download symbol" className = "downloadImage" />
107+ < progress id = "statusIndicator" value = { this . state . PROGRESS } max = "100" />
108+ < input type = "text" placeholder = "Enter Download Code" id = "input" autoComplete = "off" onFocus = { this . inputHighlight } />
109+ < button id = "download" onClick = { this . checkFiles } > Download</ button >
110+ </ div >
111+ </ div >
112+ < div color = "danger" id = "alertRecieve" >
113+ </ div >
114+ </ div >
115+ ) ;
116+ }
117+ }
118+ 119+ export default Recieve ;
0 commit comments