Messagelib

cidreferencesurlinterceptor.cpp
1 /*
2  SPDX-FileCopyrightText: 2016-2021 Laurent Montel <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "cidreferencesurlinterceptor.h"
8 #include "htmlwriter/webengineembedpart.h"
9 
10 #include <QBuffer>
11 #include <QImage>
12 #include <QUrl>
13 #include <QWebEngineUrlRequestInfo>
14 
15 using namespace MessageViewer;
16 #define LOAD_FROM_FILE 1
17 CidReferencesUrlInterceptor::CidReferencesUrlInterceptor(QObject *parent)
18  : WebEngineViewer::NetworkPluginUrlInterceptorInterface(parent)
19 {
20 }
21 
22 CidReferencesUrlInterceptor::~CidReferencesUrlInterceptor() = default;
23 
24 bool CidReferencesUrlInterceptor::interceptRequest(QWebEngineUrlRequestInfo &info)
25 {
26  const QUrl urlRequestUrl(info.requestUrl());
27  if (urlRequestUrl.scheme() == QLatin1String("cid")) {
28  if (info.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeImage) {
29  const QUrl newUrl = QUrl(MessageViewer::WebEngineEmbedPart::self()->contentUrl(urlRequestUrl.path()));
30  if (!newUrl.isEmpty()) {
31 #ifdef LOAD_FROM_FILE
32  QImage image(newUrl.path());
33  QByteArray ba;
34  QBuffer buf(&ba);
35  image.save(&buf, "png");
36  const QByteArray hexed = ba.toBase64();
37  buf.close();
38  const QUrl r = QUrl(QString::fromUtf8(QByteArray("data:image/png;base64,") + hexed));
39  info.redirect(r);
40 #else
41  info.redirect(newUrl);
42 #endif
43  }
44  }
45  }
46  return false;
47 }
bool isEmpty() const const
QString fromUtf8(const char *str, int size)
QString path(QUrl::ComponentFormattingOptions options) const const
QByteArray toBase64(QByteArray::Base64Options options) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sat Dec 4 2021 23:12:52 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.