@edgedev/firebase 2.1.42 → 2.1.44

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.
Files changed (2) hide show
  1. package/edgeFirebase.ts +54 -53
  2. package/package.json +1 -1
package/edgeFirebase.ts CHANGED
@@ -2077,81 +2077,82 @@ export const EdgeFirebase = class {
2077
2077
  };
2078
2078
 
2079
2079
 
2080
- // File functions
2081
2080
  // File functions
2082
2081
  public uploadFile = async (filePath: string, file: Blob, isPublic: boolean): Promise<actionResponse> => {
2083
2082
 
2084
2083
  // Validate if file is provided
2085
2084
  if (!file) {
2086
- return this.sendResponse({
2087
- success: false,
2088
- message: "No file provided for upload.",
2089
- meta: {}
2090
- });
2085
+ return this.sendResponse({
2086
+ success: false,
2087
+ message: "No file provided for upload.",
2088
+ meta: {}
2089
+ });
2091
2090
  }
2092
2091
 
2093
2092
  // Check if the user has write permission to the filePath
2094
2093
  let hasWritePermission = await this.permissionCheck("write", filePath);
2095
2094
  if (isPublic) {
2096
- hasWritePermission = true;
2097
- filePath = "public";
2095
+ hasWritePermission = true;
2096
+ filePath = "public";
2098
2097
  }
2099
2098
  if (!hasWritePermission) {
2100
- return this.sendResponse({
2101
- success: false,
2102
- message: "You do not have permission to upload files to this path.",
2103
- meta: {}
2104
- });
2099
+ return this.sendResponse({
2100
+ success: false,
2101
+ message: "You do not have permission to upload files to this path.",
2102
+ meta: {}
2103
+ });
2105
2104
  }
2106
2105
 
2107
2106
  try {
2108
- const chunkSize = 5 * 1024 * 1024; // 5 MB per chunk
2109
- const totalChunks = Math.ceil(file.size / chunkSize);
2110
- const uploadPath = `${filePath.replaceAll('/', '-')}/${file.name}`;
2111
- const chunkPromises = [];
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
- // Progress tracking (optional)
2127
- const progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
2128
- console.log(`Upload is ${progress}% done`);
2129
- },
2130
- (error) => reject(error),
2131
- () => resolve(uploadTask.snapshot)
2132
- );
2133
- }));
2134
- }
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);
2135
2114
 
2136
- // Wait for all chunks to finish uploading
2137
- await Promise.all(chunkPromises);
2115
+ const metadata = {
2116
+ contentType: file.type,
2117
+ cacheControl: isPublic ? 'public, max-age=31536000' : undefined,
2118
+ };
2138
2119
 
2139
- return this.sendResponse({
2140
- success: true,
2141
- message: "File uploaded successfully in chunks.",
2142
- meta: {file: uploadPath}
2143
- });
2120
+ // Resumable upload
2121
+ const uploadTask = uploadBytesResumable(fileRef, file, metadata);
2144
2122
 
2123
+ // Monitor progress and handle completion
2124
+ uploadTask.on('state_changed',
2125
+ (snapshot) => {
2126
+ // Progress, pause, and resume events
2127
+ const progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
2128
+ console.log('Upload is ' + progress + '% done');
2129
+ },
2130
+ (error) => {
2131
+ // Handle unsuccessful uploads
2132
+ return this.sendResponse({
2133
+ success: false,
2134
+ message: "An error occurred during file upload.",
2135
+ meta: { error: error.message }
2136
+ });
2137
+ },
2138
+ () => {
2139
+ // Handle successful uploads on complete
2140
+ return this.sendResponse({
2141
+ success: true,
2142
+ message: "File uploaded successfully.",
2143
+ meta: { file: tempFilePath }
2144
+ });
2145
+ }
2146
+ );
2145
2147
  } catch (error) {
2146
- return this.sendResponse({
2147
- success: false,
2148
- message: "An error occurred during file upload.",
2149
- meta: {}
2150
- });
2148
+ return this.sendResponse({
2149
+ success: false,
2150
+ message: "An error occurred during file upload.",
2151
+ meta: { error: error.message }
2152
+ });
2151
2153
  }
2152
2154
  };
2153
2155
 
2154
-
2155
2156
  public deleteFile = async (filePath: string): Promise<actionResponse> => {
2156
2157
  let hasDeletePermission = await this.permissionCheck("write", filePath);
2157
2158
  if (filePath.substring(0, 6) === 'public') {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@edgedev/firebase",
3
- "version": "2.1.42",
3
+ "version": "2.1.44",
4
4
  "description": "Vue 3 / Nuxt 3 Plugin or Nuxt 3 plugin for firebase authentication and firestore.",
5
5
  "main": "index.ts",
6
6
  "scripts": {