Skip to content

Commit 42e094f

Browse files
feat: introduce 0.15.x support
1 parent 871beac commit 42e094f

28 files changed

+859
-184
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
![Swift Package Manager](https://img.shields.io/github/v/release/appwrite/sdk-for-apple.svg?color=green&style=flat-square)
44
![License](https://img.shields.io/github/license/appwrite/sdk-for-apple.svg?style=flat-square)
5-
![Version](https://img.shields.io/badge/api%20version-0.14.0-blue.svg?style=flat-square)
5+
![Version](https://img.shields.io/badge/api%20version-0.15.0-blue.svg?style=flat-square)
66
[![Build Status](https://img.shields.io/travis/com/appwrite/sdk-generator?style=flat-square)](https://travis-ci.com/appwrite/sdk-generator)
77
[![Twitter Account](https://img.shields.io/twitter/follow/appwrite?color=00acee&label=twitter&style=flat-square)](https://twitter.com/appwrite)
88
[![Discord](https://img.shields.io/discord/564160730845151244?label=discord&style=flat-square)](https://appwrite.io/discord)
99

10-
**This SDK is compatible with Appwrite server version 0.14.x. For older versions, please check [previous releases](https://github.com/appwrite/sdk-for-apple/releases).**
10+
**This SDK is compatible with Appwrite server version 0.15.x. For older versions, please check [previous releases](https://github.com/appwrite/sdk-for-apple/releases).**
1111

1212
Appwrite is an open-source backend as a service server that abstract and simplify complex and repetitive development tasks behind a very simple to use REST API. Appwrite aims to help you develop your apps faster and in a more secure way. Use the Apple SDK to integrate your app with the Appwrite server to easily start interacting with all of Appwrite backend APIs and tools. For full API documentation and tutorials go to [https://appwrite.io/docs](https://appwrite.io/docs)
1313

@@ -31,7 +31,7 @@ Add the package to your `Package.swift` dependencies:
3131

3232
```swift
3333
dependencies: [
34-
.package(url: "[email protected]:appwrite/sdk-for-apple.git", from: "0.5.0"),
34+
.package(url: "[email protected]:appwrite/sdk-for-apple.git", from: "0.6.0"),
3535
],
3636
```
3737

Sources/Appwrite/Client.swift

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ open class Client {
2020

2121
open var headers: [String: String] = [
2222
"content-type": "",
23-
"x-sdk-version": "appwrite:swiftclient:0.5.0",
24-
"X-Appwrite-Response-Format": "0.14.0"
23+
"x-sdk-version": "appwrite:swiftclient:0.6.0",
24+
"X-Appwrite-Response-Format": "0.15.0"
2525
]
2626

2727
open var config: [String: String] = [:]
@@ -305,7 +305,7 @@ open class Client {
305305
case is Bool.Type:
306306
return true as! T
307307
case is ByteBuffer.Type:
308-
return response.body as! T
308+
return try await response.body.collect(upTo: Int.max) as! T
309309
default:
310310
let data = try await response.body.collect(upTo: Int.max)
311311
if data.readableBytes == 0 {
@@ -358,10 +358,21 @@ open class Client {
358358
converter: (([String: Any]) -> T)? = nil,
359359
onProgress: ((UploadProgress) -> Void)? = nil
360360
) async throws -> T {
361-
let file = params[paramName] as! File
362-
let size = file.buffer.readableBytes
361+
let input = params[paramName] as! InputFile
362+
363+
switch(input.sourceType) {
364+
case "path":
365+
input.data = ByteBuffer(data: try! Data(contentsOf: URL(fileURLWithPath: input.path)))
366+
case "data":
367+
input.data = ByteBuffer(data: input.data as! Data)
368+
default:
369+
break
370+
}
371+
372+
let size = (input.data as! ByteBuffer).readableBytes
363373

364374
if size < Client.chunkSize {
375+
params[paramName] = input
365376
return try await call(
366377
method: "POST",
367378
path: path,
@@ -371,7 +382,6 @@ open class Client {
371382
)
372383
}
373384

374-
let input = file.buffer
375385
var offset = 0
376386
var result = [String:Any]()
377387

@@ -389,14 +399,10 @@ open class Client {
389399
}
390400

391401
while offset < size {
392-
let slice = input.getSlice(at: offset, length: Client.chunkSize)
393-
?? input.getSlice(at: offset, length: Int(size - offset))
402+
let slice = (input.data as! ByteBuffer).getSlice(at: offset, length: Client.chunkSize)
403+
?? (input.data as! ByteBuffer).getSlice(at: offset, length: Int(size - offset))
394404

395-
params[paramName] = File(
396-
name: file.name,
397-
buffer: slice!
398-
)
399-
405+
params[paramName] = InputFile.fromBuffer(slice!, filename: input.filename, mimeType: input.mimeType)
400406
headers["content-range"] = "bytes \(offset)-\(min((offset + Client.chunkSize) - 1, size))/\(size)"
401407

402408
result = try await call(
@@ -449,12 +455,15 @@ open class Client {
449455
bodyBuffer.writeString(CRLF)
450456
bodyBuffer.writeString("Content-Disposition: form-data; name=\"\(name)\"")
451457

452-
if let file = value as? File {
453-
bodyBuffer.writeString("; filename=\"\(file.name)\"")
458+
if let file = value as? InputFile {
459+
bodyBuffer.writeString("; filename=\"\(file.filename)\"")
454460
bodyBuffer.writeString(CRLF)
455461
bodyBuffer.writeString("Content-Length: \(bodyBuffer.readableBytes)")
456462
bodyBuffer.writeString(CRLF+CRLF)
457-
bodyBuffer.writeBuffer(&file.buffer)
463+
464+
var buffer = file.data! as! ByteBuffer
465+
466+
bodyBuffer.writeBuffer(&buffer)
458467
bodyBuffer.writeString(CRLF)
459468
return
460469
}
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
import Foundation
2+
3+
fileprivate let DEFAULT_MIME_TYPE = "application/octet-stream"
4+
5+
fileprivate let mimeTypes = [
6+
"md": "text/markdown",
7+
"html": "text/html",
8+
"htm": "text/html",
9+
"shtml": "text/html",
10+
"css": "text/css",
11+
"xml": "text/xml",
12+
"gif": "image/gif",
13+
"jpeg": "image/jpeg",
14+
"jpg": "image/jpeg",
15+
"js": "application/javascript",
16+
"atom": "application/atom+xml",
17+
"rss": "application/rss+xml",
18+
"mml": "text/mathml",
19+
"txt": "text/plain",
20+
"jad": "text/vnd.sun.j2me.app-descriptor",
21+
"wml": "text/vnd.wap.wml",
22+
"htc": "text/x-component",
23+
"png": "image/png",
24+
"tif": "image/tiff",
25+
"tiff": "image/tiff",
26+
"wbmp": "image/vnd.wap.wbmp",
27+
"ico": "image/x-icon",
28+
"jng": "image/x-jng",
29+
"bmp": "image/x-ms-bmp",
30+
"svg": "image/svg+xml",
31+
"svgz": "image/svg+xml",
32+
"webp": "image/webp",
33+
"woff": "application/font-woff",
34+
"jar": "application/java-archive",
35+
"war": "application/java-archive",
36+
"ear": "application/java-archive",
37+
"json": "application/json",
38+
"hqx": "application/mac-binhex40",
39+
"doc": "application/msword",
40+
"pdf": "application/pdf",
41+
"ps": "application/postscript",
42+
"eps": "application/postscript",
43+
"ai": "application/postscript",
44+
"rtf": "application/rtf",
45+
"m3u8": "application/vnd.apple.mpegurl",
46+
"xls": "application/vnd.ms-excel",
47+
"eot": "application/vnd.ms-fontobject",
48+
"ppt": "application/vnd.ms-powerpoint",
49+
"wmlc": "application/vnd.wap.wmlc",
50+
"kml": "application/vnd.google-earth.kml+xml",
51+
"kmz": "application/vnd.google-earth.kmz",
52+
"7z": "application/x-7z-compressed",
53+
"cco": "application/x-cocoa",
54+
"jardiff": "application/x-java-archive-diff",
55+
"jnlp": "application/x-java-jnlp-file",
56+
"run": "application/x-makeself",
57+
"pl": "application/x-perl",
58+
"pm": "application/x-perl",
59+
"prc": "application/x-pilot",
60+
"pdb": "application/x-pilot",
61+
"rar": "application/x-rar-compressed",
62+
"rpm": "application/x-redhat-package-manager",
63+
"sea": "application/x-sea",
64+
"swf": "application/x-shockwave-flash",
65+
"sit": "application/x-stuffit",
66+
"tcl": "application/x-tcl",
67+
"tk": "application/x-tcl",
68+
"der": "application/x-x509-ca-cert",
69+
"pem": "application/x-x509-ca-cert",
70+
"crt": "application/x-x509-ca-cert",
71+
"xpi": "application/x-xpinstall",
72+
"xhtml": "application/xhtml+xml",
73+
"xspf": "application/xspf+xml",
74+
"zip": "application/zip",
75+
"bin": "application/octet-stream",
76+
"exe": "application/octet-stream",
77+
"dll": "application/octet-stream",
78+
"deb": "application/octet-stream",
79+
"dmg": "application/octet-stream",
80+
"iso": "application/octet-stream",
81+
"img": "application/octet-stream",
82+
"msi": "application/octet-stream",
83+
"msp": "application/octet-stream",
84+
"msm": "application/octet-stream",
85+
"docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
86+
"xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
87+
"pptx": "application/vnd.openxmlformats-officedocument.presentationml.presentation",
88+
"mid": "audio/midi",
89+
"midi": "audio/midi",
90+
"kar": "audio/midi",
91+
"mp3": "audio/mpeg",
92+
"ogg": "audio/ogg",
93+
"m4a": "audio/x-m4a",
94+
"ra": "audio/x-realaudio",
95+
"3gpp": "video/3gpp",
96+
"3gp": "video/3gpp",
97+
"ts": "video/mp2t",
98+
"mp4": "video/mp4",
99+
"mpeg": "video/mpeg",
100+
"mpg": "video/mpeg",
101+
"mov": "video/quicktime",
102+
"webm": "video/webm",
103+
"flv": "video/x-flv",
104+
"m4v": "video/x-m4v",
105+
"mng": "video/x-mng",
106+
"asx": "video/x-ms-asf",
107+
"asf": "video/x-ms-asf",
108+
"wmv": "video/x-ms-wmv",
109+
"avi": "video/x-msvideo"
110+
]
111+
112+
fileprivate func mimeFromExt(ext: String?) -> String {
113+
let lower: String = ext!.lowercased()
114+
if ext != nil && mimeTypes.contains(where: { $0.0 == lower }) {
115+
return mimeTypes[lower]!
116+
}
117+
return DEFAULT_MIME_TYPE
118+
}
119+
120+
extension NSString {
121+
public func mimeType() -> String {
122+
return mimeFromExt(ext: self.pathExtension)
123+
}
124+
}
125+
126+
extension String {
127+
public func mimeType() -> String {
128+
return (self as NSString).mimeType()
129+
}
130+
}

Sources/Appwrite/Models/File.swift

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,41 @@
11
import Foundation
22
import NIO
33

4-
open class File {
4+
open class InputFile {
55

6-
public let name: String
7-
public var buffer: ByteBuffer
6+
public var path: String = ""
7+
public var filename: String = ""
8+
public var mimeType: String = ""
9+
public var sourceType: String = ""
10+
public var data: Any? = nil
811

9-
public init(name: String, buffer: ByteBuffer) {
10-
self.name = name
11-
self.buffer = buffer
12+
internal init() {
13+
}
14+
15+
public static func fromPath(_ path: String) -> InputFile {
16+
let instance = InputFile()
17+
instance.path = path
18+
instance.filename = URL(fileURLWithPath: path, isDirectory: false).lastPathComponent
19+
instance.mimeType = path.mimeType()
20+
instance.sourceType = "path"
21+
return instance
22+
}
23+
24+
public static func fromData(_ data: Data, filename: String, mimeType: String) -> InputFile {
25+
let instance = InputFile()
26+
instance.filename = filename
27+
instance.mimeType = mimeType
28+
instance.sourceType = "data"
29+
instance.data = data
30+
return instance
31+
}
32+
33+
public static func fromBuffer(_ buffer: ByteBuffer, filename: String, mimeType: String) -> InputFile {
34+
let instance = InputFile()
35+
instance.filename = filename
36+
instance.mimeType = mimeType
37+
instance.sourceType = "buffer"
38+
instance.data = buffer
39+
return instance
1240
}
1341
}

0 commit comments

Comments
 (0)