Skip to content

Commit

Permalink
[Tea]Bump 0.1.9
Browse files Browse the repository at this point in the history
  • Loading branch information
yndu13 committed May 15, 2024
1 parent 1f10fc6 commit 5553975
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 156 deletions.
4 changes: 2 additions & 2 deletions Teafile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"scope": "darabonba",
"name": "FileForm",
"version": "0.1.8",
"version": "0.1.9",
"main": "./main.tea",
"releases": {
"go": "github.com/alibabacloud-go/tea-fileform/service:v1.1.1",
Expand All @@ -11,7 +11,7 @@
"php": "alibabacloud/tea-fileform:^0.3.0",
"python": "alibabacloud_tea_fileform:0.0.3",
"python2": "alibabacloud_tea_fileform_py2:0.0.1",
"swift": "alibabacloud-sdk-swift/tea-fileform:1.0.1"
"swift": "alibabacloud-sdk-swift/tea-fileform:1.0.2"
},
"java": {
"package": "com.aliyun.fileform"
Expand Down
151 changes: 151 additions & 0 deletions swift/Sources/TeaFileForm/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,154 @@ open class Client {
return fileForm;
}
}

public class TeaFileForm {
private var form: [String: Any]
private var keys: [String]
private var boundary: String
private var streaming: Bool
private var stream: Data?

public var bytes: [UInt8] = [UInt8]()
public var index: Int

private var readPos: Int = 0

public init(_ map: [String: Any], _ boundary: String) {
self.form = map
self.keys = Array(map.keys)
self.keys = self.keys.sorted()
self.index = 0
self.boundary = boundary
self.streaming = false
}

private func readToData(_ stream: InputStream?) -> Data {
stream?.open()
defer { stream?.close() }
let bufferSize = 1024
var buffer : [UInt8] = [UInt8](repeating: 0, count: bufferSize)
var data = Data()
while stream != nil && stream!.hasBytesAvailable {
let read = stream!.read(&buffer, maxLength: bufferSize)
if read <= 0 {
break
}
data.append(buffer, count: read)
}
return data
}

public func read(_ off: Int, _ count: Int) -> Int {
if self.streaming {
if self.stream != nil && self.stream?.count != 0 {
let dataLen: Int = self.stream?.count ?? 0
let offset: Int = self.readPos + off
if offset >= dataLen {
return self.next(endStr: "\r\n")
}
let range = dataLen < offset + count ? offset..<(dataLen - offset) : offset..<count
let data: Data = self.stream!
var readContentBytes: [UInt8] = [UInt8](repeating: 0, count: data.count)
data.copyBytes(to: &readContentBytes, from: range)
self.bytes = self.bytes + readContentBytes
self.readPos = self.readPos + readContentBytes.count
return readContentBytes.count
} else {
return self.next(endStr: "\r\n")
}
}
if self.index > self.keys.count {
return 0
}
if self.keys.count > 0 {
if self.index < self.keys.count {
self.streaming = true
let name: String = self.keys[self.index]
let fieldValue = self.form[name]
let tmp: [String]
let field: FileField? = fieldValue as? FileField
if field != nil {
if field?.filename != nil && field?.contentType != nil && field?.content != nil {
tmp = [
"--", self.boundary, "\r\n",
"Content-Disposition: form-data; name=\"", name, "\"; filename=", field!.filename!, "\r\n",
"Content-Type: ", (field?.contentType ?? ""), "\r\n\r\n"
]
let body: [UInt8] = tmp.joined().toBytes()
self.bytes = self.bytes + body
self.stream = self.readToData(field?.content)
return body.count
} else {
return self.next(endStr: "\r\n")
}
} else {
let val: String = String(describing: fieldValue ?? "").percentEncode()
tmp = [
"--", self.boundary, "\r\n",
"Content-Disposition: form-data; name=\"", name, "\"\r\n\r\n",
val, "\r\n\r\n"
]
let body: [UInt8] = tmp.joined().toBytes()
self.bytes = self.bytes + body
return body.count
}
} else if (self.index == self.keys.count) {
return self.next(endStr: "--" + boundary + "--\r\n")
} else {
return 0
}
}
return 0
}

private func next(endStr: String) -> Int {
self.streaming = false
self.bytes = self.bytes + endStr.toBytes()
self.index = self.index + 1
self.stream = nil
self.readPos = 0
return endStr.count
}

public func write(_ bytes: [UInt8]) {
self.bytes = self.bytes + bytes
}

public func ReadAsync(_ offset: Int, _ count: Int, _ callback: @escaping (Int) -> Void) {
let queue = DispatchQueue(label: "TeaFileFormQueue", attributes: .init(rawValue: 0))
queue.async {
let index: Int = self.read(offset, count)
callback(index)
}
}

public func getData() -> Data {
Data(self.bytes)
}
}

extension String {
static func randomString(len: Int, randomDict: String = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") -> String {
var ranStr = ""
for _ in 0..<len {
let index = Int.random(in: 0..<randomDict.count)
ranStr.append(randomDict[randomDict.index(randomDict.startIndex, offsetBy: index)])
}
return ranStr
}

func toBytes() -> [UInt8] {
[UInt8](self.utf8)
}

func percentEncode() -> String {
let unreserved = "*-._"
let allowedCharacterSet = NSMutableCharacterSet.alphanumeric()
allowedCharacterSet.addCharacters(in: unreserved)
allowedCharacterSet.addCharacters(in: " ")
var encoded = addingPercentEncoding(withAllowedCharacters: allowedCharacterSet as CharacterSet)
encoded = encoded?.replacingOccurrences(of: " ", with: "%20")
return encoded ?? ""
}
}
153 changes: 0 additions & 153 deletions swift/Sources/TeaFileForm/TeaFileForm.swift

This file was deleted.

2 changes: 1 addition & 1 deletion swift/TeaFileForm.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |spec|

spec.name = "TeaFileForm"
spec.version = "1.0.1"
spec.version = "1.0.2"
spec.license = "Apache 2.0"
spec.summary = "Alibaba Cloud Tea FileForm for Swift"
spec.homepage = "https://github.com/alibabacloud-sdk-swift/tea-fileform"
Expand Down

0 comments on commit 5553975

Please sign in to comment.