Skip to content

Commit 5a2af1c

Browse files
committed
fixing some errors and remove coed leftovers
1 parent 5032e6d commit 5a2af1c

File tree

2 files changed

+82
-104
lines changed

2 files changed

+82
-104
lines changed

src/pages/add-phrase/add-phrase.ts

+16-29
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ export class AddPhrasePage {
6161
private _curserPosition;
6262
private _nikudArray = Enums.NIKUD;
6363

64-
imageURL;
64+
imageURL: string = null;
6565
micText = START_REC;
66-
audioFileURL;
66+
audioFileURL: string = null;
6767

6868
//for the radio button sections
6969
sentenceOrNoun;
@@ -295,24 +295,23 @@ export class AddPhrasePage {
295295
correctOrientation: true
296296
};
297297

298-
let user = this.authentication.user.email;
299-
const imageFolder = "/images/";
298+
300299
const im_path = await this.camera.getPicture(options);//get the path to the image
301-
300+
302301
const im_type = 'data:image/jpeg;base64,';
303302
let promise = await this.storageProvider.uploadFileByPath(im_path, im_type);
304-
let res = new Promise ((resolve,reject) => {
303+
304+
305+
let res = new Promise((resolve, reject) => {
305306
resolve(promise);
306307
});
307308

308309
//Read the data from the promise
309-
res.then((data)=>{
310-
debugger
311-
this.imageURL = data;
310+
res.then((data) => {
311+
this.imageURL = data.toString();
312312
this._myForm.patchValue({ 'imagePath': this.imageURL });//insert the capture image path to the form
313-
314313
})
315-
314+
316315

317316
} catch (err) {
318317
this.errorProvider.alert("לא הצלחנו לבחור תמונה....", err);
@@ -366,25 +365,14 @@ export class AddPhrasePage {
366365

367366
/*
368367
this.timer.startTimer();
369-
let data = this._audioRecordProvider.startRecord();
370-
371-
if (data instanceof Error) {
372-
this.recording = false;
373-
this.micText = START_REC;
374-
this.errorProvider.alert("לא הצלחנו לבצע הקלטה....", data.message);
375-
} else {
376-
this.fileName = data;
377368
}*/
378369
}
379370

380371
//stop the record and save the audio file on local variable
381372
stopRecord() {
382-
if (this.recording)
383-
{
373+
if (this.recording) {
384374
this.micText = START_REC;
385375
this.recording = !this.recording;
386-
let user = this.authentication.user.email;
387-
const audioFolder = "/audio/";
388376

389377
this.audio.stopRecord();
390378
// save the new audio file to the storage
@@ -395,18 +383,17 @@ export class AddPhrasePage {
395383
// fix the encoding
396384
const audio_path = base64File.slice(base64File.indexOf(',') + 1, base64File.length);
397385
const audio_type = 'data:audio/mp3;base64,'
398-
386+
399387
let promise = await this.storageProvider.uploadFileByPath(audio_path, audio_type);
400-
let res = new Promise ((resolve,reject) => {
388+
let res = new Promise((resolve, reject) => {
401389
resolve(promise);
402390
});
403-
res.then((data)=>{
404-
debugger
405-
this.audioFileURL = data;
391+
res.then((data) => {
392+
this.audioFileURL = data.toString();
406393
this._myForm.patchValue({ 'audioFile': this.audioFileURL });//insert the recorded audio file path to the form
407394

408395
})
409-
396+
410397
}, (err) => {
411398
console.log(err);
412399
});

src/providers/storage/storage.ts

+66-75
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export class StorageProvider {
2626

2727
}
2828

29+
//TODO: should be deleted???
2930
/* This function called from file browser with an event of calling
3031
* And uploads the selected file to the storage.
3132
*
@@ -43,84 +44,76 @@ export class StorageProvider {
4344
* TODO: Should add error-dialog and percentage bar to the user.
4445
*/
4546

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).
5348
* How does it work?
5449
* This function checks if the type is image, if it is then it uploads it to
5550
* <email>/images/<name_of_file>.jpg on the storage.
5651
* same with audio file.
5752
*
5853
* This function also updates the progress of the upload
5954
* 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
6059
*/
6160
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+
})
124117
}
125118

126119
//this function creates random file-name (without extention) based on time.
@@ -140,16 +133,14 @@ export class StorageProvider {
140133
return this.audioUploadPercentage;
141134
}
142135

143-
public showImageProgressBar()
144-
{
145-
if(this.imageUploadPercentage == 0 || this.imageUploadPercentage == 100)
136+
public showImageProgressBar() {
137+
if (this.imageUploadPercentage == 0 || this.imageUploadPercentage == 100)
146138
return false;
147139
return true;
148140
}
149141

150-
public showAudioProgressBar()
151-
{
152-
if(this.audioUploadPercentage == 0 || this.audioUploadPercentage == 100)
142+
public showAudioProgressBar() {
143+
if (this.audioUploadPercentage == 0 || this.audioUploadPercentage == 100)
153144
return false;
154145
return true;
155146
}

0 commit comments

Comments
 (0)