mirror of
				https://gitlab.com/comunic/comunicmessages
				synced 2025-11-04 12:14:05 +00:00 
			
		
		
		
	Can send image to conversations
This commit is contained in:
		@@ -1,4 +1,5 @@
 | 
			
		||||
#include <QNetworkReply>
 | 
			
		||||
#include <QFile>
 | 
			
		||||
 | 
			
		||||
#include "apirequest.h"
 | 
			
		||||
 | 
			
		||||
@@ -38,6 +39,31 @@ void APIRequest::addBool(QString name, bool value)
 | 
			
		||||
    mArguments.append(APIRequestParameter(name, value ? "true" : "false"));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void APIRequest::addFileFromPath(const QString &name, const QString &path, const QString &fileType)
 | 
			
		||||
{
 | 
			
		||||
    //Determine files name for the request
 | 
			
		||||
    QString partName = name;
 | 
			
		||||
    partName.replace("\"", "\\\"");
 | 
			
		||||
    QString fileName = ("/"+path).split("/").last();
 | 
			
		||||
    fileName.replace("\"", "\\\"");
 | 
			
		||||
 | 
			
		||||
    QHttpPart part;
 | 
			
		||||
    part.setHeader(QNetworkRequest::ContentTypeHeader, fileType);
 | 
			
		||||
    part.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\""+partName+"\"; filename=\""+fileName+"\""));
 | 
			
		||||
 | 
			
		||||
    QFile *file = new QFile(path);
 | 
			
		||||
    if(!file->open(QIODevice::ReadOnly)){
 | 
			
		||||
        qWarning("Could not open file to send: %s !", path.toStdString().c_str());
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    part.setBodyDevice(file);
 | 
			
		||||
    mParts.append(part);
 | 
			
		||||
 | 
			
		||||
    //Automatically delete file object once request is completed
 | 
			
		||||
    file->setParent(this);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
QList<APIRequestParameter> APIRequest::arguments() const
 | 
			
		||||
{
 | 
			
		||||
    return mArguments;
 | 
			
		||||
@@ -52,3 +78,13 @@ void APIRequest::setNetworkReply(QNetworkReply *networkReply)
 | 
			
		||||
{
 | 
			
		||||
    mNetworkReply = networkReply;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
QList<QHttpPart> *APIRequest::parts()
 | 
			
		||||
{
 | 
			
		||||
    return &mParts;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool APIRequest::hasParts() const
 | 
			
		||||
{
 | 
			
		||||
    return mParts.size() > 0;
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -10,6 +10,7 @@
 | 
			
		||||
 | 
			
		||||
#include <QObject>
 | 
			
		||||
#include <QJsonDocument>
 | 
			
		||||
#include <QHttpPart>
 | 
			
		||||
 | 
			
		||||
#include "apirequestparameter.h"
 | 
			
		||||
 | 
			
		||||
@@ -50,6 +51,15 @@ public:
 | 
			
		||||
     */
 | 
			
		||||
    void addBool(QString name, bool value);
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Add a file from filesystem path
 | 
			
		||||
     *
 | 
			
		||||
     * @param name The name of the file to add
 | 
			
		||||
     * @param path The path to the file
 | 
			
		||||
     * @param fileType The type of the file to add
 | 
			
		||||
     */
 | 
			
		||||
    void addFileFromPath(const QString &name, const QString &path, const QString &fileType);
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Get the entire list of arguments of the request
 | 
			
		||||
     *
 | 
			
		||||
@@ -57,6 +67,14 @@ public:
 | 
			
		||||
     */
 | 
			
		||||
    QList<APIRequestParameter> arguments() const;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Get the list of HTTP parts included with this request
 | 
			
		||||
     *
 | 
			
		||||
     * @return Pointer on the list containing the list of parts
 | 
			
		||||
     */
 | 
			
		||||
    QList<QHttpPart> *parts();
 | 
			
		||||
    bool hasParts() const;
 | 
			
		||||
 | 
			
		||||
    //Get and set network reply associated with the request
 | 
			
		||||
    QNetworkReply *networkReply() const;
 | 
			
		||||
    void setNetworkReply(QNetworkReply *networkReply);
 | 
			
		||||
@@ -91,6 +109,7 @@ public slots:
 | 
			
		||||
private:
 | 
			
		||||
    QString mURI;
 | 
			
		||||
    QList<APIRequestParameter> mArguments;
 | 
			
		||||
    QList<QHttpPart> mParts;
 | 
			
		||||
    QNetworkReply *mNetworkReply = nullptr;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -24,3 +24,18 @@ void NewConversationMessage::setMessage(const QString &message)
 | 
			
		||||
{
 | 
			
		||||
    mMessage = message;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
QString NewConversationMessage::imagePath() const
 | 
			
		||||
{
 | 
			
		||||
    return mImagePath;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool NewConversationMessage::hasImage() const
 | 
			
		||||
{
 | 
			
		||||
    return !mImagePath.isEmpty();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void NewConversationMessage::setImagePath(const QString &imagePath)
 | 
			
		||||
{
 | 
			
		||||
    mImagePath = imagePath;
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -22,9 +22,14 @@ public:
 | 
			
		||||
    QString message() const;
 | 
			
		||||
    void setMessage(const QString &message);
 | 
			
		||||
 | 
			
		||||
    QString imagePath() const;
 | 
			
		||||
    bool hasImage() const;
 | 
			
		||||
    void setImagePath(const QString &imagePath);
 | 
			
		||||
 | 
			
		||||
private:
 | 
			
		||||
    int mIDConversation;
 | 
			
		||||
    QString mMessage;
 | 
			
		||||
    QString mImagePath;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#endif // NEWCONVERSATIONMESSAGE_H
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user