Skip to content

Commit

Permalink
Added upload write file verification
Browse files Browse the repository at this point in the history
  • Loading branch information
mereacre committed Feb 7, 2020
1 parent 8a7a42f commit 03b8706
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
23 changes: 21 additions & 2 deletions src/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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!");
}
Expand Down

0 comments on commit 03b8706

Please sign in to comment.