mirror of
https://github.com/fabianonline/telegram_backup.git
synced 2024-11-22 16:56:16 +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
|
/* Telegram_Backup
|
||||||
* Copyright (C) 2016 Fabian Schlenz
|
* Copyright (C) 2016 Fabian Schlenz
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* 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
|
* it under the terms of the GNU General Public License as published by
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
* (at your option) any later version.
|
* (at your option) any later version.
|
||||||
*
|
*
|
||||||
* This program is distributed in the hope that it will be useful,
|
* This program is distributed in the hope that it will be useful,
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
* GNU General Public License for more details.
|
* GNU General Public License for more details.
|
||||||
*
|
*
|
||||||
* You should have received a copy of the GNU General Public License
|
* You should have received a copy of the GNU General Public License
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||||
|
|
||||||
@ -61,14 +61,14 @@ public class DownloadManager {
|
|||||||
static TelegramClient download_client;
|
static TelegramClient download_client;
|
||||||
static boolean last_download_succeeded = true;
|
static boolean last_download_succeeded = true;
|
||||||
static final Logger logger = LoggerFactory.getLogger(DownloadManager.class);
|
static final Logger logger = LoggerFactory.getLogger(DownloadManager.class);
|
||||||
|
|
||||||
public DownloadManager(TelegramClient c, DownloadProgressInterface p) {
|
public DownloadManager(TelegramClient c, DownloadProgressInterface p) {
|
||||||
this.user = UserManager.getInstance();
|
this.user = UserManager.getInstance();
|
||||||
this.client = c;
|
this.client = c;
|
||||||
this.prog = p;
|
this.prog = p;
|
||||||
this.db = Database.getInstance();
|
this.db = Database.getInstance();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void downloadMessages(Integer limit) throws RpcErrorException, IOException {
|
public void downloadMessages(Integer limit) throws RpcErrorException, IOException {
|
||||||
boolean completed = true;
|
boolean completed = true;
|
||||||
do {
|
do {
|
||||||
@ -92,7 +92,7 @@ public class DownloadManager {
|
|||||||
}
|
}
|
||||||
} while (!completed);
|
} while (!completed);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void _downloadMessages(Integer limit) throws RpcErrorException, IOException, TimeoutException {
|
public void _downloadMessages(Integer limit) throws RpcErrorException, IOException, TimeoutException {
|
||||||
logger.info("This is _downloadMessages with limit {}", limit);
|
logger.info("This is _downloadMessages with limit {}", limit);
|
||||||
int dialog_limit = 100;
|
int dialog_limit = 100;
|
||||||
@ -133,11 +133,11 @@ public class DownloadManager {
|
|||||||
} else {
|
} else {
|
||||||
int start_id = max_database_id + 1;
|
int start_id = max_database_id + 1;
|
||||||
int end_id = max_message_id;
|
int end_id = max_message_id;
|
||||||
|
|
||||||
List<Integer> ids = makeIdList(start_id, end_id);
|
List<Integer> ids = makeIdList(start_id, end_id);
|
||||||
downloadMessages(ids);
|
downloadMessages(ids);
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.info("Searching for missing messages in the db");
|
logger.info("Searching for missing messages in the db");
|
||||||
int count_missing = 0;
|
int count_missing = 0;
|
||||||
System.out.println("Checking message database for completeness...");
|
System.out.println("Checking message database for completeness...");
|
||||||
@ -145,7 +145,7 @@ public class DownloadManager {
|
|||||||
int db_max = db.getTopMessageID();
|
int db_max = db.getTopMessageID();
|
||||||
logger.debug("db_count: {}", db_count);
|
logger.debug("db_count: {}", db_count);
|
||||||
logger.debug("db_max: {}", db_max);
|
logger.debug("db_max: {}", db_max);
|
||||||
|
|
||||||
if (db_count != db_max) {
|
if (db_count != db_max) {
|
||||||
if (limit != null) {
|
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.");
|
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();
|
count_missing = all_missing_ids.size();
|
||||||
System.out.println("" + all_missing_ids.size() + " messages are missing in your Database.");
|
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.");
|
System.out.println("I can (and will) download " + downloadable_missing_ids.size() + " of them.");
|
||||||
|
|
||||||
downloadMessages(downloadable_missing_ids);
|
downloadMessages(downloadable_missing_ids);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.info("Logging this run");
|
logger.info("Logging this run");
|
||||||
db.logRun(Math.min(max_database_id + 1, max_message_id), max_message_id, count_missing);
|
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 {
|
private void downloadMessages(List<Integer> ids) throws RpcErrorException, IOException {
|
||||||
prog.onMessageDownloadStart(ids.size());
|
prog.onMessageDownloadStart(ids.size());
|
||||||
boolean has_seen_flood_wait_message = false;
|
boolean has_seen_flood_wait_message = false;
|
||||||
|
|
||||||
logger.debug("Entering download loop");
|
logger.debug("Entering download loop");
|
||||||
while (ids.size()>0) {
|
while (ids.size()>0) {
|
||||||
logger.trace("Loop");
|
logger.trace("Loop");
|
||||||
@ -183,7 +183,7 @@ public class DownloadManager {
|
|||||||
}
|
}
|
||||||
logger.trace("vector.size(): {}", vector.size());
|
logger.trace("vector.size(): {}", vector.size());
|
||||||
logger.trace("ids.size(): {}", ids.size());
|
logger.trace("ids.size(): {}", ids.size());
|
||||||
|
|
||||||
TLAbsMessages response;
|
TLAbsMessages response;
|
||||||
int tries = 0;
|
int tries = 0;
|
||||||
while(true) {
|
while(true) {
|
||||||
@ -208,11 +208,11 @@ public class DownloadManager {
|
|||||||
if (response.getMessages().size() != vector.size()) {
|
if (response.getMessages().size() != vector.size()) {
|
||||||
CommandLineController.show_error("Requested " + vector.size() + " messages, but got " + response.getMessages().size() + ". That is unexpected. Quitting.");
|
CommandLineController.show_error("Requested " + vector.size() + " messages, but got " + response.getMessages().size() + ". That is unexpected. Quitting.");
|
||||||
}
|
}
|
||||||
|
|
||||||
//ObjectMapper om = new ObjectMapper();
|
//ObjectMapper om = new ObjectMapper();
|
||||||
//String json = om.writerWithDefaultPrettyPrinter().writeValueAsString(response.getMessages().get(1));
|
//String json = om.writerWithDefaultPrettyPrinter().writeValueAsString(response.getMessages().get(1));
|
||||||
Gson gson = Utils.getGson();
|
Gson gson = Utils.getGson();
|
||||||
|
|
||||||
prog.onMessageDownloaded(response.getMessages().size());
|
prog.onMessageDownloaded(response.getMessages().size());
|
||||||
db.saveMessages(response.getMessages(), Kotlogram.API_LAYER, gson);
|
db.saveMessages(response.getMessages(), Kotlogram.API_LAYER, gson);
|
||||||
db.saveChats(response.getChats(), gson);
|
db.saveChats(response.getChats(), gson);
|
||||||
@ -223,10 +223,10 @@ public class DownloadManager {
|
|||||||
} catch (InterruptedException e) {}
|
} catch (InterruptedException e) {}
|
||||||
}
|
}
|
||||||
logger.debug("Finished.");
|
logger.debug("Finished.");
|
||||||
|
|
||||||
prog.onMessageDownloadFinished();
|
prog.onMessageDownloadFinished();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void downloadMedia() throws RpcErrorException, IOException {
|
public void downloadMedia() throws RpcErrorException, IOException {
|
||||||
download_client = client.getDownloaderClient();
|
download_client = client.getDownloaderClient();
|
||||||
boolean completed = true;
|
boolean completed = true;
|
||||||
@ -252,7 +252,7 @@ public class DownloadManager {
|
|||||||
}*/
|
}*/
|
||||||
} while (!completed);
|
} while (!completed);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void _downloadMedia() throws RpcErrorException, IOException {
|
private void _downloadMedia() throws RpcErrorException, IOException {
|
||||||
logger.info("This is _downloadMedia");
|
logger.info("This is _downloadMedia");
|
||||||
logger.info("Checking if there are messages in the DB with a too old API layer");
|
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());
|
logger.debug("Found {} messages", ids.size());
|
||||||
downloadMessages(ids);
|
downloadMessages(ids);
|
||||||
}
|
}
|
||||||
|
|
||||||
LinkedList<TLMessage> messages = this.db.getMessagesWithMedia();
|
LinkedList<TLMessage> messages = this.db.getMessagesWithMedia();
|
||||||
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
|
||||||
@ -290,30 +290,30 @@ public class DownloadManager {
|
|||||||
}
|
}
|
||||||
prog.onMediaDownloadFinished();
|
prog.onMediaDownloadFinished();
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<Integer> makeIdList(int start, int end) {
|
private List<Integer> makeIdList(int start, int end) {
|
||||||
LinkedList<Integer> a = new LinkedList<Integer>();
|
LinkedList<Integer> a = new LinkedList<Integer>();
|
||||||
for (int i=start; i<=end; i++) a.add(i);
|
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 {
|
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);
|
TLInputFileLocation loc = new TLInputFileLocation(volumeId, localId, secret);
|
||||||
downloadFileFromDc(client, targetFilename, loc, dcId, size);
|
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 {
|
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);
|
TLInputDocumentFileLocation loc = new TLInputDocumentFileLocation(id, accessHash);
|
||||||
downloadFileFromDc(client, targetFilename, loc, dcId, size);
|
downloadFileFromDc(client, targetFilename, loc, dcId, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean downloadFileFromDc(TelegramClient client, String target, TLAbsInputFileLocation loc, Integer dcID, int size) throws RpcErrorException, IOException, TimeoutException {
|
private static boolean downloadFileFromDc(TelegramClient client, String target, TLAbsInputFileLocation loc, Integer dcID, int size) throws RpcErrorException, IOException, TimeoutException {
|
||||||
FileOutputStream fos = null;
|
FileOutputStream fos = null;
|
||||||
try {
|
try {
|
||||||
String temp_filename = target + ".downloading";
|
String temp_filename = target + ".downloading";
|
||||||
logger.debug("Downloading file {}", target);
|
logger.debug("Downloading file {}", target);
|
||||||
logger.trace("Temporary filename: {}", temp_filename);
|
logger.trace("Temporary filename: {}", temp_filename);
|
||||||
|
|
||||||
int offset = 0;
|
int offset = 0;
|
||||||
if (new File(temp_filename).isFile()) {
|
if (new File(temp_filename).isFile()) {
|
||||||
logger.info("Temporary filename already exists; continuing this file");
|
logger.info("Temporary filename already exists; continuing this file");
|
||||||
@ -347,10 +347,10 @@ public class DownloadManager {
|
|||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
offset += response.getBytes().getData().length;
|
offset += response.getBytes().getData().length;
|
||||||
logger.trace("response: {} total size: {}", response.getBytes().getData().length, offset);
|
logger.trace("response: {} total size: {}", response.getBytes().getData().length, offset);
|
||||||
|
|
||||||
fos.write(response.getBytes().getData());
|
fos.write(response.getBytes().getData());
|
||||||
fos.flush();
|
fos.flush();
|
||||||
try { TimeUnit.MILLISECONDS.sleep(Config.DELAY_AFTER_GET_FILE); } catch(InterruptedException e) {}
|
try { TimeUnit.MILLISECONDS.sleep(Config.DELAY_AFTER_GET_FILE); } catch(InterruptedException e) {}
|
||||||
@ -401,7 +401,7 @@ public class DownloadManager {
|
|||||||
throw ex;
|
throw ex;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean downloadExternalFile(String target, String url) throws IOException {
|
public static boolean downloadExternalFile(String target, String url) throws IOException {
|
||||||
FileUtils.copyURLToFile(new URL(url), new File(target), 5000, 5000);
|
FileUtils.copyURLToFile(new URL(url), new File(target), 5000, 5000);
|
||||||
return true;
|
return true;
|
||||||
|
@ -1,16 +1,16 @@
|
|||||||
/* Telegram_Backup
|
/* Telegram_Backup
|
||||||
* Copyright (C) 2016 Fabian Schlenz
|
* Copyright (C) 2016 Fabian Schlenz
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* 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
|
* it under the terms of the GNU General Public License as published by
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
* (at your option) any later version.
|
* (at your option) any later version.
|
||||||
*
|
*
|
||||||
* This program is distributed in the hope that it will be useful,
|
* This program is distributed in the hope that it will be useful,
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
* GNU General Public License for more details.
|
* GNU General Public License for more details.
|
||||||
*
|
*
|
||||||
* You should have received a copy of the GNU General Public License
|
* You should have received a copy of the GNU General Public License
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
* 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.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,42 +39,41 @@ 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;
|
||||||
}
|
}
|
||||||
public String getTargetFilename() { return "" + message.getId() + "." + getExtension(); }
|
public String getTargetFilename() { return "" + message.getId() + "." + getExtension(); }
|
||||||
public String getTargetPathAndFilename() { return getTargetPath() + getTargetFilename(); }
|
public String getTargetPathAndFilename() { return getTargetPath() + getTargetFilename(); }
|
||||||
|
|
||||||
protected String extensionFromMimetype(String mime) {
|
protected String extensionFromMimetype(String mime) {
|
||||||
switch(mime) {
|
switch(mime) {
|
||||||
case "text/plain": return "txt";
|
case "text/plain": return "txt";
|
||||||
}
|
}
|
||||||
|
|
||||||
int i = mime.lastIndexOf('/');
|
int i = mime.lastIndexOf('/');
|
||||||
String ext = mime.substring(i+1).toLowerCase();
|
String ext = mime.substring(i+1).toLowerCase();
|
||||||
|
|
||||||
if (ext=="unknown") return "dat";
|
if (ext=="unknown") return "dat";
|
||||||
|
|
||||||
return ext;
|
return ext;
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract String getLetter();
|
public abstract String getLetter();
|
||||||
public abstract String getName();
|
public abstract String getName();
|
||||||
public abstract String getDescription();
|
public abstract String getDescription();
|
||||||
|
@ -1,16 +1,16 @@
|
|||||||
/* Telegram_Backup
|
/* Telegram_Backup
|
||||||
* Copyright (C) 2016 Fabian Schlenz
|
* Copyright (C) 2016 Fabian Schlenz
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* 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
|
* it under the terms of the GNU General Public License as published by
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
* (at your option) any later version.
|
* (at your option) any later version.
|
||||||
*
|
*
|
||||||
* This program is distributed in the hope that it will be useful,
|
* This program is distributed in the hope that it will be useful,
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
* GNU General Public License for more details.
|
* GNU General Public License for more details.
|
||||||
*
|
*
|
||||||
* You should have received a copy of the GNU General Public License
|
* You should have received a copy of the GNU General Public License
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
* 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.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,69 +39,67 @@ 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getExtension() {
|
public String getExtension() {
|
||||||
if (extension != null) return extension;
|
if (extension != null) return extension;
|
||||||
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.
|
||||||
ext = ext.replace("\"", "");
|
ext = ext.replace("\"", "");
|
||||||
|
|
||||||
this.extension = ext;
|
this.extension = ext;
|
||||||
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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getLetter() { return "d"; }
|
public String getLetter() { return "d"; }
|
||||||
public String getName() { return "document"; }
|
public String getName() { return "document"; }
|
||||||
public String getDescription() { return "Document"; }
|
public String getDescription() { return "Document"; }
|
||||||
|
@ -1,16 +1,16 @@
|
|||||||
/* Telegram_Backup
|
/* Telegram_Backup
|
||||||
* Copyright (C) 2016 Fabian Schlenz
|
* Copyright (C) 2016 Fabian Schlenz
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* 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
|
* it under the terms of the GNU General Public License as published by
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
* (at your option) any later version.
|
* (at your option) any later version.
|
||||||
*
|
*
|
||||||
* This program is distributed in the hope that it will be useful,
|
* This program is distributed in the hope that it will be useful,
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
* GNU General Public License for more details.
|
* GNU General Public License for more details.
|
||||||
*
|
*
|
||||||
* You should have received a copy of the GNU General Public License
|
* You should have received a copy of the GNU General Public License
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
* 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.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);
|
||||||
}
|
}
|
||||||
|
@ -1,16 +1,16 @@
|
|||||||
/* Telegram_Backup
|
/* Telegram_Backup
|
||||||
* Copyright (C) 2016 Fabian Schlenz
|
* Copyright (C) 2016 Fabian Schlenz
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* 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
|
* it under the terms of the GNU General Public License as published by
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
* (at your option) any later version.
|
* (at your option) any later version.
|
||||||
*
|
*
|
||||||
* This program is distributed in the hope that it will be useful,
|
* This program is distributed in the hope that it will be useful,
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
* GNU General Public License for more details.
|
* GNU General Public License for more details.
|
||||||
*
|
*
|
||||||
* You should have received a copy of the GNU General Public License
|
* You should have received a copy of the GNU General Public License
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
* 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.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,39 +38,39 @@ 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) {
|
|
||||||
super(msg, user, client);
|
public GeoFileManager(Message msg) {
|
||||||
TLAbsGeoPoint g = ((TLMessageMediaGeo)msg.getMedia()).getGeo();
|
super(msg);
|
||||||
if (g instanceof TLGeoPoint) {
|
JsonObject geo = media.getAsJsonObject("geo");
|
||||||
this.geo = (TLGeoPoint) g;
|
if (geo.getAsJsonPrimitive("_constructor").getAsString().startsWith("geoPoint#")) {
|
||||||
} else if (g instanceof TLGeoPointEmpty) {
|
lat = geo.getAsJsonPrimitive("lat").getAsDouble();
|
||||||
this.isEmpty = true;
|
lon = geo.getAsJsonPrimitive("_long").getAsDouble();
|
||||||
} else {
|
} else {
|
||||||
throwUnexpectedObjectError(g);
|
isEmpty = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getSize() {
|
public int getSize() {
|
||||||
File f = new File(getTargetPathAndFilename());
|
File f = new File(getTargetPathAndFilename());
|
||||||
if (f.isFile()) return (int)f.length();
|
if (f.isFile()) return (int)f.length();
|
||||||
|
|
||||||
// We don't know the size, so we just guess.
|
// We don't know the size, so we just guess.
|
||||||
return 100000;
|
return 100000;
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getLetter() { return "g"; }
|
public String getLetter() { return "g"; }
|
||||||
public String getName() { return "geo"; }
|
public String getName() { return "geo"; }
|
||||||
public String getDescription() { return "Geolocation"; }
|
public String getDescription() { return "Geolocation"; }
|
||||||
}
|
}
|
||||||
|
@ -1,16 +1,16 @@
|
|||||||
/* Telegram_Backup
|
/* Telegram_Backup
|
||||||
* Copyright (C) 2016 Fabian Schlenz
|
* Copyright (C) 2016 Fabian Schlenz
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* 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
|
* it under the terms of the GNU General Public License as published by
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
* (at your option) any later version.
|
* (at your option) any later version.
|
||||||
*
|
*
|
||||||
* This program is distributed in the hope that it will be useful,
|
* This program is distributed in the hope that it will be useful,
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
* GNU General Public License for more details.
|
* GNU General Public License for more details.
|
||||||
*
|
*
|
||||||
* You should have received a copy of the GNU General Public License
|
* You should have received a copy of the GNU General Public License
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
* 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.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,45 +39,46 @@ 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;
|
if (photo.getAsJsonPrimitive("_constructor").getAsString().startsWith("messageMediaPhoto#")) {
|
||||||
|
int w = 0;
|
||||||
TLPhotoSize biggest = null;
|
int h = 0;
|
||||||
for (TLAbsPhotoSize s : photo.getSizes()) if (s instanceof TLPhotoSize) {
|
for (JsonElement e : photo.getAsJsonArray("sizes")) {
|
||||||
TLPhotoSize size = (TLPhotoSize) s;
|
JsonObject s = e.getAsJsonObject();
|
||||||
if (biggest == null || (size.getW()>biggest.getW() && size.getH()>biggest.getH())) {
|
if (size==null || (s.getAsJsonPrimitive("w").getAsInt()>w && s.getAsJsonPrimitive("h").getAsInt()>h)) {
|
||||||
biggest = size;
|
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"; }
|
||||||
public String getName() { return "photo"; }
|
public String getName() { return "photo"; }
|
||||||
public String getDescription() { return "Photo"; }
|
public String getDescription() { return "Photo"; }
|
||||||
|
@ -1,16 +1,16 @@
|
|||||||
/* Telegram_Backup
|
/* Telegram_Backup
|
||||||
* Copyright (C) 2016 Fabian Schlenz
|
* Copyright (C) 2016 Fabian Schlenz
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* 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
|
* it under the terms of the GNU General Public License as published by
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
* (at your option) any later version.
|
* (at your option) any later version.
|
||||||
*
|
*
|
||||||
* This program is distributed in the hope that it will be useful,
|
* This program is distributed in the hope that it will be useful,
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
* GNU General Public License for more details.
|
* GNU General Public License for more details.
|
||||||
*
|
*
|
||||||
* You should have received a copy of the GNU General Public License
|
* You should have received a copy of the GNU General Public License
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
* 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.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;
|
||||||
@ -52,56 +48,57 @@ 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();
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getTargetFilename() {
|
public String getTargetFilename() {
|
||||||
return getFilenameBase() + "." + getExtension();
|
return getFilenameBase() + "." + getExtension();
|
||||||
}
|
}
|
||||||
|
|
||||||
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());
|
||||||
|
|
||||||
if (new File(old_file).exists()) {
|
if (new File(old_file).exists()) {
|
||||||
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"; }
|
||||||
|
|
||||||
public String getLetter() { return "s"; }
|
public String getLetter() { return "s"; }
|
||||||
public String getName() { return "sticker"; }
|
public String getName() { return "sticker"; }
|
||||||
public String getDescription() { return "Sticker"; }
|
public String getDescription() { return "Sticker"; }
|
||||||
|
@ -1,16 +1,16 @@
|
|||||||
/* Telegram_Backup
|
/* Telegram_Backup
|
||||||
* Copyright (C) 2016 Fabian Schlenz
|
* Copyright (C) 2016 Fabian Schlenz
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* 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
|
* it under the terms of the GNU General Public License as published by
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
* (at your option) any later version.
|
* (at your option) any later version.
|
||||||
*
|
*
|
||||||
* This program is distributed in the hope that it will be useful,
|
* This program is distributed in the hope that it will be useful,
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
* GNU General Public License for more details.
|
* GNU General Public License for more details.
|
||||||
*
|
*
|
||||||
* You should have received a copy of the GNU General Public License
|
* You should have received a copy of the GNU General Public License
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
* 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 {
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getTargetFilename() {
|
public String getTargetFilename() {
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getTargetPath() {
|
public String getTargetPath() {
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getExtension() { return ""; }
|
public String getExtension() { return ""; }
|
||||||
|
|
||||||
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 " "; }
|
||||||
public String getName() { return type; }
|
public String getName() { return type; }
|
||||||
public String getDescription() { return "Unsupported / non-downloadable Media"; }
|
public String getDescription() { return "Unsupported / non-downloadable Media"; }
|
||||||
|
@ -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