0
votes
class FCMStorage {

var storage: Storage!

init() {
    storage = Storage.storage()
}

func storeImage(data: Data?, name: String, completion: @escaping ((String?, Error?)->Void)) {

    guard let data = data else {
        return
    }
    let metaData = StorageMetadata()
    metaData.contentType = "image/jpeg"
    let path = "images/" + name
    print("img to store = \(path)")
    let ref = storage.reference()
    let uploadTask = ref.child(path).putData(data, metadata: metaData) { (metadata, error) in
        
        if error == nil {
            print(metadata as Any)
            ref.downloadURL(completion: { (url, error) in
                completion(url?.absoluteString, error)
            })
        } else {
            print("storeImage error: \(error!)")
        }

    }
    
    uploadTask.observe(.progress) { (snapshot) in
        print((snapshot.progress?.fractionCompleted ?? 0) * 100.0)
        if snapshot.status == .success || snapshot.status == .failure {
            uploadTask.removeAllObservers(for: .progress)
        }
    }
 }
}

Using the able class I am able to upload the image on firebase successfully and I am able to see the uploaded image on the firebase too but...

When I call downloadURL() method it always giving me the following error

ref.downloadURL(completion: { (url, error) in
   completion(url?.absoluteString, error)
})

Error: Failed to retrieve a download URL.

Anyone could help me out on this issue!!

EDIT

When I print metadata of the file it prints the following....

FIRStorageMetadata 0x283f99860: {

bucket = "sportoilic.appspot.com";

contentDisposition = "inline; filename*=utf-8''8E13A816-FAF1-47ED-8F84-94BBB8C4C77F";

contentEncoding = identity;

contentType = "application/octet-stream";

generation = 1601287237056536;

md5Hash = "VgMH6NMPGJT//LCD8goaDA==";

metageneration = 1;

name = "8E13A816-FAF1-47ED-8F84-94BBB8C4C77F";

size = 114787;

timeCreated = "2020-09-28T10:00:37.056Z";

updated = "2020-09-28T10:00:37.056Z";

}

and after that when I try to get the download url for the uploaded image< I am getting the above mentioned error(Error: Failed to retrieve a download URL).

What is the issue? Am I missing something here?

1

1 Answers

0
votes

After trial and error of several hours, finally got the solution.

class FCMStorage {

var ref: StorageReference!

init(path: String) {
    ref = Storage.storage().reference(withPath: path)
    print("img to store at path = \(path)")
}

func storeImage(data: Data?, completion: @escaping ((String?, Error?)->Void)) {

    guard let data = data else {
        return
    }
    
    let uploadTask = ref.putData(data, metadata: nil) { (metadata, error) in
        
        if error == nil {
            print(metadata as Any)
            
            self.ref.downloadURL(completion: { (url, error) in
                completion(url?.absoluteString, error)
            })
        } else {
            print("storeImage error: \(error!)")
        }

    }
    
    uploadTask.observe(.progress) { (snapshot) in
        print((snapshot.progress?.fractionCompleted ?? 0) * 100.0)
        if snapshot.status == .success || snapshot.status == .failure {
            uploadTask.removeAllObservers(for: .progress)
        }
    }
  }
}

So store image just need to call like this...

  let path = "images/" + UUID().uuidString
    FCMStorage(path: path).storeImage(data: data) { (imgUrl, error) in
                
          if let strUrl = imgUrl, error == nil {
             FCMDatabase.init(OfTable: .chatRooms).setImageUrl(strUrl: strUrl, teamId: self.team?.id ?? 0, forMsgId: self.arrChats.last?.messageId ?? "") { (isDone) in
                        
                if isDone {
                         print("Image url set in database")
                        }
                    }
                } else {
                    print("Error: \(error?.localizedDescription ?? "Error while getting image url")")
                }
      }