-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ea03db4
commit 76e3e98
Showing
1 changed file
with
87 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,96 +1,100 @@ | ||
const http = require('http'); | ||
const | ||
{ | ||
URL | ||
} = require('url'); | ||
const http = require("http"); | ||
const https = require("https"); | ||
const { URL } = require("url"); | ||
|
||
const gm = require('gm').subClass( | ||
{ | ||
imageMagick: true | ||
const gm = require("gm").subClass({ | ||
imageMagick: true, | ||
}); | ||
|
||
const bucketName = process.env.BUCKET_NAME || "assets.cytoid.io"; | ||
|
||
function requestListener(req, res) | ||
{ | ||
|
||
res.setHeader('X-Powered-By', 'cytoid'); | ||
if (req.method !== 'GET') | ||
{ | ||
res.writeHead(405); | ||
res.end('Method not allowed'); | ||
} | ||
const url = new URL(req.url, 'https://images.cytoid.io'); | ||
const request = http.get({ | ||
host: 'c.storage.googleapis.com', | ||
path: url.pathname, | ||
headers: { | ||
'Host': 'assets.cytoid.io', | ||
} | ||
}, (response) => { | ||
if (response.statusCode === 200) | ||
{ | ||
// Image exists. | ||
const contentType = response.headers['content-type']; | ||
res.setHeader('content-type', contentType); | ||
if (!contentType.startsWith('image/')) { | ||
res.writeHead(response.statusCode); | ||
response.pipe(res); | ||
return; | ||
} | ||
|
||
// Copy headers | ||
for (const headerName of ['expires', 'etag', 'content-disposition', 'date', 'vary']) { | ||
const header = response.headers[headerName]; | ||
if (header) | ||
res.setHeader(headerName, header); | ||
} | ||
res.setHeader('cache-control', 'max-age=3153600,public'); | ||
function requestURLForPath(url) { | ||
let path = url.pathname; | ||
if (path.startsWith("/")) { | ||
path = path.substr(1); | ||
} | ||
return `https://assets.cytoid.io/` + path; | ||
} | ||
|
||
function requestListener(req, res) { | ||
res.setHeader("X-Powered-By", "cytoid"); | ||
if (req.method !== "GET") { | ||
res.writeHead(405); | ||
res.end("Method not allowed"); | ||
} | ||
const url = new URL(req.url, "https://images.cytoid.io"); | ||
const request = https.get( | ||
requestURLForPath(url), | ||
{ | ||
headers: { | ||
"user-agent": "cytoid-img-resizer", | ||
}, | ||
}, | ||
(response) => { | ||
if (response.statusCode === 200) { | ||
// Image exists. | ||
const contentType = response.headers["content-type"]; | ||
res.setHeader("content-type", contentType); | ||
if (!contentType.startsWith("image/")) { | ||
res.writeHead(response.statusCode); | ||
response.pipe(res); | ||
return; | ||
} | ||
|
||
let height = url.searchParams.get('h'); | ||
if (height) height = parseInt(height) || null; | ||
let width = url.searchParams.get('w'); | ||
if (width) width = parseInt(width) || null; | ||
// Copy headers | ||
for (const headerName of [ | ||
"expires", | ||
"etag", | ||
"content-disposition", | ||
"date", | ||
"vary", | ||
]) { | ||
const header = response.headers[headerName]; | ||
if (header) res.setHeader(headerName, header); | ||
} | ||
res.setHeader("cache-control", "max-age=3153600,public"); | ||
|
||
res.writeHead(response.statusCode); | ||
let height = url.searchParams.get("h"); | ||
if (height) height = parseInt(height) || null; | ||
let width = url.searchParams.get("w"); | ||
if (width) width = parseInt(width) || null; | ||
|
||
if (height && width) | ||
{ | ||
gm(response) | ||
.resize(width, height, '^') | ||
.gravity('Center') | ||
.crop(width, height) | ||
.stream() | ||
.pipe(res); | ||
} else if (height || width) { | ||
gm(response) | ||
.resize(width, height) | ||
.stream() | ||
.pipe(res); | ||
} | ||
else | ||
{ | ||
response.pipe(res); | ||
} | ||
res.writeHead(response.statusCode); | ||
|
||
} | ||
else | ||
{ | ||
// Error | ||
for (const headerName of ['content-type', 'expires', 'etag', 'content-disposition', 'date', 'vary']) { | ||
const header = response.headers[headerName]; | ||
if (header) | ||
res.setHeader(headerName, header); | ||
} | ||
res.writeHead(response.statusCode); | ||
response.pipe(res); | ||
} | ||
}); | ||
if (height && width) { | ||
gm(response) | ||
.resize(width, height, "^") | ||
.gravity("Center") | ||
.crop(width, height) | ||
.stream() | ||
.pipe(res); | ||
} else if (height || width) { | ||
gm(response).resize(width, height).stream().pipe(res); | ||
} else { | ||
response.pipe(res); | ||
} | ||
} else { | ||
// Error | ||
for (const headerName of [ | ||
"content-type", | ||
"expires", | ||
"etag", | ||
"content-disposition", | ||
"date", | ||
"vary", | ||
]) { | ||
const header = response.headers[headerName]; | ||
if (header) res.setHeader(headerName, header); | ||
} | ||
res.writeHead(response.statusCode); | ||
response.pipe(res); | ||
} | ||
} | ||
); | ||
} | ||
|
||
const port = process.env.PORT || 8040; | ||
const server = http.createServer(requestListener); | ||
server.listen(port, '0.0.0.0', () => | ||
{ | ||
console.log('Server listening on ' + port); | ||
}); | ||
server.listen(port, "0.0.0.0", () => { | ||
console.log("Server listening on " + port); | ||
}); |