72 lines
1.9 KiB
TypeScript
72 lines
1.9 KiB
TypeScript
interface Env {
|
|
R2_BUCKET: R2Bucket;
|
|
}
|
|
|
|
export async function onRequest(context: { env: Env; request: Request }) {
|
|
const url = new URL(context.request.url);
|
|
|
|
if (url.pathname === '/api/fs/list' && context.request.method === 'POST') {
|
|
try {
|
|
const files: any[] = [];
|
|
let cursor: string | undefined;
|
|
|
|
do {
|
|
const listing = await context.env.R2_BUCKET.list({
|
|
cursor: cursor,
|
|
limit: 1000
|
|
});
|
|
|
|
for (const obj of listing.objects) {
|
|
if (obj.key.endsWith('.mp4') && !obj.key.includes('/')) {
|
|
const nameWithoutExt = obj.key.replace('.mp4', '');
|
|
files.push({
|
|
name: obj.key,
|
|
size: obj.size,
|
|
modified: obj.lastModified,
|
|
thumb: `https://pub-7d4a6640bc77480c99842eea19bb2b69.r2.dev/thumbs/${nameWithoutExt}.jpg`
|
|
});
|
|
}
|
|
}
|
|
|
|
cursor = listing.cursor;
|
|
} while (cursor);
|
|
|
|
const response = {
|
|
code: 200,
|
|
message: "success",
|
|
data: {
|
|
content: files.map((f, i) => ({
|
|
id: `r2_${i}`,
|
|
path: "",
|
|
name: f.name,
|
|
size: f.size,
|
|
is_dir: false,
|
|
modified: f.modified,
|
|
created: f.modified,
|
|
sign: "",
|
|
thumb: f.thumb,
|
|
type: 2,
|
|
hashinfo: "null",
|
|
hash_info: null
|
|
})),
|
|
total: files.length,
|
|
readme: "",
|
|
header: "",
|
|
write: false,
|
|
provider: "r2"
|
|
}
|
|
};
|
|
|
|
return new Response(JSON.stringify(response), {
|
|
headers: { 'Content-Type': 'application/json' }
|
|
});
|
|
} catch (e) {
|
|
return new Response(JSON.stringify({ error: e.message }), {
|
|
status: 500,
|
|
headers: { 'Content-Type': 'application/json' }
|
|
});
|
|
}
|
|
}
|
|
|
|
return new Response('Not Found', { status: 404 });
|
|
} |