|
| 1 | +"use strict"; |
| 2 | +const webdav = require('../../../lib/index.js'), |
| 3 | + PhysicalGFSManager = require('./PhysicalGFSManager.js').PhysicalGFSManager, |
| 4 | + path = require('path'), |
| 5 | + fs = require('fs'); |
| 6 | + |
| 7 | +module.exports.PhysicalGateway = function(rootPath, customName, parent, fsManager) |
| 8 | +{ |
| 9 | + const gateway = new webdav.PhysicalFolder(rootPath, parent, fsManager ? fsManager : new PhysicalGFSManager()); |
| 10 | + gateway.constructor = module.exports.PhysicalGateway; |
| 11 | + |
| 12 | + gateway.customName = customName; |
| 13 | + gateway.rootPath = rootPath ? rootPath : '/'; |
| 14 | + gateway.cache = { |
| 15 | + '/': gateway |
| 16 | + }; |
| 17 | + |
| 18 | + const super_webName = gateway.webName; |
| 19 | + gateway.webName = function(callback) |
| 20 | + { |
| 21 | + if(this.customName) |
| 22 | + callback(null, this.customName); |
| 23 | + else |
| 24 | + super_webName(callback); |
| 25 | + } |
| 26 | + |
| 27 | + gateway.listChildren = function(parent, rpath, callback) |
| 28 | + { |
| 29 | + if(rpath.lastIndexOf('/') !== rpath.length - 1) |
| 30 | + rpath += '/'; |
| 31 | + |
| 32 | + fs.readdir(parent.realPath, (e, list) => { |
| 33 | + if(e) |
| 34 | + { |
| 35 | + callback(e); |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + new webdav.Workflow() |
| 40 | + .each(list, (file, cb) => { |
| 41 | + const resourcePath = rpath + file; |
| 42 | + let resource = this.cache[resourcePath]; |
| 43 | + const realPath = path.join(parent.realPath, file); |
| 44 | + |
| 45 | + if(resource) |
| 46 | + { |
| 47 | + cb(null, resource); |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + fs.stat(realPath, (e, stat) => { |
| 52 | + if(e) |
| 53 | + { |
| 54 | + cb(e); |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + if(stat.isFile()) |
| 59 | + resource = new webdav.PhysicalFile(realPath, parent, this.fsManager); |
| 60 | + else |
| 61 | + resource = new webdav.PhysicalFolder(realPath, parent, this.fsManager); |
| 62 | + resource.deleteOnMoved = true; |
| 63 | + this.cache[resourcePath] = resource; |
| 64 | + cb(null, resource); |
| 65 | + }) |
| 66 | + }) |
| 67 | + .error(callback) |
| 68 | + .done((resources) => callback(null, resources)); |
| 69 | + }) |
| 70 | + } |
| 71 | + |
| 72 | + gateway.find = function(path, callback, forceRefresh = false) |
| 73 | + { |
| 74 | + const resource = this.cache[path.toString()]; |
| 75 | + if(forceRefresh || !resource) |
| 76 | + { |
| 77 | + const parentPath = path.getParent(); |
| 78 | + this.find(parentPath, (e, parent) => { |
| 79 | + if(e) |
| 80 | + { |
| 81 | + callback(e); |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + parent.getChildren((e, actualChildren) => { |
| 86 | + if(e) |
| 87 | + { |
| 88 | + callback(e); |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + this.listChildren(parent, parentPath.toString(), (e, children) => { |
| 93 | + if(e) |
| 94 | + { |
| 95 | + callback(e); |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + actualChildren |
| 100 | + .filter((c) => c.constructor !== webdav.PhysicalResource && c.constructor !== webdav.PhysicalFile && c.constructor !== webdav.PhysicalFolder) |
| 101 | + .forEach((c) => children.push(c)); |
| 102 | + |
| 103 | + parent.children.children = children; |
| 104 | + |
| 105 | + new webdav.Workflow() |
| 106 | + .each(children, (child, cb) => { |
| 107 | + child.webName((e, name) => { |
| 108 | + cb(e, !e && name === path.fileName() ? child : null); |
| 109 | + }) |
| 110 | + }) |
| 111 | + .error(callback) |
| 112 | + .done((matchingChildren) => { |
| 113 | + for(const child of matchingChildren) |
| 114 | + if(child) |
| 115 | + { |
| 116 | + callback(null, child); |
| 117 | + return; |
| 118 | + } |
| 119 | + |
| 120 | + callback(webdav.Errors.ResourceNotFound); |
| 121 | + }) |
| 122 | + }) |
| 123 | + }) |
| 124 | + }) |
| 125 | + } |
| 126 | + else |
| 127 | + callback(null, resource); |
| 128 | + } |
| 129 | + |
| 130 | + gateway.gateway = function(arg, path, callback) |
| 131 | + { |
| 132 | + const updateChildren = (r, cb) => |
| 133 | + { |
| 134 | + this.listChildren(r, path.toString(), (e, children) => { |
| 135 | + if(!e) |
| 136 | + { |
| 137 | + r.children.children |
| 138 | + .filter((c) => c.constructor !== webdav.PhysicalResource && c.constructor !== webdav.PhysicalFile && c.constructor !== webdav.PhysicalFolder) |
| 139 | + .forEach((c) => children.push(c)); |
| 140 | + |
| 141 | + r.children.children = children; |
| 142 | + } |
| 143 | + cb(e); |
| 144 | + }) |
| 145 | + } |
| 146 | + |
| 147 | + if(path.isRoot()) |
| 148 | + { |
| 149 | + updateChildren(this, (e) => { |
| 150 | + callback(e, this); |
| 151 | + }) |
| 152 | + return; |
| 153 | + } |
| 154 | + |
| 155 | + this.find(path, (e, r) => { |
| 156 | + if(e) |
| 157 | + { |
| 158 | + callback(e); |
| 159 | + return; |
| 160 | + } |
| 161 | + |
| 162 | + r.type((e, type) => { |
| 163 | + if(e) |
| 164 | + { |
| 165 | + callback(e); |
| 166 | + return; |
| 167 | + } |
| 168 | + |
| 169 | + if(type.isFile) |
| 170 | + { |
| 171 | + callback(e, r); |
| 172 | + return; |
| 173 | + } |
| 174 | + |
| 175 | + updateChildren(r, (e) => { |
| 176 | + callback(e, r); |
| 177 | + }) |
| 178 | + }) |
| 179 | + }); |
| 180 | + } |
| 181 | + |
| 182 | + return gateway; |
| 183 | +} |
0 commit comments