start to implementate getFile method

This commit is contained in:
Andrei Yankovich 2023-09-21 22:28:33 +02:00
parent 9858424f4f
commit f1c28f61d1
12 changed files with 137 additions and 444 deletions

View File

@ -21,7 +21,7 @@
],
"extraLib": "crypto",
"targetDir": "/media/D/builds/qTbot/Distro",
"deployVersion": "0.21.fadcb9d",
"deployVersion": "0.22.9858424",
}

View File

@ -22,6 +22,10 @@ const QFile & File::localFile() const {
return _localFile;
}
iFile::Type File::type() const {
return Type::Local;
}
void File::handleReadReady() {
auto&& bytes = replay()->readAll();

View File

@ -24,6 +24,7 @@ public:
const QFile & localFile() const;
Type type() const override;
// iFile interface
protected slots:

View File

@ -10,7 +10,7 @@
#include <QtCore/qglobal.h>
#define QTBOT_VERSION "0.21.fadcb9d"
#define QTBOT_VERSION "0.22.9858424"
#if defined(QTBOT_LIBRARY)
# define QTBOT_EXPORT Q_DECL_EXPORT

View File

@ -13,6 +13,8 @@
#include "qTbot/imessage.h"
#include "qTbot/irequest.h"
#include "ifile.h"
#include <QMap>
#include <QHash>
#include <QSet>
@ -60,6 +62,18 @@ public:
*/
virtual bool sendMessage(const QVariant& chatId, const QString& text) = 0;
/**
* @brief Get a file by its ID.
*
* This function allows you to retrieve a file by its ID.
*
* @param fileId The ID of the file to retrieve.
* @param fileType This is a saving way, by Default will be used a iFile::Type::Ram
* @return Returns true if the file retrieval operation was successfully initiated and false in case of an error.
*/
virtual QSharedPointer<iFile> getFile(const QString& fileId, iFile::Type fileType = iFile::Type::Ram) = 0;
/**
* @brief token This is token value for authication on the remote server (bot)
* @return auth toke of the bot.

View File

@ -21,6 +21,17 @@ class iFile: public QObject
{
Q_OBJECT
public:
/**
* @brief The Type enum is type of the file object.
*/
enum Type {
/// This is local file, all receive bytes will be save directed into file.
Local,
/// This is memory saved file. All received bytes will be saved into QByteArray object.
Ram
};
iFile(const QSharedPointer<QNetworkReply>& replay);
/**
@ -65,6 +76,11 @@ public:
*/
const QSharedPointer<QNetworkReply>& replay() const;
/**
* @brief type This is type of the file object.
* @return type of the file object.
*/
virtual Type type() const = 0;
protected slots:

View File

@ -7,8 +7,10 @@
#include "itelegrambot.h"
#include "qTbot/messages/telegramupdateansver.h"
#include "qdir.h"
#include <QNetworkAccessManager>
#include <qTbot/requests/telegramgetfile.h>
#include <qTbot/requests/telegramgetme.h>
#include <qTbot/requests/telegramsendmsg.h>
@ -77,6 +79,35 @@ bool ITelegramBot::sendSpecificMessage(const QVariant & chatId,
return sendRequest(msg, cb);
}
QSharedPointer<iFile> ITelegramBot::getFile(const QString &fileId, iFile::Type fileType) {
auto localFilePath = findFileInlocatStorage(fileId);
if (!localFilePath.isEmpty()) {
if (fileType == iFile::Ram) {
QFile localFile(localFilePath);
if (localFile.open(QIODevice::ReadOnly)) {
VirtualFile result(nullptr, localFile.readAll());
localFile.close();
return result;
}
}
}
}
bool ITelegramBot::getFile(const QString &fileId, std::function<void (const QSharedPointer<File> &)> cb) {
}
bool ITelegramBot::getFile(const QString &fileId, std::function<void (const QSharedPointer<VirtualFile> &)> cb) {
}
//Message::Id qTbot::ITelegramBot::Bot::sendMessage(const QVariant &chatId, const QString &text)
//{
// if (chatId.type() != QVariant::String && chatId.type() != QVariant::Int && chatId.type() != QVariant::LongLong) {
@ -1236,6 +1267,19 @@ QSharedPointer<QNetworkReply> ITelegramBot::sendRequest(const QSharedPointer<iRe
return std::move(networkReplay);
}
QString ITelegramBot::findFileInlocatStorage(const QString &fileId) const {
QDir defaultFileDir(defaultFileStorageLocation());
auto &&localStorageList = defaultFileDir.entryInfoList(QDir::Filter::NoDotAndDotDot | QDir::Files);
for (const auto& file: localStorageList) {
if (file.fileName().contains(fileId)) {
return file .absoluteFilePath();
}
}
return "";
}
void ITelegramBot::setUsername(const QString &newUsername) {
_username = newUsername;
}

View File

@ -78,6 +78,9 @@ public:
bool disableWebPagePreview = false,
const Responce& cb = {});
QSharedPointer<iFile> getFile(const QString& fileId, iFile::Type fileType = iFile::Type::Ram) override;
/**
* @brief Get a file by its ID.
*
@ -103,488 +106,48 @@ public:
bool getFile(const QString& fileId, std::function<void(const QSharedPointer<VirtualFile>&)> cb);
// /*
// **********************************************************************************************************************
// *
// to do
// * forwardMessage implementations
// *
// */
// Message::Id Bot::forwardMessage(const QVariant& chatId, const QVariant& fromChatId, Message::Id messageId);
// Message::Id Bot::forwardMessage(const ParameterList& params);
// /*
// **********************************************************************************************************************
// *
// * copyMessage implementations
// *
// */
// Message::Id Bot::copyMessage(const QVariant& chatId, const QVariant& fromChatId, Message::Id messageId);
// Message::Id Bot::copyMessage(const ParameterList& params);
// /*
// **********************************************************************************************************************
// *
// * sendPhoto implementations
// *
// */
// Message::Id Bot::sendPhoto(const QVariant& chatId, QFile *file, const QString& caption, Message::Id replyToMessageId);
// Message::Id Bot::sendPhoto(const QVariant& chatId, const QString& fileId, const QString& caption, Message::Id replyToMessageId);
// Message::Id Bot::sendPhoto(const QVariant& chatId, const QByteArray& fileData, const QString& caption, Message::Id replyToMessageId);
// Message::Id Bot::sendPhoto(const ParameterList& params);
// /*
// **********************************************************************************************************************
// *
// * sendAudio implementations
// *
// */
// Message::Id Bot::sendAudio(const QVariant& chatId, QFile *file, qint64 duration, const QString& performer, const QString& title, Message::Id replyToMessageId);
// Message::Id Bot::sendAudio(const QVariant& chatId, const QString& fileId, qint64 duration, const QString& performer, const QString& title, Message::Id replyToMessageId);
// Message::Id Bot::sendAudio(const ParameterList& params);
// /*
// **********************************************************************************************************************
// *
// * sendDocument implementations
// *
// */
// Message::Id Bot::sendDocument(const QVariant& chatId, QFile *file, Message::Id replyToMessageId);
// Message::Id Bot::sendDocument(const QVariant& chatId, const QByteArray& fileData, Message::Id replyToMessageId);
// Message::Id Bot::sendDocument(const QVariant& chatId, const QString& fileId, Message::Id replyToMessageId);
// Message::Id Bot::sendDocument(const ParameterList& params);
// /*
// **********************************************************************************************************************
// *
// * sendVideo implementations
// *
// */
// Message::Id Bot::sendVideo(const QVariant& chatId, QFile *file, qint64 duration, const QString& caption, Message::Id replyToMessageId);
// Message::Id Bot::sendVideo(const QVariant& chatId, const QString& fileId, qint64 duration, const QString& caption, Message::Id replyToMessageId);
// Message::Id Bot::sendVideo(const ParameterList& params);
// /*
// **********************************************************************************************************************
// *
// * sendVoice implementations
// *
// */
// Message::Id Bot::sendVoice(const QVariant& chatId, QFile *file, qint64 duration, Message::Id replyToMessageId);
// Message::Id Bot::sendVoice(const QVariant& chatId, const QString& fileId, qint64 duration, Message::Id replyToMessageId);
// Message::Id Bot::sendVoice(const ParameterList& params);
// /*
// **********************************************************************************************************************
// *
// * sendLocation implementations
// *
// */
// Message::Id Bot::sendLocation(const QVariant& chatId, float latitude, float longitude, Message::Id replyToMessageId);
// Message::Id Bot::sendLocation(const ParameterList& params);
// /*
// **********************************************************************************************************************
// *
// * sendContact implementations
// *
// */
// Message::Id Bot::sendContact(const QVariant& chatId, const QString& phoneNumber, const QString& firstName);
// Message::Id Bot::sendContact(const ParameterList& params);
// /*
// **********************************************************************************************************************
// *
// * sendPoll implementations
// *
// */
// Message::Id Bot::sendPoll(const QVariant& chatId, const QString& question, const QStringList& options);
// Message::Id Bot::sendPoll(const QVariant& chatId, const QString& question, const QStringList& options, bool isAnonymous);
// Message::Id Bot::sendPoll(const ParameterList& params);
// /*
// **********************************************************************************************************************
// *
// * sendDice implementations
// *
// */
// Message::Id Bot::sendDice(const QVariant& chatId, Message::Id replyToMessageId);
// Message::Id Bot::sendDice(const ParameterList& params);
// /*
// **********************************************************************************************************************
// *
// * sendChatAction implementations
// *
// */
// bool Bot::sendChatAction(const QVariant& chatId, Bot::ChatAction action);
// bool Bot::sendChatAction(const ParameterList& params);
// /*
// **********************************************************************************************************************
// *
// * getUserProfilePhotos implementations
// *
// */
// UserProfilePhotos Bot::getUserProfilePhotos(User::Id userId, qint16 offset, qint8 limit);
// UserProfilePhotos Bot::getUserProfilePhotos(ParameterList& params);
// /*
// **********************************************************************************************************************
// **********************************************************************************************************************
// *
// * banChatMember implementations
// *
// */
// bool Bot::banChatMember(const QVariant& chatId, User::Id userId);
// bool Bot::banChatMember(const ParameterList& params);
// /*
// **********************************************************************************************************************
// *
// * unbanChatMember implementations
// *
// */
// bool Bot::unbanChatMember(const QVariant& chatId, User::Id userId);
// bool Bot::unbanChatMember(const ParameterList& params);
// /*
// **********************************************************************************************************************
// *
// * restrictChatMember implementations
// *
// */
// bool Bot::restrictChatMember(const QVariant& chatId, const QVariant& userId, qint32 untilDate, bool canSendMessages, bool can_send_media_messages,bool can_send_other_messages,bool can_add_web_page_previews);
// bool Bot::restrictChatMember(const ParameterList& params);
// /*
// **********************************************************************************************************************
// *
// * setChatAdministratorCustomTitle implementations
// *
// */
// bool Bot::setChatAdministratorCustomTitle(const QVariant& chatId, User::Id userId,const QString& customTitle);
// bool Bot::setChatAdministratorCustomTitle(const ParameterList& params);
// /*
// **********************************************************************************************************************
// *
// * banChatSenderChat implementations
// *
// */
// bool Bot::banChatSenderChat(const QVariant& chatId,const QVariant& senderChatId);
// bool Bot::banChatSenderChat(const ParameterList& params);
// /*
// **********************************************************************************************************************
// *
// * unbanChatSenderChat implementations
// *
// */
// bool Bot::unbanChatSenderChat(const QVariant& chatId,const QVariant& senderChatId);
// bool Bot::unbanChatSenderChat(const ParameterList& params);
// /*
// **********************************************************************************************************************
// *
// * exportChatInviteLink implementations
// *
// */
// QString Bot::exportChatInviteLink(const QVariant& chatId);
// QString Bot::exportChatInviteLink(const ParameterList& params);
// /*
// **********************************************************************************************************************
// *
// * approveChatJoinRequest implementations
// *
// */
// bool Bot::approveChatJoinRequest(const QVariant& chatId, User::Id userId);
// bool Bot::approveChatJoinRequest(const ParameterList& params);
// /*
// **********************************************************************************************************************
// *
// * declineChatJoinRequest implementations
// *
// */
// bool Bot::declineChatJoinRequest(const QVariant& chatId, User::Id userId);
// bool Bot::declineChatJoinRequest(const ParameterList& params);
// /*
// **********************************************************************************************************************
// *
// * setChatTitle implementations
// *
// */
// bool Bot::setChatTitle(const QVariant& chatId, const QString& title);
// bool Bot::setChatTitle(const ParameterList& params);
// /*
// **********************************************************************************************************************
// *
// * pinChatMessage implementations
// *
// */
// bool Bot::pinChatMessage(const QVariant& chatId,Message::Id messageId);
// bool Bot::pinChatMessage(const ParameterList& params);
// /*
// **********************************************************************************************************************
// *
// * unpinChatMessage implementations
// *
// */
// bool Bot::unpinChatMessage(const QVariant& chatId,Message::Id messageId);
// bool Bot::unpinChatMessage(const ParameterList& params);
// /*
// **********************************************************************************************************************
// *
// * unpinAllChatMessages implementations
// *
// */
// bool Bot::unpinAllChatMessages(const QVariant& chatId);
// bool Bot::unpinAllChatMessages(const ParameterList& params);
// /*
// **********************************************************************************************************************
// *
// * leaveChat implementations
// *
// */
// bool Bot::leaveChat(const QVariant& chatId);
// bool Bot::leaveChat(const ParameterList& params);
// /*
// **********************************************************************************************************************
// *
// * getChat implementations
// *
// */
// Chat Bot::getChat(const QVariant& chatId);
// Chat Bot::getChat(const ParameterList& params);
// /*
// **********************************************************************************************************************
// *
// * getChatAdministrators implementations
// *
// */
// QList<ChatMember> Bot::getChatAdministrators(const QVariant& chatId);
// QList<ChatMember> Bot::getChatAdministrators(const ParameterList& params);
// /*
// **********************************************************************************************************************
// *
// * getChatMemberCount implementations
// *
// */
// int Bot::getChatMemberCount(const QVariant& chatId);
// int Bot::getChatMemberCount(const ParameterList& params);
// /*
// **********************************************************************************************************************
// *
// * getChatMember implementations
// *
// */
// ChatMember Bot::getChatMember(const QVariant& chatId,User::Id userId);
// ChatMember Bot::getChatMember(const ParameterList& params);
// /*
// **********************************************************************************************************************
// *
// * answerCallbackQuery implementations
// *
// */
// bool Bot::answerCallbackQuery(const QString& callbackQueryId,const QString& text,bool showAlert, const QString& url,qint32 cahceTime);
// bool Bot::answerCallbackQuery(const ParameterList& params);
// /*
// **********************************************************************************************************************
// *
// * getMyCommands implementations
// *
// */
// bool Bot::setMyCommands(const BotCommandList& commands);
// bool Bot::setMyCommands(const BotCommandList& commands,const GenericScope& commandScope);
// bool Bot::setMyCommands(const ParameterList& params);
// /*
// **********************************************************************************************************************
// *
// * deleteMyCommands implementations
// *
// */
// bool Bot::deleteMyCommands();
// bool Bot::deleteMyCommands(const GenericScope& commandScope);
// bool Bot::deleteMyCommands(const ParameterList& params);
// /*
// **********************************************************************************************************************
// *
// * getMyCommands implementations
// *
// */
// BotCommandList Bot::getMyCommands();
// BotCommandList Bot::getMyCommands(const GenericScope& commandScope);
// BotCommandList Bot::getMyCommands(const ParameterList& params);
// /*
// **********************************************************************************************************************
// *
// * editMessageText implementations
// *
// */
// bool Bot::editMessageText(const QVariant& chatId,Message::Id messageId,const QString& text);
// bool Bot::editMessageText(const QVariant& chatId,Message::Id messageId,const QString& text, const GenericReply &replyMarkup);
// bool Bot::editMessageText(const ParameterList& params);
// /*
// **********************************************************************************************************************
// *
// * editMessageReplyMarkup implementations
// *
// */
// bool Bot::editMessageReplyMarkup(const QVariant& chatId, Message::Id messageId, const GenericReply& replyMarkup);
// bool Bot::editMessageReplyMarkup(const QString& inlineMessageId, const GenericReply& replyMarkup);
// bool Bot::editMessageReplyMarkup(const ParameterList& params);
// /*
// **********************************************************************************************************************
// *
// * stopPoll implementations
// *
// */
// bool Bot::stopPoll(const QVariant& chatId, Message::Id messageId);
// bool Bot::stopPoll(const QVariant& chatId,int messageId,const GenericReply& replyMarkup);
// bool Bot::stopPoll(const ParameterList& params);
// /*
// **********************************************************************************************************************
// *
// * deleteMessage implementations
// *
// */
// bool Bot::deleteMessage(const QVariant& chatId,Message::Id messageId);
// bool Bot::deleteMessage(const ParameterList& params);
// /*
// **********************************************************************************************************************
// *
// * sendSticker implementations
// *
// */
// Message::Id Bot::sendSticker(const QVariant& chatId, const QString& sticker);
// Message::Id Bot::sendSticker(const ParameterList& params);
// /*
// **********************************************************************************************************************
// *
// * Internal methods
// *
// */
// QJsonObject Bot::sendFilePayload(ParameterList params, QFile *filePayload, const QString& payloadType, const QString& endpoint);
// QJsonObject Bot::sendFilePayload(ParameterList params, const QByteArray& fileData, const QString& payloadType, const QString& endpoint);
/**
@ -622,6 +185,8 @@ protected:
QSharedPointer<QNetworkReply> sendRequest(const QSharedPointer<iRequest>& rquest) override;
private:
QString findFileInlocatStorage(const QString& fileId) const;
unsigned long long _id = 0;
QString _username;

View File

@ -0,0 +1,18 @@
//#
//# Copyright (C) 2023-2023 QuasarApp.
//# Distributed under the GPLv3 software license, see the accompanying
//# Everyone is permitted to copy and distribute verbatim copies
//# of this license document, but changing it is not allowed.
//#
#include "telegramgetfile.h"
namespace qTbot {
TelegramGetFile::TelegramGetFile(const QString &fileId):
TelegramSingleRquest("getFile", {{"file_id", fileId}}) {
}
}

View File

@ -0,0 +1,26 @@
//#
//# Copyright (C) 2023-2023 QuasarApp.
//# Distributed under the GPLv3 software license, see the accompanying
//# Everyone is permitted to copy and distribute verbatim copies
//# of this license document, but changing it is not allowed.
//#
#ifndef TELEGRAMGETFILE_H
#define TELEGRAMGETFILE_H
#include "telegramsinglerquest.h"
namespace qTbot {
/**
* @brief The TelegramGetFile class build http request for the getting files from tellgram messager.
*/
class QTBOT_EXPORT TelegramGetFile final: public TelegramSingleRquest
{
public:
TelegramGetFile(const QString& fileId);
};
}
#endif // TELEGRAMGETFILE_H

View File

@ -19,6 +19,10 @@ const QWeakPointer<QByteArray>& VirtualFile::array() const {
return _array;
}
iFile::Type VirtualFile::type() const {
return Type::Ram;
}
void VirtualFile::handleReadReady() {
if (auto&& strongArray = _array.lock()) {

View File

@ -23,6 +23,7 @@ public:
// iFile interface
const QWeakPointer<QByteArray>& array() const;
Type type() const override;
protected slots:
void handleReadReady() override;