Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
iiop123 committed Nov 12, 2022
1 parent 4c5f51c commit 38a0d28
Showing 1 changed file with 69 additions and 8 deletions.
77 changes: 69 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import {
normalizePathnameMiddleware
} from '@cfworker/web';
import { getAssetFromKV } from "@cloudflare/kv-asset-handler";
const u = '123';//展示页面登录用户名
const p = '123';//密码
/*
默认用户名密码都是123
*/

const router = new Router();
async function randomString(len) {
Expand All @@ -22,32 +27,77 @@ import { getAssetFromKV } from "@cloudflare/kv-asset-handler";
let pathname = new URL(event.request.url)
let asset= new RegExp('/assets/.*','i')
let index=new RegExp('/index.*','i')
let list=new RegExp('/list.*','i')
if (asset.test(pathname.pathname)||index.test(pathname.pathname)) {
event.respondWith(handleEvent(event));
}
}
if (list.test(pathname.pathname)) {
event.respondWith(listhandle(event));
}
});

async function handleEvent(event) {
let pathname = new URL(event.request.url)
console.log('path is '+pathname);
return getAssetFromKV(event)
}
async function listhandle(request) {
const has=request.request.headers.has('Authorization')
if (has) {
const { user, pass } = await basicAuthentication(request);
if (u === user && p===pass) {
return getAssetFromKV(request)
}
}

// Not authenticated.
return new Response('You need to login.', {
status: 401,
headers: {
// Prompts the user for credentials.
'WWW-Authenticate': 'Basic realm="my scope", charset="UTF-8"',
},
});
}

function basicAuthentication(request) {
const Authorization = request.request.headers.get('Authorization');

const [scheme, encoded] = Authorization.split(' ');

if (!encoded || scheme !== 'Basic') {
throw new BadRequestException('Malformed authorization header.');
}

const buffer = Uint8Array.from(atob(encoded), character => character.charCodeAt(0));
const decoded = new TextDecoder().decode(buffer).normalize();
const index = decoded.indexOf(':');

if (index === -1 || /[\0-\x1F\x7F]/.test(decoded)) {
throw new BadRequestException('Invalid authorization value.');
}

return {
user: decoded.substring(0, index),
pass: decoded.substring(index + 1),
};
}

router.get('/',({res})=>{
res.redirect('/index.html')
})
router.get('/list',({res})=>{
res.redirect('/list.html')
})
router.post(
'/api', async ({req,res})=> {
let form=req.body.formData()
let out=[]
let img=(await form).getAll('img')
const img_check=/^[\s\S]*\.(pdf|sh|zip|txt|docx|xlsx|exe|apk)$/
const img_check=/^[\s\S]*\.(jpg|jpeg|png|gif|ico|svg)$/
for (let i = 0; i < img.length; i++) {
if (img_check.test(img[i].name)) {
res.status=400
res.body={name:img[i].name,err:'非图片文件'}
}else{
let url=await randomString()
let url=await randomString()
let check=await LINK.get(url)
if (check!==null) {
url=await randomString()
Expand All @@ -62,10 +112,17 @@ router.post(
})
out.push(req.url+'/img/'+url)
res.body = {src:out}
}else{
res.status=400
res.body={name:img[i].name,err:'非图片文件'}
}
}}
);

router.get('/api/img/:p', async ({req,res})=>{
let body=await LINK.get(req.params.p,{cacheTtl:864000,type:"stream"})
res.body=body

})
// Favicon route for fun :)
router.get('/favicon.ico', ({ res }) => {
res.type = 'image/svg+xml';
Expand All @@ -75,10 +132,14 @@ router.post(
<text font-size="120" font-family="Arial, Helvetica, sans-serif" text-anchor="end" fill="#FFF" x="185" y="185">W</text>
</svg>`;
});
router.get('/query',async ({req,res})=>{
const key=await LINK.list()
res.body=key
})


// Compose the application
new Application()
.use(normalizePathnameMiddleware)
.use(router.middleware)
.listen();
.listen();

0 comments on commit 38a0d28

Please sign in to comment.