var q = {
where: {
name: fileName,
- parentId: fileId,
- fileType: 1
+ parentId: fileId
}
};
return Files.findOne(q)
if(r === null) {
reject(new Error("\"" + fileName + "\" does not exist in " + '"' + parentDirectory + "\" directory."));
} else {
- console.log(r.fileId);
+ //console.log(r.fileId);
resolve(r.fileId);
}
});
*/
function notExistFile(fileName, parentDirectory) {
return new Promise(function(resolve, reject) {
+ console.log(fileName, parentDirectory);
existDirectory(parentDirectory)
.catch(function(error) {
reject(error);
var q = {
where: {
name: fileName,
- parentId: fileId,
- fileType: 1
+ parentId: fileId
}
};
return Files.findOne(q)
/**
* createDirectory
*
- * @param fileName
- * @param parentDirectory
+ * @param directory
* @returns {promise}<sequelize.createの結果を格納したobject | Error>
*/
- function createDirectory(fileName,parentDirectory) {
+ function createDirectory(directory) {
return new Promise(function(resolve, reject) {
- Promise.all([notExistFile(fileName, parentDirectory), existDirectory(parentDirectory)])
+ var leaf = directory.split('/').pop();
+ var parentDirectory = directory.replace('/'+leaf, '');
+ if(!parentDirectory) {
+ parentDirectory = '/';
+ }
+ Promise.all([existDirectory(parentDirectory), notExistDirectory(directory), notExistFile(leaf, parentDirectory)])
.catch(function(error) {
reject(error);
})
- .then(function() {
+ .then(function(r) {
+ var parentId = r[0];
var q = {
fileId: uuid.v1(),
- name: fileName,
- parentDirectory: parentDirectory,
- fileType: 1
+ name: leaf,
+ parentId: parentId,
+ fileType: 0
}
return Files.create(q)
})
});
it('should be rejected when does not exist same name file in a directory', function() {
- return db.existFile('hogehoge.txt', '/').should.be.rejectedWith(Error, '"hogehoge.txt" does not exist in "/" directory.');
+ return db.existFile('hogehoge.txt', '/').should.be.rejectedWith(Error, '"hogehoge.txt" does not exist in "/" directory.');
});
it('should be rejected when a directory does not exist', function() {
- return db.existFile('hogehoge.txt', '/hoge').should.be.rejectedWith(Error, '"/hoge" directory doesn\'t exist.');
+ return db.existFile('hogehoge.txt', '/hoge').should.be.rejectedWith(Error, '"/hoge" directory doesn\'t exist.');
});
it('should be resolved when file exists in a directory', function() {
- return expect(db.existFile('hogehoge.txt', '/one')).to.eventually.be.a('string');
+ return expect(db.existFile('hogehoge.txt', '/one')).to.eventually.be.a('string');
});
});
});
it('should be rejected when the same name file has already existed in directory(1)', function() {
- return db.notExistFile('hoge.txt', '/').should.be.rejectedWith(Error, '"hoge.txt" has already existed in "/" directory.');
+ return db.notExistFile('hoge.txt', '/').should.be.rejectedWith(Error, '"hoge.txt" has already existed in "/" directory.');
});
it('should be rejected when the same name file has already existed in directory(2)', function() {
- return db.notExistFile('hogehoge.txt', '/one').should.be.rejectedWith(Error, '"hogehoge.txt" has already existed in "/one" directory.');
+ return db.notExistFile('hogehoge.txt', '/one').should.be.rejectedWith(Error, '"hogehoge.txt" has already existed in "/one" directory.');
});
it('should be resolved when the same name file does not exist in directory', function() {
- return expect(db.notExistFile('hoge.txt', '/one')).to.eventually.equal();
+ return expect(db.notExistFile('hoge.txt', '/one')).to.eventually.equal();
});
});
});
it('should be rejected when exists directory(1)', function() {
- return db.notExistDirectory('/').should.be.rejectedWith(Error,'"/" directory exists.');
+ return db.notExistDirectory('/').should.be.rejectedWith(Error,'"/" directory exists.');
});
it('should be rejected when exists directory(2)', function() {
- return db.notExistDirectory('/one').should.be.rejectedWith(Error,'"/one" directory exists.');
+ return db.notExistDirectory('/one').should.be.rejectedWith(Error,'"/one" directory exists.');
});
it('should be rejected when exists directory(3)', function() {
- return db.notExistDirectory('/one/two').should.be.rejectedWith(Error,'"/one/two" directory exists.');
+ return db.notExistDirectory('/one/two').should.be.rejectedWith(Error,'"/one/two" directory exists.');
});
});
+
var createFile = `
/**
* createFile
});
it('should be rejected when "parentDirectory" doesn\'t exist', function() {
- return db.createFile('hoge.txt', '/hoge').should.be.rejectedWith(Error, '"/hoge" directory doesn\'t exist.');
+ return db.createFile('hoge.txt', '/hoge').should.be.rejectedWith(Error, '"/hoge" directory doesn\'t exist.');
});
it('should be rejected when a file has already existed in a directory.', function() {
- return db.createFile('hoge.txt', '/').should.be.rejectedWith(Error, '"hoge.txt" has already existed in "/" directory.');
+ return db.createFile('hoge.txt', '/').should.be.rejectedWith(Error, '"hoge.txt" has already existed in "/" directory.');
});
it('should be resolved when file creation successed.', function() {
return expect(db.existFile('tarou.txt', '/')).to.eventually.be.a('string');
});
-
-
var removeFile = `
/**
* removeFile
});
});
it('should be resolved when removeFile() successed', function() {
- return db.removeFile('hoge.txt', '/')
- .then(function() {
- return db.existFile('hoge.txt', '/')
- })
- .should.be.rejectedWith(Error, '"hoge.txt" does not exist in "/" directory.');
+ return db.removeFile('hoge.txt', '/')
+ .then(function() {
+ return db.existFile('hoge.txt', '/')
+ })
+ .should.be.rejectedWith(Error, '"hoge.txt" does not exist in "/" directory.');
});
});
+
+
+ var createDirectory = `
+ /**
+ * createFile
+ *
+ * @param directory
+ * @returns {promise}
+ */
+ `;
+ describe(createDirectory, function() {
+ var db;
+ before(function() {
+ return DB()
+ .then(function(r) {
+ db = r;
+ })
+ .then(function() {
+ return db.test1();
+ })
+ .then(function() {
+ return db.test2();
+ })
+ .then(function() {
+ return db.createDirectory('/one/hoge');
+ });
+ /*
+ .then(function() {
+ return db.createDirectory('/one/two/three');
+ });
+ */
+ });
+
+ it('should be rejected when directory has already existed', function() {
+ return db.createDirectory('/one/two').should.be.rejectedWith(Error, '"/one/two" directory exists.');
+ });
+ it('should be rejected when parent directory does not exist.', function() {
+ return db.createDirectory('/four/hoge').should.be.rejectedWith(Error, '"/four" directory doesn\'t exist.');
+ });
+ it('should be rejected when the same name file has already existed.', function() {
+ return db.createDirectory('/hoge.txt').should.be.rejectedWith(Error, '"hoge.txt" has already existed in "/" directory.');
+ });
+ it('should be rejected when the /one/hoge directory has already existed', function() {
+ return db.notExistDirectory('/one/hoge').should.be.rejectedWith(Error, '"/one/hoge" directory exists.');
+ });
+
+ });
+
});
})();