This commit is contained in:
kunfei
2023-06-16 21:05:00 +08:00
parent 5776ffa056
commit b808097beb
4 changed files with 34 additions and 4 deletions
+1
View File
@@ -15,6 +15,7 @@
**2023/06/16**
* 修复书源拖动排序乱跳的问题
* 上传导出支持压缩
**2023/06/15**
@@ -5,7 +5,11 @@ import io.legado.app.exception.NoStackTraceException
import io.legado.app.model.analyzeRule.AnalyzeRule
import io.legado.app.model.analyzeRule.AnalyzeUrl
import io.legado.app.utils.ACache
import io.legado.app.utils.FileUtils
import io.legado.app.utils.GSON
import io.legado.app.utils.compress.ZipUtils
import io.legado.app.utils.createFileReplace
import io.legado.app.utils.externalCache
import io.legado.app.utils.fromJsonArray
import io.legado.app.utils.fromJsonObject
import splitties.init.appCtx
@@ -27,8 +31,27 @@ object DirectLinkUpload {
if (downloadUrlRule.isBlank()) {
throw NoStackTraceException("下载地址规则未配置")
}
var mFileName = fileName
var mFile = file
var mContentType = contentType
if (rule.compress && contentType != "application/zip") {
mFileName = "$fileName.zip"
mContentType = "application/zip"
mFile = when (file) {
is File -> {
val zipFile = File(FileUtils.getPath(appCtx.externalCache, "upload", mFileName))
zipFile.createFileReplace()
ZipUtils.zipFile(file, zipFile)
zipFile
}
is ByteArray -> ZipUtils.zipByteArray(file)
is String -> ZipUtils.zipString(file)
else -> ZipUtils.zipString(GSON.toJson(file))
}
}
val analyzeUrl = AnalyzeUrl(url)
val res = analyzeUrl.upload(fileName, file, contentType)
val res = analyzeUrl.upload(mFileName, mFile, mContentType)
val analyzeRule = AnalyzeRule().setContent(res.body, res.url)
val downloadUrl = analyzeRule.getString(downloadUrlRule)
if (downloadUrl.isBlank()) {
@@ -42,7 +42,9 @@ fun File.createFileIfNotExist(): File {
fun File.createFileReplace(): File {
if (!exists()) {
parentFile?.createFileIfNotExist()
parent?.let {
File(it).mkdirs()
}
createNewFile()
} else {
delete()
@@ -16,17 +16,21 @@ import java.util.zip.*
@Suppress("unused", "MemberVisibilityCanBePrivate")
object ZipUtils {
fun zipString(text: String): ByteArray {
fun zipByteArray(byteArray: ByteArray): ByteArray {
val byteOut = ByteArrayOutputStream()
val zip = GZIPOutputStream(byteOut)
zip.use {
it.write(text.toByteArray())
it.write(byteArray)
}
return byteOut.use {
it.toByteArray()
}
}
fun zipString(text: String): ByteArray {
return zipByteArray(text.toByteArray())
}
/**
* Zip the files.
*