Skip to content

Commit 2f45f4b

Browse files
committed
Implemented the COPY method
1 parent 5115994 commit 2f45f4b

File tree

5 files changed

+455
-0
lines changed

5 files changed

+455
-0
lines changed

lib/server/commands/Commands.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var Propfind_1 = require("./Propfind");
66
var Options_1 = require("./Options");
77
var Delete_1 = require("./Delete");
88
var Mkcol_1 = require("./Mkcol");
9+
var Copy_1 = require("./Copy");
910
var Move_1 = require("./Move");
1011
var Head_1 = require("./Head");
1112
var Post_1 = require("./Post");
@@ -18,6 +19,7 @@ exports.default = {
1819
Options: Options_1.default,
1920
Delete: Delete_1.default,
2021
Mkcol: Mkcol_1.default,
22+
Copy: Copy_1.default,
2123
Move: Move_1.default,
2224
Head: Head_1.default,
2325
Post: Post_1.default,

lib/server/commands/Copy.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import { MethodCallArgs } from '../WebDAVRequest';
2+
export default function (arg: MethodCallArgs, callback: any): void;

lib/server/commands/Copy.js

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
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;

src/server/commands/Commands.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import Propfind from './Propfind'
44
import Options from './Options'
55
import Delete from './Delete'
66
import Mkcol from './Mkcol'
7+
import Copy from './Copy'
78
import Move from './Move'
89
import Head from './Head'
910
import Post from './Post'
@@ -17,6 +18,7 @@ export default {
1718
Options,
1819
Delete,
1920
Mkcol,
21+
Copy,
2022
Move,
2123
Head,
2224
Post,

0 commit comments

Comments
 (0)