完成资源页面

This commit is contained in:
2026-05-21 00:31:30 +00:00
parent 78557a6ffa
commit 55f01abd74
5 changed files with 113 additions and 25 deletions

View File

@@ -44,14 +44,24 @@ async def home(request: Request):
) )
@app.get("/resources/") @app.get("/resources/")
async def resources(request: Request): @app.get("/resources/{name}")
return templates.TemplateResponse( async def resources(request: Request, name: str = None):
"resources.html", if not name:
{ return templates.TemplateResponse(
"request": request, "resources/home.html",
"title": "resources" {
} "request": request,
) "title": "resources"
}
)
else:
return templates.TemplateResponse(
"resources/test.html",
{
"request": request,
"name": f"{name}"
}
)
@app.get("/bbs/") @app.get("/bbs/")
async def bbs(request: Request): async def bbs(request: Request):

View File

@@ -1,5 +1,5 @@
from fastapi import APIRouter from fastapi import APIRouter
import json import json, os
root_url = "greendam.ww3.tw" root_url = "greendam.ww3.tw"
@@ -16,3 +16,38 @@ def 用于测试():
"code": "200 ok!", "code": "200 ok!",
"msg": "Hello, World!!" "msg": "Hello, World!!"
} }
###########
@router.get('/files/list/')
@router.get('/files/list/{name}/')
async def 根据文件夹列出文件(name: str = None):
if not name:
files_path = "../../var/static/resources/greendam"
files_name = os.listdir(files_path)
files_files = [f for f in files_name if f.lower().endswith((""))]
rule_list = []
url = f"https://{root_url}/static/resources/greendam"
for id, filename in enumerate(files_files, start=1):
url_path = f"{url}/{filename}/"
rule_list.append({
"id": id,
"filename": filename,
"url_path": url_path
})
return (rule_list)
else:
files_path = f"../../var/static/resources/greendam/{name}"
files_name = os.listdir(files_path)
files_files = [f for f in files_name if f.lower().endswith((""))]
rule_list = []
url = f"https://{root_url}/static/resources/greendam/{name}"
for id, filename in enumerate(files_files, start=1):
url_path = f"{url}/{filename}/"
rule_list.append({
"id": id,
"filename": filename,
"url_path": url_path
})
return (rule_list)
###########

View File

@@ -1,16 +0,0 @@
<html>
<head>
<title>绿坝娘资源小站 (仮) | {{ title }}</title>
<link rel="icon" type="image/png" sizes="128x128" href="/static/src/img/logo.png">
</head>
<body>
<a href="/">返回主页</a>
<br>
<h1>Index of /</h1>
<hr>
<pre>
</pre>
<hr>
</body>
</html>

View File

@@ -0,0 +1,30 @@
<html>
<head>
<title>绿坝娘资源小站 (仮) | {{ title }}</title>
<link rel="icon" type="image/png" sizes="128x128" href="/static/src/img/logo.png">
</head>
<body>
<a href="/">返回主页</a>
<br>
<h1>Index of /</h1>
<hr>
<div id="content">
</div>
<hr>
</body>
</html>
<script>
fetch("https://greendam.ww3.tw/api/files/list/")
.then(res => res.json())
.then (data => {
data.forEach(item => {
console.log('ID', item.id);
console.log('Name', item.filename);
console.log('URL', item.url_path);
let contentDocment = `<a href="https://greendam.ww3.tw/resources/${item.filename}">${item.filename}</a><br>`
document.getElementById("content").innerHTML += contentDocment
})
})
</script>

View File

@@ -0,0 +1,29 @@
<html>
<head>
<title>绿坝娘资源小站 (仮) | {{ name }}</title>
</head>
<body>
<a href="/resources">返回上级</a>
<br>
<h1>Index of /{{ name }}</h1>
<hr>
<div id="content">
</div>
<hr>
</body>
</html>
<script>
fetch("https://greendam.ww3.tw/api/files/list/{{ name }}/")
.then(res => res.json())
.then (data => {
data.forEach(item => {
console.log('ID', item.id);
console.log('Name', item.filename);
console.log('URL', item.url_path);
let contentDocment = `<a href="${item.url_path}">${item.filename}</a><br>`
document.getElementById("content").innerHTML += contentDocment
})
})
</script>