-
Notifications
You must be signed in to change notification settings - Fork 183
Expand file tree
/
Copy pathFireClient.ts
More file actions
141 lines (132 loc) · 4.05 KB
/
FireClient.ts
File metadata and controls
141 lines (132 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import { doc } from 'firebase/firestore';
import get from 'lodash/get';
import set from 'lodash/set';
import {
AddCreatedByFields,
AddUpdatedByFields,
dispatch,
IFirestoreLogger,
log,
logError,
parseStoragePath,
translateDocToFirestore,
} from '../../misc';
import {
TASK_CANCELED,
TASK_PAUSED,
TASK_RUNNING,
} from '../../misc/firebase-models';
import { RAFirebaseOptions } from '../options';
import { IFirebaseWrapper } from './firebase/IFirebaseWrapper';
import { IResource, ResourceManager } from './ResourceManager';
export class FireClient {
public rm: ResourceManager;
constructor(
public fireWrapper: IFirebaseWrapper,
public options: RAFirebaseOptions,
public flogger: IFirestoreLogger
) {
this.rm = new ResourceManager(this.fireWrapper, this.options, this.flogger);
}
public checkRemoveIdField(obj: any, docId: string) {
if (!this.options.dontAddIdFieldToDoc) {
obj.id = docId;
}
}
public transformToDb(
resourceName: string,
documentData: any,
docId: string
): any {
if (typeof this.options.transformToDb === 'function') {
return this.options.transformToDb(resourceName, documentData, docId);
}
return documentData;
}
public async parseDataAndUpload(r: IResource, id: string, data: any) {
if (!data) {
return data;
}
const docPath = doc(r.collection, id).path;
const result = translateDocToFirestore(data);
const uploads = result.uploads;
await Promise.all(
uploads.map(async (u) => {
const storagePath = parseStoragePath(
u.rawFile,
docPath,
u.fieldDotsPath,
!!this.options.useFileNamesInStorage
);
const link = await this.saveFile(storagePath, u.rawFile);
set(data, u.fieldDotsPath + '.src', link);
})
);
return data;
}
public async addCreatedByFields(obj: any) {
return AddCreatedByFields(obj, this.fireWrapper, this.rm, this.options);
}
public async addUpdatedByFields(obj: any) {
return AddUpdatedByFields(obj, this.fireWrapper, this.rm, this.options);
}
private async saveFile(
storagePath: string,
rawFile: any
): Promise<string | undefined> {
log('saveFile() saving file...', { storagePath, rawFile });
try {
const { task, taskResult, downloadUrl } = this.fireWrapper.putFile(
storagePath,
rawFile
);
const { name } = rawFile;
// monitor upload status & progress
dispatch('FILE_UPLOAD_WILL_START', name);
task.on('state_changed', (snapshot) => {
const progress =
(snapshot.bytesTransferred / snapshot.totalBytes) * 100;
log('Upload is ' + progress + '% done');
dispatch('FILE_UPLOAD_PROGRESS', name, progress);
switch (snapshot.state) {
case TASK_PAUSED:
log('Upload is paused');
dispatch('FILE_UPLOAD_PAUSED', name);
break;
case TASK_RUNNING:
log('Upload is running');
dispatch('FILE_UPLOAD_RUNNING', name);
break;
case TASK_CANCELED:
log('Upload has been canceled');
dispatch('FILE_UPLOAD_CANCELED', name);
break;
// case storage.TaskState.ERROR:
// already handled by catch
// case storage.TaskState.SUCCESS:
// already handled by then
}
});
const [getDownloadURL] = await Promise.all([downloadUrl, taskResult]);
dispatch('FILE_UPLOAD_COMPLETE', name);
dispatch('FILE_SAVED', name);
log('saveFile() saved file', {
storagePath,
taskResult,
getDownloadURL,
});
return this.options.relativeFilePaths ? storagePath : getDownloadURL;
} catch (storageError) {
if (get(storageError, 'code') === 'storage/unknown') {
logError(
'saveFile() error saving file, No bucket found! Try clicking "Get Started" in firebase -> storage',
{ storageError }
);
} else {
logError('saveFile() error saving file', {
storageError,
});
}
}
}
}