Last active
October 9, 2024 12:09
Revisions
-
unix14 revised this gist
Oct 9, 2024 . 1 changed file with 0 additions and 44 deletions.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,44 +0,0 @@ -
unix14 created this gist
Oct 9, 2024 .There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,44 @@ import 'dart:io'; import 'package:firebase_storage/firebase_storage.dart' as fbStorage; import 'package:flutter/foundation.dart'; class FirebaseStorageManager { static final FirebaseStorageManager _instance = FirebaseStorageManager._(); factory FirebaseStorageManager() { return _instance; } FirebaseStorageManager._(); Future<bool> uploadFile(dynamic file, String path) async { try { fbStorage.Reference ref = fbStorage.FirebaseStorage.instance.ref(path); var uploadTask; if (file is File) { uploadTask = await ref.putFile(file); } else if (file is Uint8List) { uploadTask = await ref.putData(file); } else { uploadTask = await ref.putBlob(file); } return uploadTask.state == fbStorage.TaskState.success; } catch (e) { print('Error uploading file: $e'); return false; } } Future<String?> downloadURL(String path) async { try { fbStorage.Reference ref = fbStorage.FirebaseStorage.instance.ref(path); return await ref.getDownloadURL(); } catch (e) { print('Error downloading URL: $e'); return null; } } } This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,49 @@ import 'dart:io'; import 'dart:typed_data'; import 'package:file_picker/file_picker.dart'; import 'firebase_storage_manager.dart'; class SmartImageUploader { static Future<String?> uploadFileToFirebase(String path) async { /// Step 1: Open the file picker and get the file FilePickerResult? result = await FilePicker.platform.pickFiles(); if(result != null) { PlatformFile platformFile = result.files.first; // Extract the file extension String? extension = platformFile.extension; // Append the extension to the path String fullPath = '$path.$extension'; Uint8List? fileBytes = platformFile.bytes; bool uploadResult = false; /// Step 2: Upload the file to Firebase Storage if (fileBytes != null) { // Use the bytes property in a web environment uploadResult = await FirebaseStorageManager().uploadFile(fileBytes, fullPath); } else if (platformFile.path != null) { // Convert the PlatformFile to a File in a non-web environment File file = File(platformFile.path!); uploadResult = await FirebaseStorageManager().uploadFile(file, fullPath); } if(uploadResult) { /// Step 3: Get the download URL of the uploaded file print('File uploaded successfully'); var downloadUrl = await FirebaseStorageManager().downloadURL(fullPath); return downloadUrl; } else { print('Failed to upload file'); } } else { // User canceled the picker print('No file selected'); } } }