forked from ankidroid/Anki-Android
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
68 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
AnkiDroid/src/main/java/com/ichi2/anki/ViewerResourceHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright (c) 2024 Brayan Oliveira <[email protected]> | ||
* | ||
* 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 | ||
|
||
import android.content.Context | ||
import android.webkit.WebResourceRequest | ||
import android.webkit.WebResourceResponse | ||
import androidx.core.net.toFile | ||
import com.ichi2.utils.AssetHelper.guessMimeType | ||
import timber.log.Timber | ||
import java.io.FileInputStream | ||
|
||
class ViewerResourceHandler(context: Context) { | ||
private val mediaDir = CollectionHelper.getMediaDirectory(context).path | ||
|
||
/** | ||
* Loads resources from `collection.media` when requested by JS scripts. | ||
* | ||
* Differently from common media requests, scripts' requests have an `Origin` header | ||
* and are susceptible to CORS policy, so `Access-Control-Allow-Origin` is necessary. | ||
*/ | ||
fun shouldInterceptRequest(request: WebResourceRequest): WebResourceResponse? { | ||
val url = request.url | ||
if (request.method != "GET" || url.scheme != "file" || "Origin" !in request.requestHeaders) { | ||
return null | ||
} | ||
try { | ||
val file = url.toFile() | ||
if (file.parent != mediaDir || !file.exists()) { | ||
return null | ||
} | ||
val inputStream = FileInputStream(file) | ||
return WebResourceResponse(guessMimeType(file.path), null, inputStream).apply { | ||
responseHeaders = mapOf("Access-Control-Allow-Origin" to "*") | ||
} | ||
} catch (e: Exception) { | ||
Timber.d("File couldn't be loaded") | ||
return null | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters