mirror of
https://gitee.com/vnotex/vnote.git
synced 2025-07-05 05:49:53 +08:00
MarkdownEditor: guess image suffix when fetching images to local
This commit is contained in:
parent
f56fe42c6c
commit
58a7745b0e
18
src/utils/imageutils.cpp
Normal file
18
src/utils/imageutils.cpp
Normal file
@ -0,0 +1,18 @@
|
||||
#include "imageutils.h"
|
||||
|
||||
#include <QMimeDatabase>
|
||||
|
||||
using namespace vnotex;
|
||||
|
||||
QImage::Format ImageUtils::guessImageFormat(const QByteArray &p_data)
|
||||
{
|
||||
auto image = QImage::fromData(p_data);
|
||||
return image.format();
|
||||
}
|
||||
|
||||
QString ImageUtils::guessImageSuffix(const QByteArray &p_data)
|
||||
{
|
||||
QMimeDatabase mimeDb;
|
||||
auto mimeType = mimeDb.mimeTypeForData(p_data);
|
||||
return mimeType.preferredSuffix();
|
||||
}
|
19
src/utils/imageutils.h
Normal file
19
src/utils/imageutils.h
Normal file
@ -0,0 +1,19 @@
|
||||
#ifndef IMAGEUTILS_H
|
||||
#define IMAGEUTILS_H
|
||||
|
||||
#include <QImage>
|
||||
|
||||
namespace vnotex
|
||||
{
|
||||
class ImageUtils
|
||||
{
|
||||
public:
|
||||
ImageUtils() = delete;
|
||||
|
||||
static QImage::Format guessImageFormat(const QByteArray &p_data);
|
||||
|
||||
static QString guessImageSuffix(const QByteArray &p_data);
|
||||
};
|
||||
}
|
||||
|
||||
#endif // IMAGEUTILS_H
|
@ -4,6 +4,7 @@ SOURCES += \
|
||||
$$PWD/contentmediautils.cpp \
|
||||
$$PWD/docsutils.cpp \
|
||||
$$PWD/htmlutils.cpp \
|
||||
$$PWD/imageutils.cpp \
|
||||
$$PWD/pathutils.cpp \
|
||||
$$PWD/processutils.cpp \
|
||||
$$PWD/textutils.cpp \
|
||||
@ -19,6 +20,7 @@ HEADERS += \
|
||||
$$PWD/contentmediautils.h \
|
||||
$$PWD/docsutils.h \
|
||||
$$PWD/htmlutils.h \
|
||||
$$PWD/imageutils.h \
|
||||
$$PWD/pathutils.h \
|
||||
$$PWD/processutils.h \
|
||||
$$PWD/textutils.h \
|
||||
|
@ -37,6 +37,7 @@
|
||||
#include <utils/widgetutils.h>
|
||||
#include <utils/textutils.h>
|
||||
#include <utils/webutils.h>
|
||||
#include <utils/imageutils.h>
|
||||
#include <core/exception.h>
|
||||
#include <core/markdowneditorconfig.h>
|
||||
#include <core/texteditorconfig.h>
|
||||
@ -1092,7 +1093,11 @@ void MarkdownEditor::fetchImagesToLocalAndReplace(QString &p_text)
|
||||
// Network path.
|
||||
QByteArray data = vte::Downloader::download(QUrl(imageUrl));
|
||||
if (!data.isEmpty()) {
|
||||
tmpFile.reset(FileUtils::createTemporaryFile(info.suffix()));
|
||||
auto suffix = info.suffix();
|
||||
if (suffix.isEmpty()) {
|
||||
suffix = ImageUtils::guessImageSuffix(data);
|
||||
}
|
||||
tmpFile.reset(FileUtils::createTemporaryFile(suffix));
|
||||
if (tmpFile->open() && tmpFile->write(data) > -1) {
|
||||
srcImagePath = tmpFile->fileName();
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user