Skip to content

Commit d3b0a68

Browse files
feat: introduce 0.15.x support
1 parent e127e8d commit d3b0a68

File tree

82 files changed

+1456
-323
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+1456
-323
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-swift.svg?color=green&style=flat-square)
44
![License](https://img.shields.io/github/license/appwrite/sdk-for-swift.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-swift/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-swift/releases).**
1111

1212
> This is the Swift SDK for integrating with Appwrite from your Swift server-side code. If you're looking for the Apple SDK you should check [appwrite/sdk-for-apple](https://github.com/appwrite/sdk-for-apple)
1313
@@ -33,7 +33,7 @@ Add the package to your `Package.swift` dependencies:
3333

3434
```swift
3535
dependencies: [
36-
.package(url: "[email protected]:appwrite/sdk-for-swift.git", from: "0.5.0"),
36+
.package(url: "[email protected]:appwrite/sdk-for-swift.git", from: "0.6.0"),
3737
],
3838
```
3939

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:swift:0.5.0",
24-
"X-Appwrite-Response-Format": "0.14.0"
23+
"x-sdk-version": "appwrite:swift:0.6.0",
24+
"X-Appwrite-Response-Format": "0.15.0"
2525
]
2626

2727
open var config: [String: String] = [:]
@@ -320,7 +320,7 @@ open class Client {
320320
case is Bool.Type:
321321
return true as! T
322322
case is ByteBuffer.Type:
323-
return response.body as! T
323+
return try await response.body.collect(upTo: Int.max) as! T
324324
default:
325325
let data = try await response.body.collect(upTo: Int.max)
326326
if data.readableBytes == 0 {
@@ -373,10 +373,21 @@ open class Client {
373373
converter: (([String: Any]) -> T)? = nil,
374374
onProgress: ((UploadProgress) -> Void)? = nil
375375
) async throws -> T {
376-
let file = params[paramName] as! File
377-
let size = file.buffer.readableBytes
376+
let input = params[paramName] as! InputFile
377+
378+
switch(input.sourceType) {
379+
case "path":
380+
input.data = ByteBuffer(data: try! Data(contentsOf: URL(fileURLWithPath: input.path)))
381+
case "data":
382+
input.data = ByteBuffer(data: input.data as! Data)
383+
default:
384+
break
385+
}
386+
387+
let size = (input.data as! ByteBuffer).readableBytes
378388

379389
if size < Client.chunkSize {
390+
params[paramName] = input
380391
return try await call(
381392
method: "POST",
382393
path: path,
@@ -386,7 +397,6 @@ open class Client {
386397
)
387398
}
388399

389-
let input = file.buffer
390400
var offset = 0
391401
var result = [String:Any]()
392402

@@ -404,14 +414,10 @@ open class Client {
404414
}
405415

406416
while offset < size {
407-
let slice = input.getSlice(at: offset, length: Client.chunkSize)
408-
?? input.getSlice(at: offset, length: Int(size - offset))
417+
let slice = (input.data as! ByteBuffer).getSlice(at: offset, length: Client.chunkSize)
418+
?? (input.data as! ByteBuffer).getSlice(at: offset, length: Int(size - offset))
409419

410-
params[paramName] = File(
411-
name: file.name,
412-
buffer: slice!
413-
)
414-
420+
params[paramName] = InputFile.fromBuffer(slice!, filename: input.filename, mimeType: input.mimeType)
415421
headers["content-range"] = "bytes \(offset)-\(min((offset + Client.chunkSize) - 1, size))/\(size)"
416422

417423
result = try await call(
@@ -464,12 +470,15 @@ open class Client {
464470
bodyBuffer.writeString(CRLF)
465471
bodyBuffer.writeString("Content-Disposition: form-data; name=\"\(name)\"")
466472

467-
if let file = value as? File {
468-
bodyBuffer.writeString("; filename=\"\(file.name)\"")
473+
if let file = value as? InputFile {
474+
bodyBuffer.writeString("; filename=\"\(file.filename)\"")
469475
bodyBuffer.writeString(CRLF)
470476
bodyBuffer.writeString("Content-Length: \(bodyBuffer.readableBytes)")
471477
bodyBuffer.writeString(CRLF+CRLF)
472-
bodyBuffer.writeBuffer(&file.buffer)
478+
479+
var buffer = file.data! as! ByteBuffer
480+
481+
bodyBuffer.writeBuffer(&buffer)
473482
bodyBuffer.writeString(CRLF)
474483
return
475484
}
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)