This repository has been archived by the owner on Oct 30, 2018. It is now read-only.
/
frame.js
134 lines (117 loc) · 2.77 KB
/
frame.js
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
'use strict';
const storj = require('storj');
const assert = require('assert');
const mongoose = require('mongoose');
const SchemaOptions = require('../options');
/**
* Represents a file staging frame
* @constructor
*/
var Frame = new mongoose.Schema({
created: {
type: Date,
default: Date.now
},
user: {
type: mongoose.SchemaTypes.Email,
ref: 'User'
},
shards: [{
index: {
type: Number,
required: true
},
hash: {
type: String,
required: true
},
size: {
type: Number,
required: true
},
tree: [{
type: String
}],
challenges: [{
type: String
}]
}]
});
Frame.virtual('size').get(function() {
let size = 0;
this.shards.forEach(function(shard) {
size += shard.size;
});
return size;
});
Frame.plugin(SchemaOptions);
Frame.set('toObject', {
transform: function(doc, ret) {
delete ret.__v;
delete ret._id;
ret.id = doc._id;
}
});
/**
* Validates the provided shard data
* @param {Object} shard
* @private
*/
Frame.methods._validateShardData = function(shard) {
let challenges = shard.challenges;
let tree = shard.tree;
assert(typeof shard.index === 'number', 'Invalid index supplied');
assert(Number.isFinite(shard.index), 'Invalid index supplied');
assert(shard.index >= 0, 'Invalid index supplied');
assert(
Buffer(storj.utils.rmd160sha256(shard.hash), 'hex').length === 20,
'Invalid RIPEMD-160 SHA-256 hash (hex) supplied'
);
assert(typeof shard.size === 'number', 'Invalid size supplied');
assert(Number.isFinite(shard.size), 'Invalid size supplied');
assert(shard.size > 0, 'Invalid size supplied');
assert(Array.isArray(challenges), 'Invalid challenges supplied');
assert(challenges.length, 'Not enough challenges supplied');
assert(Array.isArray(tree), 'Invalid tree supplied');
assert(
tree.length === storj.utils.getNextPowerOfTwo(challenges.length),
'Challenges and tree do not match'
);
};
/**
* Adds a shard item to the frame
* @param {Object} shard
* @param {Function} callback
*/
Frame.methods.addShard = function(shard, callback) {
try {
this._validateShardData(shard);
} catch (err) {
return callback(err);
}
for (let s = 0; s < this.shards.length; s++) {
if (this.shards[s].hash === shard.hash) {
return callback(null);
}
}
this.shards.push(shard);
callback(null);
};
/**
* Creates a Frame
* @param {storage.models.User} user
* @param {Function} callback
*/
Frame.statics.create = function(user, callback) {
let Frame = this;
let frame = new Frame({ user: user._id });
frame.save(function(err) {
if (err) {
return callback(err);
}
callback(null, frame);
});
};
module.exports = function(connection) {
return connection.model('Frame', Frame);
};