This commit is contained in:
Jonasz Bigda
2023-03-25 21:51:42 +01:00
parent 0db1d5117e
commit b332e9ceb0
1044 changed files with 37502 additions and 63938 deletions

View File

@@ -1,14 +1,12 @@
'use strict';
var core = require('../core');
var crypto = require('crypto');
var stream = require('stream');
var util = require('util');
var Buffer = require('safe-buffer').Buffer;
var ERROR_NAMESPACE_NOT_FOUND = 26;
module.exports = GridFSBucketWriteStream;
const MONGODB_ERROR_CODES = require('../error_codes').MONGODB_ERROR_CODES;
const core = require('../core');
const crypto = require('crypto');
const stream = require('stream');
const util = require('util');
const Buffer = require('safe-buffer').Buffer;
const deprecateOptions = require('../utils').deprecateOptions;
/**
* A writable stream that enables you to write buffers to GridFS.
@@ -22,49 +20,58 @@ module.exports = GridFSBucketWriteStream;
* @param {object} [options] Optional settings.
* @param {string|number|object} [options.id] Custom file id for the GridFS file.
* @param {number} [options.chunkSizeBytes] The chunk size to use, in bytes
* @param {number} [options.w] The write concern
* @param {number} [options.wtimeout] The write concern timeout
* @param {number} [options.j] The journal write concern
* @param {(number|string)} [options.w] **Deprecated** The write concern. Use writeConcern instead.
* @param {number} [options.wtimeout] **Deprecated** The write concern timeout. Use writeConcern instead.
* @param {boolean} [options.j=false] **Deprecated** Specify a journal write concern. Use writeConcern instead.
* @param {object|WriteConcern} [options.writeConcern] Specify write concern settings.
* @param {boolean} [options.disableMD5=false] If true, disables adding an md5 field to file data
* @fires GridFSBucketWriteStream#error
* @fires GridFSBucketWriteStream#finish
*/
function GridFSBucketWriteStream(bucket, filename, options) {
options = options || {};
this.bucket = bucket;
this.chunks = bucket.s._chunksCollection;
this.filename = filename;
this.files = bucket.s._filesCollection;
this.options = options;
// Signals the write is all done
this.done = false;
const GridFSBucketWriteStream = deprecateOptions(
{
name: 'GridFSBucketWriteStream',
deprecatedOptions: ['disableMD5'],
optionsIndex: 2
},
function(bucket, filename, options) {
options = options || {};
stream.Writable.call(this, options);
this.bucket = bucket;
this.chunks = bucket.s._chunksCollection;
this.filename = filename;
this.files = bucket.s._filesCollection;
this.options = options;
// Signals the write is all done
this.done = false;
this.id = options.id ? options.id : core.BSON.ObjectId();
this.chunkSizeBytes = this.options.chunkSizeBytes;
this.bufToStore = Buffer.alloc(this.chunkSizeBytes);
this.length = 0;
this.md5 = !options.disableMD5 && crypto.createHash('md5');
this.n = 0;
this.pos = 0;
this.state = {
streamEnd: false,
outstandingRequests: 0,
errored: false,
aborted: false,
promiseLibrary: this.bucket.s.promiseLibrary
};
this.id = options.id ? options.id : core.BSON.ObjectId();
this.chunkSizeBytes = this.options.chunkSizeBytes;
this.bufToStore = Buffer.alloc(this.chunkSizeBytes);
this.length = 0;
this.md5 = !options.disableMD5 && crypto.createHash('md5');
this.n = 0;
this.pos = 0;
this.state = {
streamEnd: false,
outstandingRequests: 0,
errored: false,
aborted: false,
promiseLibrary: this.bucket.s.promiseLibrary
};
if (!this.bucket.s.calledOpenUploadStream) {
this.bucket.s.calledOpenUploadStream = true;
if (!this.bucket.s.calledOpenUploadStream) {
this.bucket.s.calledOpenUploadStream = true;
var _this = this;
checkIndexes(this, function() {
_this.bucket.s.checkedIndexes = true;
_this.bucket.emit('index');
});
var _this = this;
checkIndexes(this, function() {
_this.bucket.s.checkedIndexes = true;
_this.bucket.emit('index');
});
}
}
}
);
util.inherits(GridFSBucketWriteStream, stream.Writable);
@@ -208,7 +215,7 @@ function checkChunksIndex(_this, callback) {
_this.chunks.listIndexes().toArray(function(error, indexes) {
if (error) {
// Collection doesn't exist so create index
if (error.code === ERROR_NAMESPACE_NOT_FOUND) {
if (error.code === MONGODB_ERROR_CODES.NamespaceNotFound) {
var index = { files_id: 1, n: 1 };
_this.chunks.createIndex(index, { background: false, unique: true }, function(error) {
if (error) {
@@ -282,6 +289,7 @@ function checkDone(_this, callback) {
return __handleError(_this, error, callback);
}
_this.emit('finish', filesDoc);
_this.emit('close');
});
return true;
@@ -306,7 +314,7 @@ function checkIndexes(_this, callback) {
_this.files.listIndexes().toArray(function(error, indexes) {
if (error) {
// Collection doesn't exist so create index
if (error.code === ERROR_NAMESPACE_NOT_FOUND) {
if (error.code === MONGODB_ERROR_CODES.NamespaceNotFound) {
var index = { filename: 1, uploadDate: 1 };
_this.files.createIndex(index, { background: false }, function(error) {
if (error) {
@@ -536,3 +544,5 @@ function checkAborted(_this, callback) {
}
return false;
}
module.exports = GridFSBucketWriteStream;