| 1 | /* |
|---|---|
| 2 | This file is part of the KFileMetaData project |
| 3 | SPDX-FileCopyrightText: 2016 Varun Joshi <varunj.1011@gmail.com> |
| 4 | |
| 5 | SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL |
| 6 | */ |
| 7 | |
| 8 | #include "externalwriter.h" |
| 9 | #include "properties.h" |
| 10 | #include "propertyinfo.h" |
| 11 | #include "kfilemetadata_debug.h" |
| 12 | |
| 13 | #include <QDir> |
| 14 | #include <QProcess> |
| 15 | #include <QJsonDocument> |
| 16 | #include <QJsonObject> |
| 17 | #include <QJsonArray> |
| 18 | |
| 19 | #define WRITER_TIMEOUT_MS 30000 |
| 20 | |
| 21 | using namespace KFileMetaData; |
| 22 | |
| 23 | class KFileMetaData::ExternalWriterPrivate |
| 24 | { |
| 25 | public: |
| 26 | QString path; |
| 27 | QStringList writeMimetypes; |
| 28 | QString mainPath; |
| 29 | }; |
| 30 | |
| 31 | |
| 32 | ExternalWriter::ExternalWriter(QObject* parent) |
| 33 | : WriterPlugin(parent), |
| 34 | d_ptr(new ExternalWriterPrivate) |
| 35 | { |
| 36 | } |
| 37 | |
| 38 | ExternalWriter::ExternalWriter(const QString& pluginPath) |
| 39 | : WriterPlugin(nullptr), |
| 40 | d_ptr(new ExternalWriterPrivate) |
| 41 | { |
| 42 | Q_D(ExternalWriter); |
| 43 | d->path = pluginPath; |
| 44 | |
| 45 | QDir pluginDir(pluginPath); |
| 46 | QStringList pluginDirContents = pluginDir.entryList(); |
| 47 | |
| 48 | if (!pluginDirContents.contains(QStringLiteral("manifest.json"))) { |
| 49 | qCDebug(KFILEMETADATA_LOG) << "Path does not seem to contain a valid plugin"; |
| 50 | return; |
| 51 | } |
| 52 | |
| 53 | QFile manifest(pluginDir.filePath(QStringLiteral("manifest.json"))); |
| 54 | manifest.open(flags: QIODevice::ReadOnly); |
| 55 | QJsonDocument manifestDoc = QJsonDocument::fromJson(json: manifest.readAll()); |
| 56 | if (!manifestDoc.isObject()) { |
| 57 | qCDebug(KFILEMETADATA_LOG) << "Manifest does not seem to be a valid JSON Object"; |
| 58 | return; |
| 59 | } |
| 60 | |
| 61 | QJsonObject rootObject = manifestDoc.object(); |
| 62 | const QJsonArray mimetypesArray = rootObject.value(QStringLiteral("mimetypes")).toArray(); |
| 63 | QStringList mimetypes; |
| 64 | for (const QJsonValue &mimetype : mimetypesArray) { |
| 65 | mimetypes << mimetype.toString(); |
| 66 | } |
| 67 | |
| 68 | d->writeMimetypes.append(l: mimetypes); |
| 69 | d->mainPath = pluginDir.absoluteFilePath(fileName: rootObject[QStringLiteral("main")].toString()); |
| 70 | } |
| 71 | |
| 72 | ExternalWriter::~ExternalWriter() = default; |
| 73 | |
| 74 | QStringList ExternalWriter::writeMimetypes() const |
| 75 | { |
| 76 | Q_D(const ExternalWriter); |
| 77 | return d->writeMimetypes; |
| 78 | } |
| 79 | |
| 80 | void ExternalWriter::write(const WriteData& data) |
| 81 | { |
| 82 | Q_D(ExternalWriter); |
| 83 | QJsonDocument writeData; |
| 84 | QJsonObject rootObject; |
| 85 | QJsonObject propertiesObject; |
| 86 | QByteArray output; |
| 87 | QByteArray errorOutput; |
| 88 | |
| 89 | const PropertyMultiMap properties = data.properties(); |
| 90 | |
| 91 | for (auto i = properties.constBegin(); i != properties.constEnd(); ++i) { |
| 92 | PropertyInfo propertyInfo(i.key()); |
| 93 | propertiesObject[propertyInfo.name()] = QJsonValue::fromVariant(variant: i.value()); |
| 94 | } |
| 95 | |
| 96 | rootObject[QStringLiteral("path")] = QJsonValue(data.inputUrl()); |
| 97 | rootObject[QStringLiteral("mimetype")] = data.inputMimetype(); |
| 98 | rootObject[QStringLiteral("properties")] = propertiesObject; |
| 99 | writeData.setObject(rootObject); |
| 100 | |
| 101 | QProcess writerProcess; |
| 102 | writerProcess.start(program: d->mainPath, arguments: QStringList(), mode: QIODevice::ReadWrite); |
| 103 | writerProcess.write(data: writeData.toJson()); |
| 104 | writerProcess.closeWriteChannel(); |
| 105 | writerProcess.waitForFinished(WRITER_TIMEOUT_MS); |
| 106 | |
| 107 | errorOutput = writerProcess.readAllStandardError(); |
| 108 | |
| 109 | if (writerProcess.exitStatus()) { |
| 110 | qCDebug(KFILEMETADATA_LOG) << "Something went wrong while trying to write data"; |
| 111 | qCDebug(KFILEMETADATA_LOG) << errorOutput; |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | output = writerProcess.readAll(); |
| 116 | |
| 117 | QJsonDocument writerExitData = QJsonDocument::fromJson(json: output); |
| 118 | if (!writerExitData.isObject()) { |
| 119 | return; |
| 120 | } |
| 121 | QJsonObject outputRootObject = writerExitData.object(); |
| 122 | |
| 123 | if (outputRootObject[QStringLiteral("status")].toString() != QStringLiteral( "OK")) { |
| 124 | qCDebug(KFILEMETADATA_LOG) << outputRootObject[QStringLiteral("error")].toString(); |
| 125 | qCDebug(KFILEMETADATA_LOG) << errorOutput; |
| 126 | } |
| 127 | |
| 128 | } |
| 129 | |
| 130 | #include "moc_externalwriter.cpp" |
| 131 |
