Files
andros 225e9a58ec Add GitHub, Codeberg, WebDAV upload backends; fix vfile download bug
- New FeedUploader protocol with VFileUploader, GitHubUploader, CodebergUploader, WebDAVUploader
- GitHub/Codeberg: fetch current file SHA via API before PUT (handles create + update)
- WebDAV: HTTP PUT with optional Basic Auth
- ComposeViewModel: always downloads from publicFeedURL (vfile token URL is upload-only, GET returns 404)
- SettingsViewModel: per-method config fields + derived public URL hints for GitHub/Codeberg
- SettingsView: method picker with conditional sections; VFile shows signup link; SFTP/FTP excluded (no native iOS support)
2026-04-19 11:13:04 +02:00

41 lines
1.5 KiB
Swift

import Foundation
/// Uploads the social.org file to a WebDAV server via HTTP PUT.
///
/// Supports optional Basic Auth. The `url` must point directly to the
/// `social.org` file on the WebDAV server.
public struct WebDAVUploader: FeedUploader {
public let url: URL
public let username: String?
public let password: String?
private let session: URLSession
public init(url: URL, username: String? = nil, password: String? = nil, session: URLSession = .shared) {
self.url = url
self.username = username
self.password = password
self.session = session
}
public func upload(content: String) async throws {
var request = URLRequest(url: url)
request.httpMethod = "PUT"
request.setValue("text/plain; charset=utf-8", forHTTPHeaderField: "Content-Type")
request.httpBody = Data(content.utf8)
if let username, let password, !username.isEmpty {
let credentials = Data("\(username):\(password)".utf8).base64EncodedString()
request.setValue("Basic \(credentials)", forHTTPHeaderField: "Authorization")
}
let (_, response): (Data, URLResponse)
do { (_, response) = try await session.data(for: request) }
catch { throw UploadError.networkError(underlying: error.localizedDescription) }
if let http = response as? HTTPURLResponse, !(200..<300).contains(http.statusCode) {
throw UploadError.uploadFailed(statusCode: http.statusCode)
}
}
}