@edgedev/firebase 2.1.43 → 2.1.45
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 +42 -35
- package/package.json +1 -1
package/edgeFirebase.ts
CHANGED
|
@@ -2104,54 +2104,61 @@ export const EdgeFirebase = class {
|
|
|
2104
2104
|
}
|
|
2105
2105
|
|
|
2106
2106
|
try {
|
|
2107
|
-
|
|
2108
|
-
|
|
2109
|
-
|
|
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
|
-
}));
|
|
2107
|
+
let randomId = '';
|
|
2108
|
+
if (isPublic) {
|
|
2109
|
+
randomId = Math.random().toString(36).substring(2, 6) + '-';
|
|
2135
2110
|
}
|
|
2111
|
+
const tempFilePath = `${filePath.replaceAll('/', '-')}` + '/' + randomId + file.name;
|
|
2112
|
+
const storage = getStorage();
|
|
2113
|
+
const fileRef = ref(storage, tempFilePath);
|
|
2136
2114
|
|
|
2137
|
-
|
|
2138
|
-
|
|
2115
|
+
const metadata = {
|
|
2116
|
+
contentType: file.type,
|
|
2117
|
+
cacheControl: isPublic ? 'public, max-age=31536000' : undefined,
|
|
2118
|
+
};
|
|
2139
2119
|
|
|
2140
|
-
|
|
2141
|
-
|
|
2142
|
-
|
|
2143
|
-
|
|
2120
|
+
// Resumable upload
|
|
2121
|
+
const uploadTask = uploadBytesResumable(fileRef, file, metadata);
|
|
2122
|
+
|
|
2123
|
+
// Wrap the upload task in a Promise
|
|
2124
|
+
const uploadPromise = new Promise<actionResponse>((resolve, reject) => {
|
|
2125
|
+
uploadTask.on('state_changed',
|
|
2126
|
+
(snapshot) => {
|
|
2127
|
+
// Progress, pause, and resume events
|
|
2128
|
+
const progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
|
|
2129
|
+
console.log('Upload is ' + progress + '% done');
|
|
2130
|
+
},
|
|
2131
|
+
(error) => {
|
|
2132
|
+
// Handle unsuccessful uploads
|
|
2133
|
+
reject(this.sendResponse({
|
|
2134
|
+
success: false,
|
|
2135
|
+
message: "An error occurred during file upload.",
|
|
2136
|
+
meta: { error: error.message }
|
|
2137
|
+
}));
|
|
2138
|
+
},
|
|
2139
|
+
() => {
|
|
2140
|
+
// Handle successful uploads on complete
|
|
2141
|
+
resolve(this.sendResponse({
|
|
2142
|
+
success: true,
|
|
2143
|
+
message: "File uploaded successfully.",
|
|
2144
|
+
meta: { file: tempFilePath }
|
|
2145
|
+
}));
|
|
2146
|
+
}
|
|
2147
|
+
);
|
|
2144
2148
|
});
|
|
2145
2149
|
|
|
2150
|
+
return await uploadPromise;
|
|
2151
|
+
|
|
2146
2152
|
} catch (error) {
|
|
2147
2153
|
return this.sendResponse({
|
|
2148
2154
|
success: false,
|
|
2149
2155
|
message: "An error occurred during file upload.",
|
|
2150
|
-
meta: {}
|
|
2156
|
+
meta: { error: error.message }
|
|
2151
2157
|
});
|
|
2152
2158
|
}
|
|
2153
2159
|
};
|
|
2154
2160
|
|
|
2161
|
+
|
|
2155
2162
|
public deleteFile = async (filePath: string): Promise<actionResponse> => {
|
|
2156
2163
|
let hasDeletePermission = await this.permissionCheck("write", filePath);
|
|
2157
2164
|
if (filePath.substring(0, 6) === 'public') {
|