1
0
mirror of https://github.com/fabianonline/telegram_backup.git synced 2024-11-23 01:06:17 +00:00

Completely reworked the file download part and made it much, much more object oriented.

This commit is contained in:
Fabian Schlenz 2016-07-07 17:10:00 +02:00
parent ecbca30a10
commit 2e43ad90a6
15 changed files with 791 additions and 219 deletions

View File

@ -17,6 +17,7 @@
package de.fabianonline.telegram_backup;
import de.fabianonline.telegram_backup.DownloadProgressInterface;
import de.fabianonline.telegram_backup.mediafilemanager.AbstractMediaFileManager;
class CommandLineDownloadProgress implements DownloadProgressInterface {
private int mediaCount = 0;
@ -38,17 +39,18 @@ class CommandLineDownloadProgress implements DownloadProgressInterface {
System.out.println("" + count + " Files to check / download");
}
public void onMediaDownloadedVideo(boolean n) { show(n, 'V'); }
public void onMediaDownloadedPhoto(boolean n) { show(n, 'P'); }
public void onMediaDownloadedDocument(boolean n) { show(n, 'D'); }
public void onMediaDownloadedSticker(boolean n) { show(n, 'S'); }
public void onMediaDownloadedOther(boolean n) { show(n, ' '); }
public void onMediaDownloadedAudio(boolean n) { show(n, 'A'); }
public void onMediaDownloadedGeo(boolean n) { show(n, 'G'); }
public void onMediaDownloadedEmpty(boolean n) { show(true, 'e'); }
public void onMediaDownloaded(AbstractMediaFileManager fm) {
show(fm.getLetter().toUpperCase());
}
public void onMediaDownloadedEmpty() { show("e"); }
public void onMediaAlreadyPresent(AbstractMediaFileManager fm) {
show(".");
}
public void onMediaDownloadFinished() { showNewLine(); System.out.println("Done."); }
private void show(boolean n, char letter) { System.out.print(n ? letter : '.'); i++; if (i % 100 == 0) showNewLine();}
private void show(String letter) { System.out.print(letter); i++; if (i % 100 == 0) showNewLine();}
private void showNewLine() { System.out.println(" - " + i + "/" + mediaCount); }
}

View File

