1

I'm trying to upload a video file using phpickerviewcontroller, but I'm running into an issue uploading the URL to FirebaseStorage. Here is some code:

func uploadVideo(videoURL: URL)
    {
        let storage = Storage.storage()
        let storageRef = storage.reference()
        
        let videoRef = storageRef.child("rPosts/\(uid!)/\(fileID)")
        let metadata = StorageMetadata()
        metadata.contentType = "video/quicktime"
        
        var videoData: Data = Data()
        
        do
        {
            videoData = try Data(contentsOf: videoURL)
        }
        catch
        {
            print(error.localizedDescription)
            return
        }
        
        videoRef.putData(videoData, metadata: metadata)
        { (metaData, error) in
            guard error == nil else
            {
                self.errorLabel.text = error!.localizedDescription
                return
            }
        }
    }
func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult])
    {
        dismiss(animated: true, completion: nil)
        guard let itemProvider = results.first?.itemProvider else { return }
        
        itemProvider.loadItem(forTypeIdentifier: "com.apple.quicktime-movie", options: nil)
        { (videoFile, error) in
            guard error == nil else { return }
            
            let videoFile = videoFile as? URL
            DispatchQueue.main.async
            {
                self.uploadVideo(videoURL: videoFile!)
                
                print(videoFile!)
            }
        
            self.uploadedYet = true
        }
    }

I've tried using .putFile but it keeps on saying

Ensure file URL is not a directory, symbolic link, or invalid url.

When I use .putData it says

The file "..." couldn’t be opened because there is no such file

EDIT:

itemProvider.loadFileRepresentation(forTypeIdentifier: "com.apple.quicktime-movie")
        { (videoURL, error) in
            guard error == nil else { return }
            print("isbeingcalled") //does not get calleed :(
            
            DispatchQueue.main.async
            {
                let storageRef = Storage.storage().reference()
                
                let videoRef = storageRef.child("rPosts/\(self.uid!)/\(self.fileID).mov")
                let metadata = StorageMetadata()
                metadata.contentType = "video/quicktime"
                
                print("run")
                
                videoRef.putFile(from: videoURL!, metadata: metadata)
                { (metaData, error) in
                    guard error == nil else
                    {
                        print(videoURL!)
                        print(videoRef.fullPath)
                        self.errorLabel.text = error!.localizedDescription
                        print(error!.localizedDescription)
                        return
                    }
                }
            }
        
            self.uploadedYet = true
        }
EAO123
  • 55
  • 9
  • Make sure to save your file to permanent location before uploading it. – Leo Dabus Sep 18 '21 at 21:58
  • what do you mean? – EAO123 Sep 18 '21 at 23:45
  • if you are trying to upload a temporary file it will be deleted as soon as didFinishPicking method finishes. Make sure you can copy the file before trying to export/upload it. – Leo Dabus Sep 18 '21 at 23:47
  • Btw I would not try to load a video to memory as it might crash your app. use `putFile` instead – Leo Dabus Sep 18 '21 at 23:51
  • @LeoDabus So I put the function `uploadVideo` inside the `DispatchQueue.main.async` using `.putFile` but that didn't work (same error that the file doesn't exist). I'm not sure if that's what you meant however. – EAO123 Sep 19 '21 at 14:39
  • **file:///Users/name/Library/Developer/CoreSimulator/Devices/CD40B2C2-18F8-4310-B272-478353C02BDF/data/Containers/Shared/AppGroup/9D622C78-BA6C-4D77-9DEC-49DE364F1E7A/File%20Provider%20Storage/photospicker/version=1&uuid=C1A18B3E-9924-4E62-B6F0-CB6EC7757047&mode=compatible.mov** This is the file link printed from `videoFile`. Is there something wrong with it? – EAO123 Sep 19 '21 at 14:52
  • Please use `itemProvider.loadFileRepresentation` to handle video files. – Kush Bhavsar Sep 20 '21 at 10:46
  • For some reason, that's not working. The function within `.loadFileRepresentation` is not even being called, which is weird. – EAO123 Sep 22 '21 at 02:32
  • @LeoDabus See my edits – EAO123 Sep 22 '21 at 02:34

0 Answers0