rubenlagus / TelegramApi

Java library to create Telegram Clients
MIT License
300 stars 108 forks source link

download uploaded file #70

Open khatarat opened 6 years ago

khatarat commented 6 years ago

hi i want to upload a file with telegram api Uploader and then download it. but for request a download task in downloader i need volume_id, secret and localId but i dont know how can i get this parameters.

uploader just give me a uniqId.

can you tell me how can i get this 3 parameters for uploaded file? volume_id secret localId

onixred commented 6 years ago

I take a message and convert it's structure

pay attention to the line class TLFileLocation

mediaEntity.setLocation(curentLocation);
    public static MessageEntity convertTLMessage2MessageEntity(TLMessage message) {
        MessageEntity messageEntity = new MessageEntity();
        messageEntity.setText(message.getMessage());
        messageEntity.setId(message.getId());
        TLAbsMessageMedia media = message.getMedia();
        if (media instanceof TLMessageMediaWebPage) {
            TLAbsWebPage absWebPage = ((TLMessageMediaWebPage) media).getWebPage();
            if (absWebPage instanceof TLWebPage) {
                MediaEntity mediaEntity = convertTLWebPage2MediaEntity((TLWebPage) absWebPage);
                messageEntity.setMedia(mediaEntity);
            }
        }
        return messageEntity;
    }

    public static MediaEntity convertTLWebPage2MediaEntity(TLWebPage media) {
        MediaEntity mediaEntity = new MediaEntity();
        mediaEntity.setTitle(media.getTitle());
        mediaEntity.setDescription(media.getDescription());
        mediaEntity.setUrlName(media.getDisplay_url());
        mediaEntity.setUrl(media.getUrl());

        TLAbsPhoto tLAbsPhoto = media.getPhoto();
        if (tLAbsPhoto instanceof TLPhoto) {
            // Java timestamp is millisecods past epoch
            Instant instant = Instant.ofEpochSecond(((TLPhoto)tLAbsPhoto).getDate());
            ZonedDateTime zdt = ZonedDateTime.ofInstant(instant, ZoneId.systemDefault());
            Calendar cal =  GregorianCalendar.from(zdt);
            mediaEntity .setDate(cal.getTime());
            TLVector<TLAbsPhotoSize> sizes = ((TLPhoto) tLAbsPhoto).getSizes();
            if (sizes != null && !sizes.isEmpty()) {
                int maxSize = 0;
                TLFileLocation curentLocation = null;
                for (TLAbsPhotoSize tLAbsPhotoSize : sizes) {
                    if (tLAbsPhotoSize instanceof TLPhotoSize) {
                        if (((TLPhotoSize) tLAbsPhotoSize).getSize() > maxSize) {
                            TLAbsFileLocation tLAbsFileLocation = ((TLPhotoSize) tLAbsPhotoSize).getLocation();
                            if (tLAbsFileLocation instanceof TLFileLocation) {
                                maxSize = ((TLPhotoSize) tLAbsPhotoSize).getSize();
                                curentLocation = ((TLFileLocation) tLAbsFileLocation);
                            }
                        }
                    }
                }
                mediaEntity.setLocation(curentLocation);
                mediaEntity.setSize(maxSize);
            }
        }
        return mediaEntity;
    }
}