1
0
mirror of https://github.com/fabianonline/telegram_backup.git synced 2024-11-22 16:56:16 +00:00

Merge branch 'master' into feature-json

This commit is contained in:
Fabian Schlenz 2017-02-22 11:09:41 +01:00
commit 0b59b35b0d
7 changed files with 97 additions and 70 deletions

View File

@ -41,11 +41,7 @@ public class CommandLineController {
public CommandLineController() {
logger.info("CommandLineController started. App version {}", Config.APP_APPVER);
System.out.println("Telegram_Backup version " + Config.APP_APPVER + ", Copyright (C) 2016 Fabian Schlenz");
System.out.println();
System.out.println("Telegram_Backup comes with ABSOLUTELY NO WARRANTY. This is free software, and you are");
System.out.println("welcome to redistribute it under certain conditions; run it with '--license' for details.");
System.out.println();
this.printHeader();
if (CommandLineOptions.cmd_version) {
System.exit(0);
@ -57,54 +53,19 @@ public class CommandLineController {
System.exit(0);
}
logger.debug("Target dir at startup: {}", Utils.anonymize(Config.FILE_BASE));
if (CommandLineOptions.val_target != null) {
Config.FILE_BASE = CommandLineOptions.val_target;
}
logger.debug("Target dir after options: {}", Utils.anonymize(Config.FILE_BASE));
this.setupFileBase();
System.out.println("Base directory for files: " + Utils.anonymize(Config.FILE_BASE));
if (CommandLineOptions.cmd_list_accounts) this.list_accounts();
if (CommandLineOptions.cmd_list_accounts) {
this.list_accounts();
System.exit(0);
}
logger.debug("Initializing TelegramApp");
app = new TelegramApp(Config.APP_ID, Config.APP_HASH, Config.APP_MODEL, Config.APP_SYSVER, Config.APP_APPVER, Config.APP_LANG);
logger.trace("Checking accounts");
String account = null;
Vector<String> accounts = Utils.getAccounts();
if (CommandLineOptions.cmd_login) {
logger.debug("Login requested, doing nothing.");
// do nothing
} else if (CommandLineOptions.val_account!=null) {
logger.debug("Account requested: {}", Utils.anonymize(CommandLineOptions.val_account));
logger.trace("Checking accounts for match.");
boolean found = false;
for (String acc : accounts) {
logger.trace("Checking {}", Utils.anonymize(acc));
if (acc.equals(CommandLineOptions.val_account)) {
found=true;
logger.trace("Matches.");
break;
}
}
if (!found) {
show_error("Couldn't find account '" + Utils.anonymize(CommandLineOptions.val_account) + "'. Maybe you want to use '--login' first?");
}
account = CommandLineOptions.val_account;
} else if (accounts.size()==0) {
System.out.println("No accounts found. Starting login process...");
CommandLineOptions.cmd_login = true;
} else if (accounts.size()==1) {
account = accounts.firstElement();
System.out.println("Using only available account: " + Utils.anonymize(account));
} else {
show_error("You didn't specify which account to use.\n" +
"Use '--account <x>' to use account <x>.\n" +
"Use '--list-accounts' to see all available accounts.");
}
logger.debug("accounts.size(): {}", accounts.size());
logger.debug("account: {}", Utils.anonymize(account));
String account = this.selectAccount();
logger.debug("CommandLineOptions.cmd_login: {}", CommandLineOptions.cmd_login);
logger.info("Initializing ApiStorage");
@ -160,6 +121,7 @@ public class CommandLineController {
System.out.println("You are logged in as " + Utils.anonymize(user.getUserString()));
} else {
System.out.println("You are not logged in.");
System.exit(1);
}
logger.info("Initializing Download Manager");
@ -190,6 +152,62 @@ public class CommandLineController {
}
}
private void printHeader() {
System.out.println("Telegram_Backup version " + Config.APP_APPVER + ", Copyright (C) 2016 Fabian Schlenz");
System.out.println();
System.out.println("Telegram_Backup comes with ABSOLUTELY NO WARRANTY. This is free software, and you are");
System.out.println("welcome to redistribute it under certain conditions; run it with '--license' for details.");
System.out.println();
}
private void setupFileBase() {
logger.debug("Target dir at startup: {}", Utils.anonymize(Config.FILE_BASE));
if (CommandLineOptions.val_target != null) {
Config.FILE_BASE = CommandLineOptions.val_target;
}
logger.debug("Target dir after options: {}", Utils.anonymize(Config.FILE_BASE));
System.out.println("Base directory for files: " + Utils.anonymize(Config.FILE_BASE));
}
private String selectAccount() {
String account = null;
Vector<String> accounts = Utils.getAccounts();
if (CommandLineOptions.cmd_login) {
logger.debug("Login requested, doing nothing.");
// do nothing
} else if (CommandLineOptions.val_account!=null) {
logger.debug("Account requested: {}", Utils.anonymize(CommandLineOptions.val_account));
logger.trace("Checking accounts for match.");
boolean found = false;
for (String acc : accounts) {
logger.trace("Checking {}", Utils.anonymize(acc));
if (acc.equals(CommandLineOptions.val_account)) {
found=true;
logger.trace("Matches.");
break;
}
}
if (!found) {
show_error("Couldn't find account '" + Utils.anonymize(CommandLineOptions.val_account) + "'. Maybe you want to use '--login' first?");
}
account = CommandLineOptions.val_account;
} else if (accounts.size()==0) {
System.out.println("No accounts found. Starting login process...");
CommandLineOptions.cmd_login = true;
} else if (accounts.size()==1) {
account = accounts.firstElement();
System.out.println("Using only available account: " + Utils.anonymize(account));
} else {
show_error("You didn't specify which account to use.\n" +
"Use '--account <x>' to use account <x>.\n" +
"Use '--list-accounts' to see all available accounts.");
}
logger.debug("accounts.size(): {}", accounts.size());
logger.debug("account: {}", Utils.anonymize(account));
return account;
}
private void cmd_login(String phone) throws RpcErrorException, IOException {
if (phone==null) {
System.out.println("Please enter your phone number in international format.");
@ -260,7 +278,6 @@ public class CommandLineController {
System.out.println("NO ACCOUNTS FOUND");
System.out.println("Use '--login' to login to a telegram account.");
}
System.exit(0);
}
public static void show_error(String error) {

View File

@ -31,7 +31,20 @@ public class CommandLineRunner {
public static void main(String[] args) {
CommandLineOptions.parseOptions(args);
// Set up logging
setupLogging();
checkVersion();
if (true || CommandLineOptions.cmd_console) {
// Always use the console for now.
new CommandLineController();
} else {
new GUIController();
}
}
public static void setupLogging() {
Logger logger = (Logger)LoggerFactory.getLogger(CommandLineRunner.class);
Logger rootLogger = (Logger)LoggerFactory.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
LoggerContext rootContext = rootLogger.getLoggerContext();
@ -59,7 +72,9 @@ public class CommandLineRunner {
if (CommandLineOptions.cmd_trace_telegram) {
((Logger)LoggerFactory.getLogger("com.github.badoualy")).setLevel(Level.TRACE);
}
}
public static boolean checkVersion() {
Version v = Utils.getNewestVersion();
if (v!=null && v.isNewer) {
System.out.println("A newer version is vailable!");
@ -70,13 +85,8 @@ public class CommandLineRunner {
System.out.println("Changes in this version:");
System.out.println(v.body);
System.out.println();
return false;
}
if (true || CommandLineOptions.cmd_console) {
// Always use the console for now.
new CommandLineController();
} else {
new GUIController();
}
return true;
}
}

View File

@ -244,7 +244,7 @@ public class DownloadManager {
} else {
throw e;
}
} catch (TimeoutException e) {
} /*catch (TimeoutException e) {
completed = false;
System.out.println("");
System.out.println("Telegram took too long to respond to our request.");
@ -252,11 +252,11 @@ public class DownloadManager {
logger.warn("TimeoutException caught", e);
try { TimeUnit.MINUTES.sleep(1); } catch(InterruptedException e2) {}
System.out.println("");
}
}*/
} while (!completed);
}
private void _downloadMedia() throws RpcErrorException, IOException, TimeoutException {
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");
LinkedList<Integer> ids = db.getIdsFromQuery("SELECT id FROM messages WHERE has_media=1 AND api_layer<" + Kotlogram.API_LAYER);
@ -282,13 +282,13 @@ public class DownloadManager {
} else if (m.isDownloaded()) {
prog.onMediaAlreadyPresent(m);
} else {
/*try {*/
try {
m.download();
prog.onMediaDownloaded(m);
/*} catch (TimeoutException e) {
} catch (TimeoutException e) {
// do nothing - skip this file
prog.onMediaSkipped();
}*/
}
}
}
prog.onMediaDownloadFinished();
@ -300,17 +300,17 @@ public class DownloadManager {
return a;
}
public static void downloadFile(TelegramClient client, String targetFilename, int size, int dcId, long volumeId, int localId, long secret) throws RpcErrorException, IOException {
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 {
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 {
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";

View File

@ -55,7 +55,7 @@ public abstract class AbstractMediaFileManager {
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;
public abstract void download() throws RpcErrorException, IOException, TimeoutException;
public static void throwUnexpectedObjectError(Object o) {
throw new RuntimeException("Unexpected " + o.getClass().getName());
}

View File

@ -100,7 +100,7 @@ public class DocumentFileManager extends AbstractMediaFileManager {
return ext;
}
public void download() throws RpcErrorException, IOException {
public void download() throws RpcErrorException, IOException, TimeoutException {
if (doc!=null) {
DownloadManager.downloadFile(client, getTargetPathAndFilename(), getSize(), doc.getDcId(), doc.getId(), doc.getAccessHash());
}

View File

@ -76,7 +76,7 @@ public class PhotoFileManager extends AbstractMediaFileManager {
public String getExtension() { return "jpg"; }
public void download() throws RpcErrorException, IOException {
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());

View File

@ -88,7 +88,7 @@ public class StickerFileManager extends DocumentFileManager {
return path;
}
public void download() throws RpcErrorException, IOException {
public void download() 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());