Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unable to get the video on web browser using GCDWebServer in swift

I created a small demo app where app access the image and videos from iPhone using UIImagePickerController. When I select any image or video, app creates a copy of it( image or video) in document directory. And create a web server on iPhone using GCDWebserver and need to expose the selected image or video. But it does not work.

Here is the sample code, not sure where I could be wrong.

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

@IBOutlet weak var btnselect: UIButton!
let videoPicker = UIImagePickerController()

@IBAction func btnSelect(_ sender: Any) {

        if UIImagePickerController.isSourceTypeAvailable(.photoLibrary){
            let myPickerController = UIImagePickerController()
            myPickerController.delegate = self;
            myPickerController.sourceType = .photoLibrary
            myPickerController.allowsEditing = false
            self.present(myPickerController, animated: true, completion: nil)
        }

    }

func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        self.dismiss(animated: true, completion: nil)
    }

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {

    // 1. saving video to documents directory

      let videoURL = info[UIImagePickerController.InfoKey.mediaURL] as! NSURL
            let videoData = NSData(contentsOf: videoURL as URL)
            let path = try! FileManager.default.url(for: FileManager.SearchPathDirectory.documentDirectory, in: FileManager.SearchPathDomainMask.userDomainMask, appropriateFor: nil, create: false)

            let newPath = path.appendingPathComponent("/myTestVideo.mp4")
            do {
                try videoData?.write(to: newPath)
            } catch {
                print(error)
            }


 // 2. Create web server on iPhone using GCDWebServer

        let webServer = GCDWebServer()
        webServer.addDefaultHandler(forMethod: "GET", request: GCDWebServerRequest.self, processBlock: {request in
        return GCDWebServerDataResponse(newPath)

       })
       webServer.start(withPort: 8080, bonjourName: "GCD Web Server")

}
}

I need this selected (image or video) to be hosted/accessible by this GCD Web Server.

like image 730
sia Avatar asked May 21 '19 14:05

sia


1 Answers

You can use GCDWebServerDataResponse to pass the file as the response like this:

webServer!.addDefaultHandler(forMethod: "GET", request: GCDWebServerRequest.self, processBlock: {request in
    let path = Bundle.main.path(forResource: "sample", ofType: "mp4")
    let handler = FileHandle.init(forReadingAtPath: path!)
    return GCDWebServerDataResponse(data: (handler?.readDataToEndOfFile())!, contentType: "video/mp4")  
})

Please notice that when you need to provide the content type and the data of the file.

like image 95
Eduardo Rodriguez Avatar answered Oct 05 '22 21:10

Eduardo Rodriguez