-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcontent.js
More file actions
188 lines (172 loc) · 8.35 KB
/
content.js
File metadata and controls
188 lines (172 loc) · 8.35 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
'use strict';
const fs = require('fs'),
configs = require('../configurations'),
helpers = require('../helpers'),
request = require('superagent'),
express = require('express'),
router = express.Router();
module.exports = function (appContext) {
/* GET /uploadhtmlcontent
* Uploads an HTML document to a module within the given course. The OrgUnitId and ModuleId are parameters
* passed in through the query parameters.
*/
router.get('/uploadhtmlcontent', function (req, res) {
const orgUnitId = req.query.orgUnitId;
const moduleId = req.query.moduleId;
const apiPath = '/d2l/api/le/1.22/' + orgUnitId + '/content/modules/' + moduleId + '/structure/';
const accessToken = req.cookies[configs.cookieName].accessToken;
const boundary = 'xxBOUNDARYxx';
const topicData = {
Title: 'Sample HTML Content',
ShortTitle: null,
Type: 1,
TopicType: 1,
Url: '/content/enforced/6952-ES100/sample-content.html',
StartDate: null,
EndDate: null,
DueDate: null,
IsHidden: false,
IsLocked: false,
OpenAsExternalResource: null,
Description: null,
MajorUpdate: null,
MajorUpdateText: null,
ResetCompletionTracking: null
};
const body = buildMultipartBody(boundary, topicData, './content/file-upload/sample-content.html', 'sample-content.html', 'text/html', 'utf8');
if (accessToken) {
console.log('Attempting to upload content using OAuth 2.0 authentication.');
const contentRoute = helpers.createUrl(apiPath, configs);
request
.post(contentRoute)
.set('Authorization', `Bearer ${accessToken}`)
.type('multipart/mixed;boundary=' + boundary)
.send(body)
.end(function(error, response) {
if (error) {
console.log('Error calling the upload content route', error);
res.status(500).send({ error: error });
} else if(response.statusCode !== 200) {
res.status(response.statusCode).send(response.error);
} else {
res.status(200).send(response.text);
}
});
} else {
console.log('Attempting to upload content using ID Key Authentication.');
const userId = req.cookies[configs.cookieName].userId;
const userKey = req.cookies[configs.cookieName].userKey;
const userContext = appContext.createUserContextWithValues(configs.instanceScheme + '//' + configs.instanceUrl, configs.instancePort, userId, userKey);
const apiCallUrl = userContext.createAuthenticatedUrl(apiPath, 'POST');
request
.post(apiCallUrl)
.type('multipart/mixed;boundary=' + boundary)
.send(body)
.end(function(error, response) {
if (error) {
console.log('Error calling the upload content route', error);
res.status(500).send({ error: error });
} else if(response.statusCode !== 200) {
res.status(response.statusCode).send(response.error);
} else {
res.status(200).send(response.text);
}
});
}
});
/* GET /uploadworddocument
* Uploads a Word document to a module within the given course. The OrgUnitId and ModuleId are parameters
* passed in through the query parameters.
*/
router.get('/uploadworddocument', function (req, res) {
const orgUnitId = req.query.orgUnitId;
const moduleId = req.query.moduleId;
const apiPath = '/d2l/api/le/1.22/' + orgUnitId + '/content/modules/' + moduleId + '/structure/';
const accessToken = req.cookies[configs.cookieName].accessToken;
const boundary = 'xxBOUNDARYxx';
const topicData = {
Title: 'Sample Word Document Content',
ShortTitle: null,
Type: 1,
TopicType: 1,
Url: '/content/enforced/6952-ES100/sample-content.docx',
StartDate: null,
EndDate: null,
DueDate: null,
IsHidden: false,
IsLocked: false,
OpenAsExternalResource: null,
Description: null,
MajorUpdate: null,
MajorUpdateText: null,
ResetCompletionTracking: null
};
const body = buildMultipartBody(boundary, topicData, './content/file-upload/sample-content.docx', 'sample-content.docx', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document');
if (accessToken) {
console.log('Attempting to make the Content Creation route call using OAuth 2.0 authentication.');
const contentRoute = helpers.createUrl(apiPath, configs);
request
.post(contentRoute)
.set('Authorization', `Bearer ${accessToken}`)
.type('multipart/mixed;boundary=' + boundary)
.query({base64: true})
.send(body)
.end(function(error, response) {
if (error) {
console.log('Error calling the upload content route', error);
res.status(500).send({ error: error });
} else if(response.statusCode !== 200) {
res.status(response.statusCode).send(response.error);
} else {
res.status(200).send(response.text);
}
});
}
else {
console.log('Attempting to make the Content Creation route call using ID Key Authentication.');
const userId = req.cookies[configs.cookieName].userId;
const userKey = req.cookies[configs.cookieName].userKey;
const userContext = appContext.createUserContextWithValues(configs.instanceScheme + '//' + configs.instanceUrl, configs.instancePort, userId, userKey);
const apiCallUrl = userContext.createAuthenticatedUrl(apiPath, 'POST');
request
.post(apiCallUrl)
.type('multipart/mixed;boundary=' + boundary)
.query({base64: true})
.send(body)
.end(function(error, response) {
if (error) {
console.log('Error calling the upload content route', error);
res.status(500).send({ error: error });
} else if(response.statusCode !== 200) {
res.status(response.statusCode).send(response.error);
} else {
res.status(200).send(response.text);
}
});
}
});
/* function buildMultipartBody
* This function is used to build up the body for a multipart/mixed request.
* If the file encoding is passed in then the file data is formated using base64 encoding
* otherwise standard utf8 encoding is used.
*/
function buildMultipartBody(boundary, jsonData, filePath, fileName, fileContentType, fileEncoding) {
const newLine = '\r\n';
const doubleDashes = '--';
const endBoundary = doubleDashes + boundary + doubleDashes + newLine;
const startAndMiddleBoundary = doubleDashes + boundary + newLine;
let content = startAndMiddleBoundary;
content += 'Content-Type: application/json' + newLine + newLine;
content += JSON.stringify(jsonData) + newLine;
content += startAndMiddleBoundary;
content += 'Content-Disposition: form-data; name=""; filename="' + fileName + '"' + newLine;
content += 'Content-Type: ' + fileContentType + newLine + newLine;
const text = fileEncoding ?
fs.readFileSync(filePath,'base64') :
fs.readFileSync(filePath).toString('utf8');
content += text + newLine;
content += endBoundary;
return content;
}
return router;
};