* @brief Returns the body of one script (UTF-8 text). */
| 222 | * @brief Returns the body of one script (UTF-8 text). |
| 223 | */ |
| 224 | API::CommandResponse API::Handlers::ScriptsHandler::get(const QString& id, |
| 225 | const QJsonObject& params) |
| 226 | { |
| 227 | if (!params.contains(QStringLiteral("kind"))) |
| 228 | return CommandResponse::makeError( |
| 229 | id, ErrorCode::MissingParam, QStringLiteral("Missing required parameter: kind")); |
| 230 | |
| 231 | if (!params.contains(QStringLiteral("id"))) |
| 232 | return CommandResponse::makeError( |
| 233 | id, ErrorCode::MissingParam, QStringLiteral("Missing required parameter: id")); |
| 234 | |
| 235 | const auto kindStr = params.value(QStringLiteral("kind")).toString(); |
| 236 | const auto entryId = params.value(QStringLiteral("id")).toString(); |
| 237 | const auto* kind = findKind(kindStr); |
| 238 | if (!kind) |
| 239 | return CommandResponse::makeError( |
| 240 | id, ErrorCode::InvalidParam, QStringLiteral("Unknown script kind: %1").arg(kindStr)); |
| 241 | |
| 242 | const auto manifest = loadManifest(*kind); |
| 243 | bool known = false; |
| 244 | for (const auto& v : manifest) { |
| 245 | if (v.toObject().value(QStringLiteral("file")).toString() == entryId) { |
| 246 | known = true; |
| 247 | break; |
| 248 | } |
| 249 | } |
| 250 | if (!known) |
| 251 | return CommandResponse::makeError( |
| 252 | id, |
| 253 | ErrorCode::InvalidParam, |
| 254 | QStringLiteral("Unknown template id '%1' for kind '%2'. " |
| 255 | "Use scripts.list {kind} to see what's available.") |
| 256 | .arg(entryId, kindStr)); |
| 257 | |
| 258 | QFile f(kind->bodyDir + entryId + kind->extension); |
| 259 | if (!f.open(QIODevice::ReadOnly | QIODevice::Text)) |
| 260 | return CommandResponse::makeError( |
| 261 | id, |
| 262 | ErrorCode::ExecutionError, |
| 263 | QStringLiteral("Failed to read template '%1' (%2)").arg(entryId, f.fileName())); |
| 264 | |
| 265 | const auto body = QString::fromUtf8(f.readAll()); |
| 266 | |
| 267 | QJsonObject result; |
| 268 | result[QStringLiteral("kind")] = kind->id; |
| 269 | result[QStringLiteral("id")] = entryId; |
| 270 | result[QStringLiteral("body")] = body; |
| 271 | result[QStringLiteral("byteCount")] = body.size(); |
| 272 | return CommandResponse::makeSuccess(id, result); |
| 273 | } |
no test coverage detected