When image is saved EXIF metadata is removed

This commit is contained in:
Muki 2019-03-21 20:55:24 +01:00
parent c048b764f9
commit d70989f0e9
9 changed files with 98 additions and 91 deletions

View file

@ -28,6 +28,7 @@ Notification {
maxContentLines: 10
function show(bodyText, summaryText, clickedHandler) {
//console.log("show: " + bodyText + " " + summaryText)
if (!bodyText || bodyText.length === 0)
return

View file

@ -899,47 +899,6 @@ Page {
}
}
/*SectionHeader {
text: qsTr("Experiments")
}
Column {
x: Theme.horizontalPageMargin
spacing: Theme.paddingMedium
Row {
spacing: Theme.paddingMedium
Image {
source: "image://icons/icon-m-good"
height: Theme.iconSizeSmall
width: Theme.iconSizeSmall
anchors.verticalCenter: parent.verticalCenter
}
Label {
text: ai.evaluationCount(1)
anchors.verticalCenter: parent.verticalCenter
}
}
Row {
spacing: Theme.paddingMedium
Image {
source: "image://icons/icon-m-bad"
height: Theme.iconSizeSmall
width: Theme.iconSizeSmall
anchors.verticalCenter: parent.verticalCenter
}
Label {
text: ai.evaluationCount(-1)
anchors.verticalCenter: parent.verticalCenter
}
}
}*/
SectionHeader {
text: qsTr("Other")
}

View file

@ -325,7 +325,7 @@ ApplicationWindow {
return;
}
} else if (code === 800) {
notification.show(qsTr("Cannot save the image file"));
notification.show(qsTr("Cannot save image"));
} else if (code === 801) {
notification.show(qsTr("Image file already exists"));
} else {
@ -402,6 +402,7 @@ ApplicationWindow {
}
function fetcherImageSaved(filename) {
//console.log("fetcherImageSaved: " + filename)
notification.show(qsTr("Image saved as \"" + filename + "\""));
}

View file

@ -24,10 +24,12 @@
#include <QJsonDocument>
#include <QJsonValue>
#include <QJsonArray>
#include <QStandardPaths>
#include <QFile>
#include <QFileInfo>
#include <QDir>
#include <QImageReader>
#include <QImageWriter>
#include <QImage>
#include <memory>
#else
#include "parser.h"
@ -434,26 +436,42 @@ void Fetcher::taskEnd()
setBusy(false);
}
void Fetcher::addExtension(const QString &contentType, QString &path)
void Fetcher::copyImage(const QString &path, const QString &contentType)
{
auto orig_ext = QFileInfo(path).suffix();
QString dpath = QDir(Settings::instance()->getImagesDir())
.absoluteFilePath("kaktus_" + QFileInfo(path).fileName());
Utils::addExtension(contentType, dpath);
if (QFile::exists(dpath)) {
emit error(801); // image already exists
return;
}
QString new_ext;
if (contentType == "image/jpeg") {
new_ext = "jpg";
} else if (contentType == "image/png") {
new_ext = "png";
} else if (contentType == "image/gif") {
new_ext = "gif";
} else if (contentType == "image/svg+xml") {
new_ext = "svg";
} else {
new_ext = orig_ext;
qWarning() << "JPEG file, so removing EXIF metadata";
QImageReader ir(path);
QImage img = ir.read();
if (img.isNull()) {
qWarning() << "Cannot read image:" << path << ir.errorString();
} else {
QImageWriter iw(dpath);
iw.setFormat(ir.format());
if (iw.write(img)) {
qDebug() << "Image saved successfully";
emit imageSaved(QFileInfo(dpath).fileName());
return;
} else {
qWarning() << "Cannot write image:" << dpath << iw.errorString();
}
}
} else if (QFile::copy(path, dpath)) {
qDebug() << "Image saved successfully";
emit imageSaved(QFileInfo(dpath).fileName());
return;
}
if (new_ext != orig_ext) {
path.append("." + new_ext);
}
emit error(800); // image save error
}
void Fetcher::saveImage(const QString &url)
@ -463,26 +481,13 @@ void Fetcher::saveImage(const QString &url)
QString path, contentType;
if (CacheServer::getPathAndContentTypeByUrl(url, path, contentType)) {
qDebug() << "Image already in cache";
//qDebug() << "path:" << path;
//qDebug() << "contentType:" << contentType;
QString dpath = QDir(QStandardPaths::writableLocation(
QStandardPaths::PicturesLocation))
.absoluteFilePath(QFileInfo(path).fileName());
addExtension(contentType, dpath);
//qDebug() << "dpath:" << dpath;
if (QFile::exists(dpath)) {
emit error(801); // image already exists
} else if (QFile::copy(path, dpath)) {
emit imageSaved(QFileInfo(dpath).fileName());
} else {
emit error(800); // image save error
}
copyImage(path, contentType);
} else {
qDebug() << "Image not cached, so downloading";
DatabaseManager::CacheItem item;
item.origUrl = url;
item.finalUrl = url;
//item.type = "entry-image";
item.type = "entry-image";
emit addDownload(item);
auto dm = DownloadManager::instance();
@ -494,18 +499,7 @@ void Fetcher::saveImage(const QString &url)
//qDebug() << "Download finished:" << url << path << contentType;
Q_UNUSED(url);
disconnect(*conn1); disconnect(*conn2);
QString dpath = QDir(QStandardPaths::writableLocation(
QStandardPaths::PicturesLocation))
.absoluteFilePath(QFileInfo(path).fileName());
addExtension(contentType, dpath);
//qDebug() << "dpath:" << dpath;
if (QFile::exists(dpath)) {
emit error(801); // image already exists
} else if (QFile::copy(path, dpath)) {
emit imageSaved(QFileInfo(dpath).fileName());
} else {
emit error(800); // image save error
}
copyImage(path, contentType);
});
*conn2 = connect(dm, &DownloadManager::downloadFailed,
[this, conn1, conn2](const QString &url) {

View file

@ -165,7 +165,7 @@ private:
DatabaseManager::ActionsTypes typeUnset,
DatabaseManager::ActionsTypes typeSetList,
DatabaseManager::ActionsTypes typeUnsetList);
void addExtension(const QString &contentType, QString &path);
void copyImage(const QString &path, const QString &contentType);
};
#endif // FETCHER_H

