@edgedev/firebase 2.1.40 → 2.1.42

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 +71 -47
  2. 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, uploadBytes, getDownloadURL, connectStorageEmulator, listAll, deleteObject} from "firebase/storage";
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
 
@@ -197,7 +197,7 @@ interface permissionStatus {
197
197
 
198
198
  interface User {
199
199
  userId: string;
200
- stagedDocId: string;
200
+ docId: string;
201
201
  }
202
202
 
203
203
  interface Meta {
@@ -818,7 +818,8 @@ export const EdgeFirebase = class {
818
818
  const users = Object.values(this.state.users) as User[];
819
819
  const user = users.find((u) => u.userId === userId);
820
820
  if (user) {
821
- stagedDocId = user.stagedDocId;
821
+ console.log(user)
822
+ stagedDocId = user.docId;
822
823
  } else {
823
824
  return this.sendResponse({
824
825
  success: false,
@@ -2076,58 +2077,81 @@ export const EdgeFirebase = class {
2076
2077
  };
2077
2078
 
2078
2079
 
2080
+ // File functions
2079
2081
  // File functions
2080
2082
  public uploadFile = async (filePath: string, file: Blob, isPublic: boolean): Promise<actionResponse> => {
2081
2083
 
2082
- // Validate if file is provided
2083
- if (!file) {
2084
- return this.sendResponse({
2085
- success: false,
2086
- message: "No file provided for upload.",
2087
- meta: {}
2088
- });
2089
- }
2084
+ // Validate if file is provided
2085
+ if (!file) {
2086
+ return this.sendResponse({
2087
+ success: false,
2088
+ message: "No file provided for upload.",
2089
+ meta: {}
2090
+ });
2091
+ }
2090
2092
 
2091
- // Check if the user has write permission to the filePath
2092
- let hasWritePermission = await this.permissionCheck("write", filePath);
2093
- if (isPublic) {
2094
- hasWritePermission = true;
2095
- filePath = "public";
2096
- }
2097
- if (!hasWritePermission) {
2098
- return this.sendResponse({
2099
- success: false,
2100
- message: "You do not have permission to upload files to this path.",
2101
- meta: {}
2102
- });
2103
- }
2093
+ // Check if the user has write permission to the filePath
2094
+ let hasWritePermission = await this.permissionCheck("write", filePath);
2095
+ if (isPublic) {
2096
+ hasWritePermission = true;
2097
+ filePath = "public";
2098
+ }
2099
+ 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
+ });
2105
+ }
2104
2106
 
2105
- try {
2106
- let randomId = ''
2107
- if (isPublic) {
2108
- randomId = Math.random().toString(36).substring(2, 6) + '-'
2109
- }
2110
- const tempFilePath = `${filePath.replaceAll('/', '-')}` + '/' + randomId + file.name;
2111
- const fileRef = ref(this.storage, tempFilePath);
2112
- if (isPublic) {
2113
- await uploadBytes(fileRef, file, { contentType: file.type, cacheControl: 'public, max-age=31536000' });
2114
- } else {
2115
- await uploadBytes(fileRef, file);
2116
- }
2117
- return this.sendResponse({
2118
- success: true,
2119
- message: "File uploaded successfully.",
2120
- meta: {file: tempFilePath}
2121
- });
2122
- } catch (error) {
2123
- return this.sendResponse({
2124
- success: false,
2125
- message: "An error occurred during file upload.",
2126
- meta: {}
2127
- });
2107
+ 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
+ }));
2128
2134
  }
2135
+
2136
+ // Wait for all chunks to finish uploading
2137
+ await Promise.all(chunkPromises);
2138
+
2139
+ return this.sendResponse({
2140
+ success: true,
2141
+ message: "File uploaded successfully in chunks.",
2142
+ meta: {file: uploadPath}
2143
+ });
2144
+
2145
+ } catch (error) {
2146
+ return this.sendResponse({
2147
+ success: false,
2148
+ message: "An error occurred during file upload.",
2149
+ meta: {}
2150
+ });
2151
+ }
2129
2152
  };
2130
2153
 
2154
+
2131
2155
  public deleteFile = async (filePath: string): Promise<actionResponse> => {
2132
2156
  let hasDeletePermission = await this.permissionCheck("write", filePath);
2133
2157
  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.40",
3
+ "version": "2.1.42",
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": {