-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.js
95 lines (83 loc) · 2.89 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
require('dotenv').config()
const express = require('express');
const bodyParser = require('body-parser');
const fs = require('fs-extra');
const path = require('path');
const {totalist} = require('totalist/sync');
const server = express();
const PORT = process.env.PORT || 8080;
server.use(bodyParser.json());
server.use(bodyParser.raw({
type: 'application/octet-stream',
limit: '50mb'
}))
server.use((req, res, next) => {
if (req.get('Authorization') !== `Bearer ${process.env.AUTH_KEY}`) {
res.status(401).json({message: 'You are not authorized'});
} else {
next();
}
})
server.get('/', (req, res) => {
res.status(200).send({
status: 'success'
})
})
server.post('/_apis/pipelines/workflows/:runId/artifacts', (req, res) => {
const {runId} = req.params;
const baseURL = `${req.protocol}://${req.get('host')}${req.baseUrl}`;
res.json({fileContainerResourceUrl: `${baseURL}/upload/${runId}`});
});
server.patch('/_apis/pipelines/workflows/:runId/artifacts', (req, res) => {
const {runId} = req.params;
res.status(200).json({message: 'success'});
});
server.get('/_apis/pipelines/workflows/:runId/artifacts', (req, res) => {
const {runId} = req.params;
const artifacts = new Set();
const baseURL = `${req.protocol}://${req.get('host')}${req.baseUrl}`;
totalist(`./${runId}`, (name, abs, stats) => {
name = name.replace('\\', '/');
const fileDetails = {
name: name.split('/')[0],
fileContainerResourceUrl: `${baseURL}/download/${runId}`
}
artifacts.add(fileDetails);
});
console.log(artifacts);
res.status(200).json({count: artifacts.count, value: [...artifacts]});
});
server.get('/download/:container', (req, res) => {
const {container} = req.params;
const baseURL = `${req.protocol}://${req.get('host')}${req.baseUrl}`;
const files = new Set();
totalist(container, (name, abs, stats) => {
console.log(name);
console.log(abs);
files.add({
path: path.normalize(name),
itemType: 'file',
contentLocation: `${baseURL}/download/${container}/${name.replace('\\', '/')}`
});
})
res.status(200).json({value: [...files]})
});
server.get('/download/:container/:path(*)', (req, res) => {
const path = `${req.params.container}/${req.params.path}`;
fs.createReadStream(path, {encoding: 'utf-8'}).pipe(res);
});
server.put('/upload/:runId', (req, res, next) => {
const { itemPath } = req.query;
const {runId} = req.params;
req.setEncoding('base64');
fs.ensureFileSync(`${runId}/${itemPath}`);
fs.writeFile(`${runId}/${path.normalize(itemPath)}`, req.body, {encoding: 'utf-8'}, (err) => {
if (err) {
console.error(err);
}
res.status(200).json({message: 'success'})
});
});
server.listen(PORT, () => {
console.log(`Listening on port ${PORT}`);
})