SPB Git

Repository Public

Multipart.swift 5.5 KB 134 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project          : MacLustr Identity9// File             : sdk/swift/Sources/MacLustrIdentityCore/Multipart.swift10// Module           : MacLustrIdentityCore11// Repository       : https://git.spboucher.ai/maclustr-identity12// Project Website  : https://identity.spboucher.ai13// Created          : 2026-12-0114// Last Modified    : 2026-12-0115//16// =============================================================================17// AUTHOR & PROFESSIONAL AFFILIATION18// =============================================================================19//20// Author           : Simon-Pierre Boucher21// Position         : Professor of Finance22// Institution      : Université du Québec en Outaouais (UQO)23// Department       : Département des sciences administratives24// Organization     : SP Boucher25//26// Website          : https://spboucher.ai27// Git              : https://git.spboucher.ai28//29// =============================================================================30// OFFICIAL CONTACT DIRECTORY31// =============================================================================32//33// General          : info@spboucher.ai34// Professional     : contact@spboucher.ai35// Direct           : simon@spboucher.ai36// Research         : research@spboucher.ai37// Development      : dev@spboucher.ai38// Support          : support@spboucher.ai39// Security/Admin   : admin@spboucher.ai40// Notifications    : notifications@spboucher.ai41//42// Preferred Contact: admin@spboucher.ai43//44// =============================================================================45// COPYRIGHT & INTELLECTUAL PROPERTY46// =============================================================================47//48// Copyright (c) 202649// Simon-Pierre Boucher50//51// All rights reserved where applicable.52//53// This file is governed by the applicable project license,54// copyright notices, institutional agreements and relevant law.55//56// Unless expressly authorized by the applicable license or57// rights holder, copying, redistribution, sublicensing,58// publication and commercial exploitation are not permitted.59//60// Existing third-party rights and notices remain unaffected.61//62// =============================================================================63// CONFIDENTIALITY & SECURITY64// =============================================================================65//66// Classification   : Public67// Distribution     : Public repository; use is governed by the project license68// Security Contact : admin@spboucher.ai69//70// Access to this source does not independently grant permission71// to redistribute or disclose confidential materials.72//73// Do not embed credentials, private keys, access tokens,74// certificates or confidential personal information.75//76// =============================================================================77// LICENSE & MAINTENANCE78// =============================================================================79//80// License          : None specified (no license file in this repository)81// Maintainer       : Simon-Pierre Boucher82// Technical Contact: dev@spboucher.ai83//84// =============================================================================85// END OF SP BOUCHER SOURCE HEADER86// =============================================================================8788// multipart/form-data encoder (RFC 7578), in memory only.89import Foundation9091struct MultipartForm {92    let boundary: String93    private(set) var body = Data()9495    init(boundary: String = "mlid-" + UUID().uuidString) {96        self.boundary = boundary97    }9899    var contentType: String { "multipart/form-data; boundary=\(boundary)" }100101    mutating func addField(name: String, value: String) {102        append("--\(boundary)\r\nContent-Disposition: form-data; name=\"\(Self.escape(name))\"\r\n\r\n\(value)\r\n")103    }104105    mutating func addFile(name: String, filename: String, contentType: String, data: Data) {106        append("--\(boundary)\r\nContent-Disposition: form-data; name=\"\(Self.escape(name))\"; filename=\"\(Self.escape(filename))\"\r\nContent-Type: \(contentType)\r\n\r\n")107        body.append(data)108        append("\r\n")109    }110111    /// The complete body, closing boundary included.112    func finalized() -> Data {113        var out = body114        out.append(Data("--\(boundary)--\r\n".utf8))115        return out116    }117118    private mutating func append(_ s: String) { body.append(Data(s.utf8)) }119120    /// Quotes, CR and LF are percent-encoded in parameter values, as browsers do.121    static func escape(_ s: String) -> String {122        s.replacingOccurrences(of: "\"", with: "%22").replacingOccurrences(of: "\r", with: "%0D").replacingOccurrences(of: "\n", with: "%0A")123    }124}125126/// Media type of an image from its first bytes; the service accepts JPEG, PNG and WebP.127func sniffImageContentType(_ data: Data) -> String? {128    let b = [UInt8](data.prefix(12))129    if b.count >= 3, b[0] == 0xFF, b[1] == 0xD8, b[2] == 0xFF { return "image/jpeg" }130    if b.count >= 8, b[0] == 0x89, b[1] == 0x50, b[2] == 0x4E, b[3] == 0x47 { return "image/png" }131    if b.count >= 12, b[0] == 0x52, b[1] == 0x49, b[2] == 0x46, b[3] == 0x46, b[8] == 0x57, b[9] == 0x45, b[10] == 0x42, b[11] == 0x50 { return "image/webp" }132    return nil133}134
Files main
Menu

Theme