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