-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabaseHelper.swift
203 lines (179 loc) · 8.32 KB
/
DatabaseHelper.swift
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
200
201
202
203
//
// DatabaseHelper.swift
// AccentEasy
//
// Created by Hai Lu on 20/01/2016.
// Copyright © 2016 Hai Lu. All rights reserved.
//
import Foundation
import SSZipArchive
public class DatabaseVersion: Mappable {
var status = false
var message: String!
var data: String!
var version = 0
required public init?(_ map: Map) {
}
required public init(){
}
// Mappable
public func mapping(map: Map) {
version <= map["version"]
status <= map["status"]
message <= map["message"]
data <= map["data"]
}
}
public class DatabaseHelper {
class func getCourseSessionFilePath(username: String) -> String {
return FileHelper.getFilePath("database/course_session/\(StringHelper.md5(string: username)).json")
}
class func updateCourses(courseSession: AECourseSession) -> Bool {
FileHelper.getFilePath("database", directory: true)
FileHelper.getFilePath("database/course_session", directory: true)
let courseSessionFilePath = getCourseSessionFilePath(courseSession.username)
var oldSession = AECourseSession()
if FileHelper.isExists(courseSessionFilePath) {
oldSession = try! JSONHelper.fromJson(FileHelper.readFile(courseSessionFilePath))
}
var status = true
if courseSession.courses.count > 0 {
for course in courseSession.courses {
var versionChanged = false
var found = false
if oldSession.courses.count > 0 {
for oldCourse in oldSession.courses {
if (oldCourse.idString != nil) && (course.idString != nil) && (oldCourse.idString == course.idString){
found = true
if (oldCourse.version != course.version) {
versionChanged = true
Logger.log("found version change. from \(oldCourse.idString) to \(course.version). Course id \(course.idString)")
}
}
}
}
if versionChanged || !found {
if !downloadCourseDatabase(course) {
status = false
break
}
}
}
}
if status {
} else {
Logger.log("no course database changed. skip by default")
}
try! FileHelper.writeFile(courseSessionFilePath, content: JSONHelper.toJson(courseSession))
return status
}
class func getCourseDbPath(course: AECourse) -> String {
return FileHelper.getFilePath("database/\(course.idString)/lesson.db")
}
class func downloadCourseDatabase(course: AECourse) -> Bool {
FileHelper.getFilePath("database", directory: true)
let dbDirPath = FileHelper.getFilePath("database/\(course.idString)", directory: true)
let tmpZip = FileHelper.getFilePath("database/\(course.idString)/tmp.zip")
let dbPath = getCourseDbPath(course)
FileHelper.deleteFile(tmpZip)
Logger.log("Download course version \(course.version). name \(course.name). url \(course.dbURL)")
HttpDownloader.loadFileSync(NSURL(string: course.dbURL)!, skipCache: true, destPath: tmpZip) { (path, error) -> Void in
do {
if (FileHelper.isExists(tmpZip)) {
FileHelper.deleteFile(dbPath)
Logger.log("Try to unzip database \(tmpZip)")
SSZipArchive.unzipFileAtPath(tmpZip, toDestination: dbDirPath)
if FileHelper.isExists(dbPath) {
Logger.log("Course database found. \(dbPath)")
} else {
Logger.log("No course database found at path \(dbPath)")
}
} else {
Logger.log("No zip file found at path \(tmpZip)")
}
} catch {
}
}
return FileHelper.isExists(dbPath)
}
class func checkDatabaseVersion(completion:(success: Bool) -> Void) {
let fileManager = NSFileManager.defaultManager()
// Create database folder if not exist
let dataPath = FileHelper.getFilePath("database", directory: true)
let versionPath = FileHelper.getFilePath("database/version")
let tmpZip = FileHelper.getFilePath("database/tmp.zip")
var dbVersion = DatabaseVersion()
if (FileHelper.isExists(versionPath)) {
do {
dbVersion = try JSONHelper.fromJson(FileHelper.readFile(versionPath))
Logger.log("Previous version \(dbVersion.version)")
} catch {
}
}
let client = Client()
.baseUrl(FileHelper.getAccentEasyBaseUrl())
.onError({e in
Logger.log(e)
completion(success: false)
});
var willLoad = false
client.get("/CheckVersion").type("json").query(["version" : String(dbVersion.version), "profile" : JSONHelper.toJson(AccountManager.currentUser())]).send([])
.end({(res:Response) -> Void in
if(res.error) { // status of 2xx
//handleResponseJson(res.body)
//Logger.log(res.body)
Logger.log(res.text)
completion(success: false)
}
else {
//handleErrorJson(res.body)
Logger.log("success CheckVersion")
Logger.log(res.text)
let dbVersionRes: DatabaseVersion = JSONHelper.fromJson(res.text!)
if dbVersionRes.status {
willLoad = true
dbVersion = dbVersionRes
}
let lessonDbFilePath = FileHelper.getFilePath(LiteDatabase.LESSON)
if willLoad {
FileHelper.deleteFile(tmpZip)
Logger.log("Try to load database zip from url \(dbVersion.data) to \(tmpZip)")
HttpDownloader.loadFileAsync(NSURL(string: dbVersion.data)!, skipCache: true, destPath: tmpZip) { (path, error) -> Void in
do {
if (FileHelper.isExists(tmpZip)) {
FileHelper.deleteFile(lessonDbFilePath)
Logger.log("Try to unzip dabase version")
SSZipArchive.unzipFileAtPath(tmpZip, toDestination: dataPath)
if fileManager.fileExistsAtPath(lessonDbFilePath) {
Logger.log("Lesson database found. Try to save database version info")
try FileHelper.writeFile(versionPath, content: JSONHelper.toJson(dbVersion))
} else {
Logger.log("No lesson database found at path \(lessonDbFilePath)")
}
} else {
Logger.log("No zip file found at path \(tmpZip)")
}
} catch {
}
completion(success: fileManager.fileExistsAtPath(lessonDbFilePath))
}
} else {
Logger.log("Use current version. Skip update database")
completion(success: fileManager.fileExistsAtPath(lessonDbFilePath))
}
}
})
}
class func getFreeStyleDatabaseFile() -> String? {
return FileHelper.getFilePath(LiteDatabase.FREESTYLE)
}
class func getLessonDatabaseFile() -> String? {
return FileHelper.getFilePath(LiteDatabase.LESSON)
}
class func getLessonUserScoreDatabaseFile() -> String? {
return FileHelper.getFilePath(LiteDatabase.LESSONUSERSCORE)
}
class func getLessonUserHistoryDatabaseFile() -> String? {
return FileHelper.getFilePath(LiteDatabase.LESSONUSERHISTORY)
}
}