Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- validate: {
- validDirectoryString () {
- // Required here - because it's not defined on init (Framework limitations)
- const Directory = require('../models').Directory
- const bookmark = this
- if (!this.directory_id && (!this.directory || (this.directory === '/'))) {
- // It belongs to root directory, and is not a problem
- return true
- }
- if (this.directory_id) {
- return Directory.findOne({where: {id: this.directory_id}}).then(dir => {
- if (!dir) throw new Error('Invalid directory ID - directory could not be found.')
- bookmark.directory = dir.directory
- })
- }
- let dirList = directoryMixin.parseDirList(this.directory)
- // Recursive promise magic - each element of the directory structure gets
- // thrown onto the promise chain, until the whole structure is built
- function recursiveDirectoryCreation (dirList, index, parentId) {
- function _recursePromise (newDir) {
- let parent = newDir ? newDir.id : null
- let directoryName = '/' + dirList.slice(0, index + 1).join('/') + '/'
- if (dirList.length === (index + 1)) {
- return Directory.create({directory: directoryName, parent: parent, user_id: bookmark.user_id})
- }
- index += 1
- return Directory.create({
- directory: directoryName,
- parent: parent,
- user_id: bookmark.user_id
- }).then(_recursePromise)
- }
- return _recursePromise(null)
- }
- return Directory.find({where: {
- user_id: bookmark.user_id,
- directory: bookmark.directory
- }}).then((matchedDir) => {
- if (!matchedDir) {
- return recursiveDirectoryCreation(dirList, 0, null).then((dir) => {
- bookmark.directory_id = dir.id
- })
- }
- bookmark.directory_id = matchedDir.id
- })
- }
- },
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement