init()

in Diffusion/Common/Downloader.swift [27:56]


    init(from url: URL, to destination: URL, using authToken: String? = nil) {
        self.destination = destination
        super.init()
        
        var config = URLSessionConfiguration.default
        #if !os(macOS)
        // .background allows downloads to proceed in the background
        // helpful for devices that may not keep the app in the foreground for the download duration
        config = URLSessionConfiguration.background(withIdentifier: "net.pcuenca.diffusion.download")
        config.isDiscretionary = false
        config.sessionSendsLaunchEvents = true
        #endif
        urlSession = URLSession(configuration: config, delegate: self, delegateQueue: OperationQueue())
        downloadState.value = .downloading(0)
        urlSession?.getAllTasks { tasks in
            // If there's an existing pending background task with the same URL, let it proceed.
            guard tasks.filter({ $0.originalRequest?.url == url }).isEmpty else {
                print("Already downloading \(url)")
                return
            }
            print("Starting download of \(url)")
            
            var request = URLRequest(url: url)
            if let authToken = authToken {
                request.setValue("Bearer \(authToken)", forHTTPHeaderField: "Authorization")
            }

            self.urlSession?.downloadTask(with: request).resume()
        }
    }