This repository has been archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
141 lines (111 loc) · 2.61 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
/**
* ccdocker... server.
*
* @author Jared Allard <[email protected]>
* @version 1.0
* @license MIT
* ばか!
**/
'use strict'
const express = require('express')
const fs = require('fs-extra')
const path = require('path')
const mkdirp = require('mkdirp')
const bodyP = require('body-parser')
const images = path.join(__dirname, 'images')
const app = express()
app.use((req, res, next) => {
/**
* Error Standardization.
**/
res.error = (error = 'None Provided', code = '0011') => {
return res.send({
success: false,
code: code,
error: errror
})
}
res.success = data => {
return res.send({
success: true,
data: data
})
}
return next()
})
app.use(function (req, res, next) {
req.rawBody = ''
req.setEncoding('utf8')
req.on('data', function (chunk) {
req.rawBody += chunk
})
req.on('end', function () {
next()
})
})
mkdirp.sync(images)
app.get('/', (req, res) => {
return res.send()
})
/**
* Return the API version.
**/
app.get('/api/version', (req, res) => {
return res.send({
version: '1.0'
})
})
/**
* Pull an image.
**/
app.get('/pull/*', async (req, res) => {
const unsafePath = req.path.replace(/^\/pull\//g, '')
const safePath = path.normalize(unsafePath).replace(/^(\.\.[\/\\])+/, '')
console.log('send image', safePath)
const imagePath = path.join(images, safePath)
try {
await fs.exists(imagePath)
} catch (err) {
console.log('image', safePath, 'not found')
return res.status(404)
}
return res.sendFile(imagePath)
})
/**
* Upload an image.
**/
app.post('/push', async (req, res) => {
// this is really bad, don't do this.
const image = JSON.parse(req.rawBody)
let { name, version } = image
if (!version) {
version = 'latest'
}
if (!name || !version) {
return res.send({
success: false,
code: '0021',
error: 'Failed to determine name/version'
})
}
const evalPath = path.join(images, `${name}/${version}`)
const latest = path.join(images, `${name}/latest`)
mkdirp.sync(path.join(evalPath, '..'))
await fs.writeFile(evalPath, req.rawBody)
// TODO: Latest not just a copy.
await fs.writeFile(latest, req.rawBody)
return res.send({
success: true
})
})
app.get('/image/:image/versions', async (req, res) => {
const safePath = path
.normalize(req.params.image)
.replace(/^(\.\.[\/\\])+/, '')
if (!safePath) return res.error('Invalid Image')
const imageDir = path.join(images, safePath)
const versions = await fs.readdir(imageDir)
return res.success(versions)
})
console.log('listening on port 8081')
app.listen(8081)