Skip to content

Commit

Permalink
feat (download): add setting to save tags in separate txt file
Browse files Browse the repository at this point in the history
  • Loading branch information
Yochyo committed May 14, 2023
1 parent 714db40 commit 83afe31
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ class PreferenceHelper(val context: Context) {
var limit: Int
get() = getPreference(context.getString(R.string.page_size), context.getString(R.string.page_size_default_value)).toInt()
set(value) = setPreference(context.getString(R.string.page_size), value.toString())
var saveTagsInTxt: Boolean
get() = getPreference(context.getString(R.string.tags_in_txt), context.resources.getBoolean(R.bool.tags_in_txt_default_value))
set(value) = setPreference(context.getString(R.string.tags_in_txt), value.toString())


var selectedServerId: Int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import de.yochyo.yummybooru.utils.general.FileUtils
import de.yochyo.yummybooru.utils.general.getDownloadPathAndId
import de.yochyo.yummybooru.utils.network.CacheableDownloader
import kotlinx.coroutines.*
import java.util.*

private class DownloadPost(val tags: String, val post: Post, val server: Server)
class DownloadService : Service() {
Expand Down Expand Up @@ -61,7 +60,7 @@ class DownloadService : Service() {
val url = getDownloadPathAndId(this@DownloadService, next.post).first
downloader.download(url, {
if (it != null) {
FileUtils.writeFile(this@DownloadService, finalNext.post, it, finalNext.server)
FileUtils.writePost(this@DownloadService, finalNext.post, it, finalNext.server)
withContext(Dispatchers.Main) {
util.announceFinishedDownload()
updateNotification(finalNext)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class InAppDownloadWorker(context: Context, parameters: WorkerParameters) : Coro
private suspend fun download(dl: Data) {
val it = downloader.downloadSync(dl.url, dl.headers)
if (it != null) {
if (FileUtils.writeFile(applicationContext, dl.post, it, dl.server) == FileWriteResult.FAILED)
if (FileUtils.writePost(applicationContext, dl.post, it, dl.server) == FileWriteResult.FAILED)
withContext(Dispatchers.Main) { Toast.makeText(applicationContext, "Saving ${dl.post.id} failed", Toast.LENGTH_SHORT).show() }
} else withContext(Dispatchers.Main) { Toast.makeText(applicationContext, "Failed downloading ${dl.post.id}", Toast.LENGTH_SHORT).show() }
setForeground(createForegroundInfo())
Expand Down
35 changes: 32 additions & 3 deletions app/src/main/java/de/yochyo/yummybooru/utils/general/FileUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ import android.content.Intent
import android.net.Uri
import androidx.documentfile.provider.DocumentFile
import de.yochyo.booruapi.api.Post
import de.yochyo.booruapi.api.TagType
import de.yochyo.yummybooru.R
import de.yochyo.yummybooru.api.entities.Resource2
import de.yochyo.yummybooru.api.entities.Server
import de.yochyo.yummybooru.database.preferences
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.io.InputStream

Expand Down Expand Up @@ -38,10 +41,15 @@ object FileUtils {
}
}

suspend fun writeFile(context: Context, post: Post, res: Resource2, server: Server): FileWriteResult {
suspend fun writePost(context: Context, post: Post, res: Resource2, server: Server): FileWriteResult {
return withContext(Dispatchers.IO) {
try {
val file = createFileOrNull(context, post, server, res.mimetype)
val file = createPostFileOrNull(context, post, server, res.mimetype)
if (context.preferences.saveTagsInTxt) {
writeTagFile(context, post, server)
}


if (file != null) {
writeBytes(context, file, res.input)
res.input.close()
Expand All @@ -54,7 +62,28 @@ object FileUtils {
}
}

private suspend fun createFileOrNull(context: Context, post: Post, server: Server, mimeType: String): DocumentFile? {
suspend fun writeTagFile(context: Context, post: Post, server: Server) {
GlobalScope.launch(Dispatchers.IO) {
try {
val file = createFileOrNull(context, server.urlHost, postToFilename(post, "txt", server), "txt")
val tags = post.getTags().joinToString("\n") {
when (it.tagType) {
TagType.GENERAL -> it.name
TagType.ARTIST -> "artist:${it.name}"
TagType.COPYRIGHT -> "copyright:${it.name}"
TagType.CHARACTER -> "character:${it.name}"
TagType.META -> "meta:${it.name}"
TagType.UNKNOWN -> it.name
}
}
if (file != null) writeBytes(context, file, tags.byteInputStream())
} catch (e: Exception) {
e.printStackTrace()
}
}
}

private suspend fun createPostFileOrNull(context: Context, post: Post, server: Server, mimeType: String): DocumentFile? {
return createFileOrNull(context, server.urlHost, postToFilename(post, mimeType, server), mimeType)
}

Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/preferences.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="page_size">page_size_string</string>
<string name="tags_in_txt">saveTagsInTxt</string>
<string name="sort_tag_comparator">sort_tag_comparator</string>
<string name="downloadWebm">downloadWebm</string>
<string name="downloadOriginal">downloadOriginal</string>
Expand All @@ -21,6 +22,7 @@
<string name="window_privacy">window_privacy</string>

<string name="page_size_default_value">50</string>
<bool name="tags_in_txt_default_value">false</bool>
<string name="sort_tag_comparator_default_value">1</string>
<integer name="combined_search_sort_default_value">0</integer>
<bool name="downloadWebm_default_value">true</bool>
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@
<!-- ShowChangelogsDialog -->
<string name="changelogs">Changelogs</string>

<!-- ShowChangelogsDialog -->
<string name="download_until">Download until %d</string>
<string name="download_until_message">All posts until (and including) the oldest selected image will be downloaded.</string>

<!-- Events -->
<string name="download_post_with_id">Download %d</string>
Expand Down
8 changes: 7 additions & 1 deletion app/src/main/res/xml/settings_downloads.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,11 @@
android:key="@string/page_size"
android:summary="More posts can cause longer loading times."
android:title="Post per page"
app:allowDividerAbove="true" />
app:allowDividerAbove="true"/>

<androidx.preference.SwitchPreference
android:defaultValue="@bool/tags_in_txt_default_value"
android:key="@string/tags_in_txt"
android:summary="save tags in separate txt file instead of filename"
android:title="save tags in .txt file"/>
</PreferenceScreen>

0 comments on commit 83afe31

Please sign in to comment.