         
         return Task(deferred)
     }
-    
+
+    // MARK: UIDocumentPickerDelegate
+    
+    public func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
+        defer {
+            self.currentRequest = nil
+        }
+        guard let currentRequest = self.currentRequest, let url = urls.first else { return }
+        currentRequest.handler(url)
+    }
+
+    public func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
+        currentRequest?.handler(nil)
+        currentRequest = nil
+    }
+
     // MARK: UIImagePickerControllerDelegate
     
     public func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {

            return nil
        }
        
        guard UIImagePickerController.isSourceTypeAvailable(source.toUIKit()) else {
            handler(nil)
            return nil
        }

        self.currentRequest = Request(handler: handler, kind: kind)
        
        imagePicker.allowsEditing = false
        imagePicker.sourceType = source.toUIKit()
        if source == .camera {
            imagePicker.cameraDevice = .front
        }
        imagePicker.delegate = self
        switch kind {
        case .video:
            imagePicker.mediaTypes = [kUTTypeMovie as String]
        case .photo, .thumbnail:
            imagePicker.mediaTypes = [kUTTypeImage as String]
        }
        
        return imagePicker
    }
    
    public func createVideoThumbnail(forURL videoURL: URL) -> Task<URL> {
        let deferred = Deferred<Task<URL>.Result>()


    public func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        currentRequest?.handler(nil)
        currentRequest = nil
    }

    @available(iOS 14.0, *)
    private func handleRequestWithFilesApp(kind: Kind, source: Source, handler: @escaping MediaHandler) -> UIViewController? {
        let vc = UIDocumentPickerViewController(forOpeningContentTypes: kind.contentTypes, asCopy: false)
        vc.delegate = self
        vc.allowsMultipleSelection = false
        self.currentRequest = Request(handler: handler, kind: kind)
        return vc
    }
    
    private func handleRequestWithImagePicker(kind: Kind, source: Source, handler: @escaping MediaHandler) -> UIViewController? {
        guard UIImagePickerController.isSourceTypeAvailable(source.imagePickerSource) else {
            handler(nil)
            return nil
        }

        self.currentRequest = Request(handler: handler, kind: kind)
        
        imagePicker.allowsEditing = false
        imagePicker.sourceType = source.imagePickerSource
        if source == .camera {
            imagePicker.cameraDevice = .front
        }
        imagePicker.delegate = self
        switch kind {
        case .video:
            imagePicker.mediaTypes = [kUTTypeMovie as String]
        case .photo, .thumbnail:
            imagePicker.mediaTypes = [kUTTypeImage as String]
        }
        
        return imagePicker
    }
    
    private func cachePathForMedia(_ kind: Kind) -> URL {
