Skip to content

Commit

Permalink
feat: support tg storage type
Browse files Browse the repository at this point in the history
  • Loading branch information
zmh-program committed Apr 22, 2024
1 parent a50be1b commit cc890e6
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 5 deletions.
17 changes: 12 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ Response

## Image Storage Config
1. ✨ No Storage (Default)
- [x] Base64 Encoding
- [x] No Storage Required & No External Dependencies
- [x] **No Storage Required & No External Dependencies**
- [x] Support Serverless Deployment **Without Storage** (e.g. Vercel)

2. 📁 Local Storage
Expand All @@ -96,8 +95,8 @@ Response
- set env `S3_BUCKET` to your AWS S3 Bucket Name
- set env `S3_REGION` to your AWS S3 Region

4. 🔔 Cloudflare R2
- [x] **Free Storage Quota ([10GB Storage](https://developers.cloudflare.com/r2/pricing/))**
4. 🔔 [Cloudflare R2](https://www.cloudflare.com/zh-cn/developer-platform/r2)
- [x] **Free Storage Quota ([10GB Storage & Zero Outbound Cost]((https://developers.cloudflare.com/r2/pricing/)))**
- [x] Support Direct URL Access
- Config *(S3 Compatible)*:
- set env `STORAGE_TYPE` to `s3` (e.g. `STORAGE_TYPE=s3`)
Expand All @@ -107,7 +106,7 @@ Response
- set env `S3_DOMAIN` to your Cloudflare R2 Domain Name (e.g. `https://<account-id>.r2.cloudflarestorage.com`)
- set env `S3_DIRECT_URL_DOMAIN` to your Cloudflare R2 Public URL Access Domain Name ([Open Public URL Access](https://developers.cloudflare.com/r2/buckets/public-buckets/), e.g. `https://pub-xxx.r2.dev`)

5. 📦 Min IO
5. 📦 [Min IO](https://min.io)
- [x] **Self Hosted**
- [x] Reliable & Flexible Storage
- Config *(S3 Compatible)*:
Expand All @@ -118,3 +117,11 @@ Response
- set env `S3_BUCKET` to your Min IO Bucket Name
- set env `S3_DOMAIN` to your Min IO Domain Name (e.g. `https://oss.example.com`)
- *[Optional] If you are using CDN, you can set `S3_DIRECT_URL_DOMAIN` to your Min IO Public URL Access Domain Name (e.g. `https://cdn-hk.example.com`)*

6.[Telegram CDN](https://github.com/csznet/tgState)
- [x] **Free Storage (Rate Limit)**
- [x] Support Direct URL Access *(China Mainland User Unfriendly)*
- [x] Config:
- set env `STORAGE_TYPE` to `tg` (e.g. `STORAGE_TYPE=tg`)
- set env `TG_ENDPOINT` to your TG-STATE Endpoint (e.g. `TG_ENDPOINT=https://tgstate.vercel.app`)
- *[Optional] if you are using password authentication, you can set `TG_PASSWORD` to your TG-STATE Password*
4 changes: 4 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,7 @@
S3_API = S3_DOMAIN or f"https://{S3_BUCKET}.s3.{S3_REGION}.amazonaws.com" # S3 API
S3_SPACE = S3_DIRECT_URL_DOMAIN or S3_API # S3 Image URL Domain

TG_ENDPOINT = environ.get("TG_ENDPOINT", "").rstrip("/") # Telegram Endpoint
TG_PASSWORD = environ.get("TG_PASSWORD", "") # Telegram Password

TG_API = TG_ENDPOINT + "/api" + (f"?pass={TG_PASSWORD}" if TG_PASSWORD and len(TG_PASSWORD) > 0 else "")
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
requests
fastapi
uvicorn[standard]
python-multipart
Expand Down
2 changes: 2 additions & 0 deletions store/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
from store.common import process_base64
from store.local import process_local
from store.s3 import process_s3
from store.telegram import process_tg

IMAGE_HANDLERS = {
"common": process_base64,
"local": process_local,
"s3": process_s3,
"tg": process_tg,
}


Expand Down
19 changes: 19 additions & 0 deletions store/telegram.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from fastapi import UploadFile
import requests
from config import TG_API


async def process_tg(file: UploadFile) -> str:
"""Process image and return its telegram url."""
response = requests.post(
TG_API,
files={"image": (file.filename, file.file, file.content_type)},
)
response.raise_for_status()

data = response.json()
url = data.get("url")
if not url:
raise ValueError(f"Telegram API error: {data}")

return url

0 comments on commit cc890e6

Please sign in to comment.