Skip to content

Instantly share code, notes, and snippets.

@unix14
Last active October 9, 2024 12:09

Revisions

  1. unix14 revised this gist Oct 9, 2024. 1 changed file with 0 additions and 44 deletions.
    44 changes: 0 additions & 44 deletions firebase_storage_manager.dart
    Original file line number Diff line number Diff line change
    @@ -1,44 +0,0 @@
    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;
    }
    }
    }
  2. unix14 created this gist Oct 9, 2024.
    44 changes: 44 additions & 0 deletions firebase_storage_manager.dart
    Original 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;
    }
    }
    }
    49 changes: 49 additions & 0 deletions smart_image_uploader.dart
    Original 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');
    }
    }
    }