SPB Git

Repository Public

DriveAPI+Sharing.swift 8.2 KB 159 lines swift

1// SP Drive iOS2// File: Core/API/DriveAPI+Sharing.swift3// Description: Share links and deposit links (« portails de dépôt »). Every permission and limit is enforced by the server;4//              a link URL exists in clear only in the answer that creates or rotates it.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import Foundation910public struct ShareLink: Sendable, Identifiable, Decodable, Hashable {11    public let id: String12    /// "file" or "folder"13    public let type: String14    public let targetId: String?15    public let targetName: String16    public let label: String17    public let canDownload: Bool18    public let hasPassword: Bool19    public let expiresAt: Date?20    public let maxViews: Int?21    public let viewCount: Int22    public let maxDownloads: Int?23    public let downloadCount: Int24    public let singleUse: Bool25    public let revokedAt: Date?26    public let createdAt: Date27    public let lastAccessAt: Date?28    /// active, revoked, expired, exhausted, target_missing29    public let status: String30}3132/// Settings of a share link. `nil` leaves a setting unchanged on update; `.some(nil)` clears it.33public struct ShareSettings: Sendable {34    public var label: String?35    public var canDownload: Bool?36    public var password: String??37    public var expiresAt: Date??38    public var maxViews: Int??39    public var maxDownloads: Int??40    public var singleUse: Bool?41    public init() {}4243    var json: [String: Any] {44        var o: [String: Any] = [:]45        if let label { o["label"] = label }46        if let canDownload { o["canDownload"] = canDownload }47        if let password { o["password"] = password ?? NSNull() }48        if let expiresAt { o["expiresAt"] = expiresAt.map(DriveAPI.isoString) ?? NSNull() }49        if let maxViews { o["maxViews"] = maxViews ?? NSNull() }50        if let maxDownloads { o["maxDownloads"] = maxDownloads ?? NSNull() }51        if let singleUse { o["singleUse"] = singleUse }52        return o53    }54}5556public struct ShareEvent: Sendable, Identifiable, Decodable { public let id: String; public let at: Date; public let kind: String; public let ok: Bool; public let ip: String; public let userAgent: String; public let detail: String }5758public struct DepositLink: Sendable, Identifiable, Decodable, Hashable {59    public let id: String60    public let folderId: String61    public let folderName: String?62    public let title: String63    public let description: String64    public let hasPassword: Bool65    public let expiresAt: Date?66    public let active: Bool67    public let revokedAt: Date?68    public let maxFileBytes: Int6469    public let maxTotalBytes: Int6470    public let maxFilesPerSubmission: Int71    public let maxSubmissions: Int?72    public let allowedExtensions: [String]73    public let subfolderPerSubmission: Bool74    public let notifyOwner: Bool75    public let bytesReceived: Int6476    public let submissions: Int77    public let createdAt: Date78    /// open, paused, revoked, expired, full, target_missing79    public let state: String80}8182public struct DepositSettings: Sendable {83    public var title: String?84    public var description: String?85    public var password: String??86    public var expiresAt: Date??87    public var active: Bool?88    public var maxFileBytes: Int64?89    public var maxTotalBytes: Int64?90    public var maxFilesPerSubmission: Int?91    public var maxSubmissions: Int??92    public var subfolderPerSubmission: Bool?93    public var notifyOwner: Bool?94    public init() {}9596    var json: [String: Any] {97        var o: [String: Any] = [:]98        if let title { o["title"] = title }99        if let description { o["description"] = description }100        if let password { o["password"] = password ?? NSNull() }101        if let expiresAt { o["expiresAt"] = expiresAt.map(DriveAPI.isoString) ?? NSNull() }102        if let active { o["active"] = active }103        if let maxFileBytes { o["maxFileBytes"] = maxFileBytes }104        if let maxTotalBytes { o["maxTotalBytes"] = maxTotalBytes }105        if let maxFilesPerSubmission { o["maxFilesPerSubmission"] = maxFilesPerSubmission }106        if let maxSubmissions { o["maxSubmissions"] = maxSubmissions ?? NSNull() }107        if let subfolderPerSubmission { o["subfolderPerSubmission"] = subfolderPerSubmission }108        if let notifyOwner { o["notifyOwner"] = notifyOwner }109        return o110    }111}112113public struct DepositSubmission: Sendable, Identifiable, Decodable {114    public let id: String; public let ref: String; public let status: String; public let name: String; public let email: String; public let reference: String115    public let createdAt: Date; public let completedAt: Date?; public let folderId: String?; public let files: Int; public let placed: Int; public let rejected: Int; public let bytes: Int64116}117public struct DepositSubmissionDetail: Sendable, Decodable {118    public struct Item: Sendable, Identifiable, Decodable { public let id: String; public let name: String; public let size: Int64; public let status: String; public let rejectCode: String?; public let fileId: String? }119    public let id: String; public let ref: String; public let status: String; public let name: String; public let email: String; public let createdAt: Date; public let folderId: String?; public let items: [Item]120}121122extension DriveAPI {123    // MARK: Share links124125    public func shares(fileID: String? = nil, folderID: String? = nil) async throws -> [ShareLink] {126        try decode([ShareLink].self, await call("GET", "api/v1/shares", query: ["fileId": fileID, "folderId": folderID]))127    }128    /// Creates a link. The URL in the answer is the only time it is available.129    public func createShare(item: DriveItem, settings: ShareSettings) async throws -> (share: ShareLink, url: URL) {130        var body = settings.json131        body[item.isFolder ? "folderId" : "fileId"] = item.id132        struct W: Decodable { let share: ShareLink; let url: URL }133        let w = try decode(W.self, await call("POST", "api/v1/shares", json: body))134        return (w.share, w.url)135    }136    public func updateShare(id: String, settings: ShareSettings) async throws -> ShareLink { try decode(ShareLink.self, await call("PATCH", "api/v1/shares/\(id)", json: settings.json)) }137    /// Revocation is immediate on the server. `purge` also removes the link and its access log.138    public func revokeShare(id: String, purge: Bool = false) async throws { _ = try await call("DELETE", "api/v1/shares/\(id)", query: ["purge": purge ? "1" : nil]) }139    /// New URL for the same link; the previous URL stops working at once.140    public func rotateShare(id: String) async throws -> URL { struct W: Decodable { let url: URL }; return try decode(W.self, await call("POST", "api/v1/shares/\(id)/rotate")).url }141    public func shareEvents(id: String) async throws -> [ShareEvent] { try decode([ShareEvent].self, await call("GET", "api/v1/shares/\(id)/events")) }142143    // MARK: Deposit links144145    public func depositLinks() async throws -> [DepositLink] { try decode([DepositLink].self, await call("GET", "api/v1/requests")) }146    public func createDepositLink(folderID: String, settings: DepositSettings) async throws -> (link: DepositLink, url: URL) {147        var body = settings.json148        body["folderId"] = folderID149        struct W: Decodable { let request: DepositLink; let url: URL }150        let w = try decode(W.self, await call("POST", "api/v1/requests", json: body))151        return (w.request, w.url)152    }153    public func updateDepositLink(id: String, settings: DepositSettings) async throws -> DepositLink { try decode(DepositLink.self, await call("PATCH", "api/v1/requests/\(id)", json: settings.json)) }154    public func revokeDepositLink(id: String, purge: Bool = false) async throws { _ = try await call("DELETE", "api/v1/requests/\(id)", query: ["purge": purge ? "1" : nil]) }155    public func rotateDepositLink(id: String) async throws -> URL { struct W: Decodable { let url: URL }; return try decode(W.self, await call("POST", "api/v1/requests/\(id)/rotate")).url }156    public func depositSubmissions(linkID: String) async throws -> [DepositSubmission] { try decode([DepositSubmission].self, await call("GET", "api/v1/requests/\(linkID)/submissions")) }157    public func depositSubmission(id: String) async throws -> DepositSubmissionDetail { try decode(DepositSubmissionDetail.self, await call("GET", "api/v1/submissions/\(id)")) }158}159
Files main
Menu

Theme