-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathDataSource.swift
More file actions
43 lines (37 loc) · 1.12 KB
/
DataSource.swift
File metadata and controls
43 lines (37 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import Foundation
import NitroModules
enum DataSource {
case http(url: URL)
case file(url: URL)
case bundle(resource: String, extension: String?)
case bytes(data: Data)
static func fromURL(_ url: URL) -> DataSource {
return url.isFileURL ? .file(url: url) : .http(url: url)
}
static func fromURL(string: String) throws -> DataSource {
guard let url = URL(string: string) else {
throw DataLoaderError.invalidURL(string)
}
return fromURL(url)
}
static func bundle(nameWithExtension: String) -> DataSource {
let name = (nameWithExtension as NSString).deletingPathExtension
let ext = (nameWithExtension as NSString).pathExtension
return .bundle(resource: name, extension: ext.isEmpty ? nil : ext)
}
static func bytes(from buffer: ArrayBuffer) -> DataSource {
return .bytes(data: buffer.toData(copyIfNeeded: false))
}
func createLoader() -> DataLoader {
switch self {
case .http:
return HTTPDataLoader.shared
case .file:
return FileDataLoader()
case .bundle:
return BundleDataLoader()
case .bytes:
return BytesDataLoader()
}
}
}