|
| 1 | +import Foundation |
| 2 | + |
| 3 | +/// Information about a file in a repository. |
| 4 | +public struct File: Hashable, Codable, Sendable { |
| 5 | + /// A Boolean value indicating whether the file exists in the repository. |
| 6 | + public let exists: Bool |
| 7 | + |
| 8 | + /// The size of the file in bytes. |
| 9 | + public let size: Int64? |
| 10 | + |
| 11 | + /// The entity tag (ETag) for the file, used for caching and change detection. |
| 12 | + public let etag: String? |
| 13 | + |
| 14 | + /// The Git revision (commit SHA) at which this file information was retrieved. |
| 15 | + public let revision: String? |
| 16 | + |
| 17 | + /// A Boolean value indicating whether the file is stored using Git Large File Storage (LFS). |
| 18 | + public let isLFS: Bool |
| 19 | + |
| 20 | + init( |
| 21 | + exists: Bool, |
| 22 | + size: Int64? = nil, |
| 23 | + etag: String? = nil, |
| 24 | + revision: String? = nil, |
| 25 | + isLFS: Bool = false |
| 26 | + ) { |
| 27 | + self.exists = exists |
| 28 | + self.size = size |
| 29 | + self.etag = etag |
| 30 | + self.revision = revision |
| 31 | + self.isLFS = isLFS |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +// MARK: - |
| 36 | + |
| 37 | +/// A collection of files to upload in a batch operation. |
| 38 | +/// |
| 39 | +/// Use `FileBatch` to prepare multiple files for uploading to a repository in a single operation. |
| 40 | +/// You can add files using subscript notation or dictionary literal syntax. |
| 41 | +/// |
| 42 | +/// ```swift |
| 43 | +/// var batch = FileBatch() |
| 44 | +/// batch["config.json"] = .path("/path/to/config.json") |
| 45 | +/// batch["model.safetensors"] = .url( |
| 46 | +/// URL(fileURLWithPath: "/path/to/model.safetensors"), |
| 47 | +/// mimeType: "application/octet-stream" |
| 48 | +/// ) |
| 49 | +/// let _ = try await client.uploadFiles(batch, to: "username/my-repo", message: "Initial commit") |
| 50 | +/// ``` |
| 51 | +/// - SeeAlso: `HubClient.uploadFiles(_:to:kind:branch:message:maxConcurrent:)` |
| 52 | +public struct FileBatch: Hashable, Codable, Sendable { |
| 53 | + /// An entry representing a file to upload. |
| 54 | + public struct Entry: Hashable, Codable, Sendable { |
| 55 | + /// The file URL pointing to the local file to upload. |
| 56 | + public var url: URL |
| 57 | + |
| 58 | + /// The MIME type of the file. |
| 59 | + public var mimeType: String? |
| 60 | + |
| 61 | + private init(url: URL, mimeType: String? = nil) { |
| 62 | + self.url = url |
| 63 | + self.mimeType = mimeType |
| 64 | + } |
| 65 | + |
| 66 | + /// Creates a file entry from a file system path. |
| 67 | + /// - Parameters: |
| 68 | + /// - path: The file system path to the local file. |
| 69 | + /// - mimeType: The MIME type of the file. If not provided, the MIME type is inferred from the file extension. |
| 70 | + /// - Returns: A file entry for the specified path. |
| 71 | + public static func path(_ path: String, mimeType: String? = nil) -> Self { |
| 72 | + return Self(url: URL(fileURLWithPath: path), mimeType: mimeType) |
| 73 | + } |
| 74 | + |
| 75 | + /// Creates a file entry from a URL. |
| 76 | + /// - Parameters: |
| 77 | + /// - url: The file URL. Must be a file URL (e.g., `file:///path/to/file`), not a remote URL. |
| 78 | + /// - mimeType: Optional MIME type for the file. |
| 79 | + /// - Returns: A file entry, or `nil` if the URL is not a file URL. |
| 80 | + /// - Note: Only file URLs are accepted because this API requires local file access for upload. |
| 81 | + /// Remote URLs (http, https, etc.) are not supported and will return `nil`. |
| 82 | + public static func url(_ url: URL, mimeType: String? = nil) -> Self? { |
| 83 | + guard url.isFileURL else { |
| 84 | + return nil |
| 85 | + } |
| 86 | + return Self(url: url, mimeType: mimeType) |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + private var entries: [String: Entry] |
| 91 | + |
| 92 | + /// Creates an empty file batch. |
| 93 | + public init() { |
| 94 | + self.entries = [:] |
| 95 | + } |
| 96 | + |
| 97 | + /// Creates a file batch with the specified entries. |
| 98 | + /// - Parameter entries: A dictionary mapping repository paths to file entries. |
| 99 | + public init(_ entries: [String: Entry]) { |
| 100 | + self.entries = entries |
| 101 | + } |
| 102 | + |
| 103 | + /// Accesses the file entry for the specified repository path. |
| 104 | + /// - Parameter path: The path in the repository where the file will be uploaded. |
| 105 | + /// - Returns: The file entry for the specified path, or `nil` if no entry exists. |
| 106 | + public subscript(path: String) -> Entry? { |
| 107 | + get { |
| 108 | + return entries[path] |
| 109 | + } |
| 110 | + set { |
| 111 | + entries[path] = newValue |
| 112 | + } |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +// MARK: - Collection |
| 117 | + |
| 118 | +extension FileBatch: Swift.Collection { |
| 119 | + public typealias Index = Dictionary<String, Entry>.Index |
| 120 | + |
| 121 | + public var startIndex: Index { entries.startIndex } |
| 122 | + public var endIndex: Index { entries.endIndex } |
| 123 | + public func index(after i: Index) -> Index { entries.index(after: i) } |
| 124 | + public subscript(position: Index) -> (key: String, value: Entry) { entries[position] } |
| 125 | + |
| 126 | + public func makeIterator() -> Dictionary<String, Entry>.Iterator { |
| 127 | + return entries.makeIterator() |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +// MARK: - ExpressibleByDictionaryLiteral |
| 132 | + |
| 133 | +extension FileBatch: ExpressibleByDictionaryLiteral { |
| 134 | + public init(dictionaryLiteral elements: (String, Entry)...) { |
| 135 | + self.init(Dictionary(uniqueKeysWithValues: elements)) |
| 136 | + } |
| 137 | +} |
0 commit comments