arnaucube
arnaucube/raspberryGPIOhtmlserverPublic Self-hosted repository
raspberryGPIOhtmlserver/node_modules/formidable/lib/file.js
node_modules/formidable/lib/file.jsRaw
 1if (global.GENTLY) require = GENTLY.hijack(require);
 2
 3var util = require('util'),
 4    WriteStream = require('fs').WriteStream,
 5    EventEmitter = require('events').EventEmitter,
 6    crypto = require('crypto');
 7
 8function File(properties) {
 9  EventEmitter.call(this);
10
11  this.size = 0;
12  this.path = null;
13  this.name = null;
14  this.type = null;
15  this.hash = null;
16  this.lastModifiedDate = null;
17
18  this._writeStream = null;
19  
20  for (var key in properties) {
21    this[key] = properties[key];
22  }
23
24  if(typeof this.hash === 'string') {
25    this.hash = crypto.createHash(properties.hash);
26  } else {
27    this.hash = null;
28  }
29}
30module.exports = File;
31util.inherits(File, EventEmitter);
32
33File.prototype.open = function() {
34  this._writeStream = new WriteStream(this.path);
35};
36
37File.prototype.toJSON = function() {
38  return {
39    size: this.size,
40    path: this.path,
41    name: this.name,
42    type: this.type,
43    mtime: this.lastModifiedDate,
44    length: this.length,
45    filename: this.filename,
46    mime: this.mime
47  };
48};
49
50File.prototype.write = function(buffer, cb) {
51  var self = this;
52  if (self.hash) {
53    self.hash.update(buffer);
54  }
55  this._writeStream.write(buffer, function() {
56    self.lastModifiedDate = new Date();
57    self.size += buffer.length;
58    self.emit('progress', self.size);
59    cb();
60  });
61};
62
63File.prototype.end = function(cb) {
64  var self = this;
65  if (self.hash) {
66    self.hash = self.hash.digest('hex');
67  }
68  this._writeStream.end(function() {
69    self.emit('end');
70    cb();
71  });
72};