From 76e3e980836f3e23358331baac97f912daa24d2f Mon Sep 17 00:00:00 2001 From: Zhixing Zhang Date: Thu, 28 Jul 2022 22:58:44 -0700 Subject: [PATCH] Update server.js --- server.js | 170 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 87 insertions(+), 83 deletions(-) diff --git a/server.js b/server.js index 95bd586..92352a9 100644 --- a/server.js +++ b/server.js @@ -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); -}); \ No newline at end of file +server.listen(port, "0.0.0.0", () => { + console.log("Server listening on " + port); +});