-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileUpload.js
54 lines (41 loc) · 1.63 KB
/
FileUpload.js
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
import { getDownloadURL, ref, uploadBytes, getStorage } from 'firebase/storage';
import { Alert } from 'react-native';
import mime from "mime";
import firebaseConfig from './firebaseConfig';
const storage = getStorage();
export async function uploadFiles(fileUris, userId) {
try {
const uploadPromises = fileUris.map(async (uri, index) => {
const response = await fetch(uri);
const blobFile = await response.blob();
const mimeType = mime.getType(uri);
const path = `files/${userId}/${new Date().getTime()}_${index}.${mimeType.split("/")[1]}`;
const reference = ref(storage, path);
const result = await uploadBytes(reference, blobFile, { contentType: mimeType });
const url = await getDownloadURL(result.ref);
return url;
});
const uploadedFiles = await Promise.all(uploadPromises);
return uploadedFiles;
} catch (err) {
return Promise.reject(err);
}
}
export async function uploadFiles2(fileData, userId) {
try {
const uploadPromises = fileData.map(async ([uri, name, size]) => {
const response = await fetch(uri);
const blobFile = await response.blob();
const mimeType = mime.getType(uri);
const path = `files/${userId}/${new Date().getTime()}_${name}`;
const reference = ref(storage, path);
const result = await uploadBytes(reference, blobFile, { contentType: mimeType });
const url = await getDownloadURL(result.ref);
return { downloadURL: url, name, size };
});
const uploadedFiles = await Promise.all(uploadPromises);
return uploadedFiles;
} catch (err) {
return Promise.reject(err);
}
}