forked from ankidroid/Anki-Android
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnkiServer.kt
More file actions
182 lines (167 loc) · 7.29 KB
/
AnkiServer.kt
File metadata and controls
182 lines (167 loc) · 7.29 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
/*
* Copyright (c) 2022 Mani <infinyte01@gmail.com>
* Copyright (c) 2022 Brayan Oliveira <brayandso.dev@gmail.com>
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 3 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.ichi2.anki.pages
import android.app.Activity
import androidx.fragment.app.FragmentActivity
import anki.collection.OpChanges
import com.ichi2.anki.CollectionManager
import com.ichi2.anki.CollectionManager.withCol
import com.ichi2.anki.NoteEditor
import com.ichi2.anki.importCsvRaw
import com.ichi2.anki.importJsonFileRaw
import com.ichi2.anki.launchCatchingTask
import com.ichi2.anki.searchInBrowser
import com.ichi2.libanki.*
import com.ichi2.libanki.sched.computeFsrsWeightsRaw
import com.ichi2.libanki.sched.computeOptimalRetentionRaw
import com.ichi2.libanki.sched.evaluateWeightsRaw
import com.ichi2.libanki.stats.*
import fi.iki.elonen.NanoHTTPD
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
import timber.log.Timber
import java.io.ByteArrayInputStream
private const val PORT = 0
// const val PORT = 40001
// local debugging:
// ~/Local/Android/Sdk/platform-tools/adb forward tcp:40001 tcp:40001
open class AnkiServer(
val activity: FragmentActivity
) : NanoHTTPD("127.0.0.1", PORT) {
fun baseUrl(): String {
return "http://127.0.0.1:$listeningPort/"
}
override fun serve(session: IHTTPSession): Response {
val uri = session.uri
val mime = getMimeFromUri(uri)
if (session.method == Method.GET) {
val resourcePath = "web$uri"
val stream = this.javaClass.classLoader!!.getResourceAsStream(resourcePath)
Timber.d("GET: Requested %s (%s), found? %b", uri, resourcePath, stream != null)
return newChunkedResponse(Response.Status.OK, mime, stream)
}
if (session.method == Method.POST) {
Timber.d("POST: Requested %s", uri)
val inputBytes = getSessionBytes(session)
if (uri.startsWith(ANKI_PREFIX)) {
return buildResponse { handlePostRequest(uri.substring(ANKI_PREFIX.length), inputBytes) }
}
}
return newFixedLengthResponse(null)
}
private suspend fun handlePostRequest(methodName: String, bytes: ByteArray): ByteArray {
return when (methodName) {
"i18nResources" -> withCol { i18nResourcesRaw(bytes) }
"getGraphPreferences" -> withCol { getGraphPreferencesRaw() }
"setGraphPreferences" -> withCol { setGraphPreferencesRaw(bytes) }
"graphs" -> withCol { graphsRaw(bytes) }
"getNotetypeNames" -> withCol { getNotetypeNamesRaw(bytes) }
"getDeckNames" -> withCol { getDeckNamesRaw(bytes) }
"getCsvMetadata" -> withCol { getCsvMetadataRaw(bytes) }
"importCsv" -> activity.importCsvRaw(bytes)
"importJsonFile" -> importJsonFileRaw(bytes)
"importDone" -> bytes
"searchInBrowser" -> activity.searchInBrowser(bytes)
"completeTag" -> withCol { completeTagRaw(bytes) }
"getFieldNames" -> withCol { getFieldNamesRaw(bytes) }
"cardStats" -> withCol { cardStatsRaw(bytes) }
"getDeckConfig" -> withCol { getDeckConfigRaw(bytes) }
"getDeckConfigsForUpdate" -> withCol { getDeckConfigsForUpdateRaw(bytes) }
"updateDeckConfigs" -> activity.updateDeckConfigsRaw(bytes)
"computeFsrsWeights" -> withCol { computeFsrsWeightsRaw(bytes) }
"computeOptimalRetention" -> withCol { computeOptimalRetentionRaw(bytes) }
"setWantsAbort" -> CollectionManager.getBackend().setWantsAbortRaw(bytes)
"evaluateWeights" -> withCol { evaluateWeightsRaw(bytes) }
"latestProgress" -> CollectionManager.getBackend().latestProgressRaw(bytes)
"getImageForOcclusion" -> withCol { getImageForOcclusionRaw(bytes) }
"getImageOcclusionNote" -> withCol { getImageOcclusionNoteRaw(bytes) }
"getImageForOcclusionFields" -> withCol { getImageOcclusionFieldsRaw(bytes) }
"addImageOcclusionNote" -> {
val data = withCol {
addImageOcclusionNoteRaw(bytes)
}
undoableOp { OpChanges.parseFrom(data) }
activity.launchCatchingTask {
// Allow time for toast message to appear before closing editor
delay(1000)
activity.setResult(Activity.RESULT_OK)
activity.finish()
}
data
}
"updateImageOcclusionNote" -> {
val data = withCol {
updateImageOcclusionNoteRaw(bytes)
}
undoableOp { OpChanges.parseFrom(data) }
activity.launchCatchingTask {
// Allow time for toast message to appear before closing editor
delay(1000)
activity.setResult(NoteEditor.RESULT_UPDATED_IO_NOTE)
activity.finish()
}
data
}
"congratsInfo" -> withCol { congratsInfoRaw(bytes) }
else -> { throw Exception("unhandled request: $methodName") }
}
}
fun getSessionBytes(session: IHTTPSession): ByteArray {
val contentLength = session.headers["content-length"]!!.toInt()
val bytes = ByteArray(contentLength)
session.inputStream.read(bytes, 0, contentLength)
return bytes
}
fun buildResponse(
block: suspend CoroutineScope.() -> ByteArray
): Response {
return try {
val data = runBlocking {
block()
}
newChunkedResponse(data)
} catch (exc: Exception) {
newChunkedResponse(exc.localizedMessage?.encodeToByteArray(), status = Response.Status.INTERNAL_ERROR)
}
}
companion object {
/** Common prefix used on Anki requests */
const val ANKI_PREFIX = "/_anki/"
const val ANKIDROID_JS_PREFIX = "/jsapi/"
fun getMimeFromUri(uri: String): String {
return when (uri.substringAfterLast(".")) {
"ico" -> "image/x-icon"
"css" -> "text/css"
"js" -> "text/javascript"
"html" -> "text/html"
else -> "application/binary"
}
}
fun newChunkedResponse(
data: ByteArray?,
mimeType: String = "application/binary",
status: Response.IStatus = Response.Status.OK
): Response {
return if (data == null) {
newFixedLengthResponse(null)
} else {
newChunkedResponse(status, mimeType, ByteArrayInputStream(data))
}
}
}
}