forked from recloudstream/cloudstream
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbackup_LocalLibraryCache.kt
More file actions
221 lines (188 loc) · 8.13 KB
/
backup_LocalLibraryCache.kt
File metadata and controls
221 lines (188 loc) · 8.13 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package com.lagradost.cloudstream3.utils
import android.content.Context
import android.graphics.Bitmap
import android.media.MediaMetadataRetriever
import com.lagradost.cloudstream3.database.*
import com.lagradost.cloudstream3.mvvm.logError
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.io.File
import java.io.FileOutputStream
import java.net.URL
class LocalLibraryCache(private val context: Context) {
companion object {
const val CACHE_FOLDER = "local_cache"
const val POSTERS_FOLDER = "posters"
const val EPISODES_FOLDER = "episodes"
const val METADATA_FOLDER = "metadata"
}
private val cacheDir = File(context.filesDir, CACHE_FOLDER)
init {
// Ensure cache directories exist
File(cacheDir, POSTERS_FOLDER).mkdirs()
File(cacheDir, EPISODES_FOLDER).mkdirs()
File(cacheDir, METADATA_FOLDER).mkdirs()
}
suspend fun cacheSeriesData(
seriesId: String,
name: String,
posterUrl: String?,
bannerUrl: String?,
description: String?,
rating: Int?,
year: Int?,
status: String?,
episodes: List<com.lagradost.cloudstream3.EpisodeResponse>
): Result<Unit> = withContext(Dispatchers.IO) {
try {
// 1. Cache poster
val posterPath = posterUrl?.let { downloadImage(it, "posters/${seriesId}_poster.jpg") }
val bannerPath = bannerUrl?.let { downloadImage(it, "posters/${seriesId}_banner.jpg") }
// 2. Cache series metadata
val cachedSeries = CachedSeriesEntity(
id = seriesId,
name = name,
posterPath = posterPath,
bannerPath = bannerPath,
description = description,
rating = rating,
year = year,
status = status,
cachedAt = System.currentTimeMillis(),
lastRefetchedAt = System.currentTimeMillis()
)
context.setKey("local_cache/series/$seriesId", cachedSeries)
// 3. Cache episodes
val cachedEpisodes = episodes.map { episode ->
val episodePosterPath = downloadEpisodeThumbnail(episode, seriesId)
CachedEpisodeEntity(
id = episode.id.toString(),
seriesId = seriesId,
name = episode.name,
posterPath = episodePosterPath,
description = episode.description,
episode = episode.episode,
season = episode.season,
duration = episode.duration,
skipIntroStart = episode.skipintro?.start,
skipIntroEnd = episode.skipintro?.end,
filePath = null, // Will be set when downloaded
cachedAt = System.currentTimeMillis()
)
}
context.setKey("local_cache/episodes/$seriesId", cachedEpisodes)
// 4. Update cache status
val cacheStatus = CacheStatusEntity(
seriesId = seriesId,
postersCached = posterPath != null,
episodesCached = true,
metadataCached = true,
totalSize = calculateCacheSize(seriesId),
lastAccessed = System.currentTimeMillis()
)
context.setKey("local_cache/status/$seriesId", cacheStatus)
Result.success(Unit)
} catch (e: Exception) {
logError(e)
Result.failure(e)
}
}
suspend fun getCachedSeries(seriesId: String): CachedSeriesEntity? {
return context.getKey<CachedSeriesEntity>("local_cache/series/$seriesId")
}
suspend fun getCachedEpisodes(seriesId: String): List<CachedEpisodeEntity> {
return context.getKey<List<CachedEpisodeEntity>>("local_cache/episodes/$seriesId") ?: emptyList()
}
suspend fun updateEpisodeFilePath(seriesId: String, episodeId: String, filePath: String) {
val episodes = getCachedEpisodes(seriesId).toMutableList()
val episodeIndex = episodes.indexOfFirst { it.id == episodeId }
if (episodeIndex != -1) {
episodes[episodeIndex] = episodes[episodeIndex].copy(filePath = filePath)
context.setKey("local_cache/episodes/$seriesId", episodes)
}
}
private suspend fun downloadImage(url: String, filename: String): String? = withContext(Dispatchers.IO) {
try {
val posterFile = File(cacheDir, filename)
if (posterFile.exists()) return@withContext posterFile.absolutePath
val connection = URL(url).openConnection()
connection.getInputStream().use { input ->
FileOutputStream(posterFile).use { output ->
input.copyTo(output)
}
}
posterFile.absolutePath
} catch (e: Exception) {
logError(e)
null
}
}
private suspend fun downloadEpisodeThumbnail(
episode: com.lagradost.cloudstream3.EpisodeResponse,
seriesId: String
): String? = withContext(Dispatchers.IO) {
try {
// Try to get thumbnail from episode poster first
episode.poster?.let { posterUrl ->
downloadImage(posterUrl, "episodes/${seriesId}_${episode.id}_thumb.jpg")?.let { return@withContext it }
}
// If no poster, we'll need to generate from video file when available
null
} catch (e: Exception) {
logError(e)
null
}
}
suspend fun generateEpisodeThumbnail(videoFile: File, outputPath: String): Boolean = withContext(Dispatchers.IO) {
try {
val retriever = MediaMetadataRetriever()
retriever.setDataSource(videoFile.absolutePath)
// Get frame at 20 seconds (20,000,000 microseconds)
val bitmap = retriever.getFrameAtTime(20_000_000)
retriever.release()
if (bitmap != null) {
val thumbnailFile = File(cacheDir, outputPath)
FileOutputStream(thumbnailFile).use { out ->
bitmap.compress(Bitmap.CompressFormat.JPEG, 85, out)
}
true
} else {
false
}
} catch (e: Exception) {
logError(e)
false
}
}
private fun calculateCacheSize(seriesId: String): Long {
val seriesDir = File(cacheDir, POSTERS_FOLDER)
val episodesDir = File(cacheDir, EPISODES_FOLDER)
var totalSize = 0L
// Calculate poster sizes
seriesDir.listFiles { file -> file.name.startsWith(seriesId) }?.forEach { file ->
totalSize += file.length()
}
// Calculate episode thumbnail sizes
episodesDir.listFiles { file -> file.name.startsWith(seriesId) }?.forEach { file ->
totalSize += file.length()
}
return totalSize
}
suspend fun getCacheStatus(seriesId: String): CacheStatusEntity? {
return context.getKey<CacheStatusEntity>("local_cache/status/$seriesId")
}
suspend fun clearCache(seriesId: String) {
context.removeKey("local_cache/series/$seriesId")
context.removeKey("local_cache/episodes/$seriesId")
context.removeKey("local_cache/status/$seriesId")
// Delete cached files
File(cacheDir, POSTERS_FOLDER).listFiles { file -> file.name.startsWith(seriesId) }?.forEach { it.delete() }
File(cacheDir, EPISODES_FOLDER).listFiles { file -> file.name.startsWith(seriesId) }?.forEach { it.delete() }
}
suspend fun getTotalCacheSize(): Long {
val posterDir = File(cacheDir, POSTERS_FOLDER)
val episodesDir = File(cacheDir, EPISODES_FOLDER)
return (posterDir.walkTopDown().sumOf { it.length() } +
episodesDir.walkTopDown().sumOf { it.length() })
}
}