-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbaserow-file.js
102 lines (94 loc) · 2.98 KB
/
baserow-file.js
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
module.exports = function (RED) {
function getJWT(username, password, url) {
const axios = require("axios");
return axios
.post(`${url}/api/user/token-auth/`, {
username: username,
password: password,
})
.then((res) => {
return res.data;
});
}
function baserowFileNode(n) {
RED.nodes.createNode(this, n);
if (RED.nodes.getNode(n.creds)) {
this.username = RED.nodes.getNode(n.creds).credentials.username;
this.password = RED.nodes.getNode(n.creds).credentials.password;
this.url = RED.nodes.getNode(n.creds).credentials.url;
} else {
this.username = "";
this.password = "";
this.url = "";
}
var node = this;
this.name = n.name;
this.tableId = n.tableId;
this.userFieldNames = n.userFieldNames;
this.operation = n.operation;
this.rowId = n.rowId;
this.beforeId = n.beforeId;
this.baserowConfig = RED.nodes.getNode(n.baserow);
this.on("input", async (msg, send, done) => {
const operation =
msg.operation !== undefined ? msg.operation : this.operation;
const tableId = msg.tableId !== undefined ? msg.tableId : this.tableId;
const rowId = msg.rowId !== undefined ? msg.rowId : this.rowId;
const userFieldNames =
msg.userFieldNames !== undefined
? msg.userFieldNames
: this.userFieldNames;
const beforeId =
msg.beforeId !== undefined ? msg.beforeId : this.beforeId;
getJWT(this.username, this.password, this.url)
.then((token) => {
const axios = require("axios");
switch (operation) {
case "upload":
axios
.post(`${this.url}/api/user-files/upload-file/`, msg.payload, {
headers: {
Authorization: `JWT ${token.access_token}`,
},
})
.then((res) => {
msg.payload = res.data;
send(msg);
})
.catch((err) => {
msg.payload = err;
done(msg);
});
break;
case "upload_via_url":
axios
.post(
`${this.url}/api/user-files/upload-via-url/`,
msg.payload,
{
headers: {
Authorization: `JWT ${token.access_token}`,
},
},
)
.then((res) => {
msg.payload = res.data;
send(msg);
})
.catch((err) => {
msg.payload = err;
done(msg);
});
break;
default:
done(`Invalid operation '${operation}'`);
}
})
.catch((err) => {
msg.payload = err;
done(msg);
});
});
}
RED.nodes.registerType("baserow-file", baserowFileNode);
};