mirror of
https://github.com/immich-app/immich.git
synced 2026-05-18 03:10:24 +03:00
set user agent
This commit is contained in:
@@ -48,6 +48,7 @@ android {
|
|||||||
}
|
}
|
||||||
|
|
||||||
buildFeatures {
|
buildFeatures {
|
||||||
|
buildConfig true
|
||||||
compose true
|
compose true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import android.graphics.ColorSpace
|
|||||||
import android.graphics.ImageDecoder
|
import android.graphics.ImageDecoder
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
import android.os.CancellationSignal
|
import android.os.CancellationSignal
|
||||||
|
import app.alextran.immich.BuildConfig
|
||||||
import app.alextran.immich.core.SSLConfig
|
import app.alextran.immich.core.SSLConfig
|
||||||
import com.google.net.cronet.okhttptransport.CronetCallFactory
|
import com.google.net.cronet.okhttptransport.CronetCallFactory
|
||||||
import okhttp3.Call
|
import okhttp3.Call
|
||||||
@@ -24,12 +25,27 @@ import java.nio.ByteBuffer
|
|||||||
import java.util.concurrent.ConcurrentHashMap
|
import java.util.concurrent.ConcurrentHashMap
|
||||||
import java.util.concurrent.Executors
|
import java.util.concurrent.Executors
|
||||||
import java.util.concurrent.TimeUnit
|
import java.util.concurrent.TimeUnit
|
||||||
|
import okhttp3.Interceptor
|
||||||
|
|
||||||
data class RemoteRequest(
|
data class RemoteRequest(
|
||||||
val callback: (Result<Map<String, Long>>) -> Unit,
|
val callback: (Result<Map<String, Long>>) -> Unit,
|
||||||
val cancellationSignal: CancellationSignal,
|
val cancellationSignal: CancellationSignal,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
class UserAgentInterceptor : Interceptor {
|
||||||
|
companion object {
|
||||||
|
const val USER_AGENT = "Immich_Android_${BuildConfig.VERSION_NAME}"
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun intercept(chain: Interceptor.Chain): Response {
|
||||||
|
return chain.proceed(
|
||||||
|
chain.request().newBuilder()
|
||||||
|
.header("User-Agent", USER_AGENT)
|
||||||
|
.build()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
class RemoteImagesImpl(context: Context) : RemoteImageApi {
|
class RemoteImagesImpl(context: Context) : RemoteImageApi {
|
||||||
private val requestMap = ConcurrentHashMap<Long, RemoteRequest>()
|
private val requestMap = ConcurrentHashMap<Long, RemoteRequest>()
|
||||||
private val lockedBitmaps = ConcurrentHashMap<Long, Bitmap>()
|
private val lockedBitmaps = ConcurrentHashMap<Long, Bitmap>()
|
||||||
@@ -96,6 +112,7 @@ class RemoteImagesImpl(context: Context) : RemoteImageApi {
|
|||||||
.enableBrotli(true)
|
.enableBrotli(true)
|
||||||
.setStoragePath(storageDir.absolutePath)
|
.setStoragePath(storageDir.absolutePath)
|
||||||
.enableHttpCache(CronetEngine.Builder.HTTP_CACHE_DISK, CACHE_SIZE_BYTES)
|
.enableHttpCache(CronetEngine.Builder.HTTP_CACHE_DISK, CACHE_SIZE_BYTES)
|
||||||
|
.setUserAgent(UserAgentInterceptor.USER_AGENT)
|
||||||
.build()
|
.build()
|
||||||
.also { cronetEngine = it }
|
.also { cronetEngine = it }
|
||||||
|
|
||||||
@@ -111,6 +128,7 @@ class RemoteImagesImpl(context: Context) : RemoteImageApi {
|
|||||||
)
|
)
|
||||||
|
|
||||||
val builder = OkHttpClient.Builder()
|
val builder = OkHttpClient.Builder()
|
||||||
|
.addInterceptor(UserAgentInterceptor())
|
||||||
.dispatcher(Dispatcher().apply { maxRequestsPerHost = MAX_REQUESTS_PER_HOST })
|
.dispatcher(Dispatcher().apply { maxRequestsPerHost = MAX_REQUESTS_PER_HOST })
|
||||||
.connectionPool(connectionPool)
|
.connectionPool(connectionPool)
|
||||||
|
|
||||||
|
|||||||
@@ -20,6 +20,8 @@ class RemoteImageApiImpl: NSObject, RemoteImageApi {
|
|||||||
private static let delegate = RemoteImageApiDelegate()
|
private static let delegate = RemoteImageApiDelegate()
|
||||||
static let session = {
|
static let session = {
|
||||||
let config = URLSessionConfiguration.default
|
let config = URLSessionConfiguration.default
|
||||||
|
let version = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String ?? "unknown"
|
||||||
|
config.httpAdditionalHeaders = ["User-Agent": "Immich_iOS_\(version)"]
|
||||||
let thumbnailPath = FileManager.default.temporaryDirectory.appendingPathComponent("thumbnails", isDirectory: true)
|
let thumbnailPath = FileManager.default.temporaryDirectory.appendingPathComponent("thumbnails", isDirectory: true)
|
||||||
try! FileManager.default.createDirectory(at: thumbnailPath, withIntermediateDirectories: true)
|
try! FileManager.default.createDirectory(at: thumbnailPath, withIntermediateDirectories: true)
|
||||||
config.urlCache = URLCache(
|
config.urlCache = URLCache(
|
||||||
|
|||||||
Reference in New Issue
Block a user