1
0
mirror of https://github.com/fabianonline/telegram_backup.git synced 2024-11-22 16:56:16 +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() { override fun _doUpdate() {
execute("ALTER TABLE messages ADD COLUMN json TEXT NULL") 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 chats ADD COLUMN json TEXT NULL")
execute("ALTER TABLE users ADD COLUMN json TEXT NULL, api_layer INTEGER NULL") execute("ALTER TABLE chats ADD COLUMN api_layer INTEGER NULL")
val limit = 1000 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 offset = 0
var i = 0 var i: Int
val ps = conn.prepareStatement("UPDATE messages SET json=? WHERE id=?") 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...") println(" Updating messages to add their JSON representation to the database. This might take a few moments...")
print(" ") print(" ")
do { do {
i = 0 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") val rs = db.executeQuery("SELECT id, data FROM messages WHERE json IS NULL AND api_layer=53 LIMIT $limit")
while (rs.next()) { while (rs.next()) {
i++ i++
val id = rs.getInt(1) val id = rs.getInt(1)
val msg = Database.bytesToTLMessage(rs.getBytes(2)) val msg = Database.bytesToTLMessage(rs.getBytes(2))
if (msg == null) continue val json = if (msg==null) Gson().toJson(null) else msg.toJson()
val json = msg.toJson()
ps.setString(1, json) ps.setString(1, json)
ps.setInt(2, id) ps.setInt(2, id)
ps.addBatch() ps.addBatch()
@ -489,7 +490,7 @@ internal class DB_Update_11(conn: Connection, db: Database) : DatabaseUpdate(con
ps.clearBatch() ps.clearBatch()
conn.commit() conn.commit()
conn.setAutoCommit(true) conn.setAutoCommit(true)
offset += limit
print(".") print(".")
} while (i >= limit) } while (i >= limit)
println() println()

View File

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

View File

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