diff options
| author | soruly <[email protected]> | 2020-04-02 23:54:22 +0800 |
|---|---|---|
| committer | soruly <[email protected]> | 2020-04-02 23:54:22 +0800 |
| commit | 00cddb2266885bb70b5336f2e5c1aefae0e096f8 (patch) | |
| tree | 66b30f85e9a998e2a184bcbe4e75e64a68152585 | |
| download | trace.moe-image-proxy-00cddb2266885bb70b5336f2e5c1aefae0e096f8.tar.xz trace.moe-image-proxy-00cddb2266885bb70b5336f2e5c1aefae0e096f8.zip | |
Init commit
| -rw-r--r-- | .gitignore | 104 | ||||
| -rw-r--r-- | LICENSE | 21 | ||||
| -rw-r--r-- | api/image-proxy.ts | 36 | ||||
| -rw-r--r-- | package-lock.json | 40 | ||||
| -rw-r--r-- | package.json | 29 |
5 files changed, 230 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6704566 --- /dev/null +++ b/.gitignore @@ -0,0 +1,104 @@ +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* +lerna-debug.log* + +# Diagnostic reports (https://nodejs.org/api/report.html) +report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json + +# Runtime data +pids +*.pid +*.seed +*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage +*.lcov + +# nyc test coverage +.nyc_output + +# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# Bower dependency directory (https://bower.io/) +bower_components + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (https://nodejs.org/api/addons.html) +build/Release + +# Dependency directories +node_modules/ +jspm_packages/ + +# TypeScript v1 declaration files +typings/ + +# TypeScript cache +*.tsbuildinfo + +# Optional npm cache directory +.npm + +# Optional eslint cache +.eslintcache + +# Microbundle cache +.rpt2_cache/ +.rts2_cache_cjs/ +.rts2_cache_es/ +.rts2_cache_umd/ + +# Optional REPL history +.node_repl_history + +# Output of 'npm pack' +*.tgz + +# Yarn Integrity file +.yarn-integrity + +# dotenv environment variables file +.env +.env.test + +# parcel-bundler cache (https://parceljs.org/) +.cache + +# Next.js build output +.next + +# Nuxt.js build / generate output +.nuxt +dist + +# Gatsby files +.cache/ +# Comment in the public line in if your project uses Gatsby and *not* Next.js +# https://nextjs.org/blog/next-9-1#public-directory-support +# public + +# vuepress build output +.vuepress/dist + +# Serverless directories +.serverless/ + +# FuseBox cache +.fusebox/ + +# DynamoDB Local files +.dynamodb/ + +# TernJS port file +.tern-port @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2020 soruly + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/api/image-proxy.ts b/api/image-proxy.ts new file mode 100644 index 0000000..79c7d6e --- /dev/null +++ b/api/image-proxy.ts @@ -0,0 +1,36 @@ +import { NowRequest, NowResponse } from "@now/node"; +import fetch from "node-fetch"; + +export default async (request: NowRequest, response: NowResponse) => { + const { url } = request.query; + const imageURL = Array.isArray(url) ? url[0] : url; + if (!imageURL) { + return response.status(400).send("Error: No url from param"); + } + + try { + new URL(imageURL); + } catch (e) { + return response.status(400).send("Error: Invalid URL string"); + } + + const res = await fetch(imageURL, { + headers: { + "User-Agent": request.headers["user-agent"], + Referer: new URL(imageURL).origin, + }, + redirect: "follow", + }); + + if (res.status >= 400) { + return response.status(res.status).send(await res.text()); + } + + if (res.headers.get("Content-Type").split("/")[0].toLowerCase() !== "image") { + return response.status(400).send("Error: Content-Type is not image"); + } + + response.setHeader("Content-Type", res.headers.get("Content-Type")); + + return response.status(200).send(await res.buffer()); +}; diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..379521f --- /dev/null +++ b/package-lock.json @@ -0,0 +1,40 @@ +{ + "name": "trace.moe-image-proxy", + "version": "1.0.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "@now/node": { + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/@now/node/-/node-1.5.0.tgz", + "integrity": "sha512-0MChNIBHsViTMPDsifTveWmMpUSUbVQYAHg9Qa8p7uF7pkfBAghyCGHSKHJVg3zxx0vVE4z+rYgaRS6xvF8qMA==", + "dev": true, + "requires": { + "@types/node": "*" + } + }, + "@types/node": { + "version": "13.9.8", + "resolved": "https://registry.npmjs.org/@types/node/-/node-13.9.8.tgz", + "integrity": "sha512-1WgO8hsyHynlx7nhP1kr0OFzsgKz5XDQL+Lfc3b1Q3qIln/n8cKD4m09NJ0+P1Rq7Zgnc7N0+SsMnoD1rEb0kA==", + "dev": true + }, + "node-fetch": { + "version": "2.6.0", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.0.tgz", + "integrity": "sha512-8dG4H5ujfvFiqDmVu9fQ5bOHUC15JMjMY/Zumv26oOvvVJjM67KF8koCWIabKQ1GJIa9r2mMZscBq/TbdOcmNA==" + }, + "prettier": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-2.0.2.tgz", + "integrity": "sha512-5xJQIPT8BraI7ZnaDwSbu5zLrB6vvi8hVV58yHQ+QK64qrY40dULy0HSRlQ2/2IdzeBpjhDkqdcFBnFeDEMVdg==", + "dev": true + }, + "typescript": { + "version": "3.8.3", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.8.3.tgz", + "integrity": "sha512-MYlEfn5VrLNsgudQTVJeNaQFUAI7DkhnOjdpAp4T+ku1TfQClewlbSuTVHiA+8skNBgaf02TL/kLOvig4y3G8w==", + "dev": true + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..1642d9a --- /dev/null +++ b/package.json @@ -0,0 +1,29 @@ +{ + "name": "trace.moe-image-proxy", + "version": "1.0.0", + "description": "Image proxy for trace.moe", + "main": "image-proxy.ts", + "scripts": { + "format": "prettier --write \"**/*.ts\"", + "lint": "prettier --check \"**/*.ts\"", + "test": "prettier --check \"**/*.ts\"" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/soruly/trace.moe-image-proxy.git" + }, + "author": "soruly", + "license": "MIT", + "bugs": { + "url": "https://github.com/soruly/trace.moe-image-proxy/issues" + }, + "homepage": "https://github.com/soruly/trace.moe-image-proxy#readme", + "devDependencies": { + "@now/node": "^1.5.0", + "prettier": "^2.0.2", + "typescript": "^3.8.3" + }, + "dependencies": { + "node-fetch": "^2.6.0" + } +} |