This repository has been archived by the owner on Apr 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
PostRequestScriptEvent.java
107 lines (96 loc) · 3.92 KB
/
PostRequestScriptEvent.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
package space.morphanone.webizen.events;
import com.sun.net.httpserver.HttpExchange;
import com.denizenscript.denizen.utilities.DenizenAPI;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import space.morphanone.webizen.server.RequestWrapper;
import java.io.*;
public class PostRequestScriptEvent extends BasicRequestScriptEvent {
// <--[event]
// @Events
// post request
//
// @Regex ^on post request$
//
// @Triggers when the web server receives a POST request
//
// @Context
// <context.address> Returns the IP address of the device that sent the request.
// <context.request> Returns the path that was requested
// <context.query> Returns a ListTag of the query included with the request
// <context.user_info> Returns info about the authenticated user sending the request, if any.
// <context.upload_name> returns the name of the file posted.
// <context.upload_size_mb> returns the size of the upload in MegaBytes (where 1 MegaByte = 1 000 000 Bytes).
//
// @Determine
// ElementTag to set the content of the response directly
// "FILE:" + ElementTag to set the file for the response via a file path
// "PARSED_FILE:" + ElementTag to set the parsed file for the response via a file path, this will parse any denizen tags inside the file
// "CODE:" + ElementTag to set the HTTP status code of the response (e.g. 200)
// "TYPE:" + ElementTag to set the MIME (multi purpose mail extension) of the response (e.g. text/html)
// "SAVE_UPLOAD:" + ElementTag to save the upload to a file.
//
// @Plugin Webizen
// -->
public PostRequestScriptEvent() {
instance = this;
}
public static PostRequestScriptEvent instance;
public ElementTag saveUpload;
public byte[] requestBody;
public ElementTag fileName;
@Override
public String getRequestType() {
return "Post";
}
@Override
public void fire(HttpExchange httpExchange) {
try {
RequestWrapper request = new RequestWrapper(httpExchange);
this.requestBody = request.getFile();
this.fileName = new ElementTag(request.getFileName());
} catch (Exception e) {
Debug.echoError(e);
}
this.saveUpload = null;
super.fire(httpExchange);
if (this.saveUpload != null) {
try {
File file = new File(DenizenAPI.getCurrentInstance().getDataFolder(), saveUpload.asString());
if (!Utilities.canWriteToFile(file)) {
Debug.echoError("Save failed: cannot save there!");
return;
}
file.getParentFile().mkdirs();
FileOutputStream outputStream = new FileOutputStream(file);
outputStream.write(this.requestBody);
outputStream.flush();
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
@Override
public boolean applyDetermination(ScriptPath path, ObjectTag determinationObj) {
String lower = CoreUtilities.toLowerCase(determinationObj.toString());
if (lower.startsWith("save_upload:")) {
saveUpload = new ElementTag(determinationObj.toString().substring(12));
return true;
}
return super.applyDetermination(path, determinationObj);
}
@Override
public ObjectTag getContext(String name) {
if (name.equals("upload_name")) {
return fileName;
}
else if (name.equals("upload_size_mb")) {
return new ElementTag(requestBody.length/(1000*1000));
}
return super.getContext(name);
}
}