From 3b88c2b537b6cb98dcd0f2066d39e426b5cc52da Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Wed, 26 Dec 2018 19:34:15 +0900 Subject: Connection::upload*: autodetect content type if not supplied --- lib/connection.cpp | 15 ++++++++++++--- 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 #include #include +#include #include 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(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 -- cgit v1.2.3