[Feature] modified image uploader to serve other mimetypes, added pickers

This commit is contained in:
Mark Washeim 2023-01-25 13:29:33 +01:00
parent 43ff5e5c78
commit 29a6086ad2
4 changed files with 100 additions and 15 deletions

View file

@ -4,3 +4,9 @@ X-Nemo-Application-Type=silica-qt5
Icon=harbour-tooterb
Exec=harbour-tooterb
Name=Tooter β
#[X-Sailjail]
#OrganizationName=de.poetaster
#ApplicationName=harbour-tooterb
#Permissions=Compatibility;Internet;Camera;Audio;MediaIndexing;RemovableMedia;UserDirs

View file

@ -1,5 +1,6 @@
import QtQuick 2.0
import QtQuick 2.6
import Sailfish.Silica 1.0
import Sailfish.Pickers 1.0
import harbour.tooterb.Uploader 1.0
import "../lib/API.js" as Logic
import "./components/"
@ -38,6 +39,17 @@ Page {
else status_link = status_uri
} else status_link = status_url
// This function is used by the upload Pickers
function fileUpload(file,mime) {
imageUploader.setUploadUrl(Logic.conf.instance + "/api/v1/media")
imageUploader.setFile(file)
imageUploader.setMime(mime)
imageUploader.setAuthorizationHeader(Logic.conf.api_user_token)
imageUploader.upload()
}
allowedOrientations: Orientation.All
onSuggestedUserChanged: {
//console.log(suggestedUser)
@ -375,7 +387,7 @@ Page {
IconButton {
id: btnAddImage
enabled: mediaModel.count < 4
icon.source: "image://theme/icon-s-attach?" + ( pressed ? Theme.highlightColor : (warningContent.visible ? Theme.secondaryHighlightColor : Theme.primaryColor) )
icon.source: "image://theme/icon-m-file-image?" + ( pressed ? Theme.highlightColor : (warningContent.visible ? Theme.secondaryHighlightColor : Theme.primaryColor) )
anchors {
top: toot.bottom
topMargin: -Theme.paddingSmall * 1.5
@ -385,20 +397,79 @@ Page {
onClicked: {
btnAddImage.enabled = false
var once = true
var imagePicker = pageStack.push("Sailfish.Pickers.ImagePickerPage", { "allowedOrientations": Orientation.All })
imagePicker.selectedContentChanged.connect(function () {
var imagePath = imagePicker.selectedContent
// console.log(imagePath)
imageUploader.setUploadUrl(Logic.conf.instance + "/api/v1/media")
imageUploader.setFile(imagePath)
imageUploader.setAuthorizationHeader(Logic.conf.api_user_token)
imageUploader.upload()
})
pageStack.push(imagePickerPage)
}
}
IconButton {
id: btnAddMusic
enabled: mediaModel.count < 4
icon.source: "image://theme/icon-m-file-audio?" + ( pressed ? Theme.highlightColor : (warningContent.visible ? Theme.secondaryHighlightColor : Theme.primaryColor) )
anchors {
top: toot.bottom
topMargin: -Theme.paddingSmall * 1.5
left: btnAddImage.right
leftMargin: Theme.paddingSmall
}
onClicked: {
btnAddMusic.enabled = false
var once = true
pageStack.push(musicPickerPage)
}
}
IconButton {
id: btnAddVideo
enabled: mediaModel.count < 4
icon.source: "image://theme/icon-m-file-video?" + ( pressed ? Theme.highlightColor : (warningContent.visible ? Theme.secondaryHighlightColor : Theme.primaryColor) )
anchors {
top: toot.bottom
topMargin: -Theme.paddingSmall * 1.5
left: btnAddMusic.right
leftMargin: Theme.paddingSmall
}
onClicked: {
btnAddVideo.enabled = false
var once = true
pageStack.push(videoPickerPage)
}
}
Component {
id: musicPickerPage
MusicPickerPage {
onSelectedContentPropertiesChanged: {
var imagePath = selectedContentProperties.url
var mimeType = selectedContentProperties.mimeType
fileUpload(imagePath,mimeType)
/*
imageUploader.setUploadUrl(Logic.conf.instance + "/api/v1/media")
imageUploader.setFile(imagePath)
imageUploader.setMime(mimeType)
imageUploader.setAuthorizationHeader(Logic.conf.api_user_token)
imageUploader.upload()
*/
}
}
}
Component {
id: imagePickerPage
ImagePickerPage {
onSelectedContentPropertiesChanged: {
var imagePath = selectedContentProperties.url
var mimeType = selectedContentProperties.mimeType
fileUpload(imagePath,mimeType)
}
}
}
Component {
id: videoPickerPage
VideoPickerPage {
onSelectedContentPropertiesChanged: {
var imagePath = selectedContentProperties.url
var mimeType = selectedContentProperties.mimeType
fileUpload(imagePath,mimeType)
}
}
}
ImageUploader {
id: imageUploader
onProgressChanged: {
@ -413,6 +484,8 @@ Page {
onFailure: {
uploadProgress.width = 0
btnAddImage.enabled = true
btnAddMusic.enabled = true
btnAddVideo.enabled = true
//console.log(status)
//console.log(statusText)
}
@ -437,7 +510,7 @@ Page {
anchors {
top: toot.bottom
topMargin: -Theme.paddingSmall * 1.5
left: btnAddImage.right
left: btnAddVideo.right
right: btnSend.left
}
}

View file

@ -25,6 +25,9 @@ ImageUploader::~ImageUploader() {
void ImageUploader::setFile(const QString &fileName) {
m_fileName = fileName;
}
void ImageUploader::setMime(const QString &mimeType) {
m_mimeType = mimeType;
}
void ImageUploader::setParameters(const QString &album, const QString &title, const QString &description) {
//if (!album.isEmpty()) {
@ -92,7 +95,8 @@ void ImageUploader::upload() {
}*/
//QByteArray fileData = file.readAll().toBase64();
imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/jpeg"));
//imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/jpeg"));
imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant(m_mimeType));
imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant(QString("form-data; name=\"file\"; filename=\"%1\"").arg(fileInfo.fileName()).toLatin1()));
imagePart.setBodyDevice(file);
file->setParent(multiPart);

View file

@ -16,6 +16,7 @@ public:
~ImageUploader();
Q_INVOKABLE void setFile(const QString &fileName);
Q_INVOKABLE void setMime(const QString &mimeType);
Q_INVOKABLE void setAuthorizationHeader(const QString &authorizationHeader);
Q_INVOKABLE void setUploadUrl(const QString &userAgent);
Q_INVOKABLE void setParameters(const QString &album, const QString &title, const QString &description);
@ -37,6 +38,7 @@ private:
QNetworkAccessManager *m_networkAccessManager;
QString m_fileName;
QString m_mimeType;
QByteArray m_authorizationHeader;
QString m_uploadUrl;
QByteArray postdata;