diff options
author | Kitsune Ral <Kitsune-Ral@users.sf.net> | 2018-12-26 19:34:15 +0900 |
---|---|---|
committer | Kitsune Ral <Kitsune-Ral@users.sf.net> | 2019-01-05 21:01:24 +0900 |
commit | 3b88c2b537b6cb98dcd0f2066d39e426b5cc52da (patch) | |
tree | fddb6d657a9fff3263f07e9cd3ca471956d791a9 | |
parent | 241f7165816624da45fca58578885b17589ec1f7 (diff) | |
download | libquotient-3b88c2b537b6cb98dcd0f2066d39e426b5cc52da.tar.gz libquotient-3b88c2b537b6cb98dcd0f2066d39e426b5cc52da.zip |
Connection::upload*: autodetect content type if not supplied
-rw-r--r-- | lib/connection.cpp | 15 | ||||
-rw-r--r-- | lib/connection.h | 6 |
2 files changed, 15 insertions, 6 deletions
diff --git a/lib/connection.cpp b/lib/connection.cpp index a16bc753..c17cbffc 100644 --- a/lib/connection.cpp +++ b/lib/connection.cpp @@ -43,6 +43,7 @@ #include <QtCore/QStringBuilder> #include <QtCore/QElapsedTimer> #include <QtCore/QRegularExpression> +#include <QtCore/QMimeDatabase> #include <QtCore/QCoreApplication> using namespace QMatrixClient; @@ -466,13 +467,21 @@ MediaThumbnailJob* Connection::getThumbnail(const QUrl& url, } UploadContentJob* Connection::uploadContent(QIODevice* contentSource, - const QString& filename, const QString& contentType) const + const QString& filename, const QString& overrideContentType) const { + auto contentType = overrideContentType; + if (contentType.isEmpty()) + { + contentType = + QMimeDatabase().mimeTypeForFileNameAndData(filename, contentSource) + .name(); + contentSource->open(QIODevice::ReadOnly); + } return callApi<UploadContentJob>(contentSource, filename, contentType); } UploadContentJob* Connection::uploadFile(const QString& fileName, - const QString& contentType) + const QString& overrideContentType) { auto sourceFile = new QFile(fileName); if (!sourceFile->open(QIODevice::ReadOnly)) @@ -482,7 +491,7 @@ UploadContentJob* Connection::uploadFile(const QString& fileName, return nullptr; } return uploadContent(sourceFile, QFileInfo(*sourceFile).fileName(), - contentType); + overrideContentType); } GetContentJob* Connection::getContent(const QString& mediaId) const diff --git a/lib/connection.h b/lib/connection.h index 9a94aad6..ff3e2028 100644 --- a/lib/connection.h +++ b/lib/connection.h @@ -402,10 +402,10 @@ namespace QMatrixClient // QIODevice* should already be open UploadContentJob* uploadContent(QIODevice* contentSource, - const QString& filename = {}, - const QString& contentType = {}) const; + const QString& filename = {}, + const QString& overrideContentType = {}) const; UploadContentJob* uploadFile(const QString& fileName, - const QString& contentType = {}); + const QString& overrideContentType = {}); GetContentJob* getContent(const QString& mediaId) const; GetContentJob* getContent(const QUrl& url) const; // If localFilename is empty, a temporary file will be created |