• R/O
  • HTTP
  • SSH
  • HTTPS

zephyr: Commit

This repository is for zephyr, which is implemented for a GUI of Eos.


Commit MetaInfo

Revision33cc0d64e6ad592434d9d4517c254a4aa934dfa6 (tree)
Time2016-01-24 17:32:45
Authorhimetani_cafe <fumifumi@yasu...>
Commiterhimetani_cafe

Log Message

existDirectory(),notExistDirectory()の仕様変更、Filesテーブルのスキーマの変更

Please enter the commit message for your changes. Lines starting
with will be ignored, and an empty message aborts the commit.
On branch master
Changes to be committed:

modified: server/class/DB.js
modified: test/mocha/DB.test.js

Change Summary

Incremental Difference

--- a/server/class/DB.js
+++ b/server/class/DB.js
@@ -154,19 +154,26 @@ function DB() {
154154 */
155155 function existFile(fileName, parentDirectory) {
156156 return new Promise(function(resolve, reject) {
157- var q = {
157+ existDirectory(parentDirectory)
158+ .catch(function(error) {
159+ reject(error);
160+ })
161+ .then(function(fileId) {
162+ var q = {
158163 where: {
159164 name: fileName,
160- parentDirectory: parentDirectory,
165+ parentId: fileId,
161166 fileType: 1
162- }
163- };
164- Files.findOne(q)
167+ }
168+ };
169+ return Files.findOne(q)
170+ })
165171 .then(function(r) {
166172 if(r === null) {
167173 reject(new Error("\"" + fileName + "\" does not exist in " + '"' + parentDirectory + "\" directory."));
168174 } else {
169- resolve();
175+ console.log(r.fileId);
176+ resolve(r.fileId);
170177 }
171178 });
172179 });
@@ -182,14 +189,20 @@ function DB() {
182189 */
183190 function notExistFile(fileName, parentDirectory) {
184191 return new Promise(function(resolve, reject) {
185- var q = {
192+ existDirectory(parentDirectory)
193+ .catch(function(error) {
194+ reject(error);
195+ })
196+ .then(function(fileId) {
197+ var q = {
186198 where: {
187199 name: fileName,
188- parentDirectory: parentDirectory,
200+ parentId: fileId,
189201 fileType: 1
190- }
191- };
192- Files.findOne(q)
202+ }
203+ };
204+ return Files.findOne(q)
205+ })
193206 .then(function(r) {
194207 if(r === null) {
195208 resolve();
@@ -204,13 +217,18 @@ function DB() {
204217 * existDirectory
205218 * ディレクトリが存在することを確認する
206219 * @param {string} directory
207- * @returns {promise} ディレクトリが存在すればresolve、存在しなければreject
220+ * @returns {promise} ディレクトリが存在すればresolve{fileId)、存在しなければreject
208221 */
209222 function existDirectory(directory) {
210223 return new Promise(function(resolve, reject) {
211- var arrayDirectory = directory.split('/');
212- arrayDirectory.shift(); // root
213- arrayDirectory.unshift('/');
224+ var arrayDirectory;
225+ if(directory === '/') {
226+ resolve('1f83f620-c1ed-11e5-9657-7942989daa00');
227+ } else {
228+ arrayDirectory = directory.split('/');
229+ arrayDirectory.shift(); // root
230+ arrayDirectory.unshift('/');
231+ }
214232
215233 var directoriesPromise = arrayDirectory.map(function(name) {
216234 return getDirectory(name);
@@ -227,7 +245,7 @@ function DB() {
227245 reject(new Error('"' + directory + '" directory doesn\'t exist.'));
228246 }
229247 }
230- resolve(directory);
248+ resolve(parentId);
231249 })
232250 .catch(function(error) {
233251 reject(new Error('"' + directory + '" directory doesn\'t exist.'));
@@ -243,19 +261,34 @@ function DB() {
243261 */
244262 function notExistDirectory(directory) {
245263 return new Promise(function(resolve, reject) {
246- var q = {
247- where: {
248- name: directory,
249- fileType: 0
250- }
251- };
252- Files.findOne(q)
264+ var arrayDirectory;
265+ if(directory === '/') {
266+ reject(new Error('"' + directory + '" directory exists.'));
267+ } else {
268+ arrayDirectory = directory.split('/');
269+ arrayDirectory.shift(); // root
270+ arrayDirectory.unshift('/');
271+ }
272+
273+ var directoriesPromise = arrayDirectory.map(function(name) {
274+ return getDirectory(name);
275+ });
276+ Promise.all(directoriesPromise)
253277 .then(function(r) {
254- if(r === null) {
255- resolve();
256- } else {
257- reject(new Error('"' + directory + '" directory exists.'));
278+ var parentId = r[0][0].fileId;
279+ var index;
280+ for(var i=1;i<r.length;i++) {
281+ index = r[i].map(function(c) { return c.parentId }).indexOf(parentId);
282+ if(index > -1) {
283+ parentId = r[i][index].fileId;
284+ } else {
285+ resolve();
286+ }
258287 }
288+ reject(new Error('"' + directory + '" directory exists.'));
289+ })
290+ .catch(function(error) {
291+ resolve();
259292 });
260293 });
261294 }
@@ -269,15 +302,16 @@ function DB() {
269302 */
270303 function createFile(fileName,parentDirectory) {
271304 return new Promise(function(resolve, reject) {
272- Promise.all([notExistFile(fileName, parentDirectory), existDirectory(parentDirectory)])
305+ Promise.all([existDirectory(parentDirectory), notExistFile(fileName, parentDirectory) ])
273306 .catch(function(error) {
274307 reject(error);
275308 })
276- .then(function() {
309+ .then(function(r) {
310+ var parentId = r[0]
277311 var q = {
278312 fileId: uuid.v1(),
279313 name: fileName,
280- parentDirectory: parentDirectory,
314+ parentId: parentId,
281315 fileType: 1
282316 }
283317 return Files.create(q)
@@ -302,11 +336,10 @@ function DB() {
302336 .catch(function(error) {
303337 reject(error);
304338 })
305- .then(function() {
339+ .then(function(fileId) {
306340 var q = {
307341 where: {
308- name: fileName,
309- parentDirectory: parentDirectory
342+ fileId: fileId
310343 }
311344 };
312345 return Files.destroy(q);
@@ -392,6 +425,15 @@ function DB() {
392425 fileType: 0
393426 };
394427 return Files.create(q3);
428+ })
429+ .then(function() {
430+ var q4 = {
431+ fileId: uuid.v1(),
432+ name: 'hogehoge.txt',
433+ parentId: q1.fileId,
434+ fileType: 1
435+ };
436+ return Files.create(q4);
395437 });
396438 }
397439
--- a/test/mocha/DB.test.js
+++ b/test/mocha/DB.test.js
@@ -155,13 +155,21 @@
155155 .then(function() {
156156 return db.test1();
157157 })
158+ .then(function() {
159+ return db.test2();
160+ });
158161 });
159162
160- /*
161- it('should be rejected when does not exist same name file in a directory', function() {
163+ it('should be rejected when does not exist same name file in a directory', function() {
162164 return db.existFile('hogehoge.txt', '/').should.be.rejectedWith(Error, '"hogehoge.txt" does not exist in "/" directory.');
163- });
164- */
165+ });
166+ it('should be rejected when a directory does not exist', function() {
167+ return db.existFile('hogehoge.txt', '/hoge').should.be.rejectedWith(Error, '"/hoge" directory doesn\'t exist.');
168+ });
169+ it('should be resolved when file exists in a directory', function() {
170+ return expect(db.existFile('hogehoge.txt', '/one')).to.eventually.be.a('string');
171+ });
172+
165173 });
166174
167175
@@ -185,17 +193,22 @@
185193 .then(function() {
186194 return db.test1();
187195 })
196+ .then(function() {
197+ return db.test2();
198+ });
188199 });
189200
190-
191- /*
192- it('should be rejected when exist the same name file in a directory', function() {
201+ it('should be rejected when the same name file has already existed in directory(1)', function() {
193202 return db.notExistFile('hoge.txt', '/').should.be.rejectedWith(Error, '"hoge.txt" has already existed in "/" directory.');
194- });
195- */
196- });
197-
203+ });
204+ it('should be rejected when the same name file has already existed in directory(2)', function() {
205+ return db.notExistFile('hogehoge.txt', '/one').should.be.rejectedWith(Error, '"hogehoge.txt" has already existed in "/one" directory.');
206+ });
207+ it('should be resolved when the same name file does not exist in directory', function() {
208+ return expect(db.notExistFile('hoge.txt', '/one')).to.eventually.equal();
209+ });
198210
211+ });
199212
200213 var existDirectory = `
201214 /**
@@ -217,25 +230,18 @@
217230 })
218231 });
219232
220- it('should be resolved when a directory exists.', function() {
221- return expect(db.existDirectory('/one/two')).to.eventually.equal('/one/two');
222- });
223233 it('should be rejected when does not exist directory', function() {
224- return db.existDirectory('/one/two/four').should.be.rejectedWith(Error, '"/one/two/four" directory doesn\'t exist.');
234+ return db.existDirectory('/one/two/four').should.be.rejectedWith(Error, '"/one/two/four" directory doesn\'t exist.');
235+ });
236+ it('should be resolved when exists directory(1)', function() {
237+ return expect(db.existDirectory('/')).to.eventually.equal('1f83f620-c1ed-11e5-9657-7942989daa00');
238+ });
239+ it('should be resolved when exists directory(2)', function() {
240+ return expect(db.existDirectory('/one')).to.eventually.be.a('string');
241+ });
242+ it('should be resolved when exists directory(3)', function() {
243+ return expect(db.existDirectory('/one/two')).to.eventually.be.a('string');
225244 });
226-
227- /*
228- it('should be resolveed when exists directory(1)', function() {
229- return expect(db.existDirectory('/')).to.eventually.equal('/');
230- });
231- it('should be resolveed when exists directory(2)', function() {
232- return db.existDirectory('/one').should.be.rejectedWith(Error, '"hage" directory doesn\'t exist.');
233- });
234- it('should be resolveed when exists directory(3)', function() {
235- return db.existDirectory('/one/two').should.be.rejectedWith(Error, '"hage" directory doesn\'t exist.');
236- });
237- */
238-
239245 });
240246
241247
@@ -257,13 +263,21 @@
257263 .then(function() {
258264 return db.test1();
259265 })
266+ .then(function() {
267+ return db.test2();
268+ });
260269 });
261270
262- /*
263- it('should be rejected when exists directory', function() {
271+ it('should be rejected when exists directory(1)', function() {
264272 return db.notExistDirectory('/').should.be.rejectedWith(Error,'"/" directory exists.');
265- });
266- */
273+ });
274+ it('should be rejected when exists directory(2)', function() {
275+ return db.notExistDirectory('/one').should.be.rejectedWith(Error,'"/one" directory exists.');
276+ });
277+ it('should be rejected when exists directory(3)', function() {
278+ return db.notExistDirectory('/one/two').should.be.rejectedWith(Error,'"/one/two" directory exists.');
279+ });
280+
267281 });
268282
269283 var createFile = `
@@ -285,16 +299,20 @@
285299 .then(function() {
286300 return db.test1();
287301 })
302+ .then(function() {
303+ return db.createFile('tarou.txt', '/');
304+ });
288305 });
289306
290- /*
291- it('should be rejected when "parentDirectory" doesn\'t exist', function() {
292- return db.createFile('hoge.txt', 'hoge').should.be.rejectedWith(Error, '"hoge" directory doesn\'t exist.');
293- });
294- it('should be rejected when a file has already existed in a directory.', function() {
307+ it('should be rejected when "parentDirectory" doesn\'t exist', function() {
308+ return db.createFile('hoge.txt', '/hoge').should.be.rejectedWith(Error, '"/hoge" directory doesn\'t exist.');
309+ });
310+ it('should be rejected when a file has already existed in a directory.', function() {
295311 return db.createFile('hoge.txt', '/').should.be.rejectedWith(Error, '"hoge.txt" has already existed in "/" directory.');
296- });
297- */
312+ });
313+ it('should be resolved when file creation successed.', function() {
314+ return expect(db.existFile('tarou.txt', '/')).to.eventually.be.a('string');
315+ });
298316 });
299317
300318
@@ -321,15 +339,13 @@
321339 return db.test1();
322340 });
323341 });
324- /*
325- it('should be resolved when removeFile() successed', function() {
342+ it('should be resolved when removeFile() successed', function() {
326343 return db.removeFile('hoge.txt', '/')
327344 .then(function() {
328345 return db.existFile('hoge.txt', '/')
329346 })
330347 .should.be.rejectedWith(Error, '"hoge.txt" does not exist in "/" directory.');
331- });
332- */
348+ });
333349 });
334350 });
335351 })();
Show on old repository browser