@ -18,7 +18,7 @@ package de.fabianonline.telegram_backup;
import java.io.File;
class Config {
public class Config {
public static final int APP_ID = 32860;
public static final String APP_HASH = "16e4ff955cd0adfc058f95ca564f562d";
public static final String APP_MODEL = "Desktop";

View File

@ -20,6 +20,8 @@ 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.mediafilemanager.FileManagerFactory;
import de.fabianonline.telegram_backup.mediafilemanager.AbstractMediaFileManager;
import com.github.badoualy.telegram.api.TelegramClient;
import com.github.badoualy.telegram.tl.core.TLIntVector;
@ -41,7 +43,7 @@ import java.util.concurrent.TimeoutException;
import org.apache.commons.io.FileUtils;
class DownloadManager {
public class DownloadManager {
UserManager user;
TelegramClient client;
Database db;
@ -182,150 +184,19 @@ class DownloadManager {
LinkedList<TLMessage> messages = this.db.getMessagesWithMedia();
prog.onMediaDownloadStart(messages.size());
for (TLMessage msg : messages) {
downloadSingleMessageMedia(msg, msg.getMedia());
AbstractMediaFileManager m = FileManagerFactory.getFileManager(msg, user, client);
if (m.isEmpty()) {
prog.onMediaDownloadedEmpty();
} else if (m.isDownloaded()) {
prog.onMediaAlreadyPresent(m);
} else {
m.download();
prog.onMediaDownloaded(m);
}
}
prog.onMediaDownloadFinished();
}
public void downloadSingleMessageMedia(TLMessage msg, TLAbsMessageMedia media) throws RpcErrorException, IOException {
if (media instanceof TLMessageMediaPhoto) {
this.downloadMessageMediaPhoto(msg, (TLMessageMediaPhoto)media);
} else if (media instanceof TLMessageMediaDocument) {
this.downloadMessageMediaDocument(msg, (TLMessageMediaDocument)media);
} else if (media instanceof TLMessageMediaVideo) {
this.downloadMessageMediaVideo(msg, (TLMessageMediaVideo)media);
} else if (media instanceof TLMessageMediaAudio) {
this.downloadMessageMediaAudio(msg, (TLMessageMediaAudio)media);
} else if (media instanceof TLMessageMediaGeo) {
this.downloadMessageMediaGeo(msg, (TLMessageMediaGeo)media);
} else if (media instanceof TLMessageMediaEmpty ||
media instanceof TLMessageMediaUnsupported ||
media instanceof TLMessageMediaWebPage ||
media instanceof TLMessageMediaContact ||
media instanceof TLMessageMediaVenue) {
prog.onMediaDownloadedOther(true);
// do nothing
} else {
throw new RuntimeException("Unexpected " + media.getClass().getName());
}
}
private void downloadMessageMediaPhoto(TLMessage msg, TLMessageMediaPhoto p) throws RpcErrorException, IOException {
if (p.getPhoto() instanceof TLPhoto) {
TLPhoto photo = (TLPhoto) p.getPhoto();
TLPhotoSize size = null;
for (TLAbsPhotoSize s : photo.getSizes()) {
if (s instanceof TLPhotoSize) {
TLPhotoSize s2 = (TLPhotoSize) s;
if (size == null || (s2.getW()>size.getW() && s2.getH()>size.getH())) {
size = s2;
}
}
}
if (size==null) {
throw new RuntimeException("Could not find a size for a photo.");
}
if (size.getLocation() instanceof TLFileLocation) {
boolean res = this.downloadPhoto(msg.getId(), (TLFileLocation)size.getLocation(), size.getSize());
prog.onMediaDownloadedPhoto(res);
}
} else if (p.getPhoto() instanceof TLPhotoEmpty) {
downloadEmptyObject(p.getPhoto());
} else {
throw new RuntimeException("Got an unexpected " + p.getPhoto().getClass().getName());
}
}
private void downloadMessageMediaDocument(TLMessage msg, TLMessageMediaDocument d) throws RpcErrorException, IOException {
if (d.getDocument() instanceof TLDocument) {
TLDocument doc = (TLDocument)d.getDocument();
//Determine files extension
String original_filename = null;
TLDocumentAttributeSticker sticker = null;
String ext = null;
for(TLAbsDocumentAttribute attr : doc.getAttributes()) {
if (attr instanceof TLDocumentAttributeFilename) {
original_filename = ((TLDocumentAttributeFilename)attr).getFileName();
} else if (attr instanceof TLDocumentAttributeSticker) {
sticker = (TLDocumentAttributeSticker)attr;
}
}
if (original_filename != null) {
int i = original_filename.lastIndexOf('.');
if (i>0) ext = original_filename.substring(i+1);
}
if (ext==null) {
int i = doc.getMimeType().lastIndexOf('/');
String type = doc.getMimeType().substring(i+1).toLowerCase();
if (type.equals("unknown")) {
ext = "dat";
} else {
ext = type;
}
}
String filename;
if (sticker != null) {
filename = StickerConverter.makeFilenameWithPath(sticker);
} else {
filename = this.makeFilename(msg.getId(), ext);
}
boolean res = this.downloadDocument(filename, doc);
if (sticker != null) {
prog.onMediaDownloadedSticker(res);
} else {
prog.onMediaDownloadedDocument(res);
}
} else if (d.getDocument() instanceof TLDocumentEmpty) {
downloadEmptyObject(d.getDocument());
} else {
throw new RuntimeException("Got an unexpected " + d.getDocument().getClass().getName());
}
}
private void downloadMessageMediaVideo(TLMessage msg, TLMessageMediaVideo v) throws RpcErrorException, IOException {
if (v.getVideo() instanceof TLVideo) {
TLVideo vid = (TLVideo)v.getVideo();
int i = vid.getMimeType().lastIndexOf('/');
String ext = vid.getMimeType().substring(i+1).toLowerCase();
boolean res = this.downloadVideo(this.makeFilename(msg.getId(), ext), vid);
prog.onMediaDownloadedVideo(res);
} else if (v.getVideo() instanceof TLVideoEmpty) {
downloadEmptyObject(v.getVideo());
} else {
throw new RuntimeException("Got an unexpected " + v.getVideo().getClass().getName());
}
}
private void downloadMessageMediaAudio(TLMessage msg, TLMessageMediaAudio a) throws RpcErrorException, IOException {
if (a.getAudio() instanceof TLAudio) {
TLAudio audio = (TLAudio)a.getAudio();
int i = audio.getMimeType().lastIndexOf('/');
String ext = audio.getMimeType().substring(i+1).toLowerCase();
boolean res = this.downloadAudio(this.makeFilename(msg.getId(), ext), audio);
prog.onMediaDownloadedAudio(res);
} else if (a.getAudio() instanceof TLAudioEmpty) {
downloadEmptyObject(a.getAudio());
} else {
throw new RuntimeException("Got an unexpected " + a.getAudio().getClass().getName());
}
}
private void downloadMessageMediaGeo(TLMessage msg, TLMessageMediaGeo g) throws IOException {
if (g.getGeo() instanceof TLGeoPoint) {
TLGeoPoint geo = (TLGeoPoint)g.getGeo();
String url = "https://maps.googleapis.com/maps/api/staticmap?center=" +
geo.getLat() + "," + geo.getLong() + "&zoom=14&size=300x150&scale=2&format=png&key=" + Config.SECRET_GMAPS;
boolean res = downloadExternalFile(this.makeFilename(msg.getId(), "png"), url);
prog.onMediaDownloadedGeo(res);
} else if (g.getGeo() instanceof TLGeoPointEmpty) {
downloadEmptyObject(g.getGeo());
} else {
throw new RuntimeException("Got an unexpected " + g.getGeo().getClass().getName());
}
}
private ArrayList<Integer> makeIdList(int start, int end) {
if (start > end) throw new RuntimeException("start and end reversed");
ArrayList<Integer> a = new ArrayList<Integer>(end - start + 1);
@ -333,57 +204,17 @@ class DownloadManager {
return a;
}
private boolean downloadPhoto(int msgId, TLFileLocation src, int size) throws RpcErrorException, IOException {
TLInputFileLocation loc = new TLInputFileLocation();
loc.setVolumeId(src.getVolumeId());
loc.setLocalId(src.getLocalId());
loc.setSecret(src.getSecret());
return this.downloadFile(this.makeFilename(msgId, "jpg"), loc, size);
public static void downloadFile(TelegramClient client, String targetFilename, int size, long volumeId, int localId, long secret) throws RpcErrorException, IOException {
TLInputFileLocation loc = new TLInputFileLocation(volumeId, localId, secret);
downloadFileFromDc(client, targetFilename, loc, null, size);
}
private boolean downloadDocument(String filename, TLDocument doc) throws RpcErrorException, IOException {
TLInputDocumentFileLocation loc = new TLInputDocumentFileLocation();
loc.setId(doc.getId());
loc.setAccessHash(doc.getAccessHash());
return this.downloadFileFromDc(filename, loc, doc.getDcId(), doc.getSize());
public static void downloadFile(TelegramClient client, String targetFilename, int size, int dcId, long id, long accessHash) throws RpcErrorException, IOException {
TLInputDocumentFileLocation loc = new TLInputDocumentFileLocation(id, accessHash);
downloadFileFromDc(client, targetFilename, loc, dcId, size);
}
private boolean downloadVideo(String filename, TLVideo vid) throws RpcErrorException, IOException {
TLInputDocumentFileLocation loc = new TLInputDocumentFileLocation();
loc.setId(vid.getId());
loc.setAccessHash(vid.getAccessHash());
return this.downloadFileFromDc(filename, loc, vid.getDcId(), vid.getSize());
}
private boolean downloadAudio(String filename, TLAudio audio) throws RpcErrorException, IOException {
TLInputDocumentFileLocation loc = new TLInputDocumentFileLocation();
loc.setId(audio.getId());
loc.setAccessHash(audio.getAccessHash());
return this.downloadFileFromDc(filename, loc, audio.getDcId(), audio.getSize());
}
private void downloadEmptyObject(TLObject obj) {
prog.onMediaDownloadedEmpty(true);
}
private String makeFilename(int id, String ext) {
String path = this.user.getFileBase() +
Config.FILE_FILES_BASE +
File.separatorChar;
new File(path).mkdirs();
if (ext!=null) return path + id + "." + ext;
return path + id + ".dat";
}
private boolean downloadFile(String target, TLAbsInputFileLocation loc, int size) throws RpcErrorException, IOException {
return downloadFileFromDc(target, loc, null, size);
}
private boolean downloadFileFromDc(String target, TLAbsInputFileLocation loc, Integer dcID, int size) throws RpcErrorException, IOException {
// Don't download already existing files.
if (new File(target).isFile()) return false;
private static boolean downloadFileFromDc(TelegramClient client, String target, TLAbsInputFileLocation loc, Integer dcID, int size) throws RpcErrorException, IOException {
FileOutputStream fos = null;
try {
fos = new FileOutputStream(target);
@ -392,9 +223,9 @@ class DownloadManager {
do {
TLRequestUploadGetFile req = new TLRequestUploadGetFile(loc, offset, Config.FILE_DOWNLOAD_BLOCK_SIZE);
if (dcID==null) {
response = (TLFile)this.client.executeRpcQuery(req);
response = (TLFile) client.executeRpcQuery(req);
} else {
response = (TLFile) this.client.executeRpcQuery(req, dcID);
response = (TLFile) client.executeRpcQuery(req, dcID);
}
offset += response.getBytes().getData().length;
fos.write(response.getBytes().getData());
@ -420,8 +251,7 @@ class DownloadManager {
}
}
private boolean downloadExternalFile(String target, String url) throws IOException {
if (new File(target).isFile()) return false;
public static boolean downloadExternalFile(String target, String url) throws IOException {
FileUtils.copyURLToFile(new URL(url), new File(target), 5000, 5000);
return true;
}

View File

@ -16,19 +16,16 @@
package de.fabianonline.telegram_backup;
interface DownloadProgressInterface {
import de.fabianonline.telegram_backup.mediafilemanager.AbstractMediaFileManager;
public interface DownloadProgressInterface {
public void onMessageDownloadStart(int count);
public void onMessageDownloaded(int number);
public void onMessageDownloadFinished();
public void onMediaDownloadStart(int count);
public void onMediaDownloadedVideo(boolean n);
public void onMediaDownloadedPhoto(boolean n);
public void onMediaDownloadedDocument(boolean n);
public void onMediaDownloadedSticker(boolean n);
public void onMediaDownloadedOther(boolean n);
public void onMediaDownloadedAudio(boolean n);
public void onMediaDownloadedGeo(boolean n);
public void onMediaDownloadedEmpty(boolean n);
public void onMediaDownloaded(AbstractMediaFileManager a);
public void onMediaDownloadedEmpty();
public void onMediaAlreadyPresent(AbstractMediaFileManager a);
public void onMediaDownloadFinished();
}

View File

@ -20,7 +20,7 @@ import com.github.badoualy.telegram.tl.api.*;
import java.lang.StringBuilder;
import java.io.File;
class StickerConverter {
public class StickerConverter {
public static String makeFilenameWithPath(TLDocumentAttributeSticker attr) {
StringBuilder file = new StringBuilder();
file.append(makePath());

View File

@ -23,6 +23,8 @@ import com.github.badoualy.telegram.tl.core.TLVector;
import de.fabianonline.telegram_backup.Database;
import de.fabianonline.telegram_backup.UserManager;
import de.fabianonline.telegram_backup.mediafilemanager.AbstractMediaFileManager;
import de.fabianonline.telegram_backup.mediafilemanager.FileManagerFactory;
class TelegramUpdateHandler implements UpdateCallback {
private UserManager user = null;
@ -131,15 +133,18 @@ class TelegramUpdateHandler implements UpdateCallback {
vector.add(abs_msg);
db.saveMessages(vector);
System.out.print('.');
if (abs_msg instanceof TLMessage && ((TLMessage)abs_msg).getMedia()!=null) {
if (abs_msg instanceof TLMessage) {
AbstractMediaFileManager fm = FileManagerFactory.getFileManager((TLMessage)abs_msg, user, client);
if (fm != null && !fm.isEmpty() && !fm.isDownloaded()) {
try {
new DownloadManager(user, client, new CommandLineDownloadProgress()).downloadSingleMessageMedia((TLMessage)abs_msg, ((TLMessage)abs_msg).getMedia());
fm.download();
} catch (Exception e) {
System.out.println("We got an exception while downloading media, but we're going to ignore it.");
System.out.println("Here it is anyway:");
e.printStackTrace();
}
}
}
} else {
// ignore everything else...
}

View File

@ -0,0 +1,79 @@
/* 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/>. */
package de.fabianonline.telegram_backup.mediafilemanager;
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.Config;
import de.fabianonline.telegram_backup.DownloadManager;
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;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.LinkedList;
import java.net.URL;
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 boolean isEmpty = false;
public AbstractMediaFileManager(TLMessage msg, UserManager user, TelegramClient client) {this.user = user; this.message = msg; this.client = client;};
public abstract int getSize();
public abstract String getExtension();
public boolean isEmpty() { return isEmpty; }
public boolean isDownloaded() { return new File(getTargetPathAndFilename()).isFile(); }
public abstract void download() throws RpcErrorException, IOException;
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;
new File(path).mkdirs();
return path;
}
public String getTargetFilename() { return "" + message.getId() + "." + getExtension(); }
public String getTargetPathAndFilename() { return getTargetPath() + getTargetFilename(); }
protected String extensionFromMimetype(String mime) {
int i = mime.lastIndexOf('/');
String ext = mime.substring(i+1).toLowerCase();
if (ext=="unknown") ext="dat";
return ext;
}
public abstract String getLetter();
public abstract String getName();
public abstract String getDescription();
}

View File

@ -0,0 +1,73 @@
/* 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/>. */
package de.fabianonline.telegram_backup.mediafilemanager;
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.DownloadManager;
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;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.LinkedList;
import java.net.URL;
import java.util.concurrent.TimeoutException;
import org.apache.commons.io.FileUtils;
public class AudioFileManager extends AbstractMediaFileManager {
protected TLAudio audio;
public AudioFileManager(TLMessage msg, UserManager user, TelegramClient client) {
super(msg, user, client);
TLAbsAudio a = ((TLMessageMediaAudio)msg.getMedia()).getAudio();
if (a instanceof TLAudio) {
this.audio = (TLAudio) a;
} else if (a instanceof TLAudioEmpty) {
this.isEmpty = true;
} else {
throwUnexpectedObjectError(a);
}
}
public int getSize() { return audio.getSize(); }
public String getExtension() {
return extensionFromMimetype(audio.getMimeType());
}
public void download() throws RpcErrorException, IOException {
DownloadManager.downloadFile(client, getTargetPathAndFilename(), getSize(), audio.getDcId(), audio.getId(), audio.getAccessHash());
}
public String getLetter() { return "a"; }
public String getName() { return "audio"; }
public String getDescription() { return "Audio"; }
}

View File

@ -0,0 +1,101 @@
/* 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/>. */
package de.fabianonline.telegram_backup.mediafilemanager;
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.DownloadManager;
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;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.LinkedList;
import java.net.URL;
import java.util.concurrent.TimeoutException;
import org.apache.commons.io.FileUtils;
public class DocumentFileManager extends AbstractMediaFileManager {
protected TLDocument 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 boolean isSticker() {
TLDocumentAttributeSticker sticker = null;
for(TLAbsDocumentAttribute attr : doc.getAttributes()) {
if (attr instanceof TLDocumentAttributeSticker) {
sticker = (TLDocumentAttributeSticker)attr;
}
}
return sticker!=null;
}
public int getSize() { return doc.getSize(); }
public String getExtension() {
if (extension != null) return extension;
String ext = null;
String original_filename = null;
for(TLAbsDocumentAttribute attr : doc.getAttributes()) {
if (attr instanceof TLDocumentAttributeFilename) {
original_filename = ((TLDocumentAttributeFilename)attr).getFileName();
}
}
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());
}
this.extension = ext;
return ext;
}
public void download() throws RpcErrorException, IOException {
DownloadManager.downloadFile(client, getTargetPathAndFilename(), getSize(), doc.getDcId(), doc.getId(), doc.getAccessHash());
}
public String getLetter() { return "d"; }
public String getName() { return "document"; }
public String getDescription() { return "Document"; }
}

View File

@ -0,0 +1,78 @@
/* 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/>. */
package de.fabianonline.telegram_backup.mediafilemanager;
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 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;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.LinkedList;
import java.net.URL;
import java.util.concurrent.TimeoutException;
import org.apache.commons.io.FileUtils;
public class FileManagerFactory {
public static AbstractMediaFileManager getFileManager(TLMessage m, UserManager u, TelegramClient c) {
TLAbsMessageMedia media = m.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);
if (d.isSticker()) {
return new StickerFileManager(m, u, c);
}
return d;
} else if (media instanceof TLMessageMediaVideo) {
return new VideoFileManager(m, u, c);
} else if (media instanceof TLMessageMediaAudio) {
return new AudioFileManager(m, u, c);
} 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 {
AbstractMediaFileManager.throwUnexpectedObjectError(media);
}
return null;
}
}

View File

@ -0,0 +1,82 @@
/* 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/>. */
package de.fabianonline.telegram_backup.mediafilemanager;
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.DownloadManager;
import de.fabianonline.telegram_backup.Config;
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;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.LinkedList;
import java.net.URL;
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;
} else {
throwUnexpectedObjectError(g);
}
}
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 {
String url = "https://maps.googleapis.com/maps/api/staticmap?" +
"center=" + geo.getLat() + "," + geo.getLong() + "&" +
"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 getDescription() { return "Geolocation"; }
}

View File

@ -0,0 +1,86 @@
/* 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/>. */
package de.fabianonline.telegram_backup.mediafilemanager;
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.DownloadManager;
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;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.LinkedList;
import java.net.URL;
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;
}
}
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;
} else {
throwUnexpectedObjectError(p);
}
}
public int getSize() {
return size.getSize();
}
public String getExtension() { return "jpg"; }
public void download() throws RpcErrorException, IOException {
TLFileLocation loc = (TLFileLocation) size.getLocation();
DownloadManager.downloadFile(client, getTargetPathAndFilename(), getSize(), loc.getVolumeId(), loc.getLocalId(), loc.getSecret());
}
public String getLetter() { return "p"; }
public String getName() { return "photo"; }
public String getDescription() { return "Photo"; }
}

View File

@ -0,0 +1,94 @@
/* 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/>. */
package de.fabianonline.telegram_backup.mediafilemanager;
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.DownloadManager;
import de.fabianonline.telegram_backup.Config;
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;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.LinkedList;
import java.net.URL;
import java.util.concurrent.TimeoutException;
import org.apache.commons.io.FileUtils;
public class StickerFileManager extends DocumentFileManager {
public StickerFileManager(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 boolean isSticker() { return true; }
public String getTargetFilename() {
TLDocumentAttributeSticker sticker = null;
for(TLAbsDocumentAttribute attr : doc.getAttributes()) {
if (attr instanceof TLDocumentAttributeSticker) {
sticker = (TLDocumentAttributeSticker)attr;
}
}
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());
}
file.append("_");
file.append(sticker.getAlt().hashCode());
file.append(".");
file.append(getExtension());
return file.toString();
}
public String getTargetPath() {
String path = Config.FILE_BASE + File.separatorChar + Config.FILE_STICKER_BASE + File.separatorChar;
new File(path).mkdirs();
return path;
}
public String getExtension() { return "webp"; }
public String getLetter() { return "s"; }
public String getName() { return "sticker"; }
public String getDescription() { return "Sticker"; }
}

View File

@ -0,0 +1,72 @@
/* 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/>. */
package de.fabianonline.telegram_backup.mediafilemanager;
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.DownloadManager;
import de.fabianonline.telegram_backup.Config;
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;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.LinkedList;
import java.net.URL;
import java.util.concurrent.TimeoutException;
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);
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 boolean isDownloaded() { return false; }
public String getLetter() { return " "; }
public String getName() { return type; }
public String getDescription() { return "Unsupported / non-downloadable Media"; }
}

View File

@ -0,0 +1,73 @@
/* 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/>. */
package de.fabianonline.telegram_backup.mediafilemanager;
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.DownloadManager;
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;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.LinkedList;
import java.net.URL;
import java.util.concurrent.TimeoutException;
import org.apache.commons.io.FileUtils;
public class VideoFileManager extends AbstractMediaFileManager {
protected TLVideo video;
public VideoFileManager(TLMessage msg, UserManager user, TelegramClient client) {
super(msg, user, client);
TLAbsVideo v = ((TLMessageMediaVideo)msg.getMedia()).getVideo();
if (v instanceof TLVideo) {
this.video = (TLVideo) v;
} else if (v instanceof TLVideoEmpty) {
this.isEmpty = true;
} else {
throwUnexpectedObjectError(v);
}
}
public int getSize() { return video.getSize(); }
public String getExtension() {
return extensionFromMimetype(video.getMimeType());
}
public void download() throws RpcErrorException, IOException {
DownloadManager.downloadFile(client, getTargetPathAndFilename(), getSize(), video.getDcId(), video.getId(), video.getAccessHash());
}
public String getLetter() { return "v"; }
public String getName() { return "video"; }
public String getDescription() { return "Video"; }
}