/
FileCopyCommand.java
118 lines (99 loc) · 4.44 KB
/
FileCopyCommand.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package net.aufdemrand.denizen.scripts.commands.core;
import net.aufdemrand.denizen.Settings;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizen.utilities.Utilities;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizencore.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizencore.objects.Element;
import net.aufdemrand.denizencore.objects.aH;
import net.aufdemrand.denizencore.scripts.ScriptEntry;
import net.aufdemrand.denizencore.scripts.commands.AbstractCommand;
import java.io.File;
import java.nio.file.Files;
public class FileCopyCommand extends AbstractCommand {
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {
if (!scriptEntry.hasObject("origin")
&& arg.matchesPrefix("origin", "o")) {
scriptEntry.addObject("origin", arg.asElement());
}
else if (!scriptEntry.hasObject("destination")
&& arg.matchesPrefix("destination", "d")) {
scriptEntry.addObject("destination", arg.asElement());
}
else if (!scriptEntry.hasObject("overwrite")
&& arg.matches("overwrite")) {
scriptEntry.addObject("overwrite", new Element("true"));
}
else {
arg.reportUnhandled();
}
}
if (!scriptEntry.hasObject("origin")) {
throw new InvalidArgumentsException("Must have a valid origin!");
}
if (!scriptEntry.hasObject("destination")) {
throw new InvalidArgumentsException("Must have a valid destination!");
}
scriptEntry.defaultObject("overwrite", new Element("false"));
}
@Override
public void execute(final ScriptEntry scriptEntry) {
Element origin = scriptEntry.getElement("origin");
Element destination = scriptEntry.getElement("destination");
Element overwrite = scriptEntry.getElement("overwrite");
if (scriptEntry.dbCallShouldDebug()) {
dB.report(scriptEntry, getName(), origin.debug() + destination.debug() + overwrite.debug());
}
if (!Settings.allowFilecopy()) {
dB.echoError(scriptEntry.getResidingQueue(), "File copy disabled by server administrator.");
scriptEntry.addObject("success", new Element("false"));
return;
}
File o = new File(DenizenAPI.getCurrentInstance().getDataFolder(), origin.asString());
File d = new File(DenizenAPI.getCurrentInstance().getDataFolder(), destination.asString());
boolean ow = overwrite.asBoolean();
boolean dexists = d.exists();
boolean disdir = d.isDirectory() || destination.asString().endsWith("/");
if (!Utilities.canReadFile(o)) {
dB.echoError("Server config denies reading files in that location.");
return;
}
if (!o.exists()) {
dB.echoError(scriptEntry.getResidingQueue(), "File copy failed, origin does not exist!");
scriptEntry.addObject("success", new Element("false"));
return;
}
if (!Utilities.isSafeFile(d)) {
dB.echoError(scriptEntry.getResidingQueue(), "Can't copy files to there!");
scriptEntry.addObject("success", new Element("false"));
return;
}
if (dexists && !disdir && !ow) {
dB.echoDebug(scriptEntry, "File copy ignored, destination file already exists!");
scriptEntry.addObject("success", new Element("false"));
return;
}
try {
if (dexists && !disdir) {
d.delete();
}
if (disdir && !dexists) {
d.mkdirs();
}
if (o.isDirectory()) {
Utilities.copyDirectory(o, d);
}
else {
Files.copy(o.toPath(), (disdir ? d.toPath().resolve(o.toPath().getFileName()) : d.toPath()));
}
scriptEntry.addObject("success", new Element("true"));
}
catch (Exception e) {
dB.echoError(scriptEntry.getResidingQueue(), e);
scriptEntry.addObject("success", new Element("false"));
return;
}
}
}