Your commit message
This commit is contained in:
0
website/__init__.py
Normal file
0
website/__init__.py
Normal file
16
website/asgi.py
Normal file
16
website/asgi.py
Normal file
@@ -0,0 +1,16 @@
|
||||
"""
|
||||
ASGI config for website project.
|
||||
|
||||
It exposes the ASGI callable as a module-level variable named ``application``.
|
||||
|
||||
For more information on this file, see
|
||||
https://docs.djangoproject.com/en/5.0/howto/deployment/asgi/
|
||||
"""
|
||||
|
||||
import os
|
||||
|
||||
from django.core.asgi import get_asgi_application
|
||||
|
||||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'website.settings')
|
||||
|
||||
application = get_asgi_application()
|
||||
162
website/settings.py
Normal file
162
website/settings.py
Normal file
@@ -0,0 +1,162 @@
|
||||
"""
|
||||
Django settings for website project.
|
||||
|
||||
Generated by 'django-admin startproject' using Django 5.0.3.
|
||||
|
||||
For more information on this file, see
|
||||
https://docs.djangoproject.com/en/5.0/topics/settings/
|
||||
|
||||
For the full list of settings and their values, see
|
||||
https://docs.djangoproject.com/en/5.0/ref/settings/
|
||||
"""
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
# Build paths inside the project like this: BASE_DIR / 'subdir'.
|
||||
BASE_DIR = Path(__file__).resolve().parent.parent
|
||||
# C:/Users/HC/Desktop/视频号/后端/website/MyApi/证书
|
||||
WX_CERT_PATH = '/www/wwwroot/unapp_admin/证书/apiclient_cert.pem' #生产环境
|
||||
WX_KEY_PATH = '/www/wwwroot/unapp_admin/证书/apiclient_key.pem' #生产环境
|
||||
# WX_CERT_PATH = 'C:/Users/HC/Desktop/视频号/后端/website/MyApi/证书/apiclient_cert.pem'
|
||||
# WX_KEY_PATH = 'C:/Users/HC/Desktop/视频号/后端/website/MyApi/证书/apiclient_key.pem'
|
||||
|
||||
# Quick-start development settings - unsuitable for production
|
||||
# See https://docs.djangoproject.com/en/5.0/howto/deployment/checklist/
|
||||
|
||||
# SECURITY WARNING: keep the secret key used in production secret!
|
||||
SECRET_KEY = 'django-insecure-#v*jmev%ibwm-)6fi5ffpb3b@rn_zr9_927nu%(kbxc@uvfj=-'
|
||||
|
||||
WX_APP_ID='wxb5a1857369e5809e'
|
||||
WX_MCH_ID='1674012932'
|
||||
WX_API_KEY='f229acb2c90b93cbc1439c185a2b26fe'
|
||||
WX_NOTIFY_URL='https://unapp.guimiaokeji.com/myapp/wx_pay_notify/'
|
||||
WX_SERIAL_NO='66DB35F836EFD4CBEC66F3815D283A2892310324'
|
||||
# SECURITY WARNING: don't run with debug turned on in production!
|
||||
DEBUG = True
|
||||
# settings.py
|
||||
|
||||
QINIU_ACCESS_KEY = 'AO6u6sDWi6L9TsPfr4awC7FYP85JTjt3bodZACCM'
|
||||
QINIU_SECRET_KEY = '2fjxweGtSAEaUdVgDkWEmN7JbBxHBQDv1cLORb9_'
|
||||
QINIU_BUCKET_NAME = '1726850085'
|
||||
QINIU_DOMAIN = 'file.guimiaokeji.com' # 如 http://yourbucket.qiniudn.com
|
||||
|
||||
|
||||
ALLOWED_HOSTS = ['*']
|
||||
|
||||
|
||||
# Application definition
|
||||
|
||||
INSTALLED_APPS = [
|
||||
'django.contrib.auth',
|
||||
'django.contrib.contenttypes',
|
||||
'django.contrib.sessions',
|
||||
'django.contrib.messages',
|
||||
'django.contrib.staticfiles',
|
||||
'MyApi.apps.MyapiConfig',
|
||||
'MyWeb.apps.MywebConfig',
|
||||
]
|
||||
|
||||
MIDDLEWARE = [
|
||||
'django.middleware.security.SecurityMiddleware',
|
||||
'django.contrib.sessions.middleware.SessionMiddleware',
|
||||
'django.middleware.common.CommonMiddleware',
|
||||
# 'django.middleware.csrf.CsrfViewMiddleware',
|
||||
'django.contrib.auth.middleware.AuthenticationMiddleware',
|
||||
'django.contrib.messages.middleware.MessageMiddleware',
|
||||
'django.middleware.clickjacking.XFrameOptionsMiddleware',
|
||||
]
|
||||
|
||||
ROOT_URLCONF = 'website.urls'
|
||||
|
||||
TEMPLATES = [
|
||||
{
|
||||
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
||||
'DIRS': [BASE_DIR / 'templates']
|
||||
,
|
||||
'APP_DIRS': True,
|
||||
'OPTIONS': {
|
||||
'context_processors': [
|
||||
'django.template.context_processors.debug',
|
||||
'django.template.context_processors.request',
|
||||
'django.contrib.auth.context_processors.auth',
|
||||
'django.contrib.messages.context_processors.messages',
|
||||
],
|
||||
},
|
||||
},
|
||||
]
|
||||
|
||||
WSGI_APPLICATION = 'website.wsgi.application'
|
||||
|
||||
|
||||
# Database
|
||||
# https://docs.djangoproject.com/en/5.0/ref/settings/#databases
|
||||
|
||||
# DATABASES = {
|
||||
# 'default': {
|
||||
# 'ENGINE': 'django.db.backends.sqlite3',
|
||||
# 'NAME': BASE_DIR / 'db.sqlite3',
|
||||
# }
|
||||
# }
|
||||
DATABASES = {
|
||||
'default':
|
||||
{
|
||||
'ENGINE': 'django.db.backends.mysql', # 数据库引擎
|
||||
'NAME': 'uinapp', # 数据库名称
|
||||
'HOST': '81.70.187.27', # 数据库地址,本机 ip 地址 127.0.0.1
|
||||
'PORT': 3306, # mysql端口
|
||||
'USER': 'uinapp', # 数据库用户名
|
||||
'PASSWORD': '6CnCBxZ77cLredtK', # 数据库密码
|
||||
'OPTIONS': {
|
||||
'charset': 'utf8mb4',
|
||||
'use_unicode': True,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
# Password validation
|
||||
# https://docs.djangoproject.com/en/5.0/ref/settings/#auth-password-validators
|
||||
|
||||
AUTH_PASSWORD_VALIDATORS = [
|
||||
{
|
||||
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
|
||||
},
|
||||
{
|
||||
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
|
||||
},
|
||||
{
|
||||
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
|
||||
},
|
||||
{
|
||||
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
|
||||
},
|
||||
]
|
||||
|
||||
|
||||
# Internationalization
|
||||
# https://docs.djangoproject.com/en/5.0/topics/i18n/
|
||||
|
||||
LANGUAGE_CODE = 'en-us'
|
||||
|
||||
TIME_ZONE = 'Asia/Shanghai'
|
||||
|
||||
USE_I18N = True
|
||||
|
||||
USE_TZ = True
|
||||
|
||||
|
||||
# Static files (CSS, JavaScript, Images)
|
||||
# https://docs.djangoproject.com/en/5.0/howto/static-files/
|
||||
MEDIA_ROOT = os.path.join(BASE_DIR, 'uploaded_videos')
|
||||
MEDIA_URL = '/media/'
|
||||
STATIC_URL = 'static/'
|
||||
STATICFILES_DIRS = [
|
||||
#设置静态文件目录
|
||||
os.path.join(BASE_DIR, 'static'),
|
||||
BASE_DIR / 'static',
|
||||
]
|
||||
|
||||
# Default primary key field type
|
||||
# https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-field
|
||||
|
||||
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
|
||||
31
website/urls.py
Normal file
31
website/urls.py
Normal file
@@ -0,0 +1,31 @@
|
||||
"""
|
||||
URL configuration for website project.
|
||||
|
||||
The `urlpatterns` list routes URLs to views. For more information please see:
|
||||
https://docs.djangoproject.com/en/5.0/topics/http/urls/
|
||||
Examples:
|
||||
Function views
|
||||
1. Add an import: from my_app import views
|
||||
2. Add a URL to urlpatterns: path('', views.home, name='home')
|
||||
Class-based views
|
||||
1. Add an import: from other_app.views import Home
|
||||
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
|
||||
Including another URLconf
|
||||
1. Import the include() function: from django.urls import include, path
|
||||
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
|
||||
"""
|
||||
# from django.contrib import admin
|
||||
from django.urls import path, include
|
||||
from . import views
|
||||
from django.conf import settings
|
||||
from django.conf.urls.static import static
|
||||
urlpatterns = [
|
||||
# path('admin/', admin.site.urls),
|
||||
path('myapp/', include('MyApi.urls')), # 包含myapp应用的URL配置
|
||||
path('admin/', include('MyWeb.urls')), # 包含myapp应用的URL配置
|
||||
path('.well-known/acme-challenge/<str:token>', views.acme_challenge),
|
||||
path('download/', views.download_proxy, name='download_proxy'),
|
||||
path('proxy/', views.ProxyView.as_view(), name='proxy'),
|
||||
]
|
||||
if settings.DEBUG:
|
||||
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
|
||||
82
website/views.py
Normal file
82
website/views.py
Normal file
@@ -0,0 +1,82 @@
|
||||
import aiohttp
|
||||
from django.http import HttpResponse, StreamingHttpResponse
|
||||
import os
|
||||
from datetime import datetime
|
||||
import requests
|
||||
from django.utils.decorators import method_decorator
|
||||
from django.views import View
|
||||
from django.views.decorators.csrf import csrf_exempt
|
||||
from urllib.parse import quote
|
||||
from django.http import HttpResponse
|
||||
from aiohttp import ClientSession
|
||||
from django.views.decorators.csrf import csrf_exempt
|
||||
|
||||
def acme_challenge(request, token):
|
||||
print(token)
|
||||
content = f'{token}.E4H2n2zqARGJeN2ulDerea5xYFCDXMy1y9v61hoQfms'
|
||||
return HttpResponse(content, content_type='text/plain')
|
||||
|
||||
|
||||
@csrf_exempt
|
||||
async def download_proxy(request):
|
||||
video_url = request.GET.get('url')
|
||||
if not video_url:
|
||||
return HttpResponse("URL is required", status=400)
|
||||
|
||||
headers = {
|
||||
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'
|
||||
}
|
||||
|
||||
async with ClientSession() as session:
|
||||
try:
|
||||
async with session.get(video_url, headers=headers) as response:
|
||||
if response.status == 200:
|
||||
content = await response.read() # 读取内容到内存中,注意这不适用于大文件
|
||||
|
||||
# 创建一个 HttpResponse 对象并设置 Content-Type
|
||||
response = HttpResponse(content, content_type=response.headers['Content-Type'])
|
||||
|
||||
# 添加自定义响应头
|
||||
response['X-Content-Length'] = str(len(content))
|
||||
|
||||
return response
|
||||
else:
|
||||
return HttpResponse("Failed to download video", status=response.status)
|
||||
except aiohttp.ClientConnectionError:
|
||||
return HttpResponse("Connection closed", status=500)
|
||||
|
||||
|
||||
class ProxyView(View):
|
||||
@method_decorator(csrf_exempt)
|
||||
def dispatch(self, *args, **kwargs):
|
||||
return super().dispatch(*args, **kwargs)
|
||||
|
||||
def get(self, request, *args, **kwargs):
|
||||
# 获取目标URL
|
||||
target_url = request.GET.get('url')
|
||||
if not target_url:
|
||||
return HttpResponse("Missing 'url' parameter", status=400)
|
||||
|
||||
try:
|
||||
# 请求目标URL
|
||||
response = requests.get(target_url, stream=True)
|
||||
response.raise_for_status()
|
||||
except requests.exceptions.RequestException as e:
|
||||
return HttpResponse(f"Error fetching target URL: {e}", status=500)
|
||||
|
||||
# 获取文件类型并强制设置为 video/mp4
|
||||
content_type = 'video/mp4'
|
||||
|
||||
# 获取文件名并强制使用 .mp4 后缀
|
||||
filename = target_url.split("/")[-1].split('?')[0] # 移除查询参数
|
||||
if not filename.endswith('.mp4'):
|
||||
filename = f"{filename}.mp4"
|
||||
|
||||
# 设置Content-Disposition头部,确保文件在浏览器中直接播放而不是下载
|
||||
content_disposition = f'inline; filename="{filename}"'
|
||||
|
||||
# 创建响应对象并将内容传递给客户端
|
||||
proxy_response = HttpResponse(content=response.content, content_type=content_type)
|
||||
proxy_response['Content-Disposition'] = content_disposition
|
||||
|
||||
return proxy_response
|
||||
16
website/wsgi.py
Normal file
16
website/wsgi.py
Normal file
@@ -0,0 +1,16 @@
|
||||
"""
|
||||
WSGI config for website project.
|
||||
|
||||
It exposes the WSGI callable as a module-level variable named ``application``.
|
||||
|
||||
For more information on this file, see
|
||||
https://docs.djangoproject.com/en/5.0/howto/deployment/wsgi/
|
||||
"""
|
||||
|
||||
import os
|
||||
|
||||
from django.core.wsgi import get_wsgi_application
|
||||
|
||||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'website.settings')
|
||||
|
||||
application = get_wsgi_application()
|
||||
Reference in New Issue
Block a user