pull/653/merge
Varun Patil 2023-10-01 20:19:24 -07:00
parent 44da3fc059
commit 882a03312f
4 changed files with 78 additions and 103 deletions

View File

@ -15,9 +15,7 @@ window.loggedIn = async () => {
let localList = null;
async function openLocal() {
// Get the list of local folders for next screen
(async () => {
const res = await fetch("http://127.0.0.1/api/config/local-folders");
localList = await res.json();
localList = JSON.parse(window.nativex?.configGetLocalFolders());
// Add HTML for folders list
document.getElementById("folder-list").innerHTML = localList
@ -30,7 +28,6 @@ async function openLocal() {
`
)
.join("");
})();
// Show the folders list
sync.classList.add("invisible");

View File

@ -189,23 +189,9 @@ class MainActivity : AppCompatActivity() {
}
fun loadDefaultUrl(): Boolean {
// Load accounts
val authHeader = nativex.account.authHeader
val memoriesUrl = nativex.account.memoriesUrl
// Load app interface if authenticated
if (authHeader != null && memoriesUrl != null) {
// Get host name
host = Uri.parse(memoriesUrl).host
// Set authorization header
binding.webview.loadUrl(
memoriesUrl, mapOf(
"Authorization" to authHeader
)
)
return true
}
host = nativex.http.loadWebView(binding.webview)
if (host != null) return true
// Load welcome page
binding.webview.loadUrl("file:///android_asset/welcome.html");

View File

@ -10,19 +10,22 @@ import android.widget.Toast
import androidx.media3.common.util.UnstableApi
import gallery.memories.service.AccountService
import gallery.memories.service.DownloadService
import gallery.memories.service.HttpService
import gallery.memories.service.ImageService
import gallery.memories.service.TimelineQuery
import org.json.JSONArray
import java.io.ByteArrayInputStream
import java.net.URLDecoder
@UnstableApi class NativeX(private val mCtx: MainActivity) {
@UnstableApi
class NativeX(private val mCtx: MainActivity) {
val TAG = NativeX::class.java.simpleName
private var themeStored = false
val query = TimelineQuery(mCtx)
val image = ImageService(mCtx, query)
val account = AccountService(mCtx)
val http = HttpService()
val account = AccountService(mCtx, http)
init {
dlService = DownloadService(mCtx, query)
@ -60,7 +63,7 @@ import java.net.URLDecoder
@JavascriptInterface
fun setThemeColor(color: String?, isDark: Boolean) {
// Save for getting it back on next start
if (!themeStored && account.authHeader != null) {
if (!themeStored && http.isLoggedIn()) {
themeStored = true
mCtx.storeTheme(color, isDark);
}
@ -162,9 +165,15 @@ import java.net.URLDecoder
"GET" -> {
routerGet(request)
}
"OPTIONS" -> {
WebResourceResponse("text/plain", "UTF-8", ByteArrayInputStream("".toByteArray()))
WebResourceResponse(
"text/plain",
"UTF-8",
ByteArrayInputStream("".toByteArray())
)
}
else -> {
throw Exception("Method Not Allowed")
}
@ -200,7 +209,12 @@ import java.net.URLDecoder
} else if (path.matches(API.IMAGE_INFO)) {
makeResponse(query.getImageInfo(parts[4].toLong()))
} else if (path.matches(API.IMAGE_DELETE)) {
makeResponse(query.delete(parseIds(parts[4]), request.url.getBooleanQueryParameter("dry", false)))
makeResponse(
query.delete(
parseIds(parts[4]),
request.url.getBooleanQueryParameter("dry", false)
)
)
} else if (path.matches(API.IMAGE_PREVIEW)) {
makeResponse(image.getPreview(parts[3].toLong()), "image/jpeg")
} else if (path.matches(API.IMAGE_FULL)) {
@ -227,7 +241,11 @@ import java.net.URLDecoder
}
private fun makeErrorResponse(): WebResourceResponse {
val response = WebResourceResponse("application/json", "UTF-8", ByteArrayInputStream("{}".toByteArray()))
val response = WebResourceResponse(
"application/json",
"UTF-8",
ByteArrayInputStream("{}".toByteArray())
)
response.setStatusCodeAndReasonPhrase(500, "Internal Server Error")
return response
}

View File

@ -2,7 +2,6 @@ package gallery.memories.service
import android.content.Intent
import android.net.Uri
import android.util.Base64
import android.util.Log
import android.widget.Toast
import androidx.media3.common.util.UnstableApi
@ -18,14 +17,11 @@ import org.json.JSONObject
import java.net.SocketTimeoutException
@UnstableApi
class AccountService(private val mCtx: MainActivity) {
class AccountService(private val mCtx: MainActivity, private val mHttp: HttpService) {
companion object {
val TAG = AccountService::class.java.simpleName
}
var authHeader: String? = null
var memoriesUrl: String? = null
/**
* Login to a server
* @param baseUrl The base URL of the server
@ -151,24 +147,11 @@ class AccountService(private val mCtx: MainActivity) {
* Makes a toast to the user if something is wrong
*/
fun checkCredentialsAndVersion() {
if (memoriesUrl == null) return
if (mHttp.isLoggedIn()) return
val request = Request.Builder()
.url(memoriesUrl + "api/describe")
.get()
.header("Authorization", authHeader ?: "")
.build()
val response: Response
try {
response = OkHttpClient().newCall(request).execute()
} catch (e: Exception) {
Log.w(TAG, "checkCredentialsAndVersion: ", e)
return
}
val body = response.body?.string()
response.body?.close()
val response = mHttp.getApiDescription()
val body = mHttp.bodyJson(response)
// Check status code
if (response.code == 401) {
@ -186,12 +169,12 @@ class AccountService(private val mCtx: MainActivity) {
return
}
val json = JSONObject(body)
val version = json.getString("version")
val uid = json.get("uid")
// Get body values
val uid = body.get("uid")
val version = body.getString("version")
// Check UID exists
if (uid.equals(null) && authHeader != null) {
if (uid.equals(null)) {
return loggedOut()
}
@ -199,6 +182,10 @@ class AccountService(private val mCtx: MainActivity) {
if (Version(version) < Version(mCtx.getString(R.string.min_server_version))) {
return toast(mCtx.getString(R.string.err_no_ver))
}
} catch (e: Exception) {
Log.w(TAG, "checkCredentialsAndVersion: ", e)
return
}
}
/**
@ -224,8 +211,8 @@ class AccountService(private val mCtx: MainActivity) {
.putString("user", user)
.putString("password", password)
.apply()
memoriesUrl = url
setAuthHeader(Pair(user, password))
mHttp.setBaseUrl(url)
mHttp.setAuthHeader(Pair(user, password))
}
/**
@ -234,7 +221,7 @@ class AccountService(private val mCtx: MainActivity) {
*/
fun getCredentials(): Pair<String, String>? {
val prefs = mCtx.getSharedPreferences("credentials", 0)
memoriesUrl = prefs.getString("memoriesUrl", null)
mHttp.setBaseUrl(prefs.getString("memoriesUrl", null))
val user = prefs.getString("user", null)
val password = prefs.getString("password", null)
if (user == null || password == null) return null
@ -245,8 +232,8 @@ class AccountService(private val mCtx: MainActivity) {
* Delete the stored credentials
*/
fun deleteCredentials() {
authHeader = null
memoriesUrl = null
mHttp.setAuthHeader(null)
mHttp.setBaseUrl(null)
mCtx.getSharedPreferences("credentials", 0).edit()
.remove("memoriesUrl")
.remove("user")
@ -258,20 +245,7 @@ class AccountService(private val mCtx: MainActivity) {
* Refresh the authorization header
*/
fun refreshAuthHeader() {
setAuthHeader(getCredentials())
}
/**
* Set the authorization header
* @param credentials The credentials to use
*/
private fun setAuthHeader(credentials: Pair<String, String>?) {
if (credentials != null) {
val auth = "${credentials.first}:${credentials.second}"
authHeader = "Basic ${Base64.encodeToString(auth.toByteArray(), Base64.NO_WRAP)}"
return
}
authHeader = null
mHttp.setAuthHeader(getCredentials())
}
/**