Upload

The uploadFile function uploads the media on the local storage to the web server, and stores that simultaneously provides the URL to retrieve

Syntax

request.upload())

Return Value

none

Events

onComplete(), onError(), onProgressChanged()

Example

private var disposeBag = Set<AnyCancellable>()
    
// Function to upload a video file to the cloud
func uploadFile(videoPath: String, viewController: UIViewController) {
    // Media Uploading
    // Create a file upload request using TruvideoSdkMedia uploader
    let builder = TruvideoSdkMedia.FileUploadRequestBuilder(fileURL: URL(string: videoPath)!)
    
    // Tags
    builder.addTag("key", "value")
    builder.addTag("color", "red")
    builder.addTag("order-number", "123")
        
    // Metadata
    var metadata = Metadata()
    metadata["key"] = "value"
    metadata["key1"] = 1
    metadata["key2"] = [4, 5, 6]
    builder.setMetadata(metadata)
    
    let request = builder.build()
    // Print the file upload request for debugging
    print("fileUploadRequest: ", request)
    // Completion of request
    // Handle the completion of the file upload request
    let completeCancellable = request.completionHandler
        .receive(on: DispatchQueue.main)
        .sink(receiveCompletion: { receiveCompletion in
            // Handle different completion cases
            switch receiveCompletion {
            case .finished:
                print("finished")
            case .failure(let error):
                // Print any errors that occur during the upload process
                print("failure:", error)
            }
        }, receiveValue: { uploadedResult in
            // Upon successful upload, retrieve the uploaded file URL
        let url = uploadedResult.uploadedFileURL
            print("uploadedResult: ", uploadedResult)
        })
        
    // Store the completion handler in the dispose bag to avoid premature deallocation
    completeCancellable.store(in: &disposeBag)
    // Progress of request
    // Track the progress of the file upload request
    let progress = request.progressHandler
        .receive(on: DispatchQueue.main)
        .sink(receiveValue: { progress in
            // Handle progress updates if needed
        })
    
    // Store the progress handler in the dispose bag to avoid premature deallocation
    progress.store(in: &disposeBag)
    do {
        try request.upload()
    } catch {
        // Handle error
    }
}

Last updated

Was this helpful?