Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Discussed fix for #147 #176

Merged
merged 3 commits into from
Feb 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion source/interface/putFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,20 @@ function getPutContentsDefaults() {
}

function putFileContents(filePath, data, options) {
const putOptions = merge(getPutContentsDefaults(), { headers: { "Content-Length": data.length } }, options || {});
const headers = {
"Content-Length": data.length
};
if (typeof WEB === "undefined") {
// We're running under NodeJS, so it's safe to check if the
// input is a stream
const stream = require("stream");
if (data instanceof stream.Readable) {
// Input was a stream, remove the content length as this
// is not known yet
delete headers["Content-Length"];
}
}
const putOptions = merge(getPutContentsDefaults(), { headers }, options || {});
if (putOptions.overwrite === false) {
putOptions.headers["If-None-Match"] = "*";
}
Expand Down
9 changes: 9 additions & 0 deletions test/specs/putFileContents.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,13 @@ describe("putFileContents", function() {
expect(written).to.equal(text);
});
});

it("can use Readable stream as data source", function() {
const imgBin = fs.readFileSync(SOURCE_BIN);
const imgStream = fs.createReadStream(SOURCE_BIN);
return this.client.putFileContents("/sub1/alrighty.jpg", imgStream).then(function() {
const written = fs.readFileSync(TARGET_BIN);
expect(bufferEquals(written, imgBin)).to.be.true;
});
});
});