View file

@ -18,6 +18,7 @@
*/
#include <QDir>
#include <QFileInfo>
#include <QDebug>
#include <QVariant>
@ -939,3 +940,23 @@ bool Settings::getIgnoreSslErrors()
{
return settings.value("ignoresslerrors", false).toBool();
}
void Settings::setImagesDir(const QString &value)
{
if (getImagesDir() != value) {
settings.setValue("imagesdir", value);
emit imagesDirChanged();
}
}
QString Settings::getImagesDir()
{
auto dir = settings.value("imagesdir", "").toString();
QFileInfo d(dir);
if (d.exists() && d.isDir()) {
return dir;
}
// default is Pictures dir
return QStandardPaths::writableLocation(QStandardPaths::PicturesLocation);
}

View file

@ -83,7 +83,7 @@ class Settings: public QObject
Q_PROPERTY (QString pocketTags READ getPocketTags WRITE setPocketTags NOTIFY pocketTagsChanged)
Q_PROPERTY (QString pocketTagsHistory READ getPocketTagsHistory WRITE setPocketTagsHistory NOTIFY pocketTagsHistoryChanged)
Q_PROPERTY (bool ignoreSslErrors READ getIgnoreSslErrors WRITE setIgnoreSslErrors NOTIFY ignoreSslErrorsChanged)
Q_PROPERTY (QString imagesDir READ getImagesDir WRITE setImagesDir NOTIFY imagesDirChanged)
public:
static Settings* instance();
Fetcher* fetcher;
@ -168,6 +168,9 @@ public:
void setOffsetLimit(int value);
int getOffsetLimit();
void setImagesDir(const QString &value);
QString getImagesDir();
/*
View modes:
0 - Tabs->Feeds->Entries
@ -323,6 +326,7 @@ signals:
void pocketFavoriteChanged();
void pocketQuickAddChanged();
void ignoreSslErrorsChanged();
void imagesDirChanged();
/*
501 - Unable create settings dir

View file

@ -24,6 +24,9 @@
#include <QCryptographicHash>
#include <QRegExp>
#include <QFile>
#include <QDir>
#include <QFileInfo>
#include <QFileInfoList>
#ifdef SAILFISH
#include <sailfishapp.h>
@ -825,3 +828,29 @@ void Utils::resetFetcher(int type)
#endif
}
QString Utils::nameFromPath(const QString &path)
{
return QFileInfo(path).fileName();
}
void Utils::addExtension(const QString &contentType, QString &path)
{
auto orig_ext = QFileInfo(path).suffix();
QString new_ext;
if (contentType == "image/jpeg") {
new_ext = "jpg";
} else if (contentType == "image/png") {
new_ext = "png";
} else if (contentType == "image/gif") {
new_ext = "gif";
} else if (contentType == "image/svg+xml") {
new_ext = "svg";
} else {
new_ext = orig_ext;
}
if (new_ext != orig_ext) {
path.append("." + new_ext);
}
}

View file

@ -23,10 +23,6 @@
#include <QObject>
#include <QList>
#include <QString>
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QFileInfoList>
#include <QDate>
#ifdef ANDROID
@ -71,11 +67,13 @@ public:
//Q_INVOKABLE bool isOnline();
Q_INVOKABLE QString formatHtml(const QString &data, bool offline, const QString &style = QString());
Q_INVOKABLE QString readAsset(const QString &path);
Q_INVOKABLE QString nameFromPath(const QString &path);
static QString hash(const QString &url);
static int monthsTo(const QDate &from, const QDate &to);
static int yearsTo(const QDate &from, const QDate &to);
static bool isSameWeek(const QDate &date1, const QDate &date2);
static void addExtension(const QString &contentType, QString &path);
Q_INVOKABLE static void log(const QString &data);
#ifdef ANDROID