Skip to content

Commit

Permalink
feat: introduce 0.15.x support
Browse files Browse the repository at this point in the history
  • Loading branch information
TorstenDittmann committed Jun 28, 2022
1 parent 871beac commit 42e094f
Show file tree
Hide file tree
Showing 28 changed files with 859 additions and 184 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

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

**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).**
**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).**

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)

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

```swift
dependencies: [
.package(url: "[email protected]:appwrite/sdk-for-apple.git", from: "0.5.0"),
.package(url: "[email protected]:appwrite/sdk-for-apple.git", from: "0.6.0"),
],
```

Expand Down
41 changes: 25 additions & 16 deletions Sources/Appwrite/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ open class Client {

open var headers: [String: String] = [
"content-type": "",
"x-sdk-version": "appwrite:swiftclient:0.5.0",
"X-Appwrite-Response-Format": "0.14.0"
"x-sdk-version": "appwrite:swiftclient:0.6.0",
"X-Appwrite-Response-Format": "0.15.0"
]

open var config: [String: String] = [:]
Expand Down Expand Up @@ -305,7 +305,7 @@ open class Client {
case is Bool.Type:
return true as! T
case is ByteBuffer.Type:
return response.body as! T
return try await response.body.collect(upTo: Int.max) as! T
default:
let data = try await response.body.collect(upTo: Int.max)
if data.readableBytes == 0 {
Expand Down Expand Up @@ -358,10 +358,21 @@ open class Client {
converter: (([String: Any]) -> T)? = nil,
onProgress: ((UploadProgress) -> Void)? = nil
) async throws -> T {
let file = params[paramName] as! File
let size = file.buffer.readableBytes
let input = params[paramName] as! InputFile

switch(input.sourceType) {
case "path":
input.data = ByteBuffer(data: try! Data(contentsOf: URL(fileURLWithPath: input.path)))
case "data":
input.data = ByteBuffer(data: input.data as! Data)
default:
break
}

let size = (input.data as! ByteBuffer).readableBytes

if size < Client.chunkSize {
params[paramName] = input
return try await call(
method: "POST",
path: path,
Expand All @@ -371,7 +382,6 @@ open class Client {
)
}

let input = file.buffer
var offset = 0
var result = [String:Any]()

Expand All @@ -389,14 +399,10 @@ open class Client {
}

while offset < size {
let slice = input.getSlice(at: offset, length: Client.chunkSize)
?? input.getSlice(at: offset, length: Int(size - offset))
let slice = (input.data as! ByteBuffer).getSlice(at: offset, length: Client.chunkSize)
?? (input.data as! ByteBuffer).getSlice(at: offset, length: Int(size - offset))

params[paramName] = File(
name: file.name,
buffer: slice!
)

params[paramName] = InputFile.fromBuffer(slice!, filename: input.filename, mimeType: input.mimeType)
headers["content-range"] = "bytes \(offset)-\(min((offset + Client.chunkSize) - 1, size))/\(size)"

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

if let file = value as? File {
bodyBuffer.writeString("; filename=\"\(file.name)\"")
if let file = value as? InputFile {
bodyBuffer.writeString("; filename=\"\(file.filename)\"")
bodyBuffer.writeString(CRLF)
bodyBuffer.writeString("Content-Length: \(bodyBuffer.readableBytes)")
bodyBuffer.writeString(CRLF+CRLF)
bodyBuffer.writeBuffer(&file.buffer)

var buffer = file.data! as! ByteBuffer

bodyBuffer.writeBuffer(&buffer)
bodyBuffer.writeString(CRLF)
return
}
Expand Down
130 changes: 130 additions & 0 deletions Sources/Appwrite/Extensions/String+MimeTypes.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
import Foundation

fileprivate let DEFAULT_MIME_TYPE = "application/octet-stream"

fileprivate let mimeTypes = [
"md": "text/markdown",
"html": "text/html",
"htm": "text/html",
"shtml": "text/html",
"css": "text/css",
"xml": "text/xml",
"gif": "image/gif",
"jpeg": "image/jpeg",
"jpg": "image/jpeg",
"js": "application/javascript",
"atom": "application/atom+xml",
"rss": "application/rss+xml",
"mml": "text/mathml",
"txt": "text/plain",
"jad": "text/vnd.sun.j2me.app-descriptor",
"wml": "text/vnd.wap.wml",
"htc": "text/x-component",
"png": "image/png",
"tif": "image/tiff",
"tiff": "image/tiff",
"wbmp": "image/vnd.wap.wbmp",
"ico": "image/x-icon",
"jng": "image/x-jng",
"bmp": "image/x-ms-bmp",
"svg": "image/svg+xml",
"svgz": "image/svg+xml",
"webp": "image/webp",
"woff": "application/font-woff",
"jar": "application/java-archive",
"war": "application/java-archive",
"ear": "application/java-archive",
"json": "application/json",
"hqx": "application/mac-binhex40",
"doc": "application/msword",
"pdf": "application/pdf",
"ps": "application/postscript",
"eps": "application/postscript",
"ai": "application/postscript",
"rtf": "application/rtf",
"m3u8": "application/vnd.apple.mpegurl",
"xls": "application/vnd.ms-excel",
"eot": "application/vnd.ms-fontobject",
"ppt": "application/vnd.ms-powerpoint",
"wmlc": "application/vnd.wap.wmlc",
"kml": "application/vnd.google-earth.kml+xml",
"kmz": "application/vnd.google-earth.kmz",
"7z": "application/x-7z-compressed",
"cco": "application/x-cocoa",
"jardiff": "application/x-java-archive-diff",
"jnlp": "application/x-java-jnlp-file",
"run": "application/x-makeself",
"pl": "application/x-perl",
"pm": "application/x-perl",
"prc": "application/x-pilot",
"pdb": "application/x-pilot",
"rar": "application/x-rar-compressed",
"rpm": "application/x-redhat-package-manager",
"sea": "application/x-sea",
"swf": "application/x-shockwave-flash",
"sit": "application/x-stuffit",
"tcl": "application/x-tcl",
"tk": "application/x-tcl",
"der": "application/x-x509-ca-cert",
"pem": "application/x-x509-ca-cert",
"crt": "application/x-x509-ca-cert",
"xpi": "application/x-xpinstall",
"xhtml": "application/xhtml+xml",
"xspf": "application/xspf+xml",
"zip": "application/zip",
"bin": "application/octet-stream",
"exe": "application/octet-stream",
"dll": "application/octet-stream",
"deb": "application/octet-stream",
"dmg": "application/octet-stream",
"iso": "application/octet-stream",
"img": "application/octet-stream",
"msi": "application/octet-stream",
"msp": "application/octet-stream",
"msm": "application/octet-stream",
"docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
"pptx": "application/vnd.openxmlformats-officedocument.presentationml.presentation",
"mid": "audio/midi",
"midi": "audio/midi",
"kar": "audio/midi",
"mp3": "audio/mpeg",
"ogg": "audio/ogg",
"m4a": "audio/x-m4a",
"ra": "audio/x-realaudio",
"3gpp": "video/3gpp",
"3gp": "video/3gpp",
"ts": "video/mp2t",
"mp4": "video/mp4",
"mpeg": "video/mpeg",
"mpg": "video/mpeg",
"mov": "video/quicktime",
"webm": "video/webm",
"flv": "video/x-flv",
"m4v": "video/x-m4v",
"mng": "video/x-mng",
"asx": "video/x-ms-asf",
"asf": "video/x-ms-asf",
"wmv": "video/x-ms-wmv",
"avi": "video/x-msvideo"
]

fileprivate func mimeFromExt(ext: String?) -> String {
let lower: String = ext!.lowercased()
if ext != nil && mimeTypes.contains(where: { $0.0 == lower }) {
return mimeTypes[lower]!
}
return DEFAULT_MIME_TYPE
}

extension NSString {
public func mimeType() -> String {
return mimeFromExt(ext: self.pathExtension)
}
}

extension String {
public func mimeType() -> String {
return (self as NSString).mimeType()
}
}
40 changes: 34 additions & 6 deletions Sources/Appwrite/Models/File.swift
Original file line number Diff line number Diff line change
@@ -1,13 +1,41 @@
import Foundation
import NIO

open class File {
open class InputFile {

public let name: String
public var buffer: ByteBuffer
public var path: String = ""
public var filename: String = ""
public var mimeType: String = ""
public var sourceType: String = ""
public var data: Any? = nil

public init(name: String, buffer: ByteBuffer) {
self.name = name
self.buffer = buffer
internal init() {
}

public static func fromPath(_ path: String) -> InputFile {
let instance = InputFile()
instance.path = path
instance.filename = URL(fileURLWithPath: path, isDirectory: false).lastPathComponent
instance.mimeType = path.mimeType()
instance.sourceType = "path"
return instance
}

public static func fromData(_ data: Data, filename: String, mimeType: String) -> InputFile {
let instance = InputFile()
instance.filename = filename
instance.mimeType = mimeType
instance.sourceType = "data"
instance.data = data
return instance
}

public static func fromBuffer(_ buffer: ByteBuffer, filename: String, mimeType: String) -> InputFile {
let instance = InputFile()
instance.filename = filename
instance.mimeType = mimeType
instance.sourceType = "buffer"
instance.data = buffer
return instance
}
}
Loading

0 comments on commit 42e094f

Please sign in to comment.