Skip to content

Commit 83f95b0

Browse files
storage: add file#save
1 parent 8d38290 commit 83f95b0

2 files changed

Lines changed: 93 additions & 0 deletions

File tree

lib/storage/file.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1427,6 +1427,37 @@ File.prototype.move = function(destination, callback) {
14271427
});
14281428
};
14291429

1430+
/**
1431+
* Write arbitrary data to a file.
1432+
*
1433+
* *This is a convenience method which wraps
1434+
* {module:storage/file#createWriteStream}.*
1435+
*
1436+
* @param {*} data - The data to write to a file.
1437+
* @param {object=} options - See {module:storage/file#createWriteStream}'s
1438+
* `options` parameter.
1439+
* @param {function} callback - The callback function.
1440+
* @param {?error} callback.err - An error returned while making this request
1441+
*
1442+
* @example
1443+
* file.save('This is the contents of the file.', function(err) {
1444+
* if (!err) {
1445+
* // File written successfully.
1446+
* }
1447+
* });
1448+
*/
1449+
File.prototype.save = function(data, options, callback) {
1450+
if (is.fn(options)) {
1451+
callback = options;
1452+
options = {};
1453+
}
1454+
1455+
this.createWriteStream(options)
1456+
.on('error', callback)
1457+
.on('finish', callback)
1458+
.end(data);
1459+
};
1460+
14301461
/**
14311462
* This creates a gcs-resumable-upload upload stream.
14321463
*

test/storage/file.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2072,6 +2072,68 @@ describe('File', function() {
20722072
});
20732073
});
20742074

2075+
describe.only('save', function() {
2076+
var DATA = 'Data!';
2077+
2078+
it('should accept an options object', function(done) {
2079+
var options = {};
2080+
2081+
file.createWriteStream = function(options_) {
2082+
assert.strictEqual(options_, options);
2083+
setImmediate(done);
2084+
return new stream.PassThrough();
2085+
};
2086+
2087+
file.save(DATA, options, assert.ifError);
2088+
});
2089+
2090+
it('should not require options', function(done) {
2091+
file.createWriteStream = function(options_) {
2092+
assert.deepEqual(options_, {});
2093+
setImmediate(done);
2094+
return new stream.PassThrough();
2095+
};
2096+
2097+
file.save(DATA, assert.ifError);
2098+
});
2099+
2100+
it('should register the error listener', function(done) {
2101+
file.createWriteStream = function() {
2102+
var writeStream = new stream.PassThrough();
2103+
writeStream.on('error', done);
2104+
setImmediate(function() {
2105+
writeStream.emit('error');
2106+
});
2107+
return writeStream;
2108+
};
2109+
2110+
file.save(DATA, assert.ifError);
2111+
});
2112+
2113+
it('should register the finish listener', function(done) {
2114+
file.createWriteStream = function() {
2115+
var writeStream = new stream.PassThrough();
2116+
writeStream.once('finish', done);
2117+
return writeStream;
2118+
};
2119+
2120+
file.save(DATA, assert.ifError);
2121+
});
2122+
2123+
it('should write the data', function(done) {
2124+
file.createWriteStream = function() {
2125+
var writeStream = new stream.PassThrough();
2126+
writeStream.on('data', function(data) {
2127+
assert.strictEqual(data.toString(), DATA);
2128+
done();
2129+
});
2130+
return writeStream;
2131+
};
2132+
2133+
file.save(DATA, assert.ifError);
2134+
});
2135+
});
2136+
20752137
describe('startResumableUpload_', function() {
20762138
describe('starting', function() {
20772139
it('should start a resumable upload', function(done) {

0 commit comments

Comments
 (0)