| 1 | /* |
| 2 | * Copyright (C) 2020 Damir Porobic <[email protected]> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 17 | * Boston, MA 02110-1301, USA. |
| 18 | */ |
| 19 | |
| 20 | #include "ScriptUploader.h" |
| 21 | |
| 22 | ScriptUploader::ScriptUploader(const QSharedPointer<IConfig> &config, const QSharedPointer<ITempFileProvider> &tempFileProvider) : |
| 23 | mConfig(config), |
| 24 | mTempFileProvider(tempFileProvider) |
| 25 | { |
| 26 | connect(&mProcessHandler, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), this, &ScriptUploader::scriptFinished); |
| 27 | connect(&mProcessHandler, &QProcess::errorOccurred, this, &ScriptUploader::errorOccurred); |
| 28 | } |
| 29 | |
| 30 | void ScriptUploader::upload(const QImage &image) |
| 31 | { |
| 32 | if(saveImageLocally(image)) { |
| 33 | mProcessHandler.start(mConfig->uploadScriptPath(), { mPathToTmpImage }); |
| 34 | } else { |
| 35 | auto result = UploadResult(UploadStatus::UnableToSaveTemporaryImage, type()); |
| 36 | emit finished(result); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | UploaderType ScriptUploader::type() const |
| 41 | { |
| 42 | return UploaderType::Script; |
| 43 | } |
| 44 | |
| 45 | bool ScriptUploader::saveImageLocally(const QImage &image) |
| 46 | { |
| 47 | mPathToTmpImage = mTempFileProvider->tempFile(); |
| 48 | return image.save(mPathToTmpImage); |
| 49 | } |
| 50 | |
| 51 | void ScriptUploader::scriptFinished(int exitCode, QProcess::ExitStatus exitStatus) |
| 52 | { |
| 53 | Q_UNUSED(exitCode); |
| 54 | Q_UNUSED(exitStatus); |
| 55 | |
| 56 | auto stdErrOutput = mProcessHandler.readAllStandardError(); |
| 57 | if(mConfig->uploadScriptStopOnStdErr() && !stdErrOutput.isEmpty()) { |
| 58 | handleError(UploadStatus::ScriptWroteToStdErr, stdErrOutput); |
| 59 | } else if(exitStatus == QProcess::ExitStatus::NormalExit) { |
| 60 | handleSuccess(); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | void ScriptUploader::handleSuccess() |
| 65 | { |
| 66 | auto output = QString(mProcessHandler.readAllStandardOutput()); |
| 67 | auto result = parseOutput(output); |
| 68 | |
| 69 | writeToConsole(output); |
| 70 | |
| 71 | emit finished(UploadResult(UploadStatus::NoError, type(), result)); |
| 72 | } |
| 73 | |
| 74 | QString ScriptUploader::parseOutput(const QString &output) const |
| 75 | { |
| 76 | auto outputFilter = mConfig->uploadScriptCopyOutputFilter(); |
| 77 | auto result = output; |
| 78 | |
| 79 | if(!outputFilter.isEmpty()) { |
| 80 | QRegularExpression regEx(outputFilter); |
| 81 | auto expressionMatch = regEx.match(output); |
| 82 | if(expressionMatch.hasMatch()) { |
| 83 | result = expressionMatch.captured(0); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | return result; |
| 88 | |
| 89 | } |
| 90 | |
| 91 | void ScriptUploader::errorOccurred(QProcess::ProcessError errorType) |
| 92 | { |
| 93 | auto status = mapErrorTypeToStatus(errorType); |
| 94 | auto stdErrOutput = mProcessHandler.readAllStandardError(); |
| 95 | handleError(status, stdErrOutput); |
| 96 | } |
| 97 | |
| 98 | void ScriptUploader::handleError(const UploadStatus &status, const QString &stdErrOutput) |
| 99 | { |
| 100 | writeToConsole(stdErrOutput); |
| 101 | |
| 102 | emit finished(UploadResult(status, type())); |
| 103 | } |
| 104 | |
| 105 | UploadStatus ScriptUploader::mapErrorTypeToStatus(QProcess::ProcessError errorType) const |
| 106 | { |
| 107 | switch (errorType) { |
| 108 | case QProcess::FailedToStart: |
| 109 | return UploadStatus::FailedToStart; |
| 110 | case QProcess::Crashed: |
| 111 | return UploadStatus::Crashed; |
| 112 | case QProcess::Timedout: |
| 113 | return UploadStatus::TimedOut; |
| 114 | case QProcess::ReadError: |
| 115 | return UploadStatus::ReadError; |
| 116 | case QProcess::WriteError: |
| 117 | return UploadStatus::WriteError; |
| 118 | default: |
| 119 | return UploadStatus::UnknownError; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | void ScriptUploader::writeToConsole(const QString &output) const |
| 124 | { |
| 125 | qInfo("%s", qPrintable(output)); |
| 126 | } |
| 127 |