-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathFiles.swift
More file actions
199 lines (123 loc) · 9.07 KB
/
Files.swift
File metadata and controls
199 lines (123 loc) · 9.07 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
//
// Files.swift
// Fetch
//
// Created by Stephen Radford on 08/08/2015.
// Copyright (c) 2015 Cocoon Development Ltd. All rights reserved.
//
import UIKit
import Alamofire
import SwiftyJSON
public class Files {
/// Fetch an array of files from a specific URL
public class func fetchWithURL(url: String, params: [String:String], sender: UIViewController, callback: ([File]) -> Void) {
Putio.networkActivityIndicatorVisible(true)
Alamofire.request(.GET, url, parameters: params)
.responseJSON { response in
Putio.networkActivityIndicatorVisible(false)
if(response.response?.statusCode >= 400 && response.response?.statusCode < 500) {
Putio.sharedInstance.delegate?.error400Received()
return
}
var files: [File] = []
if let error = response.result.error {
print(error)
} else {
var json = JSON(response.result.value!)
for (_, jFile): (String, JSON) in json["files"] {
let subtitles = ( jFile["opensubtitles_hash"].null != nil ) ? "" : jFile["opensubtitles_hash"].string!
let accessed = ( jFile["first_accessed_at"].null != nil ) ? false : true
let start_from = ( jFile["start_from"].null != nil ) ? 0 : jFile["start_from"].double!
let has_mp4 = (jFile["is_mp4_available"].bool != nil) ? jFile["is_mp4_available"].bool! : false
let file = File(id: jFile["id"].int32!, name: jFile["name"].string!, size: jFile["size"].int64!, icon: jFile["icon"].string!, content_type: jFile["content_type"].string!, has_mp4: has_mp4, parent_id: jFile["parent_id"].int32!, subtitles: subtitles, accessed: accessed, screenshot: jFile["screenshot"].string, is_shared: jFile["is_shared"].bool!, start_from: start_from)
file.created_at = jFile["created_at"].string
files.append(file)
}
}
callback(files)
}
}
public class func fetchMoviesFromURL(url: String, params: [String:String], sender: UIViewController, callback: ([File]) -> Void) {
Putio.networkActivityIndicatorVisible(true)
Alamofire.request(.GET, url, parameters: params)
.responseJSON { response in
Putio.networkActivityIndicatorVisible(false)
if(response.response?.statusCode >= 400 && response.response?.statusCode < 500) {
Putio.sharedInstance.delegate?.error400Received()
return
}
var files: [File] = []
if let error = response.result.error {
print(error)
} else {
var json = JSON(response.result.value!)
for (_, jFile): (String, JSON) in json["files"] {
let subtitles = ( jFile["opensubtitles_hash"].null != nil ) ? "" : jFile["opensubtitles_hash"].string!
let accessed = ( jFile["first_accessed_at"].null != nil ) ? false : true
let start_from = ( jFile["start_from"].null != nil ) ? 0 : jFile["start_from"].double!
let has_mp4 = (jFile["is_mp4_available"].bool != nil) ? jFile["is_mp4_available"].bool! : false
if !has_mp4 && jFile["content_type"] != "application/x-directory" && jFile["content_type"] != "video/mp4" {
continue
}
let file = File(id: jFile["id"].int32!, name: jFile["name"].string!, size: jFile["size"].int64!, icon: jFile["icon"].string!, content_type: jFile["content_type"].string!, has_mp4: has_mp4, parent_id: jFile["parent_id"].int32!, subtitles: subtitles, accessed: accessed, screenshot: jFile["screenshot"].string, is_shared: jFile["is_shared"].bool!, start_from: start_from)
file.created_at = jFile["created_at"].string
files.append(file)
}
}
callback(files)
}
}
/// Fetch an array of folders from a specific URL
public class func fetchFoldersFromURL(url: String, params: [String:String], callback: ([File]) -> Void) {
Putio.networkActivityIndicatorVisible(true)
Alamofire.request(.GET, url, parameters: params)
.responseJSON { response in
Putio.networkActivityIndicatorVisible(false)
var files: [File] = []
if let error = response.result.error {
print(error)
} else {
var json = JSON(response.result.value!)
for (_, jFile): (String, JSON) in json["files"] {
if jFile["content_type"].string! != "application/x-directory" || jFile["is_shared"].bool! {
continue
}
let subtitles = ( jFile["opensubtitles_hash"].null != nil ) ? "" : jFile["opensubtitles_hash"].string!
let accessed = ( jFile["first_accessed_at"].null != nil ) ? false : true
let start_from = ( jFile["start_from"].null != nil ) ? 0 : jFile["start_from"].double!
let file = File(id: jFile["id"].int32!, name: jFile["name"].string!, size: jFile["size"].int64!, icon: jFile["icon"].string!, content_type: jFile["content_type"].string!, has_mp4: jFile["is_mp4_available"].bool!, parent_id: jFile["parent_id"].int32!, subtitles: subtitles, accessed: accessed, screenshot: jFile["screenshot"].string, is_shared: jFile["is_shared"].bool!, start_from: start_from)
file.created_at = jFile["created_at"].string
files.append(file)
}
}
callback(files)
}
}
/// Fetch an array of folders from a specific URL but exclude a file
public class func fetchFoldersWithExclusionFromURL(url: String, params: [String:String], exclude: File?, callback: ([File]) -> Void) {
Putio.networkActivityIndicatorVisible(true)
Alamofire.request(.GET, url, parameters: params)
.responseJSON { response in
Putio.networkActivityIndicatorVisible(false)
var files: [File] = []
if let error = response.result.error {
print(error)
} else {
var json = JSON(response.result.value!)
for (_, jFile): (String, JSON) in json["files"] {
if jFile["content_type"].string! != "application/x-directory" || jFile["is_shared"].bool! || (exclude != nil && jFile["id"].int32! == exclude!.id) {
continue
}
let subtitles = ( jFile["opensubtitles_hash"].null != nil ) ? "" : jFile["opensubtitles_hash"].string!
let accessed = ( jFile["first_accessed_at"].null != nil ) ? false : true
let start_from = ( jFile["start_from"].null != nil ) ? 0 : jFile["start_from"].double!
let has_mp4 = (jFile["is_mp4_available"].bool != nil) ? jFile["is_mp4_available"].bool! : false
let file = File(id: jFile["id"].int32!, name: jFile["name"].string!, size: jFile["size"].int64!, icon: jFile["icon"].string!, content_type: jFile["content_type"].string!, has_mp4: has_mp4, parent_id: jFile["parent_id"].int32!, subtitles: subtitles, accessed: accessed, screenshot: jFile["screenshot"].string, is_shared: jFile["is_shared"].bool!, start_from: start_from)
file.created_at = jFile["created_at"].string
files.append(file)
}
}
callback(files)
}
}
}