diff --git a/CHANGELOG.md b/CHANGELOG.md index 57d3de2..697d4fe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,13 @@ The format is based on and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [0.2.7] - 2020-02-07 + +### Changed + +### Fixed +- Upload bug (added write verification). + ## [0.2.6] - 2020-02-07 ### Changed diff --git a/main.js b/main.js index fc6c13f..ed95214 100644 --- a/main.js +++ b/main.js @@ -128,7 +128,6 @@ async function run(commandName, commandProps) { break; case "upload": output = await commandHandler.upload(id, filepath); - output = "OK"; break; case "copyalias": await copyAliasConfig({appConfig, alias, copyAliasName: aliasName, configPath}); diff --git a/package.json b/package.json index 11f308e..1ca2acd 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@nqminds/nqm-tdx-terminal-cli", - "version": "0.2.6", + "version": "0.2.7", "description": "Command-line interface for accessing the TDX API", "main": "main.js", "directories": { diff --git a/src/upload.js b/src/upload.js index c3a51e8..8d5abaf 100644 --- a/src/upload.js +++ b/src/upload.js @@ -2,6 +2,23 @@ const fs = require("fs"); const request = require("request"); const path = require("path"); const databotUtils = require("@nqminds/nqm-databot-utils"); +const {getInfo} = require("./info"); + +async function getUsername(api) { + const info = await getInfo({api}); + return info.username || ""; +} + +async function verifyResource(api, id) { + const username = await getUsername(api); + const output = await api.getResource(id); + const access = await api.getResourceAccess(id); + const result = access.find((element) => (element.aid === username && "w" in element)); + + if (result === undefined) { + throw Error("No write access to resource"); + } else return output; +} function getFileStream(filepath) { const resourceStream = fs.createReadStream(filepath); @@ -32,7 +49,7 @@ async function pipeStream(req, resourceStream) { if ("message" in uploadError) { reject(uploadError); } else { - resolve("OK"); + resolve(); } }); req.on("error", (err) => reject(err)); @@ -62,8 +79,10 @@ async function uploadStream({api, resourceStream, filename, filesize, id}) { async function uploadResource({id, filepath, api}) { if (filepath) { + const output = await verifyResource(api, id); const {resourceStream, filename, filesize} = getFileStream(filepath); - return uploadStream({api, resourceStream, filename, filesize, id}); + await uploadStream({api, resourceStream, filename, filesize, id}); + return output; } else { return Error("Input streams unsupported!"); }