fix(DOS): fix the "image_resizer" to accept both paths and blobs

- accept both the blob (`data:image/jpeg;base64` payload) or a path/URL
to a local file
- remove the usage of QPixmap, QImage is enough and more suitable for
save/load and resizing as well
- remove usage of an extra file when saving
This commit is contained in:
Lukáš Tinkl 2023-06-07 17:56:09 +02:00 committed by Lukáš Tinkl
parent b83b8e7466
commit a01bb75b3f
2 changed files with 20 additions and 14 deletions

View File

@ -110,7 +110,7 @@ DOS_API char * DOS_CALL dos_escape_html(char* input);
DOS_API void DOS_CALL dos_qncm_delete(DosQNetworkConfigurationManager *vptr);
DOS_API char * DOS_CALL dos_image_resizer(char* imagePath, int maxSize, char* tmpDirPath);
DOS_API char * DOS_CALL dos_image_resizer(const char* imagePathOrData, int maxSize, const char* tmpDirPath);
DOS_API char * DOS_CALL dos_qurl_fromUserInput(char* input);

View File

@ -1444,23 +1444,29 @@ char *dos_escape_html(char* input)
return convert_to_cstring(QString(input).toHtmlEscaped().toUtf8());
}
char *dos_image_resizer(char* imagePath, int maxSize, char* tmpDirPath)
char *dos_image_resizer(const char* imagePathOrData, int maxSize, const char* tmpDirPath)
{
QImage img(imagePath);
img.setColorSpace(QColorSpace::SRgb);
int w = img.width();
int h = img.height();
const auto base64JPGPrefix = "data:image/jpeg;base64,";
QImage img;
bool loadResult = false;
QPixmap pixmap;
pixmap = pixmap.fromImage(img.scaled(maxSize < w ? maxSize : w, maxSize < h ? maxSize : h, Qt::KeepAspectRatio, Qt::SmoothTransformation));
// load the contents
if (qstrncmp(base64JPGPrefix, imagePathOrData, qstrlen(base64JPGPrefix)) == 0) { // binary BLOB
loadResult = img.loadFromData(QByteArray::fromBase64(QByteArray(imagePathOrData).mid(qstrlen(base64JPGPrefix)))); // strip the prefix, decode from b64
} else { // local file or URL
const auto localFileUrl = QUrl::fromUserInput(imagePathOrData).toLocalFile(); // accept both "file:/foo/bar" and "/foo/bar"
loadResult = img.load(localFileUrl);
}
auto newFilePath = tmpDirPath + QUuid::createUuid().toString(QUuid::WithoutBraces) + ".jpg";
QFile file(newFilePath);
file.open(QIODevice::WriteOnly);
pixmap.save(&file, "jpeg", 75);
file.close();
if (!loadResult) {
qWarning() << "dos_image_resizer: failed to (down)load image";
return nullptr;
}
// scale it
img = img.scaled(img.size().boundedTo(QSize(maxSize, maxSize)), Qt::KeepAspectRatio, Qt::SmoothTransformation);
const auto newFilePath = tmpDirPath + QUuid::createUuid().toString(QUuid::WithoutBraces) + ".jpg";
img.save(newFilePath, "JPG");
return convert_to_cstring(newFilePath.toUtf8());
}