mirror of
https://github.com/fabianonline/telegram_backup.git
synced 2024-12-25 22:35:35 +00:00
Modified the MediaFileManagers to use JSON. This is still untested and heavily WIP.
This commit is contained in:
parent
1c24585dd9
commit
ef03deb31e
@ -1,16 +1,16 @@
|
||||
/* Telegram_Backup
|
||||
* Copyright (C) 2016 Fabian Schlenz
|
||||
*
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
|
||||
@ -61,14 +61,14 @@ public class DownloadManager {
|
||||
static TelegramClient download_client;
|
||||
static boolean last_download_succeeded = true;
|
||||
static final Logger logger = LoggerFactory.getLogger(DownloadManager.class);
|
||||
|
||||
|
||||
public DownloadManager(TelegramClient c, DownloadProgressInterface p) {
|
||||
this.user = UserManager.getInstance();
|
||||
this.client = c;
|
||||
this.prog = p;
|
||||
this.db = Database.getInstance();
|
||||
}
|
||||
|
||||
|
||||
public void downloadMessages(Integer limit) throws RpcErrorException, IOException {
|
||||
boolean completed = true;
|
||||
do {
|
||||
@ -92,7 +92,7 @@ public class DownloadManager {
|
||||
}
|
||||
} while (!completed);
|
||||
}
|
||||
|
||||
|
||||
public void _downloadMessages(Integer limit) throws RpcErrorException, IOException, TimeoutException {
|
||||
logger.info("This is _downloadMessages with limit {}", limit);
|
||||
int dialog_limit = 100;
|
||||
@ -133,11 +133,11 @@ public class DownloadManager {
|
||||
} else {
|
||||
int start_id = max_database_id + 1;
|
||||
int end_id = max_message_id;
|
||||
|
||||
|
||||
List<Integer> ids = makeIdList(start_id, end_id);
|
||||
downloadMessages(ids);
|
||||
}
|
||||
|
||||
|
||||
logger.info("Searching for missing messages in the db");
|
||||
int count_missing = 0;
|
||||
System.out.println("Checking message database for completeness...");
|
||||
@ -145,7 +145,7 @@ public class DownloadManager {
|
||||
int db_max = db.getTopMessageID();
|
||||
logger.debug("db_count: {}", db_count);
|
||||
logger.debug("db_max: {}", db_max);
|
||||
|
||||
|
||||
if (db_count != db_max) {
|
||||
if (limit != null) {
|
||||
System.out.println("You are missing messages in your database. But since you're using '--limit-messages', I won't download these now.");
|
||||
@ -158,19 +158,19 @@ public class DownloadManager {
|
||||
count_missing = all_missing_ids.size();
|
||||
System.out.println("" + all_missing_ids.size() + " messages are missing in your Database.");
|
||||
System.out.println("I can (and will) download " + downloadable_missing_ids.size() + " of them.");
|
||||
|
||||
|
||||
downloadMessages(downloadable_missing_ids);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
logger.info("Logging this run");
|
||||
db.logRun(Math.min(max_database_id + 1, max_message_id), max_message_id, count_missing);
|
||||
}
|
||||
|
||||
|
||||
private void downloadMessages(List<Integer> ids) throws RpcErrorException, IOException {
|
||||
prog.onMessageDownloadStart(ids.size());
|
||||
boolean has_seen_flood_wait_message = false;
|
||||
|
||||
|
||||
logger.debug("Entering download loop");
|
||||
while (ids.size()>0) {
|
||||
logger.trace("Loop");
|
||||
@ -183,7 +183,7 @@ public class DownloadManager {
|
||||
}
|
||||
logger.trace("vector.size(): {}", vector.size());
|
||||
logger.trace("ids.size(): {}", ids.size());
|
||||
|
||||
|
||||
TLAbsMessages response;
|
||||
int tries = 0;
|
||||
while(true) {
|
||||
@ -208,11 +208,11 @@ public class DownloadManager {
|
||||
if (response.getMessages().size() != vector.size()) {
|
||||
CommandLineController.show_error("Requested " + vector.size() + " messages, but got " + response.getMessages().size() + ". That is unexpected. Quitting.");
|
||||
}
|
||||
|
||||
|
||||
//ObjectMapper om = new ObjectMapper();
|
||||
//String json = om.writerWithDefaultPrettyPrinter().writeValueAsString(response.getMessages().get(1));
|
||||
Gson gson = Utils.getGson();
|
||||
|
||||
|
||||
prog.onMessageDownloaded(response.getMessages().size());
|
||||
db.saveMessages(response.getMessages(), Kotlogram.API_LAYER, gson);
|
||||
db.saveChats(response.getChats(), gson);
|
||||
@ -223,10 +223,10 @@ public class DownloadManager {
|
||||
} catch (InterruptedException e) {}
|
||||
}
|
||||
logger.debug("Finished.");
|
||||
|
||||
|
||||
prog.onMessageDownloadFinished();
|
||||
}
|
||||
|
||||
|
||||
public void downloadMedia() throws RpcErrorException, IOException {
|
||||
download_client = client.getDownloaderClient();
|
||||
boolean completed = true;
|
||||
@ -252,7 +252,7 @@ public class DownloadManager {
|
||||
}*/
|
||||
} while (!completed);
|
||||
}
|
||||
|
||||
|
||||
private void _downloadMedia() throws RpcErrorException, IOException {
|
||||
logger.info("This is _downloadMedia");
|
||||
logger.info("Checking if there are messages in the DB with a too old API layer");
|
||||
@ -262,12 +262,12 @@ public class DownloadManager {
|
||||
logger.debug("Found {} messages", ids.size());
|
||||
downloadMessages(ids);
|
||||
}
|
||||
|
||||
|
||||
LinkedList<TLMessage> messages = this.db.getMessagesWithMedia();
|
||||
logger.debug("Database returned {} messages with media", messages.size());
|
||||
prog.onMediaDownloadStart(messages.size());
|
||||
for (TLMessage msg : messages) {
|
||||
AbstractMediaFileManager m = FileManagerFactory.getFileManager(msg, user, client);
|
||||
AbstractMediaFileManager m = FileManagerFactory.getFileManager(msg);
|
||||
logger.trace("message {}, {}, {}, {}, {}",
|
||||
msg.getId(),
|
||||
msg.getMedia().getClass().getSimpleName().replace("TLMessageMedia", "…"),
|
||||
@ -280,7 +280,7 @@ public class DownloadManager {
|
||||
prog.onMediaAlreadyPresent(m);
|
||||
} else {
|
||||
try {
|
||||
m.download();
|
||||
m.download(client);
|
||||
prog.onMediaDownloaded(m);
|
||||
} catch (TimeoutException e) {
|
||||
// do nothing - skip this file
|
||||
@ -290,30 +290,30 @@ public class DownloadManager {
|
||||
}
|
||||
prog.onMediaDownloadFinished();
|
||||
}
|
||||
|
||||
|
||||
private List<Integer> makeIdList(int start, int end) {
|
||||
LinkedList<Integer> a = new LinkedList<Integer>();
|
||||
for (int i=start; i<=end; i++) a.add(i);
|
||||
return a;
|
||||
return a;
|
||||
}
|
||||
|
||||
|
||||
public static void downloadFile(TelegramClient client, String targetFilename, int size, int dcId, long volumeId, int localId, long secret) throws RpcErrorException, IOException, TimeoutException {
|
||||
TLInputFileLocation loc = new TLInputFileLocation(volumeId, localId, secret);
|
||||
downloadFileFromDc(client, targetFilename, loc, dcId, size);
|
||||
}
|
||||
|
||||
|
||||
public static void downloadFile(TelegramClient client, String targetFilename, int size, int dcId, long id, long accessHash) throws RpcErrorException, IOException, TimeoutException {
|
||||
TLInputDocumentFileLocation loc = new TLInputDocumentFileLocation(id, accessHash);
|
||||
downloadFileFromDc(client, targetFilename, loc, dcId, size);
|
||||
}
|
||||
|
||||
|
||||
private static boolean downloadFileFromDc(TelegramClient client, String target, TLAbsInputFileLocation loc, Integer dcID, int size) throws RpcErrorException, IOException, TimeoutException {
|
||||
FileOutputStream fos = null;
|
||||
try {
|
||||
String temp_filename = target + ".downloading";
|
||||
logger.debug("Downloading file {}", target);
|
||||
logger.trace("Temporary filename: {}", temp_filename);
|
||||
|
||||
|
||||
int offset = 0;
|
||||
if (new File(temp_filename).isFile()) {
|
||||
logger.info("Temporary filename already exists; continuing this file");
|
||||
@ -347,10 +347,10 @@ public class DownloadManager {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
offset += response.getBytes().getData().length;
|
||||
logger.trace("response: {} total size: {}", response.getBytes().getData().length, offset);
|
||||
|
||||
|
||||
fos.write(response.getBytes().getData());
|
||||
fos.flush();
|
||||
try { TimeUnit.MILLISECONDS.sleep(Config.DELAY_AFTER_GET_FILE); } catch(InterruptedException e) {}
|
||||
@ -401,7 +401,7 @@ public class DownloadManager {
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static boolean downloadExternalFile(String target, String url) throws IOException {
|
||||
FileUtils.copyURLToFile(new URL(url), new File(target), 5000, 5000);
|
||||
return true;
|
||||
|
@ -1,16 +1,16 @@
|
||||
/* Telegram_Backup
|
||||
* Copyright (C) 2016 Fabian Schlenz
|
||||
*
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
|
||||
@ -22,16 +22,11 @@ import de.fabianonline.telegram_backup.StickerConverter;
|
||||
import de.fabianonline.telegram_backup.DownloadProgressInterface;
|
||||
import de.fabianonline.telegram_backup.Config;
|
||||
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.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.File;
|
||||
@ -44,42 +39,41 @@ import java.util.concurrent.TimeoutException;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
|
||||
public abstract class AbstractMediaFileManager {
|
||||
protected UserManager user;
|
||||
protected TLMessage message;
|
||||
protected TelegramClient client;
|
||||
protected Message message;
|
||||
protected JsonObject media;
|
||||
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 String getExtension();
|
||||
public boolean isEmpty() { return isEmpty; }
|
||||
public boolean isDownloaded() { return new File(getTargetPathAndFilename()).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) {
|
||||
throw new RuntimeException("Unexpected " + o.getClass().getName());
|
||||
}
|
||||
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();
|
||||
return path;
|
||||
}
|
||||
public String getTargetFilename() { return "" + message.getId() + "." + getExtension(); }
|
||||
public String getTargetPathAndFilename() { return getTargetPath() + getTargetFilename(); }
|
||||
|
||||
|
||||
protected String extensionFromMimetype(String mime) {
|
||||
switch(mime) {
|
||||
case "text/plain": return "txt";
|
||||
}
|
||||
|
||||
|
||||
int i = mime.lastIndexOf('/');
|
||||
String ext = mime.substring(i+1).toLowerCase();
|
||||
|
||||
|
||||
if (ext=="unknown") return "dat";
|
||||
|
||||
|
||||
return ext;
|
||||
}
|
||||
|
||||
|
||||
public abstract String getLetter();
|
||||
public abstract String getName();
|
||||
public abstract String getDescription();
|
||||
|
@ -1,16 +1,16 @@
|
||||
/* Telegram_Backup
|
||||
* Copyright (C) 2016 Fabian Schlenz
|
||||
*
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
|
||||
@ -21,16 +21,12 @@ import de.fabianonline.telegram_backup.Database;
|
||||
import de.fabianonline.telegram_backup.StickerConverter;
|
||||
import de.fabianonline.telegram_backup.DownloadProgressInterface;
|
||||
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.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.File;
|
||||
@ -43,69 +39,67 @@ import java.util.concurrent.TimeoutException;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
|
||||
public class DocumentFileManager extends AbstractMediaFileManager {
|
||||
protected TLDocument doc;
|
||||
protected JsonObject doc;
|
||||
private String extension = null;
|
||||
|
||||
public DocumentFileManager(TLMessage msg, UserManager user, TelegramClient client) {
|
||||
super(msg, user, client);
|
||||
TLAbsDocument d = ((TLMessageMediaDocument)msg.getMedia()).getDocument();
|
||||
if (d instanceof TLDocument) {
|
||||
this.doc = (TLDocument)d;
|
||||
} else if (d instanceof TLDocumentEmpty) {
|
||||
this.isEmpty = true;
|
||||
} else {
|
||||
throwUnexpectedObjectError(d);
|
||||
|
||||
public DocumentFileManager(Message msg) {
|
||||
super(msg);
|
||||
doc = media.getAsJsonObject("document");
|
||||
if ( ! doc.getAsJsonPrimitive("_constructor").getAsString().startsWith("document#")) {
|
||||
doc = null;
|
||||
isEmpty = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public boolean isSticker() {
|
||||
TLDocumentAttributeSticker sticker = null;
|
||||
if (this.isEmpty || doc==null) return false;
|
||||
if (doc.getAttributes() != null) for(TLAbsDocumentAttribute attr : doc.getAttributes()) {
|
||||
if (attr instanceof TLDocumentAttributeSticker) {
|
||||
sticker = (TLDocumentAttributeSticker)attr;
|
||||
}
|
||||
for(JsonElement attr : doc.getAsJsonArray("attributes")) {
|
||||
if (attr.getAsJsonObject().getAsJsonPrimitive("_costructor").getAsString().startsWith("documentAttributeSticker#")) return true;
|
||||
}
|
||||
return sticker!=null;
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public int getSize() {
|
||||
if (doc != null) return doc.getSize();
|
||||
if (doc != null) return doc.getAsJsonPrimitive("size").getAsInt();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
public String getExtension() {
|
||||
if (extension != null) return extension;
|
||||
if (doc == null) return "empty";
|
||||
String ext = null;
|
||||
String original_filename = null;
|
||||
if (doc.getAttributes() != null) for(TLAbsDocumentAttribute attr : doc.getAttributes()) {
|
||||
if (attr instanceof TLDocumentAttributeFilename) {
|
||||
original_filename = ((TLDocumentAttributeFilename)attr).getFileName();
|
||||
for(JsonElement attr : doc.getAsJsonArray("attributes")) {
|
||||
if (attr.getAsJsonObject().getAsJsonPrimitive("_constructor").getAsString().startsWith("documentAttributeFilename#")) {
|
||||
original_filename = attr.getAsJsonObject().getAsJsonPrimitive("fileName").getAsString();
|
||||
}
|
||||
}
|
||||
|
||||
if (original_filename != null) {
|
||||
int i = original_filename.lastIndexOf('.');
|
||||
if (i>0) ext = original_filename.substring(i+1);
|
||||
|
||||
}
|
||||
|
||||
if (ext==null) {
|
||||
ext = extensionFromMimetype(doc.getMimeType());
|
||||
ext = extensionFromMimetype(doc.getAsJsonPrimitive("mimeType").getAsString());
|
||||
}
|
||||
|
||||
|
||||
// Sometimes, extensions contain a trailing double quote. Remove this. Fixes #12.
|
||||
ext = ext.replace("\"", "");
|
||||
|
||||
|
||||
this.extension = ext;
|
||||
return ext;
|
||||
}
|
||||
|
||||
public void download() throws RpcErrorException, IOException, TimeoutException {
|
||||
|
||||
public void download(TelegramClient c) throws RpcErrorException, IOException, TimeoutException {
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public String getLetter() { return "d"; }
|
||||
public String getName() { return "document"; }
|
||||
public String getDescription() { return "Document"; }
|
||||
|
@ -1,16 +1,16 @@
|
||||
/* Telegram_Backup
|
||||
* Copyright (C) 2016 Fabian Schlenz
|
||||
*
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
|
||||
@ -20,6 +20,9 @@ import de.fabianonline.telegram_backup.UserManager;
|
||||
import de.fabianonline.telegram_backup.Database;
|
||||
import de.fabianonline.telegram_backup.StickerConverter;
|
||||
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.tl.core.TLIntVector;
|
||||
@ -42,31 +45,33 @@ import java.util.concurrent.TimeoutException;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
|
||||
public class FileManagerFactory {
|
||||
public static AbstractMediaFileManager getFileManager(TLMessage m, UserManager u, TelegramClient c) {
|
||||
if (m==null) return null;
|
||||
TLAbsMessageMedia media = m.getMedia();
|
||||
public static AbstractMediaFileManager getFileManager(Message msg) {
|
||||
if (msg==null) return null;
|
||||
JsonObject media = msg.getMedia();
|
||||
if (media==null) return null;
|
||||
|
||||
if (media instanceof TLMessageMediaPhoto) {
|
||||
return new PhotoFileManager(m, u, c);
|
||||
} else if (media instanceof TLMessageMediaDocument) {
|
||||
DocumentFileManager d = new DocumentFileManager(m, u, c);
|
||||
|
||||
String media_constructor = media.getAsJsonPrimitive("_constructor").getAsString();
|
||||
|
||||
if (media_constructor.startsWith("messageMediaPhoto#")) {
|
||||
return new PhotoFileManager(msg);
|
||||
} else if (media_constructor.startsWith("messageMediaDocument#")) {
|
||||
DocumentFileManager d = new DocumentFileManager(msg);
|
||||
if (d.isSticker()) {
|
||||
return new StickerFileManager(m, u, c);
|
||||
return new StickerFileManager(msg);
|
||||
}
|
||||
return d;
|
||||
} else if (media instanceof TLMessageMediaGeo) {
|
||||
return new GeoFileManager(m, u, c);
|
||||
} else if (media instanceof TLMessageMediaEmpty) {
|
||||
return new UnsupportedFileManager(m, u, c, "empty");
|
||||
} else if (media instanceof TLMessageMediaUnsupported) {
|
||||
return new UnsupportedFileManager(m, u, c, "unsupported");
|
||||
} else if (media instanceof TLMessageMediaWebPage) {
|
||||
return new UnsupportedFileManager(m, u, c, "webpage");
|
||||
} else if (media instanceof TLMessageMediaContact) {
|
||||
return new UnsupportedFileManager(m, u, c, "contact");
|
||||
} else if (media instanceof TLMessageMediaVenue) {
|
||||
return new UnsupportedFileManager(m, u, c, "venue");
|
||||
} else if (media_constructor.startsWith("messageMediaGeo#")) {
|
||||
return new GeoFileManager(msg);
|
||||
} else if (media_constructor.startsWith("messageMediaEmpty#")) {
|
||||
return new UnsupportedFileManager(msg, "empty");
|
||||
} else if (media_constructor.startsWith("messageMediaUnsupported#")) {
|
||||
return new UnsupportedFileManager(msg, "unsupported");
|
||||
} else if (media_constructor.startsWith("messageMediaWebpage#")) {
|
||||
return new UnsupportedFileManager(msg, "webpage");
|
||||
} else if (media_constructor.startsWith("messageMediaContact#")) {
|
||||
return new UnsupportedFileManager(msg, "contact");
|
||||
} else if (media_constructor.startsWith("messageMediaVenue#")) {
|
||||
return new UnsupportedFileManager(msg, "venue");
|
||||
} else {
|
||||
AbstractMediaFileManager.throwUnexpectedObjectError(media);
|
||||
}
|
||||
|
@ -1,16 +1,16 @@
|
||||
/* Telegram_Backup
|
||||
* Copyright (C) 2016 Fabian Schlenz
|
||||
*
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
|
||||
@ -22,17 +22,11 @@ import de.fabianonline.telegram_backup.StickerConverter;
|
||||
import de.fabianonline.telegram_backup.DownloadProgressInterface;
|
||||
import de.fabianonline.telegram_backup.DownloadManager;
|
||||
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.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.File;
|
||||
import java.io.FileOutputStream;
|
||||
@ -44,39 +38,39 @@ import java.util.concurrent.TimeoutException;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
|
||||
public class GeoFileManager extends AbstractMediaFileManager {
|
||||
protected TLGeoPoint geo;
|
||||
|
||||
public GeoFileManager(TLMessage msg, UserManager user, TelegramClient client) {
|
||||
super(msg, user, client);
|
||||
TLAbsGeoPoint g = ((TLMessageMediaGeo)msg.getMedia()).getGeo();
|
||||
if (g instanceof TLGeoPoint) {
|
||||
this.geo = (TLGeoPoint) g;
|
||||
} else if (g instanceof TLGeoPointEmpty) {
|
||||
this.isEmpty = true;
|
||||
protected double lat;
|
||||
protected double lon;
|
||||
|
||||
public GeoFileManager(Message msg) {
|
||||
super(msg);
|
||||
JsonObject geo = media.getAsJsonObject("geo");
|
||||
if (geo.getAsJsonPrimitive("_constructor").getAsString().startsWith("geoPoint#")) {
|
||||
lat = geo.getAsJsonPrimitive("lat").getAsDouble();
|
||||
lon = geo.getAsJsonPrimitive("_long").getAsDouble();
|
||||
} else {
|
||||
throwUnexpectedObjectError(g);
|
||||
isEmpty = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public int getSize() {
|
||||
File f = new File(getTargetPathAndFilename());
|
||||
if (f.isFile()) return (int)f.length();
|
||||
|
||||
|
||||
// We don't know the size, so we just guess.
|
||||
return 100000;
|
||||
}
|
||||
|
||||
|
||||
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?" +
|
||||
"center=" + geo.getLat() + "," + geo.getLong() + "&" +
|
||||
"center=" + lat + "," + lon + "&" +
|
||||
"zoom=14&size=300x150&scale=2&format=png&" +
|
||||
"key=" + Config.SECRET_GMAPS;
|
||||
DownloadManager.downloadExternalFile(getTargetPathAndFilename(), url);
|
||||
}
|
||||
|
||||
public String getLetter() { return "g"; }
|
||||
public String getName() { return "geo"; }
|
||||
public String getName() { return "geo"; }
|
||||
public String getDescription() { return "Geolocation"; }
|
||||
}
|
||||
|
@ -1,16 +1,16 @@
|
||||
/* Telegram_Backup
|
||||
* Copyright (C) 2016 Fabian Schlenz
|
||||
*
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
|
||||
@ -21,16 +21,12 @@ import de.fabianonline.telegram_backup.Database;
|
||||
import de.fabianonline.telegram_backup.StickerConverter;
|
||||
import de.fabianonline.telegram_backup.DownloadProgressInterface;
|
||||
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.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.File;
|
||||
@ -43,45 +39,46 @@ import java.util.concurrent.TimeoutException;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
|
||||
public class PhotoFileManager extends AbstractMediaFileManager {
|
||||
private TLPhoto photo;
|
||||
private TLPhotoSize size = null;
|
||||
public PhotoFileManager(TLMessage msg, UserManager user, TelegramClient client) {
|
||||
super(msg, user, client);
|
||||
TLAbsPhoto p = ((TLMessageMediaPhoto)msg.getMedia()).getPhoto();
|
||||
if (p instanceof TLPhoto) {
|
||||
this.photo = (TLPhoto)p;
|
||||
|
||||
TLPhotoSize biggest = null;
|
||||
for (TLAbsPhotoSize s : photo.getSizes()) if (s instanceof TLPhotoSize) {
|
||||
TLPhotoSize size = (TLPhotoSize) s;
|
||||
if (biggest == null || (size.getW()>biggest.getW() && size.getH()>biggest.getH())) {
|
||||
biggest = size;
|
||||
private JsonObject photo;
|
||||
private JsonObject size = null;
|
||||
public PhotoFileManager(Message msg) {
|
||||
super(msg);
|
||||
photo = media.getAsJsonObject("photo");
|
||||
|
||||
if (photo.getAsJsonPrimitive("_constructor").getAsString().startsWith("messageMediaPhoto#")) {
|
||||
int w = 0;
|
||||
int h = 0;
|
||||
for (JsonElement e : photo.getAsJsonArray("sizes")) {
|
||||
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) {
|
||||
throw new RuntimeException("Could not find a size for a photo.");
|
||||
}
|
||||
this.size = biggest;
|
||||
} else if (p instanceof TLPhotoEmpty) {
|
||||
this.isEmpty = true;
|
||||
if (size==null) throw new RuntimeException("Could not find a size for the photo.");
|
||||
} else {
|
||||
throwUnexpectedObjectError(p);
|
||||
throw new RuntimeException("Unexpected photo type: " + photo.getAsJsonPrimitive("_constructor").getAsString());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public int getSize() {
|
||||
if (size!=null) return size.getSize();
|
||||
if (size!=null) return size.getAsJsonPrimitive("size").getAsInt();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
public String getExtension() { return "jpg"; }
|
||||
|
||||
public void download() throws RpcErrorException, IOException, TimeoutException {
|
||||
if (isEmpty) return;
|
||||
TLFileLocation loc = (TLFileLocation) size.getLocation();
|
||||
DownloadManager.downloadFile(client, getTargetPathAndFilename(), getSize(), loc.getDcId(), loc.getVolumeId(), loc.getLocalId(), loc.getSecret());
|
||||
|
||||
public void download(TelegramClient client) throws RpcErrorException, IOException, TimeoutException {
|
||||
if (isEmpty || size==null) return;
|
||||
JsonObject loc = size.getAsJsonObject("location");
|
||||
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 getName() { return "photo"; }
|
||||
public String getDescription() { return "Photo"; }
|
||||
|
@ -1,16 +1,16 @@
|
||||
/* Telegram_Backup
|
||||
* Copyright (C) 2016 Fabian Schlenz
|
||||
*
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
|
||||
@ -22,16 +22,12 @@ import de.fabianonline.telegram_backup.StickerConverter;
|
||||
import de.fabianonline.telegram_backup.DownloadProgressInterface;
|
||||
import de.fabianonline.telegram_backup.DownloadManager;
|
||||
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.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.LoggerFactory;
|
||||
@ -52,56 +48,57 @@ import org.apache.commons.io.FileUtils;
|
||||
|
||||
public class StickerFileManager extends DocumentFileManager {
|
||||
private static Logger logger = LoggerFactory.getLogger(StickerFileManager.class);
|
||||
|
||||
public StickerFileManager(TLMessage msg, UserManager user, TelegramClient client) {
|
||||
super(msg, user, client);
|
||||
|
||||
public StickerFileManager(Message msg) {
|
||||
super(msg);
|
||||
}
|
||||
|
||||
|
||||
public boolean isSticker() { return true; }
|
||||
|
||||
|
||||
private String getFilenameBase() {
|
||||
TLDocumentAttributeSticker sticker = null;
|
||||
for(TLAbsDocumentAttribute attr : doc.getAttributes()) {
|
||||
if (attr instanceof TLDocumentAttributeSticker) {
|
||||
sticker = (TLDocumentAttributeSticker)attr;
|
||||
}
|
||||
JsonObject sticker = null;
|
||||
for (JsonElement attr : doc.getAsJsonArray("attributes")) {
|
||||
if (attr.getAsJsonObject().getAsJsonPrimitive("_costructor").getAsString().startsWith("documentAttributeSticker#")) sticker = attr.getAsJsonObject();
|
||||
}
|
||||
|
||||
|
||||
StringBuilder file = new StringBuilder();
|
||||
if (sticker.getStickerset() instanceof TLInputStickerSetShortName) {
|
||||
file.append(((TLInputStickerSetShortName)sticker.getStickerset()).getShortName());
|
||||
} else if (sticker.getStickerset() instanceof TLInputStickerSetID) {
|
||||
file.append(((TLInputStickerSetID)sticker.getStickerset()).getId());
|
||||
JsonObject stickerset = sticker.getAsJsonObject("stickerset");
|
||||
|
||||
if (stickerset.getAsJsonPrimitive("_constructor").getAsString().startsWith("inputStickerSetID#")) {
|
||||
file.append(stickerset.getAsJsonPrimitive("id").getAsString());
|
||||
} else {
|
||||
throw new RuntimeException("Unexpected sticker type: " + stickerset.getAsJsonPrimitive("_constructor").getAsString());
|
||||
}
|
||||
|
||||
file.append("_");
|
||||
file.append(sticker.getAlt().hashCode());
|
||||
file.append(sticker.getAsJsonPrimitive("alt").getAsString().hashCode());
|
||||
return file.toString();
|
||||
}
|
||||
|
||||
|
||||
public String getTargetFilename() {
|
||||
return getFilenameBase() + "." + getExtension();
|
||||
}
|
||||
|
||||
|
||||
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();
|
||||
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();
|
||||
|
||||
|
||||
logger.trace("Old filename exists: {}", new File(old_file).exists());
|
||||
|
||||
|
||||
if (new File(old_file).exists()) {
|
||||
Files.copy(Paths.get(old_file), Paths.get(getTargetPathAndFilename()), StandardCopyOption.REPLACE_EXISTING);
|
||||
return;
|
||||
}
|
||||
super.download();
|
||||
}
|
||||
|
||||
super.download(c);
|
||||
}
|
||||
|
||||
public String getExtension() { return "webp"; }
|
||||
|
||||
|
||||
public String getLetter() { return "s"; }
|
||||
public String getName() { return "sticker"; }
|
||||
public String getDescription() { return "Sticker"; }
|
||||
|
@ -1,16 +1,16 @@
|
||||
/* Telegram_Backup
|
||||
* Copyright (C) 2016 Fabian Schlenz
|
||||
*
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
|
||||
@ -45,27 +45,27 @@ import org.apache.commons.io.FileUtils;
|
||||
|
||||
public class UnsupportedFileManager extends AbstractMediaFileManager {
|
||||
String type = null;
|
||||
public UnsupportedFileManager(TLMessage msg, UserManager user, TelegramClient client, String type) {
|
||||
super(msg, user, client);
|
||||
public UnsupportedFileManager(Message msg, String type) {
|
||||
super(msg);
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public String getTargetFilename() {
|
||||
return "";
|
||||
}
|
||||
|
||||
|
||||
public String getTargetPath() {
|
||||
return "";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public String getExtension() { return ""; }
|
||||
|
||||
|
||||
public int getSize() { return 0; }
|
||||
|
||||
|
||||
public boolean isEmpty() { return false; }
|
||||
public void download() {}
|
||||
public void download(TelegramClient c) {}
|
||||
public boolean isDownloaded() { return false; }
|
||||
|
||||
|
||||
public String getLetter() { return " "; }
|
||||
public String getName() { return type; }
|
||||
public String getDescription() { return "Unsupported / non-downloadable Media"; }
|
||||
|
@ -7,6 +7,7 @@ import com.google.gson.JsonObject;
|
||||
public class Message {
|
||||
protected static String tableName = "messages";
|
||||
private JsonObject json;
|
||||
private JsonObject media;
|
||||
private String message = null;
|
||||
private Integer id = null;
|
||||
|
||||
@ -28,4 +29,9 @@ public class Message {
|
||||
if (id==null) id=json.getAsJsonPrimitive("id").getAsInt();
|
||||
return id;
|
||||
}
|
||||
|
||||
public JsonObject getMedia() {
|
||||
if (media==null) media=json.getAsJsonObject("media");
|
||||
return media;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user