一个纯 Swift 编写的 Data URI 解析器。
更新你的 Package.swift
文件。
.package(url: "https://github.com/nodes-vapor/data-uri.git", from: "2.0.0")
有两种方式可以解码 Data URI。第一种是使用 String
扩展,第二种是直接使用 DataURIParser
。
这个方法是最容易使用的。你只需要在任何 Data URI 编码的 String
上调用 .dataURIDecoded() throws -> (data: Bytes, type: String)
即可。
import Core //just for `Bytes.string`
import DataURI
let uri = "data:,Hello%2C%20World!"
let (data, type) = try uri.dataURIDecoded()
print(data.string) // "Hello, World!"
print(type) // "text/plain;charset=US-ASCII"
使用解析器会稍微复杂一些,因为它将其所有结果都以 Bytes
形式返回。
import Core //just for `Bytes.string`
import DataURI
let uri = "data:text/html,%3Ch1%3EHello%2C%20World!%3C%2Fh1%3E"
let (data, type, metadata) = try DataURIParser.parse(uri: uri)
print(data.string) // "<h1>Hello, World!</h1>"
print(type.string) // "text/html"
print(metadata == nil) // "true"
这个包由 Nodes 的 Vapor 团队开发和维护。这个项目的包所有者是 Tom。
这个包是根据 MIT 许可证 开源的软件。