Fix a mem leak and custom upl. mime handling error

This commit is contained in:
ArsenArsen 2017-06-13 01:15:57 +02:00
parent 22ab688a79
commit c6caa2c422
3 changed files with 35 additions and 63 deletions

View File

@ -15,72 +15,40 @@
#include <unistd.h>
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; };
return;
}
tmpDir = QDir(path);
tmpDir = QDir(tmp);
QString name
= QString("KShareTemp-") + QString::number(PlatformBackend::inst().pid()) + "-" + QTime::currentTime().toString();
tmpDir.mkdir(name);
tmpDir.cd(name);
switch (f) {
case formats::Recording::GIF: {
iFormat = QImage::Format_RGBA8888;
validator = [](QSize) { return true; };
consumer = [&](QImage img) { frames.push_back(img); };
finalizer = [&] {
if (frames.size() == 0) return QByteArray();
uint32_t d = 1000 / settings::settings().value("recording/framerate", 30).toInt();
QImage &startImg = frames[0];
GifWriter writer;
GifBegin(&writer, tmpDir.absoluteFilePath("resulting.gif").toLocal8Bit().constData(), startImg.width(),
startImg.height(), d);
for (QImage &a : frames) {
QByteArray alpha8((char *)a.bits(), a.byteCount());
GifWriteFrame(&writer, (uint8_t *)alpha8.data(), a.width(), a.height(), d);
iFormat = QImage::Format_RGB888;
path = tmpDir.absoluteFilePath("res." + formats::recordingFormatName(f).toLower());
finalizer = [&] {
delete enc;
QFile res(path);
if (!res.open(QFile::ReadOnly)) {
return QByteArray();
}
QByteArray data = res.readAll();
return data;
};
validator = [&](QSize s) {
if (!enc) {
enc = new Encoder(path, s);
if (!enc->isRunning()) {
delete enc;
return false;
}
GifEnd(&writer);
QFile res(tmpDir.absoluteFilePath("resulting.gif"));
if (!res.open(QFile::ReadOnly)) {
return QByteArray();
}
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;
}
}
return true;
};
consumer = [&](QImage img) { enc->addFrame(img); };
anotherFormat = formats::recordingFormatName(f);
}
RecordingFormats::~RecordingFormats() {

View File

@ -27,6 +27,7 @@ private:
std::vector<QImage> frames;
QImage::Format iFormat;
QDir tmpDir;
QString path;
Encoder *enc = NULL;
QString anotherFormat;
};

View File

@ -13,6 +13,8 @@
using formats::normalFormatFromName;
using formats::normalFormatMIME;
using formats::recordingFormatFromName;
using formats::recordingFormatMIME;
using std::runtime_error;
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!");
} else
notifications::notify("KShare Custom Uploader " + name, "Upload done, but result empty!");
QApplication::clipboard()->setText(data);
} else {
notifications::notify("KShare Custom Uploader " + name,
"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);
QByteArray data;
if (base64) imgData = imgData.toBase64();
QString mime = normalFormatMIME(normalFormatFromName(format));
if (mime.isEmpty()) mime = recordingFormatMIME(recordingFormatFromName(format));
switch (this->rFormat) {
case RequestFormat::PLAIN: {
data = imgData;
} break;
case RequestFormat::JSON: {
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++) {
data.append(split[i]);
if (i < split.size() - 1) data.append(imgData);
}
} else {
QJsonObject vo = body.toObject();
data = QJsonDocument::fromVariant(
recurseAndReplace(vo, imgData, normalFormatMIME(normalFormatFromName(format))).toVariantMap())
.toJson();
data = QJsonDocument::fromVariant(recurseAndReplace(vo, imgData, mime).toVariantMap()).toJson();
}
} break;
case RequestFormat::X_WWW_FORM_URLENCODED: {
@ -243,8 +247,7 @@ void CustomUploader::doUpload(QByteArray imgData, QString format) {
QByteArray strB;
if (str.startsWith("/") && str.endsWith("/")) {
str = str.mid(1, str.length() - 2);
QStringList split
= str.replace("%contenttype", normalFormatMIME(normalFormatFromName(format))).split("%imagedata");
QStringList split = str.replace("%contenttype", mime).split("%imagedata");
for (int i = 0; i < split.size(); i++) {
strB.append(split[i]);
if (i < split.size() - 1) strB.append(imgData);