From 6b5b9a669b99eea85061aa04346673689d82a912 Mon Sep 17 00:00:00 2001 From: Fabian Schlenz Date: Mon, 16 Apr 2018 05:57:05 +0200 Subject: [PATCH] WIP: Reworking mediafilemanagers to work with JSON. --- .../telegram_backup/DatabaseUpdates.kt | 17 +++---- .../mediafilemanager/FileManagerFactory.kt | 18 +++++-- .../mediafilemanager/PhotoFileManager.kt | 48 ++++++++++--------- 3 files changed, 49 insertions(+), 34 deletions(-) diff --git a/src/main/kotlin/de/fabianonline/telegram_backup/DatabaseUpdates.kt b/src/main/kotlin/de/fabianonline/telegram_backup/DatabaseUpdates.kt index af588d8..ddf997a 100644 --- a/src/main/kotlin/de/fabianonline/telegram_backup/DatabaseUpdates.kt +++ b/src/main/kotlin/de/fabianonline/telegram_backup/DatabaseUpdates.kt @@ -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() diff --git a/src/main/kotlin/de/fabianonline/telegram_backup/mediafilemanager/FileManagerFactory.kt b/src/main/kotlin/de/fabianonline/telegram_backup/mediafilemanager/FileManagerFactory.kt index 8248f15..af1fd1f 100644 --- a/src/main/kotlin/de/fabianonline/telegram_backup/mediafilemanager/FileManagerFactory.kt +++ b/src/main/kotlin/de/fabianonline/telegram_backup/mediafilemanager/FileManagerFactory.kt @@ -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 } } diff --git a/src/main/kotlin/de/fabianonline/telegram_backup/mediafilemanager/PhotoFileManager.kt b/src/main/kotlin/de/fabianonline/telegram_backup/mediafilemanager/PhotoFileManager.kt index 8421398..068c6d2 100644 --- a/src/main/kotlin/de/fabianonline/telegram_backup/mediafilemanager/PhotoFileManager.kt +++ b/src/main/kotlin/de/fabianonline/telegram_backup/mediafilemanager/PhotoFileManager.kt @@ -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 } }