@@ -26,6 +26,7 @@ export class StorageProvider {
26
26
27
27
}
28
28
29
+ //TODO: should be deleted???
29
30
/* This function called from file browser with an event of calling
30
31
* And uploads the selected file to the storage.
31
32
*
@@ -43,84 +44,76 @@ export class StorageProvider {
43
44
* TODO: Should add error-dialog and percentage bar to the user.
44
45
*/
45
46
46
- /* This function called from the add-phrase form (you can use it from different pages also).
47
- * The functions gets:
48
- * path: The native path of the fine on the device.
49
- * type: The type of the file (Example: 'data:audio/mp3;base64' for audio file)
50
- * form: form object (Not nessecary if you dont want to use). - default is NULL.
51
- * The form value just updates the file object string.
52
- *
47
+ /** This function called from the add-phrase form (you can use it from different pages also).
53
48
* How does it work?
54
49
* This function checks if the type is image, if it is then it uploads it to
55
50
* <email>/images/<name_of_file>.jpg on the storage.
56
51
* same with audio file.
57
52
*
58
53
* This function also updates the progress of the upload
59
54
* for each image and audio seperatly.
55
+ *
56
+ * @param path The native path of the fine on the device.
57
+ * @param type The type of the file (Example: 'data:audio/mp3;base64' for audio file)
58
+ * @returns a new promise with the new url, on error - undefine
60
59
*/
61
60
public uploadFileByPath ( path , type ) {
62
- return new Promise ( ( resolve , reject ) =>
63
- {
64
-
65
-
66
- let user = this . authentication . afAuth . auth . currentUser . email
67
- let user1 = this . authentication . user . email ;
68
-
69
- //if the file is an image file
70
- if ( type . includes ( "image" ) ) {
71
- this . imageUploadPercentage = 0
72
- const imageFolder = "/images/" ;
73
- let storage_path = user + imageFolder + this . createFileName ( ) + ".jpg" ; //create the path on the storage
74
-
75
- this . image_ref = firebase . storage ( ) . ref ( storage_path ) ;
76
-
77
- let task = this . image_ref . putString ( type + path , "data_url" )
78
-
79
- task . on (
80
- firebase . storage . TaskEvent . STATE_CHANGED ,
81
- ( snapshot : firebase . storage . UploadTaskSnapshot ) => {
82
-
83
- this . imageUploadPercentage = Math . round ( ( snapshot . bytesTransferred / snapshot . totalBytes ) * 100 )
84
- }
85
- ) ;
86
-
87
- task . then ( url => {
88
- this . imageDownloadURL = url . downloadURL ;
89
- console . log ( this . imageDownloadURL ) ;
90
- resolve ( this . imageDownloadURL )
91
-
92
- } )
93
-
94
- }
95
- //if the file is an audio file
96
- else if ( type . includes ( "audio" ) ) {
97
- this . audioUploadPercentage = 0
98
- const audioFolder = "/audio/" ;
99
-
100
- let storage_path = user + audioFolder + this . createFileName ( ) + ".mp3" ; //create the path on the storage
101
- const task = firebase . storage ( ) . ref ( storage_path ) . putString ( type + path , "data_url" )
102
-
103
- task . on (
104
- firebase . storage . TaskEvent . STATE_CHANGED ,
105
- ( snapshot : firebase . storage . UploadTaskSnapshot ) => {
106
- this . audioUploadPercentage = Math . round ( ( snapshot . bytesTransferred / snapshot . totalBytes ) * 100 )
107
- }
108
- ) ;
109
-
110
- task . then ( async url => {
111
- this . audioDownloadURL = url . downloadURL ;
112
- console . log ( this . audioDownloadURL ) ;
113
- resolve ( this . audioDownloadURL )
114
- } ) ;
115
-
116
-
117
- }
118
-
119
- else {
120
- console . log ( "ERROR UPLOADING - UNKNOWN TYPE OF FILE" ) ;
121
- reject ( "null" )
122
- }
123
- } )
61
+ return new Promise ( ( resolve , reject ) => {
62
+ let user = this . authentication . afAuth . auth . currentUser . email
63
+
64
+ //if the file is an image file
65
+ if ( type . includes ( "image" ) ) {
66
+ this . imageUploadPercentage = 0
67
+ const imageFolder = "/images/" ;
68
+ let storage_path = user + imageFolder + this . createFileName ( ) + ".jpg" ; //create the path on the storage
69
+
70
+ this . image_ref = firebase . storage ( ) . ref ( storage_path ) ;
71
+
72
+ let task = this . image_ref . putString ( type + path , "data_url" )
73
+
74
+ task . on (
75
+ firebase . storage . TaskEvent . STATE_CHANGED ,
76
+ ( snapshot : firebase . storage . UploadTaskSnapshot ) => {
77
+
78
+ this . imageUploadPercentage = Math . round ( ( snapshot . bytesTransferred / snapshot . totalBytes ) * 100 )
79
+ }
80
+ ) ;
81
+
82
+ task . then ( url => {
83
+ this . imageDownloadURL = url . downloadURL ;
84
+ resolve ( this . imageDownloadURL )
85
+
86
+ } )
87
+
88
+ }
89
+ //if the file is an audio file
90
+ else if ( type . includes ( "audio" ) ) {
91
+ this . audioUploadPercentage = 0
92
+ const audioFolder = "/audio/" ;
93
+
94
+ let storage_path = user + audioFolder + this . createFileName ( ) + ".mp3" ; //create the path on the storage
95
+ const task = firebase . storage ( ) . ref ( storage_path ) . putString ( type + path , "data_url" )
96
+
97
+ task . on (
98
+ firebase . storage . TaskEvent . STATE_CHANGED ,
99
+ ( snapshot : firebase . storage . UploadTaskSnapshot ) => {
100
+ this . audioUploadPercentage = Math . round ( ( snapshot . bytesTransferred / snapshot . totalBytes ) * 100 )
101
+ }
102
+ ) ;
103
+
104
+ task . then ( async url => {
105
+ this . audioDownloadURL = url . downloadURL ;
106
+ resolve ( this . audioDownloadURL )
107
+ } ) ;
108
+
109
+
110
+ }
111
+
112
+ else {
113
+ console . log ( "ERROR UPLOADING - UNKNOWN TYPE OF FILE" ) ;
114
+ reject ( "null" )
115
+ }
116
+ } )
124
117
}
125
118
126
119
//this function creates random file-name (without extention) based on time.
@@ -140,16 +133,14 @@ export class StorageProvider {
140
133
return this . audioUploadPercentage ;
141
134
}
142
135
143
- public showImageProgressBar ( )
144
- {
145
- if ( this . imageUploadPercentage == 0 || this . imageUploadPercentage == 100 )
136
+ public showImageProgressBar ( ) {
137
+ if ( this . imageUploadPercentage == 0 || this . imageUploadPercentage == 100 )
146
138
return false ;
147
139
return true ;
148
140
}
149
141
150
- public showAudioProgressBar ( )
151
- {
152
- if ( this . audioUploadPercentage == 0 || this . audioUploadPercentage == 100 )
142
+ public showAudioProgressBar ( ) {
143
+ if ( this . audioUploadPercentage == 0 || this . audioUploadPercentage == 100 )
153
144
return false ;
154
145
return true ;
155
146
}
0 commit comments