1
0
mirror of https://github.com/fabianonline/telegram_backup.git synced 2024-12-25 14:25:35 +00:00

WIP: Reworking mediafilemanagers to work with JSON.

This commit is contained in:
Fabian Schlenz 2018-04-16 05:57:05 +02:00
parent 6b9cc9533a
commit 6b5b9a669b
3 changed files with 49 additions and 34 deletions

View File

@ -461,24 +461,25 @@ internal class DB_Update_11(conn: Connection, db: Database) : DatabaseUpdate(con
override fun _doUpdate() {
execute("ALTER TABLE messages ADD COLUMN json TEXT NULL")
execute("ALTER TABLE chats ADD COLUMN json TEXT NULL, api_layer INTEGER NULL")
execute("ALTER TABLE users ADD COLUMN json TEXT NULL, api_layer INTEGER NULL")
val limit = 1000
execute("ALTER TABLE chats ADD COLUMN json TEXT NULL")
execute("ALTER TABLE chats ADD COLUMN api_layer INTEGER NULL")
execute("ALTER TABLE users ADD COLUMN json TEXT NULL")
execute("ALTER TABLE users ADD COLUMN api_layer INTEGER NULL")
val limit = 5000
var offset = 0
var i = 0
var i: Int
val ps = conn.prepareStatement("UPDATE messages SET json=? WHERE id=?")
println(" Updating messages to add their JSON representation to the database. This might take a few moments...")
print(" ")
do {
i = 0
logger.debug("Querying with limit $limit and offset $offset")
logger.debug("Querying with limit $limit, offset is now $offset")
val rs = db.executeQuery("SELECT id, data FROM messages WHERE json IS NULL AND api_layer=53 LIMIT $limit")
while (rs.next()) {
i++
val id = rs.getInt(1)
val msg = Database.bytesToTLMessage(rs.getBytes(2))
if (msg == null) continue
val json = msg.toJson()
val json = if (msg==null) Gson().toJson(null) else msg.toJson()
ps.setString(1, json)
ps.setInt(2, id)
ps.addBatch()
@ -489,7 +490,7 @@ internal class DB_Update_11(conn: Connection, db: Database) : DatabaseUpdate(con
ps.clearBatch()
conn.commit()
conn.setAutoCommit(true)
offset += limit
print(".")
} while (i >= limit)
println()

View File

@ -38,15 +38,27 @@ import java.util.ArrayList
import java.util.LinkedList
import java.net.URL
import java.util.concurrent.TimeoutException
import com.google.gson.*
import com.github.salomonbrys.kotson.*
import org.apache.commons.io.FileUtils
object FileManagerFactory {
fun getFileManager(m: TLMessage?, u: UserManager, file_base: String, settings: Settings?): AbstractMediaFileManager? {
if (m == null) return null
val media = m.getMedia() ?: return null
val json = Gson().toJsonTree(m).obj
return getFileManager(json, u, file_base, settings)
}
fun getFileManager(m: JsonObject?, u: UserManager, file_base: String, settings: Settings?): AbstractMediaFileManager? {
if (m == null) return null
val media = m.get("media")?.obj ?: return null
if (media.contains("photo")) {
return PhotoFileManager(media["photo"].obj, u, file_base)
}
if (media is TLMessageMediaPhoto) {
/*if (media is TLMessageMediaPhoto) {
return PhotoFileManager(m, u, file_base)
} else if (media is TLMessageMediaDocument) {
val d = DocumentFileManager(m, u, file_base)
@ -67,7 +79,7 @@ object FileManagerFactory {
return UnsupportedFileManager(m, u, file_base, "venue")
} else {
AbstractMediaFileManager.throwUnexpectedObjectError(media)
}
}*/
return null
}
}

View File

@ -25,45 +25,47 @@ import com.github.badoualy.telegram.tl.exception.RpcErrorException
import java.io.IOException
import java.util.concurrent.TimeoutException
class PhotoFileManager(msg: TLMessage, user: UserManager, file_base: String) : AbstractMediaFileManager(msg, user, file_base) {
private lateinit var photo: TLPhoto
class PhotoFileManager(json: JsonObject, user: UserManager, file_base: String) : AbstractMediaFileManager(json, user, file_base) {
//private lateinit var photo: TLPhoto
override var size = 0
private lateinit var photo_size: TLPhotoSize
//private lateinit var photo_size: TLPhotoSize
override val extension = "jpg"
override val letter = "p"
override val name = "photo"
override val description = "Photo"
var biggestSize: JsonObject?
var biggestSizeW = 0
var biggestSizeH = 0
init {
val p = (msg.getMedia() as TLMessageMediaPhoto).getPhoto()
if (p is TLPhoto) {
this.photo = p
var biggest: TLPhotoSize? = null
for (s in photo.getSizes())
if (s is TLPhotoSize) {
if (biggest == null || s.getW() > biggest.getW() && s.getH() > biggest.getH()) {
biggest = s
}
}
if (biggest == null) {
throw RuntimeException("Could not find a size for a photo.")
/*val p = (msg.getMedia() as TLMessageMediaPhoto).getPhoto()*/
for (elm in json["sizes"]) {
val s = elm.obj
if (biggestSize == null || (s["w"].int > biggestSizeW && s["h"].int > biggestSizeH)) {
biggestSize = s
biggestSizeW = s["w"].int
biggestSizeH = s["h"].int
size = s["size"].int // file size
}
this.photo_size = biggest
this.size = biggest.getSize()
} else if (p is TLPhotoEmpty) {
if (biggestSize == null) throw RuntimeException("Could not find a size for a photo.")
}
/*} else if (p is TLPhotoEmpty) {
this.isEmpty = true
} else {
throwUnexpectedObjectError(p)
}
}*/
}
@Throws(RpcErrorException::class, IOException::class, TimeoutException::class)
override fun download(): Boolean {
if (isEmpty) return true
val loc = photo_size.getLocation() as TLFileLocation
DownloadManager.downloadFile(targetPathAndFilename, size, loc.getDcId(), loc.getVolumeId(), loc.getLocalId(), loc.getSecret())
/*if (isEmpty) return true*/
//val loc = photo_size.getLocation() as TLFileLocation
val loc = biggestSize["location"].obj
DownloadManager.downloadFile(targetPathAndFilename, size, loc["dcId"].int, loc["volumeId"].long, loc["localId"].int, loc["secret"].long)
return true
}
}