| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249 |
1
1
1
1
1
1
1
1
1
41
41
41
41
41
41
41
41
41
41
41
41
41
41
41
21
41
1
1
41
41
41
41
41
41
1
129
8
8
129
1
77
77
77
1
35
1
143
143
37
37
37
106
29
77
77
77
77
41
41
41
41
36
36
36
1
35
35
36
17
36
36
36
36
1
3
3
3
3
3
3
1
86
1
62
62
62
62
62
1
8
1
8
8
8
1
8
8
8
8
8
8
8
1
| //
// Copyright (c) Microsoft and contributors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//
// See the License for the specific language governing permissions and
// limitations under the License.
//
var stream = require('stream');
var crypto = require('crypto');
var util = require('util');
var fs = require('fs');
var validator = require('validator');
var Constants = require('../util/constants');
var bufferSize = Constants.BlobConstants.DEFAULT_WRITE_BLOCK_SIZE_IN_BYTES;
var EventEmitter = require('events').EventEmitter;
/**
* File read stream
* 1. Calculate md5
* 2. Track reading offset
* 3. Work with customize memory allocator
* 4. Buffer data from stream.
* @param {object} options stream.Readable options
*/
function FileReadStream(path, options) {
stream.Stream.call(this);
this.readable = true;
Iif(!options) {
options = {};
}
this._destroyed = false;
this._streamEnded = false;
this._fd = null;
this._fileName = undefined;
this._highWaterMark = options.highWaterMark || bufferSize;
this._offset = 0;
this._paused = undefined;
this._allocator = options.allocator;
this._fileName = path;
this._md5hash = null;
this._md5sum = undefined;
if (options.calcContentMd5) {
this._md5hash = crypto.createHash('md5');
}
this._open();
}
util.inherits(FileReadStream, stream.Stream);
/**
* Open file
*/
FileReadStream.prototype._open = function () {
var flags = 'r';
var self = this;
fs.open(this._fileName, flags, function(error, fd) {
Iif (error) {
self.emit('error', error);
} else {
self._fd = fd;
self.emit('open', fd);
}
});
};
/**
* Add event listener
*/
FileReadStream.prototype.on = function(event, listener) {
if (event === 'data' && this._paused === undefined) {
this._paused = false;
this._emitData();
}
return EventEmitter.prototype.on.call(this, event, listener);
};
/**
* Get buffer
*/
FileReadStream.prototype._getBuffer = function(size) {
Iif(this._allocator && this._allocator.getBuffer) {
return this._allocator.getBuffer(size);
} else {
var buffer = new Buffer(size);
return buffer;
}
};
/**
* Release buffer
*/
FileReadStream.prototype._releaseBuffer = function(buffer) {
Iif(this._allocator && this._allocator.releaseBuffer) {
this._allocator.releaseBuffer(buffer);
}
};
/**
* Emit the data from file
*/
FileReadStream.prototype._emitData = function() {
var self = this;
if(!this._fd) {
this.once('open', function() {
self._emitData();
});
return;
}
if (this._paused || this._streamEnded) {
return;
}
var buffer = this._getBuffer(this._highWaterMark);
fs.read(this._fd, buffer, 0, this._highWaterMark, this._offset, function(error, bytesRead, readBuffer) {
Iif (error) {
self.emit('error', error);
return;
}
if(bytesRead === 0) {
Eif(!self._streamEnded) {
self._streamEnded = true;
self.emit('end');
}
return;
}
var range = {
start : self._offset,
end : self._offset + bytesRead - 1,
size : bytesRead
};
var data;
if(bytesRead == self._highWaterMark) {
data = readBuffer;
} else {
data = readBuffer.slice(0, bytesRead);
//Release the current buffer since we created a new one
self._releaseBuffer(readBuffer);
}
if(self._md5hash) {
self._md5hash.update(data);
}
self.emit('data', data, range);
// cleanup
self._offset += bytesRead;
buffer = readBuffer = data = null;
self._emitData();
});
};
/**
* Get file content md5 when read completely.
*/
FileReadStream.prototype.getContentMd5 = function(encoding) {
Iif (!encoding) encoding = 'base64';
Iif(!this._md5hash) {
throw new Error('Can\'t get content md5, please set the calcContentMd5 option for FileReadStream.');
} else {
Eif (this._streamEnded) {
Eif (!this._md5sum) {
this._md5sum = this._md5hash.digest(encoding);
}
return this._md5sum;
} else {
throw new Error('FileReadStream has not ended.');
}
}
};
/**
* Pause chunk stream
*/
FileReadStream.prototype.pause = function() {
this._paused = true;
};
/**
* Resume read stream
*/
FileReadStream.prototype.resume = function() {
var previousState = this._paused;
Eif (this._paused) {
this._paused = false;
Eif(previousState === true) {
//Only start to emit data when it's in pause state
this._emitData();
}
}
};
FileReadStream.prototype.finish = function () {
this.destroy();
};
FileReadStream.prototype.destroy = function () {
Iif (this._destroyed) {
return;
}
var self = this;
this.readable = false;
function close(fd) {
fs.close(fd || self._fd, function(err) {
Iif (err) {
self.emit('error', err);
}
else {
self.emit('close');
}
});
self._fd = null;
self._destroyed = true;
}
// when the stream is closed immediately after creating it
Iif (!validator.isInt(this._fd)) {
this.once('open', close);
return;
}
close();
};
module.exports = FileReadStream;
|