|
| 1 | +"use strict"; |
| 2 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 3 | +var WebDAVRequest_1 = require("../WebDAVRequest"); |
| 4 | +var FSManager_1 = require("../../manager/FSManager"); |
| 5 | +function copyAllProperties(source, destination, callback) { |
| 6 | + source.getProperties(function (e, props) { |
| 7 | + if (e) { |
| 8 | + callback(e); |
| 9 | + return; |
| 10 | + } |
| 11 | + var nb = Object.keys(props).length; |
| 12 | + function go(error) { |
| 13 | + if (nb <= 0) |
| 14 | + return; |
| 15 | + if (error) { |
| 16 | + nb = -1; |
| 17 | + callback(error); |
| 18 | + return; |
| 19 | + } |
| 20 | + --nb; |
| 21 | + if (nb === 0) |
| 22 | + callback(null); |
| 23 | + } |
| 24 | + if (nb === 0) { |
| 25 | + callback(null); |
| 26 | + return; |
| 27 | + } |
| 28 | + for (var name_1 in props) { |
| 29 | + if (nb <= 0) |
| 30 | + break; |
| 31 | + destination.setProperty(name_1, JSON.parse(JSON.stringify(props[name_1])), go); |
| 32 | + } |
| 33 | + }); |
| 34 | +} |
| 35 | +function copy(source, rDest, destination, callback) { |
| 36 | + function _(error, cb) { |
| 37 | + if (error) |
| 38 | + callback(error); |
| 39 | + else |
| 40 | + cb(); |
| 41 | + } |
| 42 | + source.type(function (e, type) { return _(e, function () { |
| 43 | + var dest = rDest.fsManager.newResource(destination.toString(), destination.fileName(), type, rDest); |
| 44 | + dest.create(function (e) { return _(e, function () { |
| 45 | + rDest.addChild(dest, function (e) { return _(e, function () { |
| 46 | + copyAllProperties(source, dest, function (e) { return _(e, function () { |
| 47 | + if (!type.isFile) { |
| 48 | + next(); |
| 49 | + return; |
| 50 | + } |
| 51 | + source.read(function (e, data) { return _(e, function () { |
| 52 | + dest.write(data, function (e) { return _(e, next); }); |
| 53 | + }); }); |
| 54 | + function next() { |
| 55 | + if (!type.isDirectory) { |
| 56 | + callback(null); |
| 57 | + return; |
| 58 | + } |
| 59 | + source.getChildren(function (e, children) { return _(e, function () { |
| 60 | + var nb = children.length; |
| 61 | + function done(error) { |
| 62 | + if (nb <= 0) |
| 63 | + return; |
| 64 | + if (error) { |
| 65 | + nb = -1; |
| 66 | + callback(e); |
| 67 | + return; |
| 68 | + } |
| 69 | + --nb; |
| 70 | + if (nb === 0) |
| 71 | + callback(null); |
| 72 | + } |
| 73 | + if (nb === 0) { |
| 74 | + callback(null); |
| 75 | + return; |
| 76 | + } |
| 77 | + children.forEach(function (child) { |
| 78 | + child.webName(function (e, name) { |
| 79 | + if (e) |
| 80 | + done(e); |
| 81 | + else |
| 82 | + copy(child, dest, destination.getChildPath(name), done); |
| 83 | + }); |
| 84 | + }); |
| 85 | + }); }); |
| 86 | + } |
| 87 | + }); }); |
| 88 | + }); }); |
| 89 | + }); }); |
| 90 | + }); }); |
| 91 | +} |
| 92 | +function default_1(arg, callback) { |
| 93 | + arg.getResource(function (e, source) { |
| 94 | + if (e) { |
| 95 | + arg.setCode(WebDAVRequest_1.HTTPCodes.NotFound); |
| 96 | + callback(); |
| 97 | + return; |
| 98 | + } |
| 99 | + var overwrite = arg.findHeader('overwrite') !== 'F'; |
| 100 | + var destination = arg.findHeader('destination'); |
| 101 | + if (!destination) { |
| 102 | + arg.setCode(WebDAVRequest_1.HTTPCodes.BadRequest); |
| 103 | + callback(); |
| 104 | + return; |
| 105 | + } |
| 106 | + destination = destination.substring(destination.indexOf('://') + '://'.length); |
| 107 | + destination = destination.substring(destination.indexOf('/')); |
| 108 | + destination = new FSManager_1.FSPath(destination); |
| 109 | + arg.server.getResourceFromPath(destination.getParent(), function (e, rDest) { |
| 110 | + if (e) { |
| 111 | + arg.setCode(WebDAVRequest_1.HTTPCodes.InternalServerError); |
| 112 | + callback(); |
| 113 | + return; |
| 114 | + } |
| 115 | + source.type(function (e, type) { |
| 116 | + if (e) { |
| 117 | + arg.setCode(WebDAVRequest_1.HTTPCodes.InternalServerError); |
| 118 | + callback(); |
| 119 | + return; |
| 120 | + } |
| 121 | + function done(overridded) { |
| 122 | + copy(source, rDest, destination, function (e) { |
| 123 | + if (e) |
| 124 | + arg.setCode(WebDAVRequest_1.HTTPCodes.InternalServerError); |
| 125 | + else if (overridded) |
| 126 | + arg.setCode(WebDAVRequest_1.HTTPCodes.NoContent); |
| 127 | + else |
| 128 | + arg.setCode(WebDAVRequest_1.HTTPCodes.Created); |
| 129 | + callback(); |
| 130 | + }); |
| 131 | + } |
| 132 | + var nb = 0; |
| 133 | + function go(error, destCollision) { |
| 134 | + if (nb <= 0) |
| 135 | + return; |
| 136 | + if (error) { |
| 137 | + nb = -1; |
| 138 | + arg.setCode(WebDAVRequest_1.HTTPCodes.InternalServerError); |
| 139 | + callback(); |
| 140 | + return; |
| 141 | + } |
| 142 | + if (destCollision) { |
| 143 | + nb = -1; |
| 144 | + if (!overwrite) { |
| 145 | + arg.setCode(WebDAVRequest_1.HTTPCodes.InternalServerError); |
| 146 | + callback(); |
| 147 | + return; |
| 148 | + } |
| 149 | + destCollision.type(function (e, destType) { |
| 150 | + if (e) { |
| 151 | + callback(e); |
| 152 | + return; |
| 153 | + } |
| 154 | + if (destType !== type) { |
| 155 | + arg.setCode(WebDAVRequest_1.HTTPCodes.InternalServerError); |
| 156 | + callback(); |
| 157 | + return; |
| 158 | + } |
| 159 | + destCollision.delete(function (e) { |
| 160 | + if (e) { |
| 161 | + callback(e); |
| 162 | + return; |
| 163 | + } |
| 164 | + done(true); |
| 165 | + }); |
| 166 | + }); |
| 167 | + return; |
| 168 | + } |
| 169 | + --nb; |
| 170 | + if (nb === 0) { |
| 171 | + done(false); |
| 172 | + } |
| 173 | + } |
| 174 | + rDest.getChildren(function (e, children) { |
| 175 | + if (e) { |
| 176 | + go(e, null); |
| 177 | + return; |
| 178 | + } |
| 179 | + nb += children.length; |
| 180 | + if (nb === 0) { |
| 181 | + done(false); |
| 182 | + return; |
| 183 | + } |
| 184 | + children.forEach(function (child) { |
| 185 | + child.webName(function (e, name) { |
| 186 | + go(e, name === destination.fileName() ? child : null); |
| 187 | + }); |
| 188 | + }); |
| 189 | + }); |
| 190 | + }); |
| 191 | + }); |
| 192 | + }); |
| 193 | +} |
| 194 | +exports.default = default_1; |
0 commit comments