Fix a mem leak and custom upl. mime handling error
This commit is contained in:
parent
22ab688a79
commit
c6caa2c422
@ -15,72 +15,40 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
RecordingFormats::RecordingFormats(formats::Recording f) {
|
RecordingFormats::RecordingFormats(formats::Recording f) {
|
||||||
QString path = QStandardPaths::writableLocation(QStandardPaths::TempLocation);
|
QString tmp = QStandardPaths::writableLocation(QStandardPaths::TempLocation);
|
||||||
|
|
||||||
if (path.isEmpty()) {
|
if (tmp.isEmpty()) {
|
||||||
validator = [](QSize) { return false; };
|
validator = [](QSize) { return false; };
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
tmpDir = QDir(path);
|
tmpDir = QDir(tmp);
|
||||||
QString name
|
QString name
|
||||||
= QString("KShareTemp-") + QString::number(PlatformBackend::inst().pid()) + "-" + QTime::currentTime().toString();
|
= QString("KShareTemp-") + QString::number(PlatformBackend::inst().pid()) + "-" + QTime::currentTime().toString();
|
||||||
tmpDir.mkdir(name);
|
tmpDir.mkdir(name);
|
||||||
tmpDir.cd(name);
|
tmpDir.cd(name);
|
||||||
switch (f) {
|
iFormat = QImage::Format_RGB888;
|
||||||
case formats::Recording::GIF: {
|
path = tmpDir.absoluteFilePath("res." + formats::recordingFormatName(f).toLower());
|
||||||
iFormat = QImage::Format_RGBA8888;
|
finalizer = [&] {
|
||||||
validator = [](QSize) { return true; };
|
delete enc;
|
||||||
consumer = [&](QImage img) { frames.push_back(img); };
|
QFile res(path);
|
||||||
finalizer = [&] {
|
if (!res.open(QFile::ReadOnly)) {
|
||||||
if (frames.size() == 0) return QByteArray();
|
return QByteArray();
|
||||||
uint32_t d = 1000 / settings::settings().value("recording/framerate", 30).toInt();
|
}
|
||||||
QImage &startImg = frames[0];
|
QByteArray data = res.readAll();
|
||||||
GifWriter writer;
|
return data;
|
||||||
GifBegin(&writer, tmpDir.absoluteFilePath("resulting.gif").toLocal8Bit().constData(), startImg.width(),
|
};
|
||||||
startImg.height(), d);
|
validator = [&](QSize s) {
|
||||||
for (QImage &a : frames) {
|
if (!enc) {
|
||||||
QByteArray alpha8((char *)a.bits(), a.byteCount());
|
enc = new Encoder(path, s);
|
||||||
GifWriteFrame(&writer, (uint8_t *)alpha8.data(), a.width(), a.height(), d);
|
if (!enc->isRunning()) {
|
||||||
|
delete enc;
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
GifEnd(&writer);
|
}
|
||||||
QFile res(tmpDir.absoluteFilePath("resulting.gif"));
|
return true;
|
||||||
if (!res.open(QFile::ReadOnly)) {
|
};
|
||||||
return QByteArray();
|
consumer = [&](QImage img) { enc->addFrame(img); };
|
||||||
}
|
anotherFormat = formats::recordingFormatName(f);
|
||||||
QByteArray data = res.readAll();
|
|
||||||
return data;
|
|
||||||
};
|
|
||||||
anotherFormat = formats::recordingFormatName(f);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case formats::Recording::WebM: {
|
|
||||||
iFormat = QImage::Format_RGB888;
|
|
||||||
finalizer = [&] {
|
|
||||||
delete enc;
|
|
||||||
QFile res(tmpDir.absoluteFilePath("res.webm"));
|
|
||||||
if (!res.open(QFile::ReadOnly)) {
|
|
||||||
return QByteArray();
|
|
||||||
}
|
|
||||||
QByteArray data = res.readAll();
|
|
||||||
return data;
|
|
||||||
};
|
|
||||||
validator = [&](QSize s) {
|
|
||||||
if (!enc) {
|
|
||||||
QString path = tmpDir.absoluteFilePath("res.webm");
|
|
||||||
enc = new Encoder(path, s);
|
|
||||||
if (!enc->isRunning()) {
|
|
||||||
delete enc;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
};
|
|
||||||
consumer = [&](QImage img) { enc->addFrame(img); };
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
RecordingFormats::~RecordingFormats() {
|
RecordingFormats::~RecordingFormats() {
|
||||||
|
@ -27,6 +27,7 @@ private:
|
|||||||
std::vector<QImage> frames;
|
std::vector<QImage> frames;
|
||||||
QImage::Format iFormat;
|
QImage::Format iFormat;
|
||||||
QDir tmpDir;
|
QDir tmpDir;
|
||||||
|
QString path;
|
||||||
Encoder *enc = NULL;
|
Encoder *enc = NULL;
|
||||||
QString anotherFormat;
|
QString anotherFormat;
|
||||||
};
|
};
|
||||||
|
@ -13,6 +13,8 @@
|
|||||||
|
|
||||||
using formats::normalFormatFromName;
|
using formats::normalFormatFromName;
|
||||||
using formats::normalFormatMIME;
|
using formats::normalFormatMIME;
|
||||||
|
using formats::recordingFormatFromName;
|
||||||
|
using formats::recordingFormatMIME;
|
||||||
using std::runtime_error;
|
using std::runtime_error;
|
||||||
|
|
||||||
void error(QString absFilePath, QString err) {
|
void error(QString absFilePath, QString err) {
|
||||||
@ -205,6 +207,7 @@ void parseResult(QJsonDocument result, QByteArray data, QString returnPathspec,
|
|||||||
notifications::notify("KShare Custom Uploader " + name, "Copied upload link to clipboard!");
|
notifications::notify("KShare Custom Uploader " + name, "Copied upload link to clipboard!");
|
||||||
} else
|
} else
|
||||||
notifications::notify("KShare Custom Uploader " + name, "Upload done, but result empty!");
|
notifications::notify("KShare Custom Uploader " + name, "Upload done, but result empty!");
|
||||||
|
QApplication::clipboard()->setText(data);
|
||||||
} else {
|
} else {
|
||||||
notifications::notify("KShare Custom Uploader " + name,
|
notifications::notify("KShare Custom Uploader " + name,
|
||||||
"Upload done, but result is not JSON Object! Result in clipboard.");
|
"Upload done, but result is not JSON Object! Result in clipboard.");
|
||||||
@ -216,22 +219,23 @@ void CustomUploader::doUpload(QByteArray imgData, QString format) {
|
|||||||
auto h = getHeaders(headers, format, this->rFormat);
|
auto h = getHeaders(headers, format, this->rFormat);
|
||||||
QByteArray data;
|
QByteArray data;
|
||||||
if (base64) imgData = imgData.toBase64();
|
if (base64) imgData = imgData.toBase64();
|
||||||
|
QString mime = normalFormatMIME(normalFormatFromName(format));
|
||||||
|
if (mime.isEmpty()) mime = recordingFormatMIME(recordingFormatFromName(format));
|
||||||
|
|
||||||
switch (this->rFormat) {
|
switch (this->rFormat) {
|
||||||
case RequestFormat::PLAIN: {
|
case RequestFormat::PLAIN: {
|
||||||
data = imgData;
|
data = imgData;
|
||||||
} break;
|
} break;
|
||||||
case RequestFormat::JSON: {
|
case RequestFormat::JSON: {
|
||||||
if (body.isString()) {
|
if (body.isString()) {
|
||||||
QStringList split = body.toString().replace("%contenttype", normalFormatMIME(normalFormatFromName(format))).split("%imagedata");
|
QStringList split = body.toString().replace("%contenttype", mime).split("%imagedata");
|
||||||
for (int i = 0; i < split.size(); i++) {
|
for (int i = 0; i < split.size(); i++) {
|
||||||
data.append(split[i]);
|
data.append(split[i]);
|
||||||
if (i < split.size() - 1) data.append(imgData);
|
if (i < split.size() - 1) data.append(imgData);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
QJsonObject vo = body.toObject();
|
QJsonObject vo = body.toObject();
|
||||||
data = QJsonDocument::fromVariant(
|
data = QJsonDocument::fromVariant(recurseAndReplace(vo, imgData, mime).toVariantMap()).toJson();
|
||||||
recurseAndReplace(vo, imgData, normalFormatMIME(normalFormatFromName(format))).toVariantMap())
|
|
||||||
.toJson();
|
|
||||||
}
|
}
|
||||||
} break;
|
} break;
|
||||||
case RequestFormat::X_WWW_FORM_URLENCODED: {
|
case RequestFormat::X_WWW_FORM_URLENCODED: {
|
||||||
@ -243,8 +247,7 @@ void CustomUploader::doUpload(QByteArray imgData, QString format) {
|
|||||||
QByteArray strB;
|
QByteArray strB;
|
||||||
if (str.startsWith("/") && str.endsWith("/")) {
|
if (str.startsWith("/") && str.endsWith("/")) {
|
||||||
str = str.mid(1, str.length() - 2);
|
str = str.mid(1, str.length() - 2);
|
||||||
QStringList split
|
QStringList split = str.replace("%contenttype", mime).split("%imagedata");
|
||||||
= str.replace("%contenttype", normalFormatMIME(normalFormatFromName(format))).split("%imagedata");
|
|
||||||
for (int i = 0; i < split.size(); i++) {
|
for (int i = 0; i < split.size(); i++) {
|
||||||
strB.append(split[i]);
|
strB.append(split[i]);
|
||||||
if (i < split.size() - 1) strB.append(imgData);
|
if (i < split.size() - 1) strB.append(imgData);
|
||||||
|
Loading…
Reference in New Issue
Block a user