feat: custom CDN for GitHub storage (#172) (#174)

Co-authored-by: Innei <tukon479@gmail.com>
This commit is contained in:
Chrys
2025-11-30 23:36:05 +08:00
committed by GitHub
parent 7b02aeaba6
commit a4ed1ecbc2
11 changed files with 107 additions and 2 deletions

View File

@@ -170,6 +170,14 @@ export type GitHubConfig = {
token?: string
path?: string
useRawUrl?: boolean
/**
* Optional custom CDN domain for generated URLs.
* When set, URLs will use this domain instead of raw.githubusercontent.com.
* Useful for jsDelivr, Cloudflare CDN, or other GitHub CDN proxies.
* @example 'cdn.jsdelivr.net/gh/owner/repo@branch'
* @example 'cdn.example.com'
*/
customDomain?: string
}
export type LocalConfig = {

View File

@@ -96,9 +96,38 @@ const githubConfig: StorageConfig = {
token: 'ghp_xxxxxxxxxxxx', // GitHub 访问令牌(可选)
path: 'photos', // 照片存储路径(可选)
useRawUrl: true, // 使用 raw.githubusercontent.com默认 true
customDomain: 'cdn.jsdelivr.net/gh/your-username/photo-gallery@main', // 自定义 CDN 域名(可选)
}
```
### 自定义 CDN 域名
如果 `raw.githubusercontent.com` 在你的地区访问速度较慢,可以配置自定义 CDN
```typescript
const githubConfig: StorageConfig = {
provider: 'github',
owner: 'your-username',
repo: 'photo-gallery',
branch: 'main',
path: 'photos',
// 使用 jsDelivr CDN
customDomain: 'cdn.jsdelivr.net/gh/your-username/photo-gallery@main',
}
```
**常用 CDN 选项:**
- **jsDelivr**: `cdn.jsdelivr.net/gh/owner/repo@branch`
- **Statically**: `cdn.statically.io/gh/owner/repo/branch`
- **自定义代理**: 你自己的 CDN 代理域名
当配置了 `customDomain` 时,它会优先于 `useRawUrl`。生成的 URL 格式为:
```
https://{customDomain}/{path}/{filename}
```
### 设置步骤
1. **创建 GitHub 仓库**
@@ -123,6 +152,7 @@ const githubConfig: StorageConfig = {
```
4. **更新配置文件**
```typescript
// builder.config.ts
export const builderConfig: BuilderConfig = {

View File

@@ -341,6 +341,12 @@ export class GitHubStorageProvider implements StorageProvider {
generatePublicUrl(key: string): string {
const fullPath = this.getFullPath(key)
// 如果设置了自定义 CDN 域名,直接使用
if (this.githubConfig.customDomain) {
const customDomain = this.githubConfig.customDomain.replace(/\/+$/, '') // 移除末尾的斜杠
return `https://${customDomain.replace(/^https?:\/\//, '')}/${fullPath}`
}
if (this.githubConfig.useRawUrl) {
// 使用 raw.githubusercontent.com 获取文件
return `https://raw.githubusercontent.com/${this.githubConfig.owner}/${this.githubConfig.repo}/${this.githubConfig.branch}/${fullPath}`