Skip to content

Commit

Permalink
UGC interface setup
Browse files Browse the repository at this point in the history
  • Loading branch information
mattholy committed May 21, 2024
1 parent ce9a543 commit f44c917
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 38 deletions.
32 changes: 0 additions & 32 deletions backend/alembic/versions/460ee9251587_dev.py

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""dev
Revision ID: 55d5621cc1e9
Revision ID: 646cc544a4c3
Revises:
Create Date: 2024-05-20 23:32:19.819093
Create Date: 2024-05-21 16:18:47.156884
"""
from typing import Sequence, Union
Expand All @@ -12,7 +12,7 @@
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision: str = '55d5621cc1e9'
revision: str = '646cc544a4c3'
down_revision: Union[str, None] = None
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
Expand All @@ -22,12 +22,14 @@ def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('auth_challenges',
sa.Column('uuid', sa.UUID(), nullable=False),
sa.Column('user', sa.String(), nullable=True),
sa.Column('challenge', postgresql.BYTEA(), nullable=False),
sa.Column('user_agent', sa.String(), nullable=False),
sa.Column('access_address', sa.String(), nullable=False),
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True),
sa.PrimaryKeyConstraint('uuid')
)
op.create_index(op.f('ix_auth_challenges_user'), 'auth_challenges', ['user'], unique=False)
op.create_table('auth_passkeys',
sa.Column('id', sa.UUID(), nullable=False),
sa.Column('user', sa.String(), nullable=False),
Expand Down Expand Up @@ -222,6 +224,22 @@ def upgrade() -> None:
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('key')
)
op.create_table('user_assets',
sa.Column('id', sa.BigInteger(), autoincrement=True, nullable=False, comment='自增主键,唯一标识每个资源'),
sa.Column('resource_uuid', sa.UUID(), nullable=False, comment='资源的唯一标识符(UUID),确保每个资源唯一且不变'),
sa.Column('username', sa.Integer(), nullable=False, comment='用户名或用户ID,用于标识资源所属用户'),
sa.Column('mime_type', sa.String(), nullable=False, comment="资源的 MIME 类型,如 'image/png', 'video/mp4' 等"),
sa.Column('content_length', sa.BigInteger(), nullable=False, comment='资源大小,单位字节'),
sa.Column('s3_url', sa.String(), nullable=True, comment='如果资源存储在 S3 中,这里保存其 URL'),
sa.Column('content', sa.LargeBinary(), nullable=True, comment='如果资源存储在数据库中,这里保存其二进制数据'),
sa.Column('uploaded_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True, comment='资源上传时间,默认值为当前时间'),
sa.Column('deleted', sa.Boolean(), nullable=False, comment='标记记录是否被删除,软删除标志'),
sa.Column('tags', postgresql.ARRAY(sa.String()), nullable=True, comment='标签字段,存储字符串数组,用于标记资源内容'),
sa.Column('checksum', sa.String(), nullable=False, comment='文件内容的校验和,用于验证文件完整性'),
sa.Column('public', sa.Boolean(), nullable=False, comment='标记资源是否公开,公开资源可以被所有用户访问'),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('resource_uuid')
)
op.create_table('user_config',
sa.Column('id', sa.BigInteger(), autoincrement=True, nullable=False),
sa.Column('user', sa.String(), nullable=False),
Expand Down Expand Up @@ -259,6 +277,7 @@ def downgrade() -> None:
op.drop_index(op.f('ix_user_config_user'), table_name='user_config')
op.drop_index(op.f('ix_user_config_key'), table_name='user_config')
op.drop_table('user_config')
op.drop_table('user_assets')
op.drop_table('system_config')
op.drop_table('rules')
op.drop_table('oauth_authorization_codes')
Expand Down Expand Up @@ -293,5 +312,6 @@ def downgrade() -> None:
op.drop_index(op.f('ix_auth_passkeys_user'), table_name='auth_passkeys')
op.drop_index(op.f('ix_auth_passkeys_raw_id'), table_name='auth_passkeys')
op.drop_table('auth_passkeys')
op.drop_index(op.f('ix_auth_challenges_user'), table_name='auth_challenges')
op.drop_table('auth_challenges')
# ### end Alembic commands ###
1 change: 1 addition & 0 deletions backend/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ def generate_uuid_from_ip():
BACKEND_URL = os.environ.get('CLUSTER_ID', 'http://localhost:8000')

RELEASE_VERSION = os.environ.get('RELEASE_TAG', get_git_commit_id())
PUBLIC_BASE_URL = '/public'
API_BASE_URL = '/api'

RP_SOURCE = os.environ.get('CLUSTER_ID', 'http://localhost:5173')
Expand Down
2 changes: 2 additions & 0 deletions backend/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from routers.nodeinfo.router import router as nodeinfo_router
from routers.setup.router import router as setup_router
from routers.oauth.router import router as oauth_router
from routers.public.router import router as public_router
from utils.logger import async_log_error_to_db, logger
from env import NODE_ID, BACKEND_URL, ALLOWED_ORIGINS
from utils.system.security import async_load_key_pair
Expand Down Expand Up @@ -84,6 +85,7 @@ async def fetch_favicon():
app.include_router(nodeinfo_router)
app.include_router(setup_router)
app.include_router(oauth_router)
app.include_router(public_router)

# 静态文件服务
app.mount("/", StaticFiles(directory="static"), name="Frontend Pages")
Expand Down
26 changes: 25 additions & 1 deletion backend/routers/api/m1/user/endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def set_empty_string_if_none(cls, v):
return v or ""

@validator('fields', pre=True, always=True)
def set_empty_string_if_none(cls, v):
def set_empty_list_if_none(cls, v):
return v or []


Expand Down Expand Up @@ -183,3 +183,27 @@ async def fetch_user_profile(profile_data: UserProfile, user_session: UserSessio
)
await db.commit()
return UserProfilesResp(payload=user_profile)


@router.get('/asset/{res_id: uuid.UUID}', tags=['Assets'])
async def fetch_user_asset(res_id: uuid.UUID, user_session: UserSession = Depends(get_current_user_session), db: AsyncSession = Depends(get_db)):
'''Get the asset of the user'''
pass


@router.get('/asset', tags=['Assets'])
async def fetch_user_asset(res_id: uuid.UUID, user_session: UserSession = Depends(get_current_user_session), db: AsyncSession = Depends(get_db)):
'''Get the assets list of the user'''
pass


@router.post('/asset/{res_id: uuid.UUID}', tags=['Assets'])
async def update_user_asset(res_id: uuid.UUID, user_session: UserSession = Depends(get_current_user_session), db: AsyncSession = Depends(get_db)):
'''Post any binary here to update the asset, post nothing to delete the asset'''
pass


@router.post('/asset', tags=['Assets'])
async def upload_user_asset(user_session: UserSession = Depends(get_current_user_session), db: AsyncSession = Depends(get_db)):
'''Post any binary here to upload user asset'''
pass
24 changes: 24 additions & 0 deletions backend/routers/public/asset/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# -*- encoding: utf-8 -*-
'''
__init__.py
----
Public asset router
@Time : 2024/05/21 16:08:53
@Author : Mattholy
@Version : 1.0
@Contact : [email protected]
@License : MIT License
'''

import uuid
from fastapi import APIRouter
from fastapi.responses import FileResponse

router = APIRouter(prefix='/asset', tags=['Assets'])


@router.get('/{res_id: uuid.UUID}', response_class=FileResponse)
async def fetch_public_asset(res_id: uuid.UUID):
pass
22 changes: 22 additions & 0 deletions backend/routers/public/router.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# -*- encoding: utf-8 -*-
'''
router.py
----
put some words here
@Time : 2024/05/21 16:06:41
@Author : Mattholy
@Version : 1.0
@Contact : [email protected]
@License : MIT License
'''

from fastapi import APIRouter
from routers.public.asset import router as asset_router

from env import PUBLIC_BASE_URL

router = APIRouter(prefix=PUBLIC_BASE_URL, tags=['Public Access'])

router.include_router(asset_router)
6 changes: 4 additions & 2 deletions backend/utils/model/orm.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,11 +305,13 @@ class UserAssets(Base):
comment="如果资源存储在数据库中,这里保存其二进制数据")
uploaded_at = Column(DateTime(timezone=True),
server_default=func.now(), comment="资源上传时间,默认值为当前时间")
is_deleted = Column(Boolean, default=False,
nullable=False, comment="标记记录是否被删除,软删除标志")
deleted = Column(Boolean, default=False,
nullable=False, comment="标记记录是否被删除,软删除标志")
tags = Column(ARRAY(String), nullable=True,
comment="标签字段,存储字符串数组,用于标记资源内容")
checksum = Column(String, nullable=False, comment="文件内容的校验和,用于验证文件完整性")
public = Column(Boolean, default=True, nullable=False,
comment="标记资源是否公开,公开资源可以被所有用户访问")

def __repr__(self):
return (
Expand Down

0 comments on commit f44c917

Please sign in to comment.