-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
296 lines (254 loc) · 10.7 KB
/
index.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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
var mime = require('mime-types')
const fetch = require('node-fetch');
const express = require('express');
const path = require('path')
// var ZIP = require('@ronomon/zip');
let app = express();
let contentAge = 120;
let cacheControlStr = "private, must-revalidate, max-age=" + contentAge
// Need to use https://docs.microsoft.com/en-us/rest/api/azure/devops/git/items/get?view=azure-devops-rest-5.0 instead of other options, seems to work for listings and
// add support for listing https://docs.microsoft.com/en-us/rest/api/azure/devops/build/source%20providers/get%20path%20contents?view=azure-devops-rest-5.0
async function serveFile(req, res, versionType, org, repo, project, branch, remainder, isListing, unPW, mimeType) {
// var str = "https://" + unPW + "dev.azure.com/" + org + "/" + project + "/_apis/sourceProviders/tfsgit/filecontents?&repository=" + repo + "&commitOrBranch=" + branch +"&path=" + remainder + "&api-version=5.0-preview.1"
// var str = "https://" + unPW + "dev.azure.com/" + org + "/" + project + "/_apis/sourceProviders/tfsgit/pathcontents?&repository=" + repo + "&commitOrBranch=" + branch +"&path=" + remainder + "&api-version=5.0-preview.1"
var str = "https://" + unPW + "dev.azure.com/" + org + "/" + project + "/_apis/git/repositories/" + repo + "/items?path=" + remainder + "&$format=zip&versionDescriptor.version=" + branch + "&versionDescriptor.versionType="+ versionType + "&api-version=5.0"
// you can drop &$format=zip and add &download=true
var str = "https://" + unPW + "dev.azure.com/" + org + "/" + project + "/_apis/git/repositories/" + repo + "/items?path=" + remainder + "&download=true&versionDescriptor.version=" + branch + "&versionDescriptor.versionType="+ versionType + "&api-version=5.0"
console.log(str)
console.log("\n")
try {
var data = await fetch(str)
b = await data.buffer()
// console.log(b)
// try {
// var headers = ZIP.decode(b);
// } catch (error) {
// console.error(error.message);
// }
// var file = ZIP.inflate(headers[0], b);
// b = file
if(mimeType == "normal" ){
var mimeType = mime.lookup(remainder) || 'application/octet-stream'
res.writeHead(200, { "Content-Type": mime.contentType(mimeType), "Cache-Control": cacheControlStr });
}else if(mimeType == "bin"){
res.writeHead(200, { "Content-Type": 'application/octet-stream', "Cache-Control": cacheControlStr });
}
else {
res.writeHead(200, { "Content-Type": "text/plain", "Cache-Control": cacheControlStr });
}
res.write(b)
res.end()
return
}
catch (err) {
res.writeHead(200, { "Content-Type": "text/plain" });
res.end(err)
return
}
res.writeHead(200, { "Content-Type": "text/plain" });
res.end("org: " + org
+ "\nproject: " + project
+ "\nrepo: " + repo
+ "\nbranch: " + branch
+ "\npath: " + remainder
+ "\nisListing: " + isListing
+ "\nun: " + login
+ "\npassword: " + password
+ "\n" + str
+ "\n");
}
// for root page send rootpage url
app.get('/', (req, res) => {
const fileDirectory = __dirname + "/" ;
res.sendFile('rootpage.html', {root: fileDirectory}, (err) => {
res.end();
if (err) throw(err);
});
})
/**
* @param {string} remainder
*/
async function isFolder(req, res, versionType, org, repo, project, branch, remainder, isListing, unPW) {
if (remainder.includes(".")) {
return false;
}
var str = "https://" + unPW + "dev.azure.com/" + org + "/" + project + "/_apis/git/repositories/" + repo + "/items?path=" + remainder + "&$format=json&versionDescriptor.version=" + branch + "&versionDescriptor.versionType="+ versionType + "&api-version=5.0"
console.log(str)
console.log("\n")
try {
var data = await fetch(str)
b = await data.buffer()
// console.log(b)
try {
obj = JSON.parse(b.toString('utf8'))
return obj.gitObjectType == "tree"
} catch (error) {
return false
}
}
catch (err) {
return false
}
}
/**
* @param {string} remainder
*/
async function listFolder(req, res, versionType, org, repo, project, branch, remainder, isListing, unPW) {
var str = "https://" + unPW + "dev.azure.com/" + org + "/" + project + "/_apis/git/repositories/" + repo + "/items?scopePath=" + remainder + "&recursionLevel=oneLevel&includeContentMetadata=true&versionDescriptor.version=" + branch + "&versionDescriptor.versionType="+ versionType + "&api-version=5.0"
console.log(str)
console.log("\n")
try {
var data = await fetch(str)
b = await data.buffer()
// console.log(b)
try {
obj = JSON.parse(b.toString('utf8'))
var objects = {
folders: [],
files: []
}
obj.value.forEach(function (ob, index) {
if (index == 0) {
return;
}
if (ob.gitObjectType == "tree") {
objects.folders.push(ob.path)
} else {
objects.files.push(ob.path)
}
});
return objects
} catch (error) {
return {
folders: [],
files: []
}
}
}
catch (err) {
return {
folders: [],
files: []
}
}
}
function writeList(response, baseUrl, remainder, folders, files) {
response.writeHeader(200, { "Content-Type": "text/html", "Cache-Control": cacheControlStr });
var data = ` <table><tr>
<th>View Txt</th>
<th>Download</th>
<th>Normal Website (correct mime-type)</th>
</tr>
<tr>
`
// add current page
data = data + '<tr>'
data = data + '<td><a href="' + '/raw'+ baseUrl + remainder + '">txt</a></td>'
data = data + '<td></td>'
data = data + '<td><a href="' + baseUrl + remainder + '">' + remainder + '</a></td>'
data = data + '</tr>\n'
if(remainder != '/'){
// add parent page
data = data + '<tr>'
data = data + '<td><a href="' + '/raw'+ baseUrl + remainder + '../' + '">txt</a></td>'
data = data + '<td></td>'
data = data + '<td><a href="' + baseUrl + remainder+ '../'+ '">' + remainder + '</a></td>'
data = data + '</tr>\n'
}
folders.forEach(function (folder, index) {
data = data + '<tr>'
data = data + '<td><a href="' + '/raw'+ baseUrl + folder + "/" + '">txt</a></td>'
data = data + '<td></td>'
data = data + '<td><a href="' + baseUrl + folder + "/" + '">' + folder + '</a></td>'
data = data + '</tr>\n'
});
files.forEach(function (file, index) {
data = data + '<tr>'
data = data + '<td><a href="' + '/raw'+ baseUrl + file + '">txt</a></td>'
data = data + '<td><a href="' + '/bin' +baseUrl + file + '">bin</a></td>'
data = data + '<td><a href="' + baseUrl + file+ '">' + file + '</a></td>'
data = data + '</tr>\n'
});
let htmlPrefix = `<html><head>
<style>
table,
th,
td {
border: 1px solid black;
border-collapse: collapse;
}
</style></head><body>
`
response.write(htmlPrefix + data + "</table></body></html>");
response.end();
}
async function processRequest(req, res, mimeType) {
// // var needsAuth = req.params.needsAuth;
// const b64auth = (req.headers.authorization || '').split(' ')[1] || ''
// const [login, password] = new Buffer(b64auth, 'base64').toString().split(':')
// // console.log(req.originalUrl)
// // lets make it always require token
// // TODO: figure out how to make it not always require token
// if (b64auth == '') {
// res.set("WWW-Authenticate", "Basic realm=\"Authorization Required\"");
// return res.status(401).send("Authorization Required");
// }
var token = req.params.token;
var login = token;
var org = req.params.organization;
var repo = req.params.repo;
var project = req.params.project;
var branch = encodeURIComponent(req.params.branch);
var remainder = req.path;
var versionType = req.params.versionType
var isListing = req.originalUrl[req.originalUrl.length - 1] == '/'
var baseUrl = '/' + token + '/' + versionType + '/' + org +'/' + project + '/' + repo + '/' + branch
var unPW = ''
if (login != '') {
unPW = "empty:" + login + "@"
}
// if (await isFolder(req, res, versionType, org, repo, project, branch, remainder, isListing, unPW)) {
if (isListing) {
var objects = await listFolder(req, res, versionType, org, repo, project, branch, remainder, isListing, unPW)
console.log(objects)
if(mimeType != "normal"){
writeList(res, baseUrl, remainder, objects.folders, objects.files)
}else{
// search for index.htm(l)
let indexHtml = -1;
for(var i =0; i<objects.files.length; i++){
let filePath = objects.files[i].toLowerCase()
let filePathChunked = filePath.split('/')
let fileName = filePathChunked[filePathChunked.length -1]
if(fileName == "index.html" || fileName == "index.htm"){
indexHtml = i;
break;
}
}
if(indexHtml != -1){
await serveFile(req, res, versionType, org, repo, project, branch, objects.files[indexHtml], isListing, unPW, mimeType)
}
else{
writeList(res, baseUrl, remainder, objects.folders, objects.files)
}
}
}
else {
await serveFile(req, res, versionType, org, repo, project, branch, remainder, isListing, unPW, mimeType)
}
}
app.use('/raw/:token/:versionType/:organization/:project/:repo/:branch/', async function (req, res) {
console.log("I am raw")
await processRequest(req, res,'raw')
});
app.use('/bin/:token/:versionType/:organization/:project/:repo/:branch/', async function (req, res) {
console.log("I am bin")
await processRequest(req, res,'bin')
});
app.use('/:token/:versionType/:organization/:project/:repo/:branch/', async function (req, res) {
console.log("I am website")
await processRequest(req, res,'normal')
});
let server = app.listen(8080, function () {
console.log('Server is listening on port 8080')
});