mirror of
https://github.com/fabianonline/telegram_backup.git
synced 2024-11-23 01:06:17 +00:00
Modified the MediaFileManagers to use JSON. This is still untested and heavily WIP.
This commit is contained in:
parent
1c24585dd9
commit
ef03deb31e
@ -267,7 +267,7 @@ public class DownloadManager {
|
|||||||
logger.debug("Database returned {} messages with media", messages.size());
|
logger.debug("Database returned {} messages with media", messages.size());
|
||||||
prog.onMediaDownloadStart(messages.size());
|
prog.onMediaDownloadStart(messages.size());
|
||||||
for (TLMessage msg : messages) {
|
for (TLMessage msg : messages) {
|
||||||
AbstractMediaFileManager m = FileManagerFactory.getFileManager(msg, user, client);
|
AbstractMediaFileManager m = FileManagerFactory.getFileManager(msg);
|
||||||
logger.trace("message {}, {}, {}, {}, {}",
|
logger.trace("message {}, {}, {}, {}, {}",
|
||||||
msg.getId(),
|
msg.getId(),
|
||||||
msg.getMedia().getClass().getSimpleName().replace("TLMessageMedia", "…"),
|
msg.getMedia().getClass().getSimpleName().replace("TLMessageMedia", "…"),
|
||||||
@ -280,7 +280,7 @@ public class DownloadManager {
|
|||||||
prog.onMediaAlreadyPresent(m);
|
prog.onMediaAlreadyPresent(m);
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
m.download();
|
m.download(client);
|
||||||
prog.onMediaDownloaded(m);
|
prog.onMediaDownloaded(m);
|
||||||
} catch (TimeoutException e) {
|
} catch (TimeoutException e) {
|
||||||
// do nothing - skip this file
|
// do nothing - skip this file
|
||||||
|
@ -22,16 +22,11 @@ import de.fabianonline.telegram_backup.StickerConverter;
|
|||||||
import de.fabianonline.telegram_backup.DownloadProgressInterface;
|
import de.fabianonline.telegram_backup.DownloadProgressInterface;
|
||||||
import de.fabianonline.telegram_backup.Config;
|
import de.fabianonline.telegram_backup.Config;
|
||||||
import de.fabianonline.telegram_backup.DownloadManager;
|
import de.fabianonline.telegram_backup.DownloadManager;
|
||||||
|
import de.fabianonline.telegram_backup.models.Message;
|
||||||
|
|
||||||
|
import com.google.gson.JsonObject;
|
||||||
|
|
||||||
import com.github.badoualy.telegram.api.TelegramClient;
|
import com.github.badoualy.telegram.api.TelegramClient;
|
||||||
import com.github.badoualy.telegram.tl.core.TLIntVector;
|
|
||||||
import com.github.badoualy.telegram.tl.core.TLObject;
|
|
||||||
import com.github.badoualy.telegram.tl.api.messages.TLAbsMessages;
|
|
||||||
import com.github.badoualy.telegram.tl.api.messages.TLAbsDialogs;
|
|
||||||
import com.github.badoualy.telegram.tl.api.*;
|
|
||||||
import com.github.badoualy.telegram.tl.api.upload.TLFile;
|
|
||||||
import com.github.badoualy.telegram.tl.exception.RpcErrorException;
|
|
||||||
import com.github.badoualy.telegram.tl.api.request.TLRequestUploadGetFile;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
@ -44,23 +39,22 @@ import java.util.concurrent.TimeoutException;
|
|||||||
import org.apache.commons.io.FileUtils;
|
import org.apache.commons.io.FileUtils;
|
||||||
|
|
||||||
public abstract class AbstractMediaFileManager {
|
public abstract class AbstractMediaFileManager {
|
||||||
protected UserManager user;
|
protected Message message;
|
||||||
protected TLMessage message;
|
protected JsonObject media;
|
||||||
protected TelegramClient client;
|
|
||||||
protected boolean isEmpty = false;
|
protected boolean isEmpty = false;
|
||||||
|
|
||||||
public AbstractMediaFileManager(TLMessage msg, UserManager user, TelegramClient client) {this.user = user; this.message = msg; this.client = client;};
|
public AbstractMediaFileManager(Message msg) {this.message = msg; this.media = msg.getMedia();}
|
||||||
public abstract int getSize();
|
public abstract int getSize();
|
||||||
public abstract String getExtension();
|
public abstract String getExtension();
|
||||||
public boolean isEmpty() { return isEmpty; }
|
public boolean isEmpty() { return isEmpty; }
|
||||||
public boolean isDownloaded() { return new File(getTargetPathAndFilename()).isFile(); }
|
public boolean isDownloaded() { return new File(getTargetPathAndFilename()).isFile(); }
|
||||||
public boolean isDownloading() { return new File(getTargetPathAndFilename() + ".downloading").isFile(); }
|
public boolean isDownloading() { return new File(getTargetPathAndFilename() + ".downloading").isFile(); }
|
||||||
public abstract void download() throws RpcErrorException, IOException, TimeoutException;
|
public abstract void download(TelegramClient c) throws RpcErrorException, IOException, TimeoutException;
|
||||||
public static void throwUnexpectedObjectError(Object o) {
|
public static void throwUnexpectedObjectError(Object o) {
|
||||||
throw new RuntimeException("Unexpected " + o.getClass().getName());
|
throw new RuntimeException("Unexpected " + o.getClass().getName());
|
||||||
}
|
}
|
||||||
public String getTargetPath() {
|
public String getTargetPath() {
|
||||||
String path = user.getFileBase() + Config.FILE_FILES_BASE + File.separatorChar;
|
String path = UserManager.getInstance().getFileBase() + Config.FILE_FILES_BASE + File.separatorChar;
|
||||||
new File(path).mkdirs();
|
new File(path).mkdirs();
|
||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
|
@ -21,16 +21,12 @@ import de.fabianonline.telegram_backup.Database;
|
|||||||
import de.fabianonline.telegram_backup.StickerConverter;
|
import de.fabianonline.telegram_backup.StickerConverter;
|
||||||
import de.fabianonline.telegram_backup.DownloadProgressInterface;
|
import de.fabianonline.telegram_backup.DownloadProgressInterface;
|
||||||
import de.fabianonline.telegram_backup.DownloadManager;
|
import de.fabianonline.telegram_backup.DownloadManager;
|
||||||
|
import de.fabianonline.telegram_backup.models.Message;
|
||||||
|
|
||||||
|
import com.google.gson.JsonObject;
|
||||||
|
import com.google.gson.JsonElement;
|
||||||
|
|
||||||
import com.github.badoualy.telegram.api.TelegramClient;
|
import com.github.badoualy.telegram.api.TelegramClient;
|
||||||
import com.github.badoualy.telegram.tl.core.TLIntVector;
|
|
||||||
import com.github.badoualy.telegram.tl.core.TLObject;
|
|
||||||
import com.github.badoualy.telegram.tl.api.messages.TLAbsMessages;
|
|
||||||
import com.github.badoualy.telegram.tl.api.messages.TLAbsDialogs;
|
|
||||||
import com.github.badoualy.telegram.tl.api.*;
|
|
||||||
import com.github.badoualy.telegram.tl.api.upload.TLFile;
|
|
||||||
import com.github.badoualy.telegram.tl.exception.RpcErrorException;
|
|
||||||
import com.github.badoualy.telegram.tl.api.request.TLRequestUploadGetFile;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
@ -43,34 +39,28 @@ import java.util.concurrent.TimeoutException;
|
|||||||
import org.apache.commons.io.FileUtils;
|
import org.apache.commons.io.FileUtils;
|
||||||
|
|
||||||
public class DocumentFileManager extends AbstractMediaFileManager {
|
public class DocumentFileManager extends AbstractMediaFileManager {
|
||||||
protected TLDocument doc;
|
protected JsonObject doc;
|
||||||
private String extension = null;
|
private String extension = null;
|
||||||
|
|
||||||
public DocumentFileManager(TLMessage msg, UserManager user, TelegramClient client) {
|
public DocumentFileManager(Message msg) {
|
||||||
super(msg, user, client);
|
super(msg);
|
||||||
TLAbsDocument d = ((TLMessageMediaDocument)msg.getMedia()).getDocument();
|
doc = media.getAsJsonObject("document");
|
||||||
if (d instanceof TLDocument) {
|
if ( ! doc.getAsJsonPrimitive("_constructor").getAsString().startsWith("document#")) {
|
||||||
this.doc = (TLDocument)d;
|
doc = null;
|
||||||
} else if (d instanceof TLDocumentEmpty) {
|
isEmpty = true;
|
||||||
this.isEmpty = true;
|
|
||||||
} else {
|
|
||||||
throwUnexpectedObjectError(d);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isSticker() {
|
public boolean isSticker() {
|
||||||
TLDocumentAttributeSticker sticker = null;
|
|
||||||
if (this.isEmpty || doc==null) return false;
|
if (this.isEmpty || doc==null) return false;
|
||||||
if (doc.getAttributes() != null) for(TLAbsDocumentAttribute attr : doc.getAttributes()) {
|
for(JsonElement attr : doc.getAsJsonArray("attributes")) {
|
||||||
if (attr instanceof TLDocumentAttributeSticker) {
|
if (attr.getAsJsonObject().getAsJsonPrimitive("_costructor").getAsString().startsWith("documentAttributeSticker#")) return true;
|
||||||
sticker = (TLDocumentAttributeSticker)attr;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return sticker!=null;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getSize() {
|
public int getSize() {
|
||||||
if (doc != null) return doc.getSize();
|
if (doc != null) return doc.getAsJsonPrimitive("size").getAsInt();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -79,18 +69,19 @@ public class DocumentFileManager extends AbstractMediaFileManager {
|
|||||||
if (doc == null) return "empty";
|
if (doc == null) return "empty";
|
||||||
String ext = null;
|
String ext = null;
|
||||||
String original_filename = null;
|
String original_filename = null;
|
||||||
if (doc.getAttributes() != null) for(TLAbsDocumentAttribute attr : doc.getAttributes()) {
|
for(JsonElement attr : doc.getAsJsonArray("attributes")) {
|
||||||
if (attr instanceof TLDocumentAttributeFilename) {
|
if (attr.getAsJsonObject().getAsJsonPrimitive("_constructor").getAsString().startsWith("documentAttributeFilename#")) {
|
||||||
original_filename = ((TLDocumentAttributeFilename)attr).getFileName();
|
original_filename = attr.getAsJsonObject().getAsJsonPrimitive("fileName").getAsString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (original_filename != null) {
|
if (original_filename != null) {
|
||||||
int i = original_filename.lastIndexOf('.');
|
int i = original_filename.lastIndexOf('.');
|
||||||
if (i>0) ext = original_filename.substring(i+1);
|
if (i>0) ext = original_filename.substring(i+1);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ext==null) {
|
if (ext==null) {
|
||||||
ext = extensionFromMimetype(doc.getMimeType());
|
ext = extensionFromMimetype(doc.getAsJsonPrimitive("mimeType").getAsString());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sometimes, extensions contain a trailing double quote. Remove this. Fixes #12.
|
// Sometimes, extensions contain a trailing double quote. Remove this. Fixes #12.
|
||||||
@ -100,9 +91,12 @@ public class DocumentFileManager extends AbstractMediaFileManager {
|
|||||||
return ext;
|
return ext;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void download() throws RpcErrorException, IOException, TimeoutException {
|
public void download(TelegramClient c) throws RpcErrorException, IOException, TimeoutException {
|
||||||
if (doc!=null) {
|
if (doc!=null) {
|
||||||
DownloadManager.downloadFile(client, getTargetPathAndFilename(), getSize(), doc.getDcId(), doc.getId(), doc.getAccessHash());
|
DownloadManager.downloadFile(c, getTargetPathAndFilename(), getSize(),
|
||||||
|
doc.getAsJsonPrimitive("dcId").getAsInt(),
|
||||||
|
doc.getAsJsonPrimitive("id").getAsLong(),
|
||||||
|
doc.getAsJsonPrimitive("accessHash").getAsLong());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -20,6 +20,9 @@ import de.fabianonline.telegram_backup.UserManager;
|
|||||||
import de.fabianonline.telegram_backup.Database;
|
import de.fabianonline.telegram_backup.Database;
|
||||||
import de.fabianonline.telegram_backup.StickerConverter;
|
import de.fabianonline.telegram_backup.StickerConverter;
|
||||||
import de.fabianonline.telegram_backup.DownloadProgressInterface;
|
import de.fabianonline.telegram_backup.DownloadProgressInterface;
|
||||||
|
import de.fabianonline.telegram_backup.models.Message;
|
||||||
|
|
||||||
|
import com.google.gson.JsonObject;
|
||||||
|
|
||||||
import com.github.badoualy.telegram.api.TelegramClient;
|
import com.github.badoualy.telegram.api.TelegramClient;
|
||||||
import com.github.badoualy.telegram.tl.core.TLIntVector;
|
import com.github.badoualy.telegram.tl.core.TLIntVector;
|
||||||
@ -42,31 +45,33 @@ import java.util.concurrent.TimeoutException;
|
|||||||
import org.apache.commons.io.FileUtils;
|
import org.apache.commons.io.FileUtils;
|
||||||
|
|
||||||
public class FileManagerFactory {
|
public class FileManagerFactory {
|
||||||
public static AbstractMediaFileManager getFileManager(TLMessage m, UserManager u, TelegramClient c) {
|
public static AbstractMediaFileManager getFileManager(Message msg) {
|
||||||
if (m==null) return null;
|
if (msg==null) return null;
|
||||||
TLAbsMessageMedia media = m.getMedia();
|
JsonObject media = msg.getMedia();
|
||||||
if (media==null) return null;
|
if (media==null) return null;
|
||||||
|
|
||||||
if (media instanceof TLMessageMediaPhoto) {
|
String media_constructor = media.getAsJsonPrimitive("_constructor").getAsString();
|
||||||
return new PhotoFileManager(m, u, c);
|
|
||||||
} else if (media instanceof TLMessageMediaDocument) {
|
if (media_constructor.startsWith("messageMediaPhoto#")) {
|
||||||
DocumentFileManager d = new DocumentFileManager(m, u, c);
|
return new PhotoFileManager(msg);
|
||||||
|
} else if (media_constructor.startsWith("messageMediaDocument#")) {
|
||||||
|
DocumentFileManager d = new DocumentFileManager(msg);
|
||||||
if (d.isSticker()) {
|
if (d.isSticker()) {
|
||||||
return new StickerFileManager(m, u, c);
|
return new StickerFileManager(msg);
|
||||||
}
|
}
|
||||||
return d;
|
return d;
|
||||||
} else if (media instanceof TLMessageMediaGeo) {
|
} else if (media_constructor.startsWith("messageMediaGeo#")) {
|
||||||
return new GeoFileManager(m, u, c);
|
return new GeoFileManager(msg);
|
||||||
} else if (media instanceof TLMessageMediaEmpty) {
|
} else if (media_constructor.startsWith("messageMediaEmpty#")) {
|
||||||
return new UnsupportedFileManager(m, u, c, "empty");
|
return new UnsupportedFileManager(msg, "empty");
|
||||||
} else if (media instanceof TLMessageMediaUnsupported) {
|
} else if (media_constructor.startsWith("messageMediaUnsupported#")) {
|
||||||
return new UnsupportedFileManager(m, u, c, "unsupported");
|
return new UnsupportedFileManager(msg, "unsupported");
|
||||||
} else if (media instanceof TLMessageMediaWebPage) {
|
} else if (media_constructor.startsWith("messageMediaWebpage#")) {
|
||||||
return new UnsupportedFileManager(m, u, c, "webpage");
|
return new UnsupportedFileManager(msg, "webpage");
|
||||||
} else if (media instanceof TLMessageMediaContact) {
|
} else if (media_constructor.startsWith("messageMediaContact#")) {
|
||||||
return new UnsupportedFileManager(m, u, c, "contact");
|
return new UnsupportedFileManager(msg, "contact");
|
||||||
} else if (media instanceof TLMessageMediaVenue) {
|
} else if (media_constructor.startsWith("messageMediaVenue#")) {
|
||||||
return new UnsupportedFileManager(m, u, c, "venue");
|
return new UnsupportedFileManager(msg, "venue");
|
||||||
} else {
|
} else {
|
||||||
AbstractMediaFileManager.throwUnexpectedObjectError(media);
|
AbstractMediaFileManager.throwUnexpectedObjectError(media);
|
||||||
}
|
}
|
||||||
|
@ -22,17 +22,11 @@ import de.fabianonline.telegram_backup.StickerConverter;
|
|||||||
import de.fabianonline.telegram_backup.DownloadProgressInterface;
|
import de.fabianonline.telegram_backup.DownloadProgressInterface;
|
||||||
import de.fabianonline.telegram_backup.DownloadManager;
|
import de.fabianonline.telegram_backup.DownloadManager;
|
||||||
import de.fabianonline.telegram_backup.Config;
|
import de.fabianonline.telegram_backup.Config;
|
||||||
|
import de.fabianonline.telegram_backup.models.Message;
|
||||||
|
|
||||||
import com.github.badoualy.telegram.api.TelegramClient;
|
import com.github.badoualy.telegram.api.TelegramClient;
|
||||||
import com.github.badoualy.telegram.tl.core.TLIntVector;
|
|
||||||
import com.github.badoualy.telegram.tl.core.TLObject;
|
|
||||||
import com.github.badoualy.telegram.tl.api.messages.TLAbsMessages;
|
|
||||||
import com.github.badoualy.telegram.tl.api.messages.TLAbsDialogs;
|
|
||||||
import com.github.badoualy.telegram.tl.api.*;
|
|
||||||
import com.github.badoualy.telegram.tl.api.upload.TLFile;
|
|
||||||
import com.github.badoualy.telegram.tl.exception.RpcErrorException;
|
|
||||||
import com.github.badoualy.telegram.tl.api.request.TLRequestUploadGetFile;
|
|
||||||
|
|
||||||
|
import com.google.gson.JsonObject;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
@ -44,17 +38,17 @@ import java.util.concurrent.TimeoutException;
|
|||||||
import org.apache.commons.io.FileUtils;
|
import org.apache.commons.io.FileUtils;
|
||||||
|
|
||||||
public class GeoFileManager extends AbstractMediaFileManager {
|
public class GeoFileManager extends AbstractMediaFileManager {
|
||||||
protected TLGeoPoint geo;
|
protected double lat;
|
||||||
|
protected double lon;
|
||||||
|
|
||||||
public GeoFileManager(TLMessage msg, UserManager user, TelegramClient client) {
|
public GeoFileManager(Message msg) {
|
||||||
super(msg, user, client);
|
super(msg);
|
||||||
TLAbsGeoPoint g = ((TLMessageMediaGeo)msg.getMedia()).getGeo();
|
JsonObject geo = media.getAsJsonObject("geo");
|
||||||
if (g instanceof TLGeoPoint) {
|
if (geo.getAsJsonPrimitive("_constructor").getAsString().startsWith("geoPoint#")) {
|
||||||
this.geo = (TLGeoPoint) g;
|
lat = geo.getAsJsonPrimitive("lat").getAsDouble();
|
||||||
} else if (g instanceof TLGeoPointEmpty) {
|
lon = geo.getAsJsonPrimitive("_long").getAsDouble();
|
||||||
this.isEmpty = true;
|
|
||||||
} else {
|
} else {
|
||||||
throwUnexpectedObjectError(g);
|
isEmpty = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -68,9 +62,9 @@ public class GeoFileManager extends AbstractMediaFileManager {
|
|||||||
|
|
||||||
public String getExtension() { return "png"; }
|
public String getExtension() { return "png"; }
|
||||||
|
|
||||||
public void download() throws IOException {
|
public void download(TelegramClient c) throws IOException {
|
||||||
String url = "https://maps.googleapis.com/maps/api/staticmap?" +
|
String url = "https://maps.googleapis.com/maps/api/staticmap?" +
|
||||||
"center=" + geo.getLat() + "," + geo.getLong() + "&" +
|
"center=" + lat + "," + lon + "&" +
|
||||||
"zoom=14&size=300x150&scale=2&format=png&" +
|
"zoom=14&size=300x150&scale=2&format=png&" +
|
||||||
"key=" + Config.SECRET_GMAPS;
|
"key=" + Config.SECRET_GMAPS;
|
||||||
DownloadManager.downloadExternalFile(getTargetPathAndFilename(), url);
|
DownloadManager.downloadExternalFile(getTargetPathAndFilename(), url);
|
||||||
|
@ -21,16 +21,12 @@ import de.fabianonline.telegram_backup.Database;
|
|||||||
import de.fabianonline.telegram_backup.StickerConverter;
|
import de.fabianonline.telegram_backup.StickerConverter;
|
||||||
import de.fabianonline.telegram_backup.DownloadProgressInterface;
|
import de.fabianonline.telegram_backup.DownloadProgressInterface;
|
||||||
import de.fabianonline.telegram_backup.DownloadManager;
|
import de.fabianonline.telegram_backup.DownloadManager;
|
||||||
|
import de.fabianonline.telegram_backup.models.Message;
|
||||||
|
|
||||||
|
import com.google.gson.JsonObject;
|
||||||
|
import com.google.gson.JsonElement;
|
||||||
|
|
||||||
import com.github.badoualy.telegram.api.TelegramClient;
|
import com.github.badoualy.telegram.api.TelegramClient;
|
||||||
import com.github.badoualy.telegram.tl.core.TLIntVector;
|
|
||||||
import com.github.badoualy.telegram.tl.core.TLObject;
|
|
||||||
import com.github.badoualy.telegram.tl.api.messages.TLAbsMessages;
|
|
||||||
import com.github.badoualy.telegram.tl.api.messages.TLAbsDialogs;
|
|
||||||
import com.github.badoualy.telegram.tl.api.*;
|
|
||||||
import com.github.badoualy.telegram.tl.api.upload.TLFile;
|
|
||||||
import com.github.badoualy.telegram.tl.exception.RpcErrorException;
|
|
||||||
import com.github.badoualy.telegram.tl.api.request.TLRequestUploadGetFile;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
@ -43,43 +39,44 @@ import java.util.concurrent.TimeoutException;
|
|||||||
import org.apache.commons.io.FileUtils;
|
import org.apache.commons.io.FileUtils;
|
||||||
|
|
||||||
public class PhotoFileManager extends AbstractMediaFileManager {
|
public class PhotoFileManager extends AbstractMediaFileManager {
|
||||||
private TLPhoto photo;
|
private JsonObject photo;
|
||||||
private TLPhotoSize size = null;
|
private JsonObject size = null;
|
||||||
public PhotoFileManager(TLMessage msg, UserManager user, TelegramClient client) {
|
public PhotoFileManager(Message msg) {
|
||||||
super(msg, user, client);
|
super(msg);
|
||||||
TLAbsPhoto p = ((TLMessageMediaPhoto)msg.getMedia()).getPhoto();
|
photo = media.getAsJsonObject("photo");
|
||||||
if (p instanceof TLPhoto) {
|
|
||||||
this.photo = (TLPhoto)p;
|
|
||||||
|
|
||||||
TLPhotoSize biggest = null;
|
if (photo.getAsJsonPrimitive("_constructor").getAsString().startsWith("messageMediaPhoto#")) {
|
||||||
for (TLAbsPhotoSize s : photo.getSizes()) if (s instanceof TLPhotoSize) {
|
int w = 0;
|
||||||
TLPhotoSize size = (TLPhotoSize) s;
|
int h = 0;
|
||||||
if (biggest == null || (size.getW()>biggest.getW() && size.getH()>biggest.getH())) {
|
for (JsonElement e : photo.getAsJsonArray("sizes")) {
|
||||||
biggest = size;
|
JsonObject s = e.getAsJsonObject();
|
||||||
|
if (size==null || (s.getAsJsonPrimitive("w").getAsInt()>w && s.getAsJsonPrimitive("h").getAsInt()>h)) {
|
||||||
|
size = s;
|
||||||
|
w = s.getAsJsonPrimitive("w").getAsInt();
|
||||||
|
h = s.getAsJsonPrimitive("h").getAsInt();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (biggest==null) {
|
if (size==null) throw new RuntimeException("Could not find a size for the photo.");
|
||||||
throw new RuntimeException("Could not find a size for a photo.");
|
|
||||||
}
|
|
||||||
this.size = biggest;
|
|
||||||
} else if (p instanceof TLPhotoEmpty) {
|
|
||||||
this.isEmpty = true;
|
|
||||||
} else {
|
} else {
|
||||||
throwUnexpectedObjectError(p);
|
throw new RuntimeException("Unexpected photo type: " + photo.getAsJsonPrimitive("_constructor").getAsString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getSize() {
|
public int getSize() {
|
||||||
if (size!=null) return size.getSize();
|
if (size!=null) return size.getAsJsonPrimitive("size").getAsInt();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getExtension() { return "jpg"; }
|
public String getExtension() { return "jpg"; }
|
||||||
|
|
||||||
public void download() throws RpcErrorException, IOException, TimeoutException {
|
public void download(TelegramClient client) throws RpcErrorException, IOException, TimeoutException {
|
||||||
if (isEmpty) return;
|
if (isEmpty || size==null) return;
|
||||||
TLFileLocation loc = (TLFileLocation) size.getLocation();
|
JsonObject loc = size.getAsJsonObject("location");
|
||||||
DownloadManager.downloadFile(client, getTargetPathAndFilename(), getSize(), loc.getDcId(), loc.getVolumeId(), loc.getLocalId(), loc.getSecret());
|
DownloadManager.downloadFile(client, getTargetPathAndFilename(), getSize(),
|
||||||
|
loc.getAsJsonPrimitive("dcId").getAsInt(),
|
||||||
|
loc.getAsJsonPrimitive("volumeId").getAsLong(),
|
||||||
|
loc.getAsJsonPrimitive("localId").getAsInt(),
|
||||||
|
loc.getAsJsonPrimitive("secret").getAsLong());
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getLetter() { return "p"; }
|
public String getLetter() { return "p"; }
|
||||||
|
@ -22,16 +22,12 @@ import de.fabianonline.telegram_backup.StickerConverter;
|
|||||||
import de.fabianonline.telegram_backup.DownloadProgressInterface;
|
import de.fabianonline.telegram_backup.DownloadProgressInterface;
|
||||||
import de.fabianonline.telegram_backup.DownloadManager;
|
import de.fabianonline.telegram_backup.DownloadManager;
|
||||||
import de.fabianonline.telegram_backup.Config;
|
import de.fabianonline.telegram_backup.Config;
|
||||||
|
import de.fabianonline.telegram_backup.models.Message;
|
||||||
|
|
||||||
|
import com.google.gson.JsonObject;
|
||||||
|
import com.google.gson.JsonElement;
|
||||||
|
|
||||||
import com.github.badoualy.telegram.api.TelegramClient;
|
import com.github.badoualy.telegram.api.TelegramClient;
|
||||||
import com.github.badoualy.telegram.tl.core.TLIntVector;
|
|
||||||
import com.github.badoualy.telegram.tl.core.TLObject;
|
|
||||||
import com.github.badoualy.telegram.tl.api.messages.TLAbsMessages;
|
|
||||||
import com.github.badoualy.telegram.tl.api.messages.TLAbsDialogs;
|
|
||||||
import com.github.badoualy.telegram.tl.api.*;
|
|
||||||
import com.github.badoualy.telegram.tl.api.upload.TLFile;
|
|
||||||
import com.github.badoualy.telegram.tl.exception.RpcErrorException;
|
|
||||||
import com.github.badoualy.telegram.tl.api.request.TLRequestUploadGetFile;
|
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
@ -53,28 +49,29 @@ import org.apache.commons.io.FileUtils;
|
|||||||
public class StickerFileManager extends DocumentFileManager {
|
public class StickerFileManager extends DocumentFileManager {
|
||||||
private static Logger logger = LoggerFactory.getLogger(StickerFileManager.class);
|
private static Logger logger = LoggerFactory.getLogger(StickerFileManager.class);
|
||||||
|
|
||||||
public StickerFileManager(TLMessage msg, UserManager user, TelegramClient client) {
|
public StickerFileManager(Message msg) {
|
||||||
super(msg, user, client);
|
super(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isSticker() { return true; }
|
public boolean isSticker() { return true; }
|
||||||
|
|
||||||
private String getFilenameBase() {
|
private String getFilenameBase() {
|
||||||
TLDocumentAttributeSticker sticker = null;
|
JsonObject sticker = null;
|
||||||
for(TLAbsDocumentAttribute attr : doc.getAttributes()) {
|
for (JsonElement attr : doc.getAsJsonArray("attributes")) {
|
||||||
if (attr instanceof TLDocumentAttributeSticker) {
|
if (attr.getAsJsonObject().getAsJsonPrimitive("_costructor").getAsString().startsWith("documentAttributeSticker#")) sticker = attr.getAsJsonObject();
|
||||||
sticker = (TLDocumentAttributeSticker)attr;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
StringBuilder file = new StringBuilder();
|
StringBuilder file = new StringBuilder();
|
||||||
if (sticker.getStickerset() instanceof TLInputStickerSetShortName) {
|
JsonObject stickerset = sticker.getAsJsonObject("stickerset");
|
||||||
file.append(((TLInputStickerSetShortName)sticker.getStickerset()).getShortName());
|
|
||||||
} else if (sticker.getStickerset() instanceof TLInputStickerSetID) {
|
if (stickerset.getAsJsonPrimitive("_constructor").getAsString().startsWith("inputStickerSetID#")) {
|
||||||
file.append(((TLInputStickerSetID)sticker.getStickerset()).getId());
|
file.append(stickerset.getAsJsonPrimitive("id").getAsString());
|
||||||
|
} else {
|
||||||
|
throw new RuntimeException("Unexpected sticker type: " + stickerset.getAsJsonPrimitive("_constructor").getAsString());
|
||||||
}
|
}
|
||||||
|
|
||||||
file.append("_");
|
file.append("_");
|
||||||
file.append(sticker.getAlt().hashCode());
|
file.append(sticker.getAsJsonPrimitive("alt").getAsString().hashCode());
|
||||||
return file.toString();
|
return file.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -83,12 +80,12 @@ public class StickerFileManager extends DocumentFileManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public String getTargetPath() {
|
public String getTargetPath() {
|
||||||
String path = user.getFileBase() + Config.FILE_FILES_BASE + File.separatorChar + Config.FILE_STICKER_BASE + File.separatorChar;
|
String path = UserManager.getInstance().getFileBase() + Config.FILE_FILES_BASE + File.separatorChar + Config.FILE_STICKER_BASE + File.separatorChar;
|
||||||
new File(path).mkdirs();
|
new File(path).mkdirs();
|
||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void download() throws RpcErrorException, IOException, TimeoutException {
|
public void download(TelegramClient c) throws RpcErrorException, IOException, TimeoutException {
|
||||||
String old_file = Config.FILE_BASE + File.separatorChar + Config.FILE_STICKER_BASE + File.separatorChar + getTargetFilename();
|
String old_file = Config.FILE_BASE + File.separatorChar + Config.FILE_STICKER_BASE + File.separatorChar + getTargetFilename();
|
||||||
|
|
||||||
logger.trace("Old filename exists: {}", new File(old_file).exists());
|
logger.trace("Old filename exists: {}", new File(old_file).exists());
|
||||||
@ -97,7 +94,7 @@ public class StickerFileManager extends DocumentFileManager {
|
|||||||
Files.copy(Paths.get(old_file), Paths.get(getTargetPathAndFilename()), StandardCopyOption.REPLACE_EXISTING);
|
Files.copy(Paths.get(old_file), Paths.get(getTargetPathAndFilename()), StandardCopyOption.REPLACE_EXISTING);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
super.download();
|
super.download(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getExtension() { return "webp"; }
|
public String getExtension() { return "webp"; }
|
||||||
|
@ -45,8 +45,8 @@ import org.apache.commons.io.FileUtils;
|
|||||||
|
|
||||||
public class UnsupportedFileManager extends AbstractMediaFileManager {
|
public class UnsupportedFileManager extends AbstractMediaFileManager {
|
||||||
String type = null;
|
String type = null;
|
||||||
public UnsupportedFileManager(TLMessage msg, UserManager user, TelegramClient client, String type) {
|
public UnsupportedFileManager(Message msg, String type) {
|
||||||
super(msg, user, client);
|
super(msg);
|
||||||
this.type = type;
|
this.type = type;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -63,7 +63,7 @@ public class UnsupportedFileManager extends AbstractMediaFileManager {
|
|||||||
public int getSize() { return 0; }
|
public int getSize() { return 0; }
|
||||||
|
|
||||||
public boolean isEmpty() { return false; }
|
public boolean isEmpty() { return false; }
|
||||||
public void download() {}
|
public void download(TelegramClient c) {}
|
||||||
public boolean isDownloaded() { return false; }
|
public boolean isDownloaded() { return false; }
|
||||||
|
|
||||||
public String getLetter() { return " "; }
|
public String getLetter() { return " "; }
|
||||||
|
@ -7,6 +7,7 @@ import com.google.gson.JsonObject;
|
|||||||
public class Message {
|
public class Message {
|
||||||
protected static String tableName = "messages";
|
protected static String tableName = "messages";
|
||||||
private JsonObject json;
|
private JsonObject json;
|
||||||
|
private JsonObject media;
|
||||||
private String message = null;
|
private String message = null;
|
||||||
private Integer id = null;
|
private Integer id = null;
|
||||||
|
|
||||||
@ -28,4 +29,9 @@ public class Message {
|
|||||||
if (id==null) id=json.getAsJsonPrimitive("id").getAsInt();
|
if (id==null) id=json.getAsJsonPrimitive("id").getAsInt();
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public JsonObject getMedia() {
|
||||||
|
if (media==null) media=json.getAsJsonObject("media");
|
||||||
|
return media;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user