Skip to content

Commit 9626511

Browse files
author
Hermes Cron
committed
feat: 添加资源管理脚本 - CDN迁移和上传工具
- scripts/update_manifest.py: 更新manifest.json中的CDN地址(R2->七牛云) - scripts/upload_gitee.py: 通过Gitee API上传资源文件 - scripts/upload_qiniu.py: 上传本地资源到七牛云
1 parent 1bbfbeb commit 9626511

3 files changed

Lines changed: 149 additions & 14 deletions

File tree

scripts/update_manifest.py

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,41 @@
11
#!/usr/bin/env python3
2-
"""更新 manifest status"""
2+
"""更新 manifest.json 中的 CDN 地址:从 R2 迁移到七牛云"""
3+
34
import json
4-
import sys
5-
from pathlib import Path
6-
from datetime import datetime, timezone
7-
8-
target = sys.argv[1] if len(sys.argv) > 1 else "ancient_eastern_zhiguai"
9-
new_status = sys.argv[2] if len(sys.argv) > 2 else "generating"
10-
11-
m = Path(f"/home/ubuntu/project/MoRanJiangHu/data/era_assets/{target}/manifest.json")
12-
d = json.load(open(m))
13-
d["status"] = new_status
14-
d["updated_at"] = datetime.now(timezone.utc).isoformat()
15-
json.dump(d, open(m, "w"), ensure_ascii=False, indent=2)
16-
print(f"Updated {target} -> {new_status}")
5+
import os
6+
from datetime import datetime
7+
8+
CDN_BASE = "https://ted3eqr3p.hn-bkt.clouddn.com"
9+
10+
manifest_path = "resources/manifest.json"
11+
12+
with open(manifest_path, "r", encoding="utf-8") as f:
13+
manifest = json.load(f)
14+
15+
count = 0
16+
def replace_url(obj):
17+
global count
18+
if isinstance(obj, dict):
19+
if "cdn_url" in obj and obj["cdn_url"].startswith("r2://"):
20+
old = obj["cdn_url"]
21+
# r2://mosheqiankun/resources/images/xxx.png -> https://cdn/resources/images/xxx.png
22+
path = old.replace("r2://mosheqiankun/", "")
23+
obj["cdn_url"] = f"{CDN_BASE}/{path}"
24+
count += 1
25+
print(f" {old}")
26+
print(f" -> {obj['cdn_url']}")
27+
for v in obj.values():
28+
replace_url(v)
29+
elif isinstance(obj, list):
30+
for item in obj:
31+
replace_url(item)
32+
33+
print("替换 CDN URL:")
34+
replace_url(manifest["resources"])
35+
36+
manifest["updated_at"] = datetime.now().isoformat()
37+
38+
with open(manifest_path, "w", encoding="utf-8") as f:
39+
json.dump(manifest, f, ensure_ascii=False, indent=2)
40+
41+
print(f"\n完成: 替换了 {count} 个 URL")

scripts/upload_gitee.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/usr/bin/env python3
2+
"""通过 Gitee API 上传资源文件(使用 POST 创建新文件)"""
3+
4+
import requests
5+
import base64
6+
import os
7+
8+
TOKEN = '8de01ccd489a2818ce1607ecf673db30'
9+
OWNER = 'onemuggle'
10+
REPO = 'moran-resources'
11+
API = 'https://gitee.com/api/v5/repos'
12+
GITEE_PAGES = "https://onemuggle.gitee.io/moran-resources"
13+
14+
def upload_file(filepath):
15+
"""上传单个文件"""
16+
path = filepath.replace('\\', '/')
17+
url = f'{API}/{OWNER}/{REPO}/contents/{path}'
18+
19+
with open(filepath, 'rb') as f:
20+
content = base64.b64encode(f.read()).decode()
21+
22+
data = {
23+
'access_token': TOKEN,
24+
'message': f'Upload {path}',
25+
'content': content,
26+
'branch': 'master',
27+
}
28+
29+
r = requests.post(url, json=data, timeout=30)
30+
if r.status_code in (201, 200):
31+
return True, r.json()
32+
else:
33+
return False, r.json()
34+
35+
def get_cdn_url(path):
36+
return f"{GITEE_PAGES}/{path}"
37+
38+
resources_dir = 'resources'
39+
uploaded = []
40+
failed = []
41+
42+
for root, dirs, files in os.walk(resources_dir):
43+
for filename in files:
44+
filepath = os.path.join(root, filename)
45+
key = filepath.replace('\\', '/')
46+
47+
print(f"上传: {key} ...", end=' ', flush=True)
48+
ok, result = upload_file(filepath)
49+
if ok:
50+
cdn_url = get_cdn_url(key)
51+
uploaded.append((key, cdn_url))
52+
print("✅")
53+
else:
54+
failed.append((key, result))
55+
print(f"❌ {result.get('message', str(result))[:80]}")
56+
57+
print(f"\n完成: {len(uploaded)} 成功, {len(failed)} 失败")
58+
if failed:
59+
print("\n失败文件:")
60+
for k, e in failed:
61+
print(f" {k}: {e}")

scripts/upload_qiniu.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/usr/bin/env python3
2+
"""上传本地资源文件到七牛云"""
3+
4+
import os
5+
import sys
6+
sys.path.insert(0, os.path.dirname(__file__))
7+
from dotenv import load_dotenv
8+
load_dotenv('.env.qiniu')
9+
10+
import qiniu
11+
12+
ACCESS_KEY = os.getenv('QINIU_ACCESS_KEY')
13+
SECRET_KEY = os.getenv('QINIU_SECRET_KEY')
14+
BUCKET = os.getenv('QINIU_BUCKET')
15+
CDN_DOMAIN = os.getenv('QINIU_CDN_DOMAIN')
16+
17+
print(f"Bucket: {BUCKET}")
18+
print(f"CDN Domain: {CDN_DOMAIN}")
19+
print(f"Access Key: {ACCESS_KEY[:8]}...")
20+
21+
auth = qiniu.Auth(ACCESS_KEY, SECRET_KEY)
22+
23+
# 上传本地 resources 目录
24+
resources_dir = 'resources'
25+
26+
uploaded = []
27+
failed = []
28+
29+
for root, dirs, files in os.walk(resources_dir):
30+
for filename in files:
31+
local_path = os.path.join(root, filename)
32+
# 七牛云 key = resources/images/xxx.png
33+
key = local_path.replace('\\', '/')
34+
35+
token = auth.upload_token(BUCKET, key)
36+
ret, info = qiniu.put_file(token, key, local_path)
37+
if ret is not None:
38+
url = f"https://{CDN_DOMAIN}/{key}"
39+
uploaded.append((key, url))
40+
print(f"✅ {key} -> {url}")
41+
else:
42+
failed.append((key, info))
43+
print(f"❌ {key} failed: {info}")
44+
45+
print(f"\n完成: {len(uploaded)} 成功, {len(failed)} 失败")
46+
if failed:
47+
print("失败的文件:")
48+
for k, e in failed:
49+
print(f" {k}: {e}")

0 commit comments

Comments
 (0)