@edgedev/firebase 2.1.41 → 2.1.43
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/edgeFirebase.ts +71 -48
- package/package.json +1 -1
package/edgeFirebase.ts
CHANGED
|
@@ -55,7 +55,7 @@ import {
|
|
|
55
55
|
} from "firebase/auth";
|
|
56
56
|
|
|
57
57
|
|
|
58
|
-
import { getStorage, ref,
|
|
58
|
+
import { getStorage, ref, uploadBytesResumable, getDownloadURL, connectStorageEmulator, listAll, deleteObject} from "firebase/storage";
|
|
59
59
|
|
|
60
60
|
import { getFunctions, httpsCallable, connectFunctionsEmulator } from "firebase/functions";
|
|
61
61
|
|
|
@@ -2080,55 +2080,78 @@ export const EdgeFirebase = class {
|
|
|
2080
2080
|
// File functions
|
|
2081
2081
|
public uploadFile = async (filePath: string, file: Blob, isPublic: boolean): Promise<actionResponse> => {
|
|
2082
2082
|
|
|
2083
|
-
|
|
2084
|
-
|
|
2085
|
-
|
|
2086
|
-
|
|
2087
|
-
|
|
2088
|
-
|
|
2089
|
-
|
|
2090
|
-
|
|
2091
|
-
|
|
2092
|
-
|
|
2093
|
-
|
|
2094
|
-
|
|
2095
|
-
|
|
2096
|
-
|
|
2097
|
-
|
|
2098
|
-
|
|
2099
|
-
|
|
2100
|
-
|
|
2101
|
-
|
|
2102
|
-
|
|
2103
|
-
|
|
2104
|
-
|
|
2105
|
-
|
|
2106
|
-
|
|
2107
|
-
|
|
2108
|
-
|
|
2109
|
-
|
|
2110
|
-
|
|
2111
|
-
|
|
2112
|
-
|
|
2113
|
-
|
|
2114
|
-
|
|
2115
|
-
|
|
2116
|
-
|
|
2117
|
-
|
|
2118
|
-
|
|
2119
|
-
|
|
2120
|
-
|
|
2121
|
-
|
|
2122
|
-
|
|
2123
|
-
|
|
2124
|
-
|
|
2125
|
-
|
|
2126
|
-
|
|
2127
|
-
|
|
2128
|
-
|
|
2083
|
+
// Validate if file is provided
|
|
2084
|
+
if (!file) {
|
|
2085
|
+
return this.sendResponse({
|
|
2086
|
+
success: false,
|
|
2087
|
+
message: "No file provided for upload.",
|
|
2088
|
+
meta: {}
|
|
2089
|
+
});
|
|
2090
|
+
}
|
|
2091
|
+
|
|
2092
|
+
// Check if the user has write permission to the filePath
|
|
2093
|
+
let hasWritePermission = await this.permissionCheck("write", filePath);
|
|
2094
|
+
if (isPublic) {
|
|
2095
|
+
hasWritePermission = true;
|
|
2096
|
+
filePath = "public";
|
|
2097
|
+
}
|
|
2098
|
+
if (!hasWritePermission) {
|
|
2099
|
+
return this.sendResponse({
|
|
2100
|
+
success: false,
|
|
2101
|
+
message: "You do not have permission to upload files to this path.",
|
|
2102
|
+
meta: {}
|
|
2103
|
+
});
|
|
2104
|
+
}
|
|
2105
|
+
|
|
2106
|
+
try {
|
|
2107
|
+
const chunkSize = 5 * 1024 * 1024; // 5 MB per chunk
|
|
2108
|
+
const totalChunks = Math.ceil(file.size / chunkSize);
|
|
2109
|
+
const uploadPath = `${filePath.replaceAll('/', '-')}/${file.name}`;
|
|
2110
|
+
const chunkPromises = [];
|
|
2111
|
+
let totalBytesTransferred = 0;
|
|
2112
|
+
|
|
2113
|
+
for (let i = 0; i < totalChunks; i++) {
|
|
2114
|
+
const chunkStart = i * chunkSize;
|
|
2115
|
+
const chunkEnd = Math.min(chunkStart + chunkSize, file.size);
|
|
2116
|
+
const chunk = file.slice(chunkStart, chunkEnd);
|
|
2117
|
+
|
|
2118
|
+
const chunkFilePath = `${uploadPath}.part-${i + 1}`;
|
|
2119
|
+
const fileRef = ref(this.storage, chunkFilePath);
|
|
2120
|
+
const uploadTask = uploadBytesResumable(fileRef, chunk, { contentType: file.type });
|
|
2121
|
+
|
|
2122
|
+
chunkPromises.push(new Promise((resolve, reject) => {
|
|
2123
|
+
uploadTask.on(
|
|
2124
|
+
'state_changed',
|
|
2125
|
+
(snapshot) => {
|
|
2126
|
+
const chunkProgress = (snapshot.bytesTransferred / snapshot.totalBytes) * (chunkEnd - chunkStart);
|
|
2127
|
+
totalBytesTransferred += chunkProgress;
|
|
2128
|
+
const overallProgress = (totalBytesTransferred / file.size) * 100;
|
|
2129
|
+
console.log(`Upload is ${overallProgress.toFixed(2)}% done`);
|
|
2130
|
+
},
|
|
2131
|
+
(error) => reject(error),
|
|
2132
|
+
() => resolve(uploadTask.snapshot)
|
|
2133
|
+
);
|
|
2134
|
+
}));
|
|
2129
2135
|
}
|
|
2136
|
+
|
|
2137
|
+
// Wait for all chunks to finish uploading
|
|
2138
|
+
await Promise.all(chunkPromises);
|
|
2139
|
+
|
|
2140
|
+
return this.sendResponse({
|
|
2141
|
+
success: true,
|
|
2142
|
+
message: "File uploaded successfully in chunks.",
|
|
2143
|
+
meta: {file: uploadPath}
|
|
2144
|
+
});
|
|
2145
|
+
|
|
2146
|
+
} catch (error) {
|
|
2147
|
+
return this.sendResponse({
|
|
2148
|
+
success: false,
|
|
2149
|
+
message: "An error occurred during file upload.",
|
|
2150
|
+
meta: {}
|
|
2151
|
+
});
|
|
2152
|
+
}
|
|
2130
2153
|
};
|
|
2131
|
-
|
|
2154
|
+
|
|
2132
2155
|
public deleteFile = async (filePath: string): Promise<actionResponse> => {
|
|
2133
2156
|
let hasDeletePermission = await this.permissionCheck("write", filePath);
|
|
2134
2157
|
if (filePath.substring(0, 6) === 'public') {
|