mirror of
https://github.com/MartialBE/get-image-by-cf.git
synced 2025-05-19 01:50:16 +09:00
🎉 init
This commit is contained in:
commit
7606598c8a
12
.editorconfig
Normal file
12
.editorconfig
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
# http://editorconfig.org
|
||||||
|
root = true
|
||||||
|
|
||||||
|
[*]
|
||||||
|
indent_style = tab
|
||||||
|
end_of_line = lf
|
||||||
|
charset = utf-8
|
||||||
|
trim_trailing_whitespace = true
|
||||||
|
insert_final_newline = true
|
||||||
|
|
||||||
|
[*.yml]
|
||||||
|
indent_style = space
|
172
.gitignore
vendored
Normal file
172
.gitignore
vendored
Normal file
@ -0,0 +1,172 @@
|
|||||||
|
# Logs
|
||||||
|
|
||||||
|
logs
|
||||||
|
_.log
|
||||||
|
npm-debug.log_
|
||||||
|
yarn-debug.log*
|
||||||
|
yarn-error.log*
|
||||||
|
lerna-debug.log*
|
||||||
|
.pnpm-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/
|
||||||
|
|
||||||
|
# Snowpack dependency directory (https://snowpack.dev/)
|
||||||
|
|
||||||
|
web_modules/
|
||||||
|
|
||||||
|
# TypeScript cache
|
||||||
|
|
||||||
|
\*.tsbuildinfo
|
||||||
|
|
||||||
|
# Optional npm cache directory
|
||||||
|
|
||||||
|
.npm
|
||||||
|
|
||||||
|
# Optional eslint cache
|
||||||
|
|
||||||
|
.eslintcache
|
||||||
|
|
||||||
|
# Optional stylelint cache
|
||||||
|
|
||||||
|
.stylelintcache
|
||||||
|
|
||||||
|
# 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 variable files
|
||||||
|
|
||||||
|
.env
|
||||||
|
.env.development.local
|
||||||
|
.env.test.local
|
||||||
|
.env.production.local
|
||||||
|
.env.local
|
||||||
|
|
||||||
|
# parcel-bundler cache (https://parceljs.org/)
|
||||||
|
|
||||||
|
.cache
|
||||||
|
.parcel-cache
|
||||||
|
|
||||||
|
# Next.js build output
|
||||||
|
|
||||||
|
.next
|
||||||
|
out
|
||||||
|
|
||||||
|
# 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
|
||||||
|
|
||||||
|
# vuepress v2.x temp and cache directory
|
||||||
|
|
||||||
|
.temp
|
||||||
|
.cache
|
||||||
|
|
||||||
|
# Docusaurus cache and generated files
|
||||||
|
|
||||||
|
.docusaurus
|
||||||
|
|
||||||
|
# Serverless directories
|
||||||
|
|
||||||
|
.serverless/
|
||||||
|
|
||||||
|
# FuseBox cache
|
||||||
|
|
||||||
|
.fusebox/
|
||||||
|
|
||||||
|
# DynamoDB Local files
|
||||||
|
|
||||||
|
.dynamodb/
|
||||||
|
|
||||||
|
# TernJS port file
|
||||||
|
|
||||||
|
.tern-port
|
||||||
|
|
||||||
|
# Stores VSCode versions used for testing VSCode extensions
|
||||||
|
|
||||||
|
.vscode-test
|
||||||
|
|
||||||
|
# yarn v2
|
||||||
|
|
||||||
|
.yarn/cache
|
||||||
|
.yarn/unplugged
|
||||||
|
.yarn/build-state.yml
|
||||||
|
.yarn/install-state.gz
|
||||||
|
.pnp.\*
|
||||||
|
|
||||||
|
# wrangler project
|
||||||
|
|
||||||
|
.dev.vars
|
||||||
|
.wrangler/
|
6
.prettierrc
Normal file
6
.prettierrc
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"printWidth": 140,
|
||||||
|
"singleQuote": true,
|
||||||
|
"semi": true,
|
||||||
|
"useTabs": true
|
||||||
|
}
|
1346
package-lock.json
generated
Normal file
1346
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
13
package.json
Normal file
13
package.json
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
{
|
||||||
|
"name": "get-image-by-cf",
|
||||||
|
"version": "0.0.0",
|
||||||
|
"private": true,
|
||||||
|
"scripts": {
|
||||||
|
"deploy": "wrangler deploy",
|
||||||
|
"dev": "wrangler dev",
|
||||||
|
"start": "wrangler dev"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"wrangler": "^3.60.3"
|
||||||
|
}
|
||||||
|
}
|
161
src/index.js
Normal file
161
src/index.js
Normal file
@ -0,0 +1,161 @@
|
|||||||
|
export default {
|
||||||
|
async fetch(request, env, ctx) {
|
||||||
|
return await handleRequest(request, env);
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
async function handleRequest(request, env) {
|
||||||
|
if (request.method !== 'POST') {
|
||||||
|
return errHandler(405, 'Method Not Allowed');
|
||||||
|
}
|
||||||
|
|
||||||
|
let req;
|
||||||
|
try {
|
||||||
|
req = await request.json();
|
||||||
|
} catch (error) {
|
||||||
|
return errHandler(400, 'Invalid JSON');
|
||||||
|
}
|
||||||
|
|
||||||
|
const { action, url, api_key } = req;
|
||||||
|
if (env.API_KEY && api_key !== env.API_KEY) {
|
||||||
|
return errHandler(401, 'Unauthorized');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!url) {
|
||||||
|
return errHandler(400, 'URL is required');
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (action) {
|
||||||
|
case 'get':
|
||||||
|
return getImage(url);
|
||||||
|
case 'get16kb':
|
||||||
|
return getImage16kb(url);
|
||||||
|
case 'base64':
|
||||||
|
return getImageBase64(url);
|
||||||
|
case 'base64_16kb':
|
||||||
|
return getImageBase64_16kb(url);
|
||||||
|
default:
|
||||||
|
return errHandler(400, 'Invalid action');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getImage(url) {
|
||||||
|
const response = await fetch(url);
|
||||||
|
if (!response.ok) {
|
||||||
|
return errHandler(response.status, response.statusText);
|
||||||
|
}
|
||||||
|
return new Response(response.body, { headers: response.headers, status: response.status, statusText: response.statusText });
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getImage16kb(url) {
|
||||||
|
const response = await fetch(url);
|
||||||
|
if (!response.ok) {
|
||||||
|
return errHandler(response.status, response.statusText);
|
||||||
|
}
|
||||||
|
|
||||||
|
const chunksAll = await handleImage16kb(response);
|
||||||
|
|
||||||
|
// 返回处理后的数据
|
||||||
|
return new Response(chunksAll, { headers: response.headers, status: response.status, statusText: response.statusText });
|
||||||
|
}
|
||||||
|
|
||||||
|
async function handleImage16kb(response) {
|
||||||
|
const reader = response.body.getReader();
|
||||||
|
let receivedLength = 0; // 已接收的字节数
|
||||||
|
let chunks = []; // 接收到的数据块数组
|
||||||
|
const maxBytes = 16 * 1024; // 最大字节数,16KB
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
const { done, value } = await reader.read();
|
||||||
|
if (done) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
chunks.push(value);
|
||||||
|
receivedLength += value.length;
|
||||||
|
if (receivedLength >= maxBytes) {
|
||||||
|
// 如果达到或超过16KB,则停止读取
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 合并Uint8Array
|
||||||
|
let chunksAll = new Uint8Array(receivedLength); // 创建一个新的、足够大的数组来容纳所有数据
|
||||||
|
let position = 0;
|
||||||
|
for (let chunk of chunks) {
|
||||||
|
chunksAll.set(chunk, position); // 将数据块复制到chunksAll中
|
||||||
|
position += chunk.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
return chunksAll;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getImageBase64(url) {
|
||||||
|
const response = await fetch(url);
|
||||||
|
if (!response.ok) {
|
||||||
|
return errHandler(response.status, response.statusText);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 首先判断是否是图片
|
||||||
|
let contentType = response.headers.get('content-type');
|
||||||
|
if (!contentType.startsWith('image')) {
|
||||||
|
contentType = '';
|
||||||
|
}
|
||||||
|
|
||||||
|
const buffer = await response.arrayBuffer();
|
||||||
|
const base64 = await arrayBufferToBase64(buffer);
|
||||||
|
|
||||||
|
return new Response(
|
||||||
|
JSON.stringify({
|
||||||
|
status: true,
|
||||||
|
data: base64,
|
||||||
|
mimeType: contentType,
|
||||||
|
}),
|
||||||
|
{ status: 200, contentType: 'application/json' }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getImageBase64_16kb(url) {
|
||||||
|
const response = await fetch(url);
|
||||||
|
if (!response.ok) {
|
||||||
|
return errHandler(response.status, response.statusText);
|
||||||
|
}
|
||||||
|
|
||||||
|
let contentType = response.headers.get('content-type');
|
||||||
|
if (!contentType.startsWith('image')) {
|
||||||
|
contentType = '';
|
||||||
|
}
|
||||||
|
|
||||||
|
const chunksAll = await handleImage16kb(response);
|
||||||
|
const base64 = await arrayBufferToBase64(chunksAll);
|
||||||
|
|
||||||
|
return new Response(
|
||||||
|
JSON.stringify({
|
||||||
|
status: true,
|
||||||
|
data: base64,
|
||||||
|
mimeType: contentType,
|
||||||
|
}),
|
||||||
|
{ status: 200, contentType: 'application/json' }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function errHandler(statusCode, msg) {
|
||||||
|
return new Response(
|
||||||
|
JSON.stringify({
|
||||||
|
status: false,
|
||||||
|
message: msg,
|
||||||
|
}),
|
||||||
|
{ status: statusCode, contentType: 'application/json' }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function arrayBufferToBase64(buffer) {
|
||||||
|
let binary = '';
|
||||||
|
const bytes = new Uint8Array(buffer);
|
||||||
|
const len = bytes.byteLength;
|
||||||
|
|
||||||
|
for (let i = 0; i < len; i += 1024) {
|
||||||
|
binary += String.fromCharCode.apply(null, bytes.subarray(i, Math.min(i + 1024, len)));
|
||||||
|
}
|
||||||
|
|
||||||
|
return btoa(binary);
|
||||||
|
}
|
7
wrangler.toml
Normal file
7
wrangler.toml
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
name = "get-image-by-cf"
|
||||||
|
main = "src/index.js"
|
||||||
|
compatibility_date = "2024-05-28"
|
||||||
|
workers_dev = true
|
||||||
|
|
||||||
|
[vars]
|
||||||
|
API_KEY = ""
|
Loading…
Reference in New Issue
Block a user