@@ -8,7 +8,7 @@ const { getFirebaseStorage } = require('./initialize');
8
8
* @param {string } fileName - The name of the file to delete.
9
9
* @returns {Promise<void> } A promise that resolves when the file is deleted.
10
10
*/
11
- async function deleteFileFromFirebase ( basePath , fileName ) {
11
+ async function deleteFile ( basePath , fileName ) {
12
12
const storage = getFirebaseStorage ( ) ;
13
13
if ( ! storage ) {
14
14
console . error ( 'Firebase is not initialized. Cannot delete file from Firebase Storage.' ) ;
@@ -27,39 +27,38 @@ async function deleteFileFromFirebase(basePath, fileName) {
27
27
}
28
28
29
29
/**
30
- * Saves an image from a given URL to Firebase Storage. The function first initializes the Firebase Storage
31
- * reference, then uploads the image to a specified basePath in the Firebase Storage. It handles initialization
30
+ * Saves an file from a given URL to Firebase Storage. The function first initializes the Firebase Storage
31
+ * reference, then uploads the file to a specified basePath in the Firebase Storage. It handles initialization
32
32
* errors and upload errors, logging them to the console. If the upload is successful, the file name is returned.
33
33
*
34
34
* @param {Object } params - The parameters object.
35
35
* @param {string } params.userId - The user's unique identifier. This is used to create a user-specific basePath
36
36
* in Firebase Storage.
37
- * @param {string } params.URL - The URL of the image to be uploaded. The image at this URL will be fetched
37
+ * @param {string } params.URL - The URL of the file to be uploaded. The file at this URL will be fetched
38
38
* and uploaded to Firebase Storage.
39
- * @param {string } params.fileName - The name that will be used to save the image in Firebase Storage. This
39
+ * @param {string } params.fileName - The name that will be used to save the file in Firebase Storage. This
40
40
* should include the file extension.
41
- * @param {string } [params.basePath='images'] - Optional. The base basePath in Firebase Storage where the image will
41
+ * @param {string } [params.basePath='images'] - Optional. The base basePath in Firebase Storage where the file will
42
42
* be stored. Defaults to 'images' if not specified.
43
43
*
44
44
* @returns {Promise<string|null> }
45
- * A promise that resolves to the file name if the image is successfully uploaded, or null if there
45
+ * A promise that resolves to the file name if the file is successfully uploaded, or null if there
46
46
* is an error in initialization or upload.
47
47
*/
48
48
async function saveURLToFirebase ( { userId, URL , fileName, basePath = 'images' } ) {
49
49
const storage = getFirebaseStorage ( ) ;
50
50
if ( ! storage ) {
51
- console . error ( 'Firebase is not initialized. Cannot save image to Firebase Storage.' ) ;
51
+ console . error ( 'Firebase is not initialized. Cannot save file to Firebase Storage.' ) ;
52
52
return null ;
53
53
}
54
54
55
55
const storageRef = ref ( storage , `${ basePath } /${ userId . toString ( ) } /${ fileName } ` ) ;
56
56
57
57
try {
58
- // Upload image to Firebase Storage using the image URL
59
58
await uploadBytes ( storageRef , await fetch ( URL ) . then ( ( response ) => response . buffer ( ) ) ) ;
60
59
return fileName ;
61
60
} catch ( error ) {
62
- console . error ( 'Error uploading image to Firebase Storage:' , error . message ) ;
61
+ console . error ( 'Error uploading file to Firebase Storage:' , error . message ) ;
63
62
return null ;
64
63
}
65
64
}
@@ -97,8 +96,78 @@ async function getFirebaseURL({ fileName, basePath = 'images' }) {
97
96
}
98
97
}
99
98
99
+ /**
100
+ * Uploads a buffer to Firebase Storage.
101
+ *
102
+ * @param {Object } params - The parameters object.
103
+ * @param {string } params.userId - The user's unique identifier. This is used to create a user-specific basePath
104
+ * in Firebase Storage.
105
+ * @param {string } params.fileName - The name of the file to be saved in Firebase Storage.
106
+ * @param {string } params.buffer - The buffer to be uploaded.
107
+ * @param {string } [params.basePath='images'] - Optional. The base basePath in Firebase Storage where the file will
108
+ * be stored. Defaults to 'images' if not specified.
109
+ *
110
+ * @returns {Promise<string> } - A promise that resolves to the download URL of the uploaded file.
111
+ */
112
+ async function saveBufferToFirebase ( { userId, buffer, fileName, basePath = 'images' } ) {
113
+ const storage = getFirebaseStorage ( ) ;
114
+ if ( ! storage ) {
115
+ throw new Error ( 'Firebase is not initialized' ) ;
116
+ }
117
+
118
+ const storageRef = ref ( storage , `${ basePath } /${ userId } /${ fileName } ` ) ;
119
+ await uploadBytes ( storageRef , buffer ) ;
120
+
121
+ // Assuming you have a function to get the download URL
122
+ return await getFirebaseURL ( { fileName, basePath : `${ basePath } /${ userId } ` } ) ;
123
+ }
124
+
125
+ /**
126
+ * Extracts and decodes the file path from a Firebase Storage URL.
127
+ *
128
+ * @param {string } urlString - The Firebase Storage URL.
129
+ * @returns {string } The decoded file path.
130
+ */
131
+ function extractFirebaseFilePath ( urlString ) {
132
+ try {
133
+ const url = new URL ( urlString ) ;
134
+ const pathRegex = / \/ o \/ ( .+ ?) ( \? | $ ) / ;
135
+ const match = url . pathname . match ( pathRegex ) ;
136
+
137
+ if ( match && match [ 1 ] ) {
138
+ return decodeURIComponent ( match [ 1 ] ) ;
139
+ }
140
+
141
+ return '' ;
142
+ } catch ( error ) {
143
+ // If URL parsing fails, return an empty string
144
+ return '' ;
145
+ }
146
+ }
147
+
148
+ /**
149
+ * Deletes a file from Firebase storage. This function determines the subfolder (either 'images' or 'documents')
150
+ * based on the file type and constructs a Firebase storage path using the user's ID and the file name.
151
+ * It then deletes the file from Firebase storage.
152
+ *
153
+ * @param {Object } req - The request object from Express. It should contain a `user` object with an `id` property.
154
+ * @param {Object } file - The file object to be deleted. It should have `filepath` and `type` properties.
155
+ * `filepath` is a string representing the file's name, and `type` is used to determine
156
+ * the file's storage subfolder in Firebase.
157
+ *
158
+ * @returns {Promise<void> }
159
+ * A promise that resolves when the file has been successfully deleted from Firebase storage.
160
+ * Throws an error if there is an issue with deletion.
161
+ */
162
+ const deleteFirebaseFile = async ( req , file ) => {
163
+ const fileName = extractFirebaseFilePath ( file . filepath ) ;
164
+ await deleteFile ( '' , fileName ) ;
165
+ } ;
166
+
100
167
module . exports = {
101
- saveURLToFirebase ,
168
+ deleteFile ,
102
169
getFirebaseURL,
103
- deleteFileFromFirebase,
170
+ saveURLToFirebase,
171
+ deleteFirebaseFile,
172
+ saveBufferToFirebase,
104
173
} ;
0 commit comments