@edgedev/firebase 2.1.44 → 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 +63 -57
- package/package.json +1 -1
package/edgeFirebase.ts
CHANGED
|
@@ -2082,76 +2082,82 @@ export const EdgeFirebase = class {
|
|
|
2082
2082
|
|
|
2083
2083
|
// Validate if file is provided
|
|
2084
2084
|
if (!file) {
|
|
2085
|
-
|
|
2086
|
-
|
|
2087
|
-
|
|
2088
|
-
|
|
2089
|
-
|
|
2085
|
+
return this.sendResponse({
|
|
2086
|
+
success: false,
|
|
2087
|
+
message: "No file provided for upload.",
|
|
2088
|
+
meta: {}
|
|
2089
|
+
});
|
|
2090
2090
|
}
|
|
2091
|
-
|
|
2091
|
+
|
|
2092
2092
|
// Check if the user has write permission to the filePath
|
|
2093
2093
|
let hasWritePermission = await this.permissionCheck("write", filePath);
|
|
2094
2094
|
if (isPublic) {
|
|
2095
|
-
|
|
2096
|
-
|
|
2095
|
+
hasWritePermission = true;
|
|
2096
|
+
filePath = "public";
|
|
2097
2097
|
}
|
|
2098
2098
|
if (!hasWritePermission) {
|
|
2099
|
-
|
|
2100
|
-
|
|
2101
|
-
|
|
2102
|
-
|
|
2103
|
-
|
|
2099
|
+
return this.sendResponse({
|
|
2100
|
+
success: false,
|
|
2101
|
+
message: "You do not have permission to upload files to this path.",
|
|
2102
|
+
meta: {}
|
|
2103
|
+
});
|
|
2104
2104
|
}
|
|
2105
|
-
|
|
2105
|
+
|
|
2106
2106
|
try {
|
|
2107
|
-
|
|
2108
|
-
|
|
2109
|
-
|
|
2110
|
-
|
|
2111
|
-
|
|
2112
|
-
|
|
2113
|
-
|
|
2114
|
-
|
|
2115
|
-
|
|
2116
|
-
|
|
2117
|
-
|
|
2118
|
-
|
|
2119
|
-
|
|
2120
|
-
|
|
2121
|
-
|
|
2122
|
-
|
|
2123
|
-
|
|
2107
|
+
let randomId = '';
|
|
2108
|
+
if (isPublic) {
|
|
2109
|
+
randomId = Math.random().toString(36).substring(2, 6) + '-';
|
|
2110
|
+
}
|
|
2111
|
+
const tempFilePath = `${filePath.replaceAll('/', '-')}` + '/' + randomId + file.name;
|
|
2112
|
+
const storage = getStorage();
|
|
2113
|
+
const fileRef = ref(storage, tempFilePath);
|
|
2114
|
+
|
|
2115
|
+
const metadata = {
|
|
2116
|
+
contentType: file.type,
|
|
2117
|
+
cacheControl: isPublic ? 'public, max-age=31536000' : undefined,
|
|
2118
|
+
};
|
|
2119
|
+
|
|
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) => {
|
|
2124
2125
|
uploadTask.on('state_changed',
|
|
2125
|
-
|
|
2126
|
-
|
|
2127
|
-
|
|
2128
|
-
|
|
2129
|
-
|
|
2130
|
-
|
|
2131
|
-
|
|
2132
|
-
|
|
2133
|
-
|
|
2134
|
-
|
|
2135
|
-
|
|
2136
|
-
|
|
2137
|
-
|
|
2138
|
-
|
|
2139
|
-
|
|
2140
|
-
|
|
2141
|
-
|
|
2142
|
-
|
|
2143
|
-
|
|
2144
|
-
|
|
2145
|
-
|
|
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
|
+
}
|
|
2146
2147
|
);
|
|
2148
|
+
});
|
|
2149
|
+
|
|
2150
|
+
return await uploadPromise;
|
|
2151
|
+
|
|
2147
2152
|
} catch (error) {
|
|
2148
|
-
|
|
2149
|
-
|
|
2150
|
-
|
|
2151
|
-
|
|
2152
|
-
|
|
2153
|
+
return this.sendResponse({
|
|
2154
|
+
success: false,
|
|
2155
|
+
message: "An error occurred during file upload.",
|
|
2156
|
+
meta: { error: error.message }
|
|
2157
|
+
});
|
|
2153
2158
|
}
|
|
2154
2159
|
};
|
|
2160
|
+
|
|
2155
2161
|
|
|
2156
2162
|
public deleteFile = async (filePath: string): Promise<actionResponse> => {
|
|
2157
2163
|
let hasDeletePermission = await this.permissionCheck("write", filePath);
|