Skip to content
This repository has been archived by the owner on Oct 30, 2018. It is now read-only.

Specify id for bucket entry #57

Merged
merged 3 commits into from
Feb 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"undef": true,
"unused": true,
"maxparams": 4,
"maxstatements": 14,
"maxstatements": 20,
"maxcomplexity": 6,
"maxdepth": 3,
"maxlen": 80,
Expand Down
16 changes: 15 additions & 1 deletion lib/models/bucketentry.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@ const mongoose = require('mongoose');
const mimetypes = require('mime-db');
const SchemaOptions = require('../options');
const storj = require('storj-lib');
const errors = require('storj-service-error-types');
const utils = storj.utils;

/**
* Represents a bucket entry that points to a file
* @constructor
*/
var BucketEntry = new mongoose.Schema({
_id: {
type: String
},
frame: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Frame'
Expand Down Expand Up @@ -70,7 +75,16 @@ BucketEntry.set('toObject', {
BucketEntry.statics.create = function(data, callback) {
var BucketEntry = this;

var id = storj.utils.calculateFileId(data.bucket, data.name);
let id = null;
if (data.id) {
if (data.id.length <= 64 && utils.isHexaString(data.id)) {
id = data.id;
} else {
return callback(new errors.BadRequestError('Invalid id supplied'));
}
} else {
id = storj.utils.calculateFileId(data.bucket, data.name);
}

var query = {
_id: id,
Expand Down
44 changes: 44 additions & 0 deletions test/bucketentry.unit.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const storj = require('storj-lib');
const errors = require('storj-service-error-types');
const expect = require('chai').expect;
const mongoose = require('mongoose');

Expand Down Expand Up @@ -64,6 +65,49 @@ describe('Storage/models/BucketEntry', function() {
});
});

it('should create the bucket entry with id', function(done) {
Bucket.create({ _id: 'user@domain.tld' }, { name: 'New Bucket3' },
function(err, bucket) {
var frame = new Frame({});
frame.save(function(err) {
expect(err).to.not.be.instanceOf(Error);
BucketEntry.create({
frame: frame._id,
bucket: bucket._id,
id: 'ed6c1becbcef808463e4d38b58d5d310115e42ca',
name: 'test2.txt',
mimetype: 'text/plain'
}, function(err, entry) {
expect(entry.filename).to.equal('test2.txt');
expect(entry.id).to.equal('ed6c1becbcef808463e4d38b58d5d310115e42ca');
done();
});
});
});
});

it('should NOT create the bucket entry with invalid id', function(done) {
Bucket.create({ _id: 'user@domain.tld' }, { name: 'New Bucket4' },
function(err, bucket) {
var frame = new Frame({});
frame.save(function(err) {
expect(err).to.not.be.instanceOf(Error);
BucketEntry.create({
frame: frame._id,
bucket: bucket._id,
id: '#######################################',
name: 'test2.txt',
mimetype: 'text/plain'
}, function(err, entry) {
expect(err).to.be.instanceOf(errors.BadRequestError);
expect(err.message).to.equal('Invalid id supplied');
expect(entry).to.equal(undefined);
done();
});
});
});
});

it('should replace a duplicate name in this same bucket', function(done) {
var frame = new Frame({

Expand Down