commit d2352318f4171a4d2c3287274784b6961d7ffb83 Author: hxuanyu <2252193204@qq.com> Date: Tue Jan 13 17:00:49 2026 +0800 项目初始化 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a28a1ce --- /dev/null +++ b/.gitignore @@ -0,0 +1,85 @@ +# ========================= +# Go 常用 .gitignore +# ========================= + +# 编译产物 / 可执行文件 +*.exe +*.exe~ +*.dll +*.so +*.dylib +*.a +*.o +*.out +*.test +*.prof +*.pprof +*.cover +*.cov +*.trace + +# Go workspace / 依赖缓存(本地开发常见,不建议入库) +/bin/ +/pkg/ +/dist/ +/build/ +/out/ + +# Go build cache(通常不需要忽略;如你有需要可开启) +# /tmp/ +# /cache/ + +# 调试/日志/临时文件 +*.log +*.tmp +*.swp +*.swo +*.bak +*.old +*.pid + +# 运行时数据/本地数据 +*.db +*.sqlite +*.sqlite3 +data/ +storage/ +tmp/ + +# 环境变量与配置(按需:如果你会提交示例配置,建议仅忽略真实配置文件) +.env +.env.* +!.env.example +config.local.* +*.local.yaml +*.local.yml +*.local.json + +# Go 测试覆盖率 +coverage.out +cover.out + +# vendor(Go Modules 通常不提交 vendor;如你需要 vendor 则删除这一行) +/vendor/ + +# 工具生成文件 +*.gen.go + +# IDE / 编辑器 +.idea/ +.vscode/ +*.code-workspace + +# macOS / Windows +.DS_Store +Thumbs.db +desktop.ini + +# Vim / Emacs +*~ +\#*\# +.\#* + +# GoLand/IntelliJ +*.iml +/storage_data/ diff --git a/config/config.yaml b/config/config.yaml new file mode 100644 index 0000000..506958a --- /dev/null +++ b/config/config.yaml @@ -0,0 +1,40 @@ +site: + name: "文件暂存柜" + description: "临时文件中转服务" + +security: + # 管理员密码的 bcrypt 哈希。如果首次运行且此处为空,系统将自动生成随机密码并打印在控制台 + admin_password_hash: "$2a$10$Bm0TEmU4uj.bVHYiIPFBheUkcdg6XHpsanLvmpoAtgU1UnKbo9.vy" # 默认密码: admin + pickup_code_length: 6 + pickup_fail_limit: 5 + jwt_secret: "file-relay-secret" + +upload: + max_file_size_mb: 500 + max_batch_files: 20 + max_retention_days: 30 + +storage: + type: "local" + local: + path: "storage_data" + webdav: + url: "https://dav.example.com" + username: "user" + password: "pass" + root: "/file-relay" + s3: + endpoint: "s3.amazonaws.com" + region: "us-east-1" + access_key: "your-access-key" + secret_key: "your-secret-key" + bucket: "file-relay-bucket" + use_ssl: true + +api_token: + enabled: true + allow_admin_api: false + max_tokens: 20 + +database: + path: "file_relay.db" diff --git a/docs/docs.go b/docs/docs.go new file mode 100644 index 0000000..700e62b --- /dev/null +++ b/docs/docs.go @@ -0,0 +1,912 @@ +// Package docs Code generated by swaggo/swag. DO NOT EDIT +package docs + +import "github.com/swaggo/swag" + +const docTemplate = `{ + "schemes": {{ marshal .Schemes }}, + "swagger": "2.0", + "info": { + "description": "{{escape .Description}}", + "title": "{{.Title}}", + "termsOfService": "http://swagger.io/terms/", + "contact": { + "name": "API Support", + "url": "http://www.swagger.io/support", + "email": "support@swagger.io" + }, + "license": { + "name": "Apache 2.0", + "url": "http://www.apache.org/licenses/LICENSE-2.0.html" + }, + "version": "{{.Version}}" + }, + "host": "{{.Host}}", + "basePath": "{{.BasePath}}", + "paths": { + "/admin/api-tokens": { + "get": { + "security": [ + { + "AdminAuth": [] + } + ], + "description": "获取系统中所有 API Token 的详详信息(不包含哈希)", + "produces": [ + "application/json" + ], + "tags": [ + "Admin" + ], + "summary": "获取 API Token 列表", + "responses": { + "200": { + "description": "OK", + "schema": { + "allOf": [ + { + "$ref": "#/definitions/model.Response" + }, + { + "type": "object", + "properties": { + "data": { + "type": "array", + "items": { + "$ref": "#/definitions/model.APIToken" + } + } + } + } + ] + } + }, + "401": { + "description": "Unauthorized", + "schema": { + "$ref": "#/definitions/model.Response" + } + } + } + }, + "post": { + "security": [ + { + "AdminAuth": [] + } + ], + "description": "创建一个新的 API Token,返回原始 Token(仅显示一次)", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Admin" + ], + "summary": "创建 API Token", + "parameters": [ + { + "description": "Token 信息", + "name": "request", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/admin.CreateTokenRequest" + } + } + ], + "responses": { + "201": { + "description": "Created", + "schema": { + "allOf": [ + { + "$ref": "#/definitions/model.Response" + }, + { + "type": "object", + "properties": { + "data": { + "$ref": "#/definitions/admin.CreateTokenResponse" + } + } + } + ] + } + }, + "400": { + "description": "Bad Request", + "schema": { + "$ref": "#/definitions/model.Response" + } + } + } + } + }, + "/admin/api-tokens/{id}": { + "delete": { + "security": [ + { + "AdminAuth": [] + } + ], + "description": "根据 ID 永久删除 API Token", + "produces": [ + "application/json" + ], + "tags": [ + "Admin" + ], + "summary": "删除 API Token", + "parameters": [ + { + "type": "integer", + "description": "Token ID", + "name": "id", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/model.Response" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/model.Response" + } + } + } + } + }, + "/admin/batch/{batch_id}": { + "get": { + "security": [ + { + "AdminAuth": [] + } + ], + "description": "根据批次 ID 获取批次信息及关联的文件列表", + "produces": [ + "application/json" + ], + "tags": [ + "Admin" + ], + "summary": "获取批次详情", + "parameters": [ + { + "type": "integer", + "description": "批次 ID", + "name": "batch_id", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "allOf": [ + { + "$ref": "#/definitions/model.Response" + }, + { + "type": "object", + "properties": { + "data": { + "$ref": "#/definitions/model.FileBatch" + } + } + } + ] + } + }, + "404": { + "description": "Not Found", + "schema": { + "$ref": "#/definitions/model.Response" + } + } + } + }, + "put": { + "security": [ + { + "AdminAuth": [] + } + ], + "description": "允许修改备注、过期策略、最大下载次数、状态等", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Admin" + ], + "summary": "修改批次信息", + "parameters": [ + { + "type": "integer", + "description": "批次 ID", + "name": "batch_id", + "in": "path", + "required": true + }, + { + "description": "修改内容", + "name": "request", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/admin.UpdateBatchRequest" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "allOf": [ + { + "$ref": "#/definitions/model.Response" + }, + { + "type": "object", + "properties": { + "data": { + "$ref": "#/definitions/model.FileBatch" + } + } + } + ] + } + }, + "400": { + "description": "Bad Request", + "schema": { + "$ref": "#/definitions/model.Response" + } + } + } + }, + "delete": { + "security": [ + { + "AdminAuth": [] + } + ], + "description": "标记批次为已删除,并物理删除关联的存储文件", + "produces": [ + "application/json" + ], + "tags": [ + "Admin" + ], + "summary": "删除批次", + "parameters": [ + { + "type": "integer", + "description": "批次 ID", + "name": "batch_id", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/model.Response" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/model.Response" + } + } + } + } + }, + "/admin/batches": { + "get": { + "security": [ + { + "AdminAuth": [] + } + ], + "description": "分页查询所有文件批次,支持按状态过滤和取件码模糊搜索", + "produces": [ + "application/json" + ], + "tags": [ + "Admin" + ], + "summary": "获取批次列表", + "parameters": [ + { + "type": "integer", + "description": "页码 (默认 1)", + "name": "page", + "in": "query" + }, + { + "type": "integer", + "description": "每页数量 (默认 20)", + "name": "page_size", + "in": "query" + }, + { + "type": "string", + "description": "状态 (active/expired/deleted)", + "name": "status", + "in": "query" + }, + { + "type": "string", + "description": "取件码 (模糊搜索)", + "name": "pickup_code", + "in": "query" + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "allOf": [ + { + "$ref": "#/definitions/model.Response" + }, + { + "type": "object", + "properties": { + "data": { + "$ref": "#/definitions/admin.ListBatchesResponse" + } + } + } + ] + } + }, + "401": { + "description": "Unauthorized", + "schema": { + "$ref": "#/definitions/model.Response" + } + } + } + } + }, + "/admin/login": { + "post": { + "description": "通过密码换取 JWT Token", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Admin" + ], + "summary": "管理员登录", + "parameters": [ + { + "description": "登录请求", + "name": "request", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/admin.LoginRequest" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "allOf": [ + { + "$ref": "#/definitions/model.Response" + }, + { + "type": "object", + "properties": { + "data": { + "$ref": "#/definitions/admin.LoginResponse" + } + } + } + ] + } + }, + "401": { + "description": "Unauthorized", + "schema": { + "$ref": "#/definitions/model.Response" + } + } + } + } + }, + "/api/download/batch/{pickup_code}": { + "get": { + "description": "根据取件码将批次内的所有文件打包为 ZIP 格式一次性下载", + "produces": [ + "application/zip" + ], + "tags": [ + "Public" + ], + "summary": "批量下载文件", + "parameters": [ + { + "type": "string", + "description": "取件码", + "name": "pickup_code", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "type": "file" + } + }, + "404": { + "description": "Not Found", + "schema": { + "$ref": "#/definitions/model.Response" + } + } + } + } + }, + "/api/download/file/{file_id}": { + "get": { + "description": "根据文件 ID 下载单个文件", + "produces": [ + "application/octet-stream" + ], + "tags": [ + "Public" + ], + "summary": "下载单个文件", + "parameters": [ + { + "type": "integer", + "description": "文件 ID", + "name": "file_id", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "type": "file" + } + }, + "404": { + "description": "Not Found", + "schema": { + "$ref": "#/definitions/model.Response" + } + }, + "410": { + "description": "Gone", + "schema": { + "$ref": "#/definitions/model.Response" + } + } + } + } + }, + "/api/pickup/{pickup_code}": { + "get": { + "description": "根据取件码获取文件批次详详情和文件列表", + "produces": [ + "application/json" + ], + "tags": [ + "Public" + ], + "summary": "获取批次信息", + "parameters": [ + { + "type": "string", + "description": "取件码", + "name": "pickup_code", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "allOf": [ + { + "$ref": "#/definitions/model.Response" + }, + { + "type": "object", + "properties": { + "data": { + "$ref": "#/definitions/public.PickupResponse" + } + } + } + ] + } + }, + "404": { + "description": "Not Found", + "schema": { + "$ref": "#/definitions/model.Response" + } + } + } + } + }, + "/api/upload": { + "post": { + "description": "上传一个或多个文件并创建一个提取批次", + "consumes": [ + "multipart/form-data" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Public" + ], + "summary": "上传文件", + "parameters": [ + { + "type": "file", + "description": "文件列表", + "name": "files", + "in": "formData", + "required": true + }, + { + "type": "string", + "description": "备注", + "name": "remark", + "in": "formData" + }, + { + "type": "string", + "description": "过期类型 (time/download/permanent)", + "name": "expire_type", + "in": "formData" + }, + { + "type": "integer", + "description": "过期天数 (针对 time 类型)", + "name": "expire_days", + "in": "formData" + }, + { + "type": "integer", + "description": "最大下载次数 (针对 download 类型)", + "name": "max_downloads", + "in": "formData" + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "allOf": [ + { + "$ref": "#/definitions/model.Response" + }, + { + "type": "object", + "properties": { + "data": { + "$ref": "#/definitions/public.UploadResponse" + } + } + } + ] + } + }, + "400": { + "description": "Bad Request", + "schema": { + "$ref": "#/definitions/model.Response" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/model.Response" + } + } + } + } + } + }, + "definitions": { + "admin.CreateTokenRequest": { + "type": "object", + "required": [ + "name" + ], + "properties": { + "expire_at": { + "type": "string" + }, + "name": { + "type": "string", + "example": "Test Token" + }, + "scope": { + "type": "string", + "example": "upload,pickup" + } + } + }, + "admin.CreateTokenResponse": { + "type": "object", + "properties": { + "data": { + "$ref": "#/definitions/model.APIToken" + }, + "token": { + "type": "string" + } + } + }, + "admin.ListBatchesResponse": { + "type": "object", + "properties": { + "data": { + "type": "array", + "items": { + "$ref": "#/definitions/model.FileBatch" + } + }, + "page": { + "type": "integer" + }, + "page_size": { + "type": "integer" + }, + "total": { + "type": "integer" + } + } + }, + "admin.LoginRequest": { + "type": "object", + "required": [ + "password" + ], + "properties": { + "password": { + "type": "string", + "example": "admin" + } + } + }, + "admin.LoginResponse": { + "type": "object", + "properties": { + "token": { + "type": "string" + } + } + }, + "admin.UpdateBatchRequest": { + "type": "object", + "properties": { + "expire_at": { + "type": "string" + }, + "expire_type": { + "type": "string" + }, + "max_downloads": { + "type": "integer" + }, + "remark": { + "type": "string" + }, + "status": { + "type": "string" + } + } + }, + "model.APIToken": { + "type": "object", + "properties": { + "created_at": { + "type": "string" + }, + "expire_at": { + "type": "string" + }, + "id": { + "type": "integer" + }, + "last_used_at": { + "type": "string" + }, + "name": { + "type": "string" + }, + "revoked": { + "type": "boolean" + }, + "scope": { + "type": "string" + } + } + }, + "model.FileBatch": { + "type": "object", + "properties": { + "created_at": { + "type": "string" + }, + "download_count": { + "type": "integer" + }, + "expire_at": { + "type": "string" + }, + "expire_type": { + "description": "time / download / permanent", + "type": "string" + }, + "file_items": { + "type": "array", + "items": { + "$ref": "#/definitions/model.FileItem" + } + }, + "id": { + "type": "integer" + }, + "max_downloads": { + "type": "integer" + }, + "pickup_code": { + "type": "string" + }, + "remark": { + "type": "string" + }, + "status": { + "description": "active / expired / deleted", + "type": "string" + }, + "updated_at": { + "type": "string" + } + } + }, + "model.FileItem": { + "type": "object", + "properties": { + "batch_id": { + "type": "integer" + }, + "created_at": { + "type": "string" + }, + "id": { + "type": "integer" + }, + "mime_type": { + "type": "string" + }, + "original_name": { + "type": "string" + }, + "size": { + "type": "integer" + }, + "storage_path": { + "type": "string" + } + } + }, + "model.Response": { + "type": "object", + "properties": { + "code": { + "type": "integer", + "example": 200 + }, + "data": {}, + "msg": { + "type": "string", + "example": "success" + } + } + }, + "public.PickupResponse": { + "type": "object", + "properties": { + "download_count": { + "type": "integer" + }, + "expire_at": { + "type": "string" + }, + "expire_type": { + "type": "string" + }, + "files": { + "type": "array", + "items": { + "$ref": "#/definitions/model.FileItem" + } + }, + "max_downloads": { + "type": "integer" + }, + "remark": { + "type": "string" + } + } + }, + "public.UploadResponse": { + "type": "object", + "properties": { + "batch_id": { + "type": "integer" + }, + "expire_at": { + "type": "string" + }, + "pickup_code": { + "type": "string" + } + } + } + }, + "securityDefinitions": { + "AdminAuth": { + "description": "Type \"Bearer \u003cyour-jwt-token\u003e\" to authenticate.", + "type": "apiKey", + "name": "Authorization", + "in": "header" + } + } +}` + +// SwaggerInfo holds exported Swagger Info so clients can modify it +var SwaggerInfo = &swag.Spec{ + Version: "1.0", + Host: "", + BasePath: "/", + Schemes: []string{}, + Title: "文件暂存柜 API", + Description: "自托管的文件暂存柜后端系统 API 文档", + InfoInstanceName: "swagger", + SwaggerTemplate: docTemplate, + LeftDelim: "{{", + RightDelim: "}}", +} + +func init() { + swag.Register(SwaggerInfo.InstanceName(), SwaggerInfo) +} diff --git a/docs/swagger.json b/docs/swagger.json new file mode 100644 index 0000000..15bf873 --- /dev/null +++ b/docs/swagger.json @@ -0,0 +1,887 @@ +{ + "swagger": "2.0", + "info": { + "description": "自托管的文件暂存柜后端系统 API 文档", + "title": "文件暂存柜 API", + "termsOfService": "http://swagger.io/terms/", + "contact": { + "name": "API Support", + "url": "http://www.swagger.io/support", + "email": "support@swagger.io" + }, + "license": { + "name": "Apache 2.0", + "url": "http://www.apache.org/licenses/LICENSE-2.0.html" + }, + "version": "1.0" + }, + "basePath": "/", + "paths": { + "/admin/api-tokens": { + "get": { + "security": [ + { + "AdminAuth": [] + } + ], + "description": "获取系统中所有 API Token 的详详信息(不包含哈希)", + "produces": [ + "application/json" + ], + "tags": [ + "Admin" + ], + "summary": "获取 API Token 列表", + "responses": { + "200": { + "description": "OK", + "schema": { + "allOf": [ + { + "$ref": "#/definitions/model.Response" + }, + { + "type": "object", + "properties": { + "data": { + "type": "array", + "items": { + "$ref": "#/definitions/model.APIToken" + } + } + } + } + ] + } + }, + "401": { + "description": "Unauthorized", + "schema": { + "$ref": "#/definitions/model.Response" + } + } + } + }, + "post": { + "security": [ + { + "AdminAuth": [] + } + ], + "description": "创建一个新的 API Token,返回原始 Token(仅显示一次)", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Admin" + ], + "summary": "创建 API Token", + "parameters": [ + { + "description": "Token 信息", + "name": "request", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/admin.CreateTokenRequest" + } + } + ], + "responses": { + "201": { + "description": "Created", + "schema": { + "allOf": [ + { + "$ref": "#/definitions/model.Response" + }, + { + "type": "object", + "properties": { + "data": { + "$ref": "#/definitions/admin.CreateTokenResponse" + } + } + } + ] + } + }, + "400": { + "description": "Bad Request", + "schema": { + "$ref": "#/definitions/model.Response" + } + } + } + } + }, + "/admin/api-tokens/{id}": { + "delete": { + "security": [ + { + "AdminAuth": [] + } + ], + "description": "根据 ID 永久删除 API Token", + "produces": [ + "application/json" + ], + "tags": [ + "Admin" + ], + "summary": "删除 API Token", + "parameters": [ + { + "type": "integer", + "description": "Token ID", + "name": "id", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/model.Response" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/model.Response" + } + } + } + } + }, + "/admin/batch/{batch_id}": { + "get": { + "security": [ + { + "AdminAuth": [] + } + ], + "description": "根据批次 ID 获取批次信息及关联的文件列表", + "produces": [ + "application/json" + ], + "tags": [ + "Admin" + ], + "summary": "获取批次详情", + "parameters": [ + { + "type": "integer", + "description": "批次 ID", + "name": "batch_id", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "allOf": [ + { + "$ref": "#/definitions/model.Response" + }, + { + "type": "object", + "properties": { + "data": { + "$ref": "#/definitions/model.FileBatch" + } + } + } + ] + } + }, + "404": { + "description": "Not Found", + "schema": { + "$ref": "#/definitions/model.Response" + } + } + } + }, + "put": { + "security": [ + { + "AdminAuth": [] + } + ], + "description": "允许修改备注、过期策略、最大下载次数、状态等", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Admin" + ], + "summary": "修改批次信息", + "parameters": [ + { + "type": "integer", + "description": "批次 ID", + "name": "batch_id", + "in": "path", + "required": true + }, + { + "description": "修改内容", + "name": "request", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/admin.UpdateBatchRequest" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "allOf": [ + { + "$ref": "#/definitions/model.Response" + }, + { + "type": "object", + "properties": { + "data": { + "$ref": "#/definitions/model.FileBatch" + } + } + } + ] + } + }, + "400": { + "description": "Bad Request", + "schema": { + "$ref": "#/definitions/model.Response" + } + } + } + }, + "delete": { + "security": [ + { + "AdminAuth": [] + } + ], + "description": "标记批次为已删除,并物理删除关联的存储文件", + "produces": [ + "application/json" + ], + "tags": [ + "Admin" + ], + "summary": "删除批次", + "parameters": [ + { + "type": "integer", + "description": "批次 ID", + "name": "batch_id", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/model.Response" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/model.Response" + } + } + } + } + }, + "/admin/batches": { + "get": { + "security": [ + { + "AdminAuth": [] + } + ], + "description": "分页查询所有文件批次,支持按状态过滤和取件码模糊搜索", + "produces": [ + "application/json" + ], + "tags": [ + "Admin" + ], + "summary": "获取批次列表", + "parameters": [ + { + "type": "integer", + "description": "页码 (默认 1)", + "name": "page", + "in": "query" + }, + { + "type": "integer", + "description": "每页数量 (默认 20)", + "name": "page_size", + "in": "query" + }, + { + "type": "string", + "description": "状态 (active/expired/deleted)", + "name": "status", + "in": "query" + }, + { + "type": "string", + "description": "取件码 (模糊搜索)", + "name": "pickup_code", + "in": "query" + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "allOf": [ + { + "$ref": "#/definitions/model.Response" + }, + { + "type": "object", + "properties": { + "data": { + "$ref": "#/definitions/admin.ListBatchesResponse" + } + } + } + ] + } + }, + "401": { + "description": "Unauthorized", + "schema": { + "$ref": "#/definitions/model.Response" + } + } + } + } + }, + "/admin/login": { + "post": { + "description": "通过密码换取 JWT Token", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Admin" + ], + "summary": "管理员登录", + "parameters": [ + { + "description": "登录请求", + "name": "request", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/admin.LoginRequest" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "allOf": [ + { + "$ref": "#/definitions/model.Response" + }, + { + "type": "object", + "properties": { + "data": { + "$ref": "#/definitions/admin.LoginResponse" + } + } + } + ] + } + }, + "401": { + "description": "Unauthorized", + "schema": { + "$ref": "#/definitions/model.Response" + } + } + } + } + }, + "/api/download/batch/{pickup_code}": { + "get": { + "description": "根据取件码将批次内的所有文件打包为 ZIP 格式一次性下载", + "produces": [ + "application/zip" + ], + "tags": [ + "Public" + ], + "summary": "批量下载文件", + "parameters": [ + { + "type": "string", + "description": "取件码", + "name": "pickup_code", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "type": "file" + } + }, + "404": { + "description": "Not Found", + "schema": { + "$ref": "#/definitions/model.Response" + } + } + } + } + }, + "/api/download/file/{file_id}": { + "get": { + "description": "根据文件 ID 下载单个文件", + "produces": [ + "application/octet-stream" + ], + "tags": [ + "Public" + ], + "summary": "下载单个文件", + "parameters": [ + { + "type": "integer", + "description": "文件 ID", + "name": "file_id", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "type": "file" + } + }, + "404": { + "description": "Not Found", + "schema": { + "$ref": "#/definitions/model.Response" + } + }, + "410": { + "description": "Gone", + "schema": { + "$ref": "#/definitions/model.Response" + } + } + } + } + }, + "/api/pickup/{pickup_code}": { + "get": { + "description": "根据取件码获取文件批次详详情和文件列表", + "produces": [ + "application/json" + ], + "tags": [ + "Public" + ], + "summary": "获取批次信息", + "parameters": [ + { + "type": "string", + "description": "取件码", + "name": "pickup_code", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "allOf": [ + { + "$ref": "#/definitions/model.Response" + }, + { + "type": "object", + "properties": { + "data": { + "$ref": "#/definitions/public.PickupResponse" + } + } + } + ] + } + }, + "404": { + "description": "Not Found", + "schema": { + "$ref": "#/definitions/model.Response" + } + } + } + } + }, + "/api/upload": { + "post": { + "description": "上传一个或多个文件并创建一个提取批次", + "consumes": [ + "multipart/form-data" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Public" + ], + "summary": "上传文件", + "parameters": [ + { + "type": "file", + "description": "文件列表", + "name": "files", + "in": "formData", + "required": true + }, + { + "type": "string", + "description": "备注", + "name": "remark", + "in": "formData" + }, + { + "type": "string", + "description": "过期类型 (time/download/permanent)", + "name": "expire_type", + "in": "formData" + }, + { + "type": "integer", + "description": "过期天数 (针对 time 类型)", + "name": "expire_days", + "in": "formData" + }, + { + "type": "integer", + "description": "最大下载次数 (针对 download 类型)", + "name": "max_downloads", + "in": "formData" + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "allOf": [ + { + "$ref": "#/definitions/model.Response" + }, + { + "type": "object", + "properties": { + "data": { + "$ref": "#/definitions/public.UploadResponse" + } + } + } + ] + } + }, + "400": { + "description": "Bad Request", + "schema": { + "$ref": "#/definitions/model.Response" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/model.Response" + } + } + } + } + } + }, + "definitions": { + "admin.CreateTokenRequest": { + "type": "object", + "required": [ + "name" + ], + "properties": { + "expire_at": { + "type": "string" + }, + "name": { + "type": "string", + "example": "Test Token" + }, + "scope": { + "type": "string", + "example": "upload,pickup" + } + } + }, + "admin.CreateTokenResponse": { + "type": "object", + "properties": { + "data": { + "$ref": "#/definitions/model.APIToken" + }, + "token": { + "type": "string" + } + } + }, + "admin.ListBatchesResponse": { + "type": "object", + "properties": { + "data": { + "type": "array", + "items": { + "$ref": "#/definitions/model.FileBatch" + } + }, + "page": { + "type": "integer" + }, + "page_size": { + "type": "integer" + }, + "total": { + "type": "integer" + } + } + }, + "admin.LoginRequest": { + "type": "object", + "required": [ + "password" + ], + "properties": { + "password": { + "type": "string", + "example": "admin" + } + } + }, + "admin.LoginResponse": { + "type": "object", + "properties": { + "token": { + "type": "string" + } + } + }, + "admin.UpdateBatchRequest": { + "type": "object", + "properties": { + "expire_at": { + "type": "string" + }, + "expire_type": { + "type": "string" + }, + "max_downloads": { + "type": "integer" + }, + "remark": { + "type": "string" + }, + "status": { + "type": "string" + } + } + }, + "model.APIToken": { + "type": "object", + "properties": { + "created_at": { + "type": "string" + }, + "expire_at": { + "type": "string" + }, + "id": { + "type": "integer" + }, + "last_used_at": { + "type": "string" + }, + "name": { + "type": "string" + }, + "revoked": { + "type": "boolean" + }, + "scope": { + "type": "string" + } + } + }, + "model.FileBatch": { + "type": "object", + "properties": { + "created_at": { + "type": "string" + }, + "download_count": { + "type": "integer" + }, + "expire_at": { + "type": "string" + }, + "expire_type": { + "description": "time / download / permanent", + "type": "string" + }, + "file_items": { + "type": "array", + "items": { + "$ref": "#/definitions/model.FileItem" + } + }, + "id": { + "type": "integer" + }, + "max_downloads": { + "type": "integer" + }, + "pickup_code": { + "type": "string" + }, + "remark": { + "type": "string" + }, + "status": { + "description": "active / expired / deleted", + "type": "string" + }, + "updated_at": { + "type": "string" + } + } + }, + "model.FileItem": { + "type": "object", + "properties": { + "batch_id": { + "type": "integer" + }, + "created_at": { + "type": "string" + }, + "id": { + "type": "integer" + }, + "mime_type": { + "type": "string" + }, + "original_name": { + "type": "string" + }, + "size": { + "type": "integer" + }, + "storage_path": { + "type": "string" + } + } + }, + "model.Response": { + "type": "object", + "properties": { + "code": { + "type": "integer", + "example": 200 + }, + "data": {}, + "msg": { + "type": "string", + "example": "success" + } + } + }, + "public.PickupResponse": { + "type": "object", + "properties": { + "download_count": { + "type": "integer" + }, + "expire_at": { + "type": "string" + }, + "expire_type": { + "type": "string" + }, + "files": { + "type": "array", + "items": { + "$ref": "#/definitions/model.FileItem" + } + }, + "max_downloads": { + "type": "integer" + }, + "remark": { + "type": "string" + } + } + }, + "public.UploadResponse": { + "type": "object", + "properties": { + "batch_id": { + "type": "integer" + }, + "expire_at": { + "type": "string" + }, + "pickup_code": { + "type": "string" + } + } + } + }, + "securityDefinitions": { + "AdminAuth": { + "description": "Type \"Bearer \u003cyour-jwt-token\u003e\" to authenticate.", + "type": "apiKey", + "name": "Authorization", + "in": "header" + } + } +} \ No newline at end of file diff --git a/docs/swagger.yaml b/docs/swagger.yaml new file mode 100644 index 0000000..119cbc0 --- /dev/null +++ b/docs/swagger.yaml @@ -0,0 +1,554 @@ +basePath: / +definitions: + admin.CreateTokenRequest: + properties: + expire_at: + type: string + name: + example: Test Token + type: string + scope: + example: upload,pickup + type: string + required: + - name + type: object + admin.CreateTokenResponse: + properties: + data: + $ref: '#/definitions/model.APIToken' + token: + type: string + type: object + admin.ListBatchesResponse: + properties: + data: + items: + $ref: '#/definitions/model.FileBatch' + type: array + page: + type: integer + page_size: + type: integer + total: + type: integer + type: object + admin.LoginRequest: + properties: + password: + example: admin + type: string + required: + - password + type: object + admin.LoginResponse: + properties: + token: + type: string + type: object + admin.UpdateBatchRequest: + properties: + expire_at: + type: string + expire_type: + type: string + max_downloads: + type: integer + remark: + type: string + status: + type: string + type: object + model.APIToken: + properties: + created_at: + type: string + expire_at: + type: string + id: + type: integer + last_used_at: + type: string + name: + type: string + revoked: + type: boolean + scope: + type: string + type: object + model.FileBatch: + properties: + created_at: + type: string + download_count: + type: integer + expire_at: + type: string + expire_type: + description: time / download / permanent + type: string + file_items: + items: + $ref: '#/definitions/model.FileItem' + type: array + id: + type: integer + max_downloads: + type: integer + pickup_code: + type: string + remark: + type: string + status: + description: active / expired / deleted + type: string + updated_at: + type: string + type: object + model.FileItem: + properties: + batch_id: + type: integer + created_at: + type: string + id: + type: integer + mime_type: + type: string + original_name: + type: string + size: + type: integer + storage_path: + type: string + type: object + model.Response: + properties: + code: + example: 200 + type: integer + data: {} + msg: + example: success + type: string + type: object + public.PickupResponse: + properties: + download_count: + type: integer + expire_at: + type: string + expire_type: + type: string + files: + items: + $ref: '#/definitions/model.FileItem' + type: array + max_downloads: + type: integer + remark: + type: string + type: object + public.UploadResponse: + properties: + batch_id: + type: integer + expire_at: + type: string + pickup_code: + type: string + type: object +info: + contact: + email: support@swagger.io + name: API Support + url: http://www.swagger.io/support + description: 自托管的文件暂存柜后端系统 API 文档 + license: + name: Apache 2.0 + url: http://www.apache.org/licenses/LICENSE-2.0.html + termsOfService: http://swagger.io/terms/ + title: 文件暂存柜 API + version: "1.0" +paths: + /admin/api-tokens: + get: + description: 获取系统中所有 API Token 的详详信息(不包含哈希) + produces: + - application/json + responses: + "200": + description: OK + schema: + allOf: + - $ref: '#/definitions/model.Response' + - properties: + data: + items: + $ref: '#/definitions/model.APIToken' + type: array + type: object + "401": + description: Unauthorized + schema: + $ref: '#/definitions/model.Response' + security: + - AdminAuth: [] + summary: 获取 API Token 列表 + tags: + - Admin + post: + consumes: + - application/json + description: 创建一个新的 API Token,返回原始 Token(仅显示一次) + parameters: + - description: Token 信息 + in: body + name: request + required: true + schema: + $ref: '#/definitions/admin.CreateTokenRequest' + produces: + - application/json + responses: + "201": + description: Created + schema: + allOf: + - $ref: '#/definitions/model.Response' + - properties: + data: + $ref: '#/definitions/admin.CreateTokenResponse' + type: object + "400": + description: Bad Request + schema: + $ref: '#/definitions/model.Response' + security: + - AdminAuth: [] + summary: 创建 API Token + tags: + - Admin + /admin/api-tokens/{id}: + delete: + description: 根据 ID 永久删除 API Token + parameters: + - description: Token ID + in: path + name: id + required: true + type: integer + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/model.Response' + "500": + description: Internal Server Error + schema: + $ref: '#/definitions/model.Response' + security: + - AdminAuth: [] + summary: 删除 API Token + tags: + - Admin + /admin/batch/{batch_id}: + delete: + description: 标记批次为已删除,并物理删除关联的存储文件 + parameters: + - description: 批次 ID + in: path + name: batch_id + required: true + type: integer + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/model.Response' + "500": + description: Internal Server Error + schema: + $ref: '#/definitions/model.Response' + security: + - AdminAuth: [] + summary: 删除批次 + tags: + - Admin + get: + description: 根据批次 ID 获取批次信息及关联的文件列表 + parameters: + - description: 批次 ID + in: path + name: batch_id + required: true + type: integer + produces: + - application/json + responses: + "200": + description: OK + schema: + allOf: + - $ref: '#/definitions/model.Response' + - properties: + data: + $ref: '#/definitions/model.FileBatch' + type: object + "404": + description: Not Found + schema: + $ref: '#/definitions/model.Response' + security: + - AdminAuth: [] + summary: 获取批次详情 + tags: + - Admin + put: + consumes: + - application/json + description: 允许修改备注、过期策略、最大下载次数、状态等 + parameters: + - description: 批次 ID + in: path + name: batch_id + required: true + type: integer + - description: 修改内容 + in: body + name: request + required: true + schema: + $ref: '#/definitions/admin.UpdateBatchRequest' + produces: + - application/json + responses: + "200": + description: OK + schema: + allOf: + - $ref: '#/definitions/model.Response' + - properties: + data: + $ref: '#/definitions/model.FileBatch' + type: object + "400": + description: Bad Request + schema: + $ref: '#/definitions/model.Response' + security: + - AdminAuth: [] + summary: 修改批次信息 + tags: + - Admin + /admin/batches: + get: + description: 分页查询所有文件批次,支持按状态过滤和取件码模糊搜索 + parameters: + - description: 页码 (默认 1) + in: query + name: page + type: integer + - description: 每页数量 (默认 20) + in: query + name: page_size + type: integer + - description: 状态 (active/expired/deleted) + in: query + name: status + type: string + - description: 取件码 (模糊搜索) + in: query + name: pickup_code + type: string + produces: + - application/json + responses: + "200": + description: OK + schema: + allOf: + - $ref: '#/definitions/model.Response' + - properties: + data: + $ref: '#/definitions/admin.ListBatchesResponse' + type: object + "401": + description: Unauthorized + schema: + $ref: '#/definitions/model.Response' + security: + - AdminAuth: [] + summary: 获取批次列表 + tags: + - Admin + /admin/login: + post: + consumes: + - application/json + description: 通过密码换取 JWT Token + parameters: + - description: 登录请求 + in: body + name: request + required: true + schema: + $ref: '#/definitions/admin.LoginRequest' + produces: + - application/json + responses: + "200": + description: OK + schema: + allOf: + - $ref: '#/definitions/model.Response' + - properties: + data: + $ref: '#/definitions/admin.LoginResponse' + type: object + "401": + description: Unauthorized + schema: + $ref: '#/definitions/model.Response' + summary: 管理员登录 + tags: + - Admin + /api/download/batch/{pickup_code}: + get: + description: 根据取件码将批次内的所有文件打包为 ZIP 格式一次性下载 + parameters: + - description: 取件码 + in: path + name: pickup_code + required: true + type: string + produces: + - application/zip + responses: + "200": + description: OK + schema: + type: file + "404": + description: Not Found + schema: + $ref: '#/definitions/model.Response' + summary: 批量下载文件 + tags: + - Public + /api/download/file/{file_id}: + get: + description: 根据文件 ID 下载单个文件 + parameters: + - description: 文件 ID + in: path + name: file_id + required: true + type: integer + produces: + - application/octet-stream + responses: + "200": + description: OK + schema: + type: file + "404": + description: Not Found + schema: + $ref: '#/definitions/model.Response' + "410": + description: Gone + schema: + $ref: '#/definitions/model.Response' + summary: 下载单个文件 + tags: + - Public + /api/pickup/{pickup_code}: + get: + description: 根据取件码获取文件批次详详情和文件列表 + parameters: + - description: 取件码 + in: path + name: pickup_code + required: true + type: string + produces: + - application/json + responses: + "200": + description: OK + schema: + allOf: + - $ref: '#/definitions/model.Response' + - properties: + data: + $ref: '#/definitions/public.PickupResponse' + type: object + "404": + description: Not Found + schema: + $ref: '#/definitions/model.Response' + summary: 获取批次信息 + tags: + - Public + /api/upload: + post: + consumes: + - multipart/form-data + description: 上传一个或多个文件并创建一个提取批次 + parameters: + - description: 文件列表 + in: formData + name: files + required: true + type: file + - description: 备注 + in: formData + name: remark + type: string + - description: 过期类型 (time/download/permanent) + in: formData + name: expire_type + type: string + - description: 过期天数 (针对 time 类型) + in: formData + name: expire_days + type: integer + - description: 最大下载次数 (针对 download 类型) + in: formData + name: max_downloads + type: integer + produces: + - application/json + responses: + "200": + description: OK + schema: + allOf: + - $ref: '#/definitions/model.Response' + - properties: + data: + $ref: '#/definitions/public.UploadResponse' + type: object + "400": + description: Bad Request + schema: + $ref: '#/definitions/model.Response' + "500": + description: Internal Server Error + schema: + $ref: '#/definitions/model.Response' + summary: 上传文件 + tags: + - Public +securityDefinitions: + AdminAuth: + description: Type "Bearer " to authenticate. + in: header + name: Authorization + type: apiKey +swagger: "2.0" diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..30bb891 --- /dev/null +++ b/go.mod @@ -0,0 +1,91 @@ +module FileRelay + +go 1.24.0 + +toolchain go1.24.11 + +require ( + github.com/aws/aws-sdk-go-v2 v1.41.1 + github.com/aws/aws-sdk-go-v2/config v1.32.7 + github.com/aws/aws-sdk-go-v2/credentials v1.19.7 + github.com/aws/aws-sdk-go-v2/service/s3 v1.95.1 + github.com/gin-contrib/cors v1.7.6 + github.com/gin-gonic/gin v1.11.0 + github.com/glebarez/sqlite v1.11.0 + github.com/golang-jwt/jwt/v5 v5.3.0 + github.com/google/uuid v1.6.0 + github.com/studio-b12/gowebdav v0.11.0 + github.com/swaggo/files v1.0.1 + github.com/swaggo/gin-swagger v1.6.1 + github.com/swaggo/swag v1.16.6 + golang.org/x/crypto v0.47.0 + gopkg.in/yaml.v3 v3.0.1 + gorm.io/gorm v1.31.1 +) + +require ( + github.com/KyleBanks/depth v1.2.1 // indirect + github.com/PuerkitoBio/purell v1.1.1 // indirect + github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect + github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.7.4 // indirect + github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.18.17 // indirect + github.com/aws/aws-sdk-go-v2/internal/configsources v1.4.17 // indirect + github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.7.17 // indirect + github.com/aws/aws-sdk-go-v2/internal/ini v1.8.4 // indirect + github.com/aws/aws-sdk-go-v2/internal/v4a v1.4.17 // indirect + github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.13.4 // indirect + github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.9.8 // indirect + github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.13.17 // indirect + github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.19.17 // indirect + github.com/aws/aws-sdk-go-v2/service/signin v1.0.5 // indirect + github.com/aws/aws-sdk-go-v2/service/sso v1.30.9 // indirect + github.com/aws/aws-sdk-go-v2/service/ssooidc v1.35.13 // indirect + github.com/aws/aws-sdk-go-v2/service/sts v1.41.6 // indirect + github.com/aws/smithy-go v1.24.0 // indirect + github.com/bytedance/sonic v1.14.0 // indirect + github.com/bytedance/sonic/loader v0.3.0 // indirect + github.com/cloudwego/base64x v0.1.6 // indirect + github.com/dustin/go-humanize v1.0.1 // indirect + github.com/gabriel-vasile/mimetype v1.4.9 // indirect + github.com/gin-contrib/sse v1.1.0 // indirect + github.com/glebarez/go-sqlite v1.21.2 // indirect + github.com/go-openapi/jsonpointer v0.19.5 // indirect + github.com/go-openapi/jsonreference v0.19.6 // indirect + github.com/go-openapi/spec v0.20.4 // indirect + github.com/go-openapi/swag v0.19.15 // indirect + github.com/go-playground/locales v0.14.1 // indirect + github.com/go-playground/universal-translator v0.18.1 // indirect + github.com/go-playground/validator/v10 v10.27.0 // indirect + github.com/goccy/go-json v0.10.5 // indirect + github.com/goccy/go-yaml v1.18.0 // indirect + github.com/jinzhu/inflection v1.0.0 // indirect + github.com/jinzhu/now v1.1.5 // indirect + github.com/josharian/intern v1.0.0 // indirect + github.com/json-iterator/go v1.1.12 // indirect + github.com/klauspost/cpuid/v2 v2.3.0 // indirect + github.com/leodido/go-urn v1.4.0 // indirect + github.com/mailru/easyjson v0.7.6 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect + github.com/modern-go/reflect2 v1.0.2 // indirect + github.com/pelletier/go-toml/v2 v2.2.4 // indirect + github.com/quic-go/qpack v0.5.1 // indirect + github.com/quic-go/quic-go v0.54.0 // indirect + github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect + github.com/twitchyliquid64/golang-asm v0.15.1 // indirect + github.com/ugorji/go/codec v1.3.0 // indirect + go.uber.org/mock v0.5.0 // indirect + golang.org/x/arch v0.20.0 // indirect + golang.org/x/mod v0.31.0 // indirect + golang.org/x/net v0.48.0 // indirect + golang.org/x/sync v0.19.0 // indirect + golang.org/x/sys v0.40.0 // indirect + golang.org/x/text v0.33.0 // indirect + golang.org/x/tools v0.40.0 // indirect + google.golang.org/protobuf v1.36.9 // indirect + gopkg.in/yaml.v2 v2.4.0 // indirect + modernc.org/libc v1.22.5 // indirect + modernc.org/mathutil v1.5.0 // indirect + modernc.org/memory v1.5.0 // indirect + modernc.org/sqlite v1.23.1 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..b4b9873 --- /dev/null +++ b/go.sum @@ -0,0 +1,240 @@ +github.com/KyleBanks/depth v1.2.1 h1:5h8fQADFrWtarTdtDudMmGsC7GPbOAu6RVB3ffsVFHc= +github.com/KyleBanks/depth v1.2.1/go.mod h1:jzSb9d0L43HxTQfT+oSA1EEp2q+ne2uh6XgeJcm8brE= +github.com/PuerkitoBio/purell v1.1.1 h1:WEQqlqaGbrPkxLJWfBwQmfEAE1Z7ONdDLqrN38tNFfI= +github.com/PuerkitoBio/purell v1.1.1/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0= +github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 h1:d+Bc7a5rLufV/sSk/8dngufqelfh6jnri85riMAaF/M= +github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE= +github.com/aws/aws-sdk-go-v2 v1.41.1 h1:ABlyEARCDLN034NhxlRUSZr4l71mh+T5KAeGh6cerhU= +github.com/aws/aws-sdk-go-v2 v1.41.1/go.mod h1:MayyLB8y+buD9hZqkCW3kX1AKq07Y5pXxtgB+rRFhz0= +github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.7.4 h1:489krEF9xIGkOaaX3CE/Be2uWjiXrkCH6gUX+bZA/BU= +github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.7.4/go.mod h1:IOAPF6oT9KCsceNTvvYMNHy0+kMF8akOjeDvPENWxp4= +github.com/aws/aws-sdk-go-v2/config v1.32.7 h1:vxUyWGUwmkQ2g19n7JY/9YL8MfAIl7bTesIUykECXmY= +github.com/aws/aws-sdk-go-v2/config v1.32.7/go.mod h1:2/Qm5vKUU/r7Y+zUk/Ptt2MDAEKAfUtKc1+3U1Mo3oY= +github.com/aws/aws-sdk-go-v2/credentials v1.19.7 h1:tHK47VqqtJxOymRrNtUXN5SP/zUTvZKeLx4tH6PGQc8= +github.com/aws/aws-sdk-go-v2/credentials v1.19.7/go.mod h1:qOZk8sPDrxhf+4Wf4oT2urYJrYt3RejHSzgAquYeppw= +github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.18.17 h1:I0GyV8wiYrP8XpA70g1HBcQO1JlQxCMTW9npl5UbDHY= +github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.18.17/go.mod h1:tyw7BOl5bBe/oqvoIeECFJjMdzXoa/dfVz3QQ5lgHGA= +github.com/aws/aws-sdk-go-v2/internal/configsources v1.4.17 h1:xOLELNKGp2vsiteLsvLPwxC+mYmO6OZ8PYgiuPJzF8U= +github.com/aws/aws-sdk-go-v2/internal/configsources v1.4.17/go.mod h1:5M5CI3D12dNOtH3/mk6minaRwI2/37ifCURZISxA/IQ= +github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.7.17 h1:WWLqlh79iO48yLkj1v3ISRNiv+3KdQoZ6JWyfcsyQik= +github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.7.17/go.mod h1:EhG22vHRrvF8oXSTYStZhJc1aUgKtnJe+aOiFEV90cM= +github.com/aws/aws-sdk-go-v2/internal/ini v1.8.4 h1:WKuaxf++XKWlHWu9ECbMlha8WOEGm0OUEZqm4K/Gcfk= +github.com/aws/aws-sdk-go-v2/internal/ini v1.8.4/go.mod h1:ZWy7j6v1vWGmPReu0iSGvRiise4YI5SkR3OHKTZ6Wuc= +github.com/aws/aws-sdk-go-v2/internal/v4a v1.4.17 h1:JqcdRG//czea7Ppjb+g/n4o8i/R50aTBHkA7vu0lK+k= +github.com/aws/aws-sdk-go-v2/internal/v4a v1.4.17/go.mod h1:CO+WeGmIdj/MlPel2KwID9Gt7CNq4M65HUfBW97liM0= +github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.13.4 h1:0ryTNEdJbzUCEWkVXEXoqlXV72J5keC1GvILMOuD00E= +github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.13.4/go.mod h1:HQ4qwNZh32C3CBeO6iJLQlgtMzqeG17ziAA/3KDJFow= +github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.9.8 h1:Z5EiPIzXKewUQK0QTMkutjiaPVeVYXX7KIqhXu/0fXs= +github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.9.8/go.mod h1:FsTpJtvC4U1fyDXk7c71XoDv3HlRm8V3NiYLeYLh5YE= +github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.13.17 h1:RuNSMoozM8oXlgLG/n6WLaFGoea7/CddrCfIiSA+xdY= +github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.13.17/go.mod h1:F2xxQ9TZz5gDWsclCtPQscGpP0VUOc8RqgFM3vDENmU= +github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.19.17 h1:bGeHBsGZx0Dvu/eJC0Lh9adJa3M1xREcndxLNZlve2U= +github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.19.17/go.mod h1:dcW24lbU0CzHusTE8LLHhRLI42ejmINN8Lcr22bwh/g= +github.com/aws/aws-sdk-go-v2/service/s3 v1.95.1 h1:C2dUPSnEpy4voWFIq3JNd8gN0Y5vYGDo44eUE58a/p8= +github.com/aws/aws-sdk-go-v2/service/s3 v1.95.1/go.mod h1:5jggDlZ2CLQhwJBiZJb4vfk4f0GxWdEDruWKEJ1xOdo= +github.com/aws/aws-sdk-go-v2/service/signin v1.0.5 h1:VrhDvQib/i0lxvr3zqlUwLwJP4fpmpyD9wYG1vfSu+Y= +github.com/aws/aws-sdk-go-v2/service/signin v1.0.5/go.mod h1:k029+U8SY30/3/ras4G/Fnv/b88N4mAfliNn08Dem4M= +github.com/aws/aws-sdk-go-v2/service/sso v1.30.9 h1:v6EiMvhEYBoHABfbGB4alOYmCIrcgyPPiBE1wZAEbqk= +github.com/aws/aws-sdk-go-v2/service/sso v1.30.9/go.mod h1:yifAsgBxgJWn3ggx70A3urX2AN49Y5sJTD1UQFlfqBw= +github.com/aws/aws-sdk-go-v2/service/ssooidc v1.35.13 h1:gd84Omyu9JLriJVCbGApcLzVR3XtmC4ZDPcAI6Ftvds= +github.com/aws/aws-sdk-go-v2/service/ssooidc v1.35.13/go.mod h1:sTGThjphYE4Ohw8vJiRStAcu3rbjtXRsdNB0TvZ5wwo= +github.com/aws/aws-sdk-go-v2/service/sts v1.41.6 h1:5fFjR/ToSOzB2OQ/XqWpZBmNvmP/pJ1jOWYlFDJTjRQ= +github.com/aws/aws-sdk-go-v2/service/sts v1.41.6/go.mod h1:qgFDZQSD/Kys7nJnVqYlWKnh0SSdMjAi0uSwON4wgYQ= +github.com/aws/smithy-go v1.24.0 h1:LpilSUItNPFr1eY85RYgTIg5eIEPtvFbskaFcmmIUnk= +github.com/aws/smithy-go v1.24.0/go.mod h1:LEj2LM3rBRQJxPZTB4KuzZkaZYnZPnvgIhb4pu07mx0= +github.com/bytedance/sonic v1.14.0 h1:/OfKt8HFw0kh2rj8N0F6C/qPGRESq0BbaNZgcNXXzQQ= +github.com/bytedance/sonic v1.14.0/go.mod h1:WoEbx8WTcFJfzCe0hbmyTGrfjt8PzNEBdxlNUO24NhA= +github.com/bytedance/sonic/loader v0.3.0 h1:dskwH8edlzNMctoruo8FPTJDF3vLtDT0sXZwvZJyqeA= +github.com/bytedance/sonic/loader v0.3.0/go.mod h1:N8A3vUdtUebEY2/VQC0MyhYeKUFosQU6FxH2JmUe6VI= +github.com/cloudwego/base64x v0.1.6 h1:t11wG9AECkCDk5fMSoxmufanudBtJ+/HemLstXDLI2M= +github.com/cloudwego/base64x v0.1.6/go.mod h1:OFcloc187FXDaYHvrNIjxSe8ncn0OOM8gEHfghB2IPU= +github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= +github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= +github.com/gabriel-vasile/mimetype v1.4.9 h1:5k+WDwEsD9eTLL8Tz3L0VnmVh9QxGjRmjBvAG7U/oYY= +github.com/gabriel-vasile/mimetype v1.4.9/go.mod h1:WnSQhFKJuBlRyLiKohA/2DtIlPFAbguNaG7QCHcyGok= +github.com/gin-contrib/cors v1.7.6 h1:3gQ8GMzs1Ylpf70y8bMw4fVpycXIeX1ZemuSQIsnQQY= +github.com/gin-contrib/cors v1.7.6/go.mod h1:Ulcl+xN4jel9t1Ry8vqph23a60FwH9xVLd+3ykmTjOk= +github.com/gin-contrib/gzip v0.0.6 h1:NjcunTcGAj5CO1gn4N8jHOSIeRFHIbn51z6K+xaN4d4= +github.com/gin-contrib/gzip v0.0.6/go.mod h1:QOJlmV2xmayAjkNS2Y8NQsMneuRShOU/kjovCXNuzzk= +github.com/gin-contrib/sse v1.1.0 h1:n0w2GMuUpWDVp7qSpvze6fAu9iRxJY4Hmj6AmBOU05w= +github.com/gin-contrib/sse v1.1.0/go.mod h1:hxRZ5gVpWMT7Z0B0gSNYqqsSCNIJMjzvm6fqCz9vjwM= +github.com/gin-gonic/gin v1.11.0 h1:OW/6PLjyusp2PPXtyxKHU0RbX6I/l28FTdDlae5ueWk= +github.com/gin-gonic/gin v1.11.0/go.mod h1:+iq/FyxlGzII0KHiBGjuNn4UNENUlKbGlNmc+W50Dls= +github.com/glebarez/go-sqlite v1.21.2 h1:3a6LFC4sKahUunAmynQKLZceZCOzUthkRkEAl9gAXWo= +github.com/glebarez/go-sqlite v1.21.2/go.mod h1:sfxdZyhQjTM2Wry3gVYWaW072Ri1WMdWJi0k6+3382k= +github.com/glebarez/sqlite v1.11.0 h1:wSG0irqzP6VurnMEpFGer5Li19RpIRi2qvQz++w0GMw= +github.com/glebarez/sqlite v1.11.0/go.mod h1:h8/o8j5wiAsqSPoWELDUdJXhjAhsVliSn7bWZjOhrgQ= +github.com/go-openapi/jsonpointer v0.19.3/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg= +github.com/go-openapi/jsonpointer v0.19.5 h1:gZr+CIYByUqjcgeLXnQu2gHYQC9o73G2XUeOFYEICuY= +github.com/go-openapi/jsonpointer v0.19.5/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg= +github.com/go-openapi/jsonreference v0.19.6 h1:UBIxjkht+AWIgYzCDSv2GN+E/togfwXUJFRTWhl2Jjs= +github.com/go-openapi/jsonreference v0.19.6/go.mod h1:diGHMEHg2IqXZGKxqyvWdfWU/aim5Dprw5bqpKkTvns= +github.com/go-openapi/spec v0.20.4 h1:O8hJrt0UMnhHcluhIdUgCLRWyM2x7QkBXRvOs7m+O1M= +github.com/go-openapi/spec v0.20.4/go.mod h1:faYFR1CvsJZ0mNsmsphTMSoRrNV3TEDoAM7FOEWeq8I= +github.com/go-openapi/swag v0.19.5/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh66Z9tfKk= +github.com/go-openapi/swag v0.19.15 h1:D2NRCBzS9/pEY3gP9Nl8aDqGUcPFrwG2p+CNFrLyrCM= +github.com/go-openapi/swag v0.19.15/go.mod h1:QYRuS/SOXUCsnplDa677K7+DxSOj6IPNl/eQntq43wQ= +github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s= +github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= +github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= +github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= +github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY= +github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= +github.com/go-playground/validator/v10 v10.27.0 h1:w8+XrWVMhGkxOaaowyKH35gFydVHOvC0/uWoy2Fzwn4= +github.com/go-playground/validator/v10 v10.27.0/go.mod h1:I5QpIEbmr8On7W0TktmJAumgzX4CA1XNl4ZmDuVHKKo= +github.com/goccy/go-json v0.10.5 h1:Fq85nIqj+gXn/S5ahsiTlK3TmC85qgirsdTP/+DeaC4= +github.com/goccy/go-json v0.10.5/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M= +github.com/goccy/go-yaml v1.18.0 h1:8W7wMFS12Pcas7KU+VVkaiCng+kG8QiFeFwzFb+rwuw= +github.com/goccy/go-yaml v1.18.0/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA= +github.com/golang-jwt/jwt/v5 v5.3.0 h1:pv4AsKCKKZuqlgs5sUmn4x8UlGa0kEVt/puTpKx9vvo= +github.com/golang-jwt/jwt/v5 v5.3.0/go.mod h1:fxCRLWMO43lRc8nhHWY6LGqRcf+1gQWArsqaEUEa5bE= +github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8= +github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26 h1:Xim43kblpZXfIBQsbuBVKCudVG457BR2GZFIz3uw3hQ= +github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26/go.mod h1:dDKJzRmX4S37WGHujM7tX//fmj1uioxKzKxz3lo4HJo= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= +github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= +github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= +github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= +github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= +github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= +github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= +github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= +github.com/klauspost/cpuid/v2 v2.3.0 h1:S4CRMLnYUhGeDFDqkGriYKdfoFlDnMtqTiI/sFzhA9Y= +github.com/klauspost/cpuid/v2 v2.3.0/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0= +github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ= +github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI= +github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= +github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= +github.com/mailru/easyjson v0.7.6 h1:8yTIVnZgCoiM1TgqoeTl+LfU5Jg6/xL3QhGQnimLYnA= +github.com/mailru/easyjson v0.7.6/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= +github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= +github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= +github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4= +github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/quic-go/qpack v0.5.1 h1:giqksBPnT/HDtZ6VhtFKgoLOWmlyo9Ei6u9PqzIMbhI= +github.com/quic-go/qpack v0.5.1/go.mod h1:+PC4XFrEskIVkcLzpEkbLqq1uCoxPhQuvK5rH1ZgaEg= +github.com/quic-go/quic-go v0.54.0 h1:6s1YB9QotYI6Ospeiguknbp2Znb/jZYjZLRXn9kMQBg= +github.com/quic-go/quic-go v0.54.0/go.mod h1:e68ZEaCdyviluZmy44P6Iey98v/Wfz6HCjQEm+l8zTY= +github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= +github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE= +github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= +github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8= +github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U= +github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U= +github.com/studio-b12/gowebdav v0.11.0 h1:qbQzq4USxY28ZYsGJUfO5jR+xkFtcnwWgitp4Zp1irU= +github.com/studio-b12/gowebdav v0.11.0/go.mod h1:bHA7t77X/QFExdeAnDzK6vKM34kEZAcE1OX4MfiwjkE= +github.com/swaggo/files v1.0.1 h1:J1bVJ4XHZNq0I46UU90611i9/YzdrF7x92oX1ig5IdE= +github.com/swaggo/files v1.0.1/go.mod h1:0qXmMNH6sXNf+73t65aKeB+ApmgxdnkQzVTAj2uaMUg= +github.com/swaggo/gin-swagger v1.6.1 h1:Ri06G4gc9N4t4k8hekMigJ9zKTFSlqj/9paAQCQs7cY= +github.com/swaggo/gin-swagger v1.6.1/go.mod h1:LQ+hJStHakCWRiK/YNYtJOu4mR2FP+pxLnILT/qNiTw= +github.com/swaggo/swag v1.16.6 h1:qBNcx53ZaX+M5dxVyTrgQ0PJ/ACK+NzhwcbieTt+9yI= +github.com/swaggo/swag v1.16.6/go.mod h1:ngP2etMK5a0P3QBizic5MEwpRmluJZPHjXcMoj4Xesg= +github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= +github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= +github.com/ugorji/go/codec v1.3.0 h1:Qd2W2sQawAfG8XSvzwhBeoGq71zXOC/Q1E9y/wUcsUA= +github.com/ugorji/go/codec v1.3.0/go.mod h1:pRBVtBSKl77K30Bv8R2P+cLSGaTtex6fsA2Wjqmfxj4= +github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= +go.uber.org/mock v0.5.0 h1:KAMbZvZPyBPWgD14IrIQ38QCyjwpvVVV6K/bHl1IwQU= +go.uber.org/mock v0.5.0/go.mod h1:ge71pBPLYDk7QIi1LupWxdAykm7KIEFchiOqd6z7qMM= +golang.org/x/arch v0.20.0 h1:dx1zTU0MAE98U+TQ8BLl7XsJbgze2WnNKF/8tGp/Q6c= +golang.org/x/arch v0.20.0/go.mod h1:bdwinDaKcfZUGpH09BB7ZmOfhalA8lQdzl62l8gGWsk= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/crypto v0.47.0 h1:V6e3FRj+n4dbpw86FJ8Fv7XVOql7TEwpHapKoMJ/GO8= +golang.org/x/crypto v0.47.0/go.mod h1:ff3Y9VzzKbwSSEzWqJsJVBnWmRwRSHt/6Op5n9bQc4A= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= +golang.org/x/mod v0.31.0 h1:HaW9xtz0+kOcWKwli0ZXy79Ix+UW/vOfmWI5QVd2tgI= +golang.org/x/mod v0.31.0/go.mod h1:43JraMp9cGx1Rx3AqioxrbrhNsLl2l/iNAvuBkrezpg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20210421230115-4e50805a0758/go.mod h1:72T/g9IO56b78aLF+1Kcs5dz7/ng1VjMUvfKvpfy+jM= +golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= +golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= +golang.org/x/net v0.48.0 h1:zyQRTTrjc33Lhh0fBgT/H3oZq9WuvRR5gPC70xpDiQU= +golang.org/x/net v0.48.0/go.mod h1:+ndRgGjkh8FGtu1w1FGbEC31if4VrNVMuKTgcAAnQRY= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.19.0 h1:vV+1eWNmZ5geRlYjzm2adRgW2/mcpevXNg50YZtPCE4= +golang.org/x/sync v0.19.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210420072515-93ed5bcd2bfe/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.40.0 h1:DBZZqJ2Rkml6QMQsZywtnjnnGvHza6BTfYFWY9kjEWQ= +golang.org/x/sys v0.40.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= +golang.org/x/text v0.33.0 h1:B3njUFyqtHDUI5jMn1YIr5B0IE2U0qck04r6d4KPAxE= +golang.org/x/text v0.33.0/go.mod h1:LuMebE6+rBincTi9+xWTY8TztLzKHc/9C1uBCG27+q8= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= +golang.org/x/tools v0.40.0 h1:yLkxfA+Qnul4cs9QA3KnlFu0lVmd8JJfoq+E41uSutA= +golang.org/x/tools v0.40.0/go.mod h1:Ik/tzLRlbscWpqqMRjyWYDisX8bG13FrdXp3o4Sr9lc= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/protobuf v1.36.9 h1:w2gp2mA27hUeUzj9Ex9FBjsBm40zfaDtEWow293U7Iw= +google.golang.org/protobuf v1.36.9/go.mod h1:fuxRtAxBytpl4zzqUh6/eyUujkJdNiuEkXntxiD/uRU= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gorm.io/gorm v1.31.1 h1:7CA8FTFz/gRfgqgpeKIBcervUn3xSyPUmr6B2WXJ7kg= +gorm.io/gorm v1.31.1/go.mod h1:XyQVbO2k6YkOis7C2437jSit3SsDK72s7n7rsSHd+Gs= +modernc.org/libc v1.22.5 h1:91BNch/e5B0uPbJFgqbxXuOnxBQjlS//icfQEGmvyjE= +modernc.org/libc v1.22.5/go.mod h1:jj+Z7dTNX8fBScMVNRAYZ/jF91K8fdT2hYMThc3YjBY= +modernc.org/mathutil v1.5.0 h1:rV0Ko/6SfM+8G+yKiyI830l3Wuz1zRutdslNoQ0kfiQ= +modernc.org/mathutil v1.5.0/go.mod h1:mZW8CKdRPY1v87qxC/wUdX5O1qDzXMP5TH3wjfpga6E= +modernc.org/memory v1.5.0 h1:N+/8c5rE6EqugZwHii4IFsaJ7MUhoWX07J5tC/iI5Ds= +modernc.org/memory v1.5.0/go.mod h1:PkUhL0Mugw21sHPeskwZW4D6VscE/GQJOnIpCnW6pSU= +modernc.org/sqlite v1.23.1 h1:nrSBg4aRQQwq59JpvGEQ15tNxoO5pX/kUjcRNwSAGQM= +modernc.org/sqlite v1.23.1/go.mod h1:OrDj17Mggn6MhE+iPbBNf7RGKODDE9NFT0f3EwDzJqk= diff --git a/internal/api/admin/auth.go b/internal/api/admin/auth.go new file mode 100644 index 0000000..0fe4bfa --- /dev/null +++ b/internal/api/admin/auth.go @@ -0,0 +1,70 @@ +package admin + +import ( + "FileRelay/internal/auth" + "FileRelay/internal/bootstrap" + "FileRelay/internal/model" + "net/http" + "time" + + "github.com/gin-gonic/gin" + "golang.org/x/crypto/bcrypt" +) + +type AuthHandler struct{} + +func NewAuthHandler() *AuthHandler { + return &AuthHandler{} +} + +type LoginRequest struct { + Password string `json:"password" binding:"required" example:"admin"` +} + +type LoginResponse struct { + Token string `json:"token"` +} + +// Login 管理员登录 +// @Summary 管理员登录 +// @Description 通过密码换取 JWT Token +// @Tags Admin +// @Accept json +// @Produce json +// @Param request body LoginRequest true "登录请求" +// @Success 200 {object} model.Response{data=LoginResponse} +// @Failure 401 {object} model.Response +// @Router /admin/login [post] +func (h *AuthHandler) Login(c *gin.Context) { + var req LoginRequest + + if err := c.ShouldBindJSON(&req); err != nil { + c.JSON(http.StatusBadRequest, model.ErrorResponse(model.CodeBadRequest, "Invalid request")) + return + } + + var admin model.Admin + if err := bootstrap.DB.First(&admin).Error; err != nil { + c.JSON(http.StatusInternalServerError, model.ErrorResponse(model.CodeInternalError, "Admin not found")) + return + } + + if err := bcrypt.CompareHashAndPassword([]byte(admin.PasswordHash), []byte(req.Password)); err != nil { + c.JSON(http.StatusUnauthorized, model.ErrorResponse(model.CodeUnauthorized, "Incorrect password")) + return + } + + token, err := auth.GenerateToken(admin.ID) + if err != nil { + c.JSON(http.StatusInternalServerError, model.ErrorResponse(model.CodeInternalError, "Failed to generate token")) + return + } + + // 更新登录时间 + now := time.Now() + bootstrap.DB.Model(&admin).Update("last_login", &now) + + c.JSON(http.StatusOK, model.SuccessResponse(LoginResponse{ + Token: token, + })) +} diff --git a/internal/api/admin/batch.go b/internal/api/admin/batch.go new file mode 100644 index 0000000..84046dd --- /dev/null +++ b/internal/api/admin/batch.go @@ -0,0 +1,173 @@ +package admin + +import ( + "FileRelay/internal/bootstrap" + "FileRelay/internal/model" + "FileRelay/internal/service" + "net/http" + "strconv" + "time" + + "github.com/gin-gonic/gin" +) + +type BatchHandler struct { + batchService *service.BatchService +} + +func NewBatchHandler() *BatchHandler { + return &BatchHandler{ + batchService: service.NewBatchService(), + } +} + +type ListBatchesResponse struct { + Total int64 `json:"total"` + Page int `json:"page"` + PageSize int `json:"page_size"` + Data []model.FileBatch `json:"data"` +} + +type UpdateBatchRequest struct { + Remark string `json:"remark"` + ExpireType string `json:"expire_type"` + ExpireAt *time.Time `json:"expire_at"` + MaxDownloads int `json:"max_downloads"` + Status string `json:"status"` +} + +// ListBatches 获取批次列表 +// @Summary 获取批次列表 +// @Description 分页查询所有文件批次,支持按状态过滤和取件码模糊搜索 +// @Tags Admin +// @Security AdminAuth +// @Param page query int false "页码 (默认 1)" +// @Param page_size query int false "每页数量 (默认 20)" +// @Param status query string false "状态 (active/expired/deleted)" +// @Param pickup_code query string false "取件码 (模糊搜索)" +// @Produce json +// @Success 200 {object} model.Response{data=ListBatchesResponse} +// @Failure 401 {object} model.Response +// @Router /admin/batches [get] +func (h *BatchHandler) ListBatches(c *gin.Context) { + page, _ := strconv.Atoi(c.DefaultQuery("page", "1")) + pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "20")) + + if page < 1 { + page = 1 + } + if pageSize < 1 { + pageSize = 20 + } + status := c.Query("status") + pickupCode := c.Query("pickup_code") + + query := bootstrap.DB.Model(&model.FileBatch{}) + if status != "" { + query = query.Where("status = ?", status) + } + if pickupCode != "" { + query = query.Where("pickup_code LIKE ?", "%"+pickupCode+"%") + } + + var total int64 + query.Count(&total) + + var batches []model.FileBatch + err := query.Offset((page - 1) * pageSize).Limit(pageSize).Order("created_at DESC").Find(&batches).Error + if err != nil { + c.JSON(http.StatusInternalServerError, model.ErrorResponse(model.CodeInternalError, err.Error())) + return + } + + c.JSON(http.StatusOK, model.SuccessResponse(ListBatchesResponse{ + Total: total, + Page: page, + PageSize: pageSize, + Data: batches, + })) +} + +// GetBatch 获取批次详情 +// @Summary 获取批次详情 +// @Description 根据批次 ID 获取批次信息及关联的文件列表 +// @Tags Admin +// @Security AdminAuth +// @Param batch_id path int true "批次 ID" +// @Produce json +// @Success 200 {object} model.Response{data=model.FileBatch} +// @Failure 404 {object} model.Response +// @Router /admin/batch/{batch_id} [get] +func (h *BatchHandler) GetBatch(c *gin.Context) { + id := c.Param("batch_id") + var batch model.FileBatch + if err := bootstrap.DB.Preload("FileItems").First(&batch, id).Error; err != nil { + c.JSON(http.StatusNotFound, model.ErrorResponse(model.CodeNotFound, "batch not found")) + return + } + c.JSON(http.StatusOK, model.SuccessResponse(batch)) +} + +// UpdateBatch 修改批次信息 +// @Summary 修改批次信息 +// @Description 允许修改备注、过期策略、最大下载次数、状态等 +// @Tags Admin +// @Security AdminAuth +// @Accept json +// @Produce json +// @Param batch_id path int true "批次 ID" +// @Param request body UpdateBatchRequest true "修改内容" +// @Success 200 {object} model.Response{data=model.FileBatch} +// @Failure 400 {object} model.Response +// @Router /admin/batch/{batch_id} [put] +func (h *BatchHandler) UpdateBatch(c *gin.Context) { + id := c.Param("batch_id") + var batch model.FileBatch + if err := bootstrap.DB.First(&batch, id).Error; err != nil { + c.JSON(http.StatusNotFound, model.ErrorResponse(model.CodeNotFound, "batch not found")) + return + } + + var input UpdateBatchRequest + + if err := c.ShouldBindJSON(&input); err != nil { + c.JSON(http.StatusBadRequest, model.ErrorResponse(model.CodeBadRequest, err.Error())) + return + } + + updates := make(map[string]interface{}) + updates["remark"] = input.Remark + updates["expire_type"] = input.ExpireType + updates["expire_at"] = input.ExpireAt + updates["max_downloads"] = input.MaxDownloads + updates["status"] = input.Status + + if err := bootstrap.DB.Model(&batch).Updates(updates).Error; err != nil { + c.JSON(http.StatusInternalServerError, model.ErrorResponse(model.CodeInternalError, err.Error())) + return + } + + c.JSON(http.StatusOK, model.SuccessResponse(batch)) +} + +// DeleteBatch 删除批次 +// @Summary 删除批次 +// @Description 标记批次为已删除,并物理删除关联的存储文件 +// @Tags Admin +// @Security AdminAuth +// @Param batch_id path int true "批次 ID" +// @Produce json +// @Success 200 {object} model.Response +// @Failure 500 {object} model.Response +// @Router /admin/batch/{batch_id} [delete] +func (h *BatchHandler) DeleteBatch(c *gin.Context) { + idStr := c.Param("batch_id") + id, _ := strconv.ParseUint(idStr, 10, 32) + + if err := h.batchService.DeleteBatch(c.Request.Context(), uint(id)); err != nil { + c.JSON(http.StatusInternalServerError, model.ErrorResponse(model.CodeInternalError, err.Error())) + return + } + + c.JSON(http.StatusOK, model.SuccessResponse(map[string]interface{}{})) +} diff --git a/internal/api/admin/token.go b/internal/api/admin/token.go new file mode 100644 index 0000000..8416943 --- /dev/null +++ b/internal/api/admin/token.go @@ -0,0 +1,109 @@ +package admin + +import ( + "FileRelay/internal/bootstrap" + "FileRelay/internal/model" + "FileRelay/internal/service" + "net/http" + "time" + + "github.com/gin-gonic/gin" +) + +type TokenHandler struct { + tokenService *service.TokenService +} + +func NewTokenHandler() *TokenHandler { + return &TokenHandler{ + tokenService: service.NewTokenService(), + } +} + +type CreateTokenRequest struct { + Name string `json:"name" binding:"required" example:"Test Token"` + Scope string `json:"scope" example:"upload,pickup"` + ExpireAt *time.Time `json:"expire_at"` +} + +type CreateTokenResponse struct { + Token string `json:"token"` + Data *model.APIToken `json:"data"` +} + +// ListTokens 获取 API Token 列表 +// @Summary 获取 API Token 列表 +// @Description 获取系统中所有 API Token 的详详信息(不包含哈希) +// @Tags Admin +// @Security AdminAuth +// @Produce json +// @Success 200 {object} model.Response{data=[]model.APIToken} +// @Failure 401 {object} model.Response +// @Router /admin/api-tokens [get] +func (h *TokenHandler) ListTokens(c *gin.Context) { + var tokens []model.APIToken + if err := bootstrap.DB.Find(&tokens).Error; err != nil { + c.JSON(http.StatusInternalServerError, model.ErrorResponse(model.CodeInternalError, err.Error())) + return + } + c.JSON(http.StatusOK, model.SuccessResponse(tokens)) +} + +// CreateToken 创建 API Token +// @Summary 创建 API Token +// @Description 创建一个新的 API Token,返回原始 Token(仅显示一次) +// @Tags Admin +// @Security AdminAuth +// @Accept json +// @Produce json +// @Param request body CreateTokenRequest true "Token 信息" +// @Success 201 {object} model.Response{data=CreateTokenResponse} +// @Failure 400 {object} model.Response +// @Router /admin/api-tokens [post] +func (h *TokenHandler) CreateToken(c *gin.Context) { + var input CreateTokenRequest + + if err := c.ShouldBindJSON(&input); err != nil { + c.JSON(http.StatusBadRequest, model.ErrorResponse(model.CodeBadRequest, err.Error())) + return + } + + rawToken, token, err := h.tokenService.CreateToken(input.Name, input.Scope, input.ExpireAt) + if err != nil { + c.JSON(http.StatusInternalServerError, model.ErrorResponse(model.CodeInternalError, err.Error())) + return + } + + c.JSON(http.StatusCreated, model.SuccessResponse(CreateTokenResponse{ + Token: rawToken, + Data: token, + })) +} + +func (h *TokenHandler) RevokeToken(c *gin.Context) { + id := c.Param("id") + if err := bootstrap.DB.Model(&model.APIToken{}).Where("id = ?", id).Update("revoked", true).Error; err != nil { + c.JSON(http.StatusInternalServerError, model.ErrorResponse(model.CodeInternalError, err.Error())) + return + } + c.JSON(http.StatusOK, model.SuccessResponse(map[string]interface{}{})) +} + +// DeleteToken 删除 API Token +// @Summary 删除 API Token +// @Description 根据 ID 永久删除 API Token +// @Tags Admin +// @Security AdminAuth +// @Param id path int true "Token ID" +// @Produce json +// @Success 200 {object} model.Response +// @Failure 500 {object} model.Response +// @Router /admin/api-tokens/{id} [delete] +func (h *TokenHandler) DeleteToken(c *gin.Context) { + id := c.Param("id") + if err := bootstrap.DB.Delete(&model.APIToken{}, id).Error; err != nil { + c.JSON(http.StatusInternalServerError, model.ErrorResponse(model.CodeInternalError, err.Error())) + return + } + c.JSON(http.StatusOK, model.SuccessResponse(map[string]interface{}{})) +} diff --git a/internal/api/middleware/auth.go b/internal/api/middleware/auth.go new file mode 100644 index 0000000..519b412 --- /dev/null +++ b/internal/api/middleware/auth.go @@ -0,0 +1,76 @@ +package middleware + +import ( + "FileRelay/internal/auth" + "FileRelay/internal/config" + "FileRelay/internal/model" + "FileRelay/internal/service" + "net/http" + "strings" + + "github.com/gin-gonic/gin" +) + +func AdminAuth() gin.HandlerFunc { + return func(c *gin.Context) { + authHeader := c.GetHeader("Authorization") + if authHeader == "" { + c.JSON(http.StatusUnauthorized, model.ErrorResponse(model.CodeUnauthorized, "Authorization header required")) + c.Abort() + return + } + + parts := strings.SplitN(authHeader, " ", 2) + if !(len(parts) == 2 && parts[0] == "Bearer") { + c.JSON(http.StatusUnauthorized, model.ErrorResponse(model.CodeUnauthorized, "Invalid authorization format")) + c.Abort() + return + } + + claims, err := auth.ParseToken(parts[1]) + if err != nil { + c.JSON(http.StatusUnauthorized, model.ErrorResponse(model.CodeUnauthorized, "Invalid or expired token")) + c.Abort() + return + } + + c.Set("admin_id", claims.AdminID) + c.Next() + } +} + +func APITokenAuth(requiredScope string) gin.HandlerFunc { + tokenService := service.NewTokenService() + return func(c *gin.Context) { + if !config.GlobalConfig.APIToken.Enabled { + c.JSON(http.StatusForbidden, model.ErrorResponse(model.CodeForbidden, "API Token is disabled")) + c.Abort() + return + } + + authHeader := c.GetHeader("Authorization") + if authHeader == "" { + c.JSON(http.StatusUnauthorized, model.ErrorResponse(model.CodeUnauthorized, "Authorization header required")) + c.Abort() + return + } + + parts := strings.SplitN(authHeader, " ", 2) + if !(len(parts) == 2 && parts[0] == "Bearer") { + c.JSON(http.StatusUnauthorized, model.ErrorResponse(model.CodeUnauthorized, "Invalid authorization format")) + c.Abort() + return + } + + token, err := tokenService.ValidateToken(parts[1], requiredScope) + if err != nil { + c.JSON(http.StatusUnauthorized, model.ErrorResponse(model.CodeUnauthorized, err.Error())) + c.Abort() + return + } + + c.Set("token_id", token.ID) + c.Set("token_scope", token.Scope) + c.Next() + } +} diff --git a/internal/api/middleware/limit.go b/internal/api/middleware/limit.go new file mode 100644 index 0000000..f5f2c3d --- /dev/null +++ b/internal/api/middleware/limit.go @@ -0,0 +1,52 @@ +package middleware + +import ( + "FileRelay/internal/config" + "FileRelay/internal/model" + "net/http" + "sync" + "time" + + "github.com/gin-gonic/gin" +) + +var ( + pickupFailures = make(map[string]int) + failureMutex sync.Mutex +) + +func PickupRateLimit() gin.HandlerFunc { + return func(c *gin.Context) { + ip := c.ClientIP() + code := c.Param("pickup_code") + key := ip + ":" + code + + failureMutex.Lock() + count, exists := pickupFailures[key] + failureMutex.Unlock() + + if exists && count >= config.GlobalConfig.Security.PickupFailLimit { + c.JSON(http.StatusTooManyRequests, model.ErrorResponse(http.StatusTooManyRequests, "Too many failed attempts. Please try again later.")) + c.Abort() + return + } + + c.Next() + } +} + +func RecordPickupFailure(ip, code string) { + key := ip + ":" + code + failureMutex.Lock() + pickupFailures[key]++ + + // 设置 1 小时后清除记录 (简单实现) + go func() { + time.Sleep(1 * time.Hour) + failureMutex.Lock() + delete(pickupFailures, key) + failureMutex.Unlock() + }() + + failureMutex.Unlock() +} diff --git a/internal/api/public/pickup.go b/internal/api/public/pickup.go new file mode 100644 index 0000000..c5a0242 --- /dev/null +++ b/internal/api/public/pickup.go @@ -0,0 +1,162 @@ +package public + +import ( + "FileRelay/internal/api/middleware" + "FileRelay/internal/bootstrap" + "FileRelay/internal/model" + "FileRelay/internal/service" + "FileRelay/internal/storage" + "archive/zip" + "fmt" + "io" + "net/http" + "strconv" + "time" + + "github.com/gin-gonic/gin" +) + +type PickupResponse struct { + Remark string `json:"remark"` + ExpireAt *time.Time `json:"expire_at"` + ExpireType string `json:"expire_type"` + DownloadCount int `json:"download_count"` + MaxDownloads int `json:"max_downloads"` + Files []model.FileItem `json:"files"` +} + +// DownloadBatch 批量下载文件 (ZIP) +// @Summary 批量下载文件 +// @Description 根据取件码将批次内的所有文件打包为 ZIP 格式一次性下载 +// @Tags Public +// @Param pickup_code path string true "取件码" +// @Produce application/zip +// @Success 200 {file} file +// @Failure 404 {object} model.Response +// @Router /api/download/batch/{pickup_code} [get] +func (h *PickupHandler) DownloadBatch(c *gin.Context) { + code := c.Param("pickup_code") + batch, err := h.batchService.GetBatchByPickupCode(code) + if err != nil { + c.JSON(http.StatusNotFound, model.ErrorResponse(model.CodeNotFound, "batch not found or expired")) + return + } + + c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=\"batch_%s.zip\"", code)) + c.Header("Content-Type", "application/zip") + + zw := zip.NewWriter(c.Writer) + defer zw.Close() + + for _, item := range batch.FileItems { + reader, err := storage.GlobalStorage.Open(c.Request.Context(), item.StoragePath) + if err != nil { + continue // Skip failed files + } + + f, err := zw.Create(item.OriginalName) + if err != nil { + reader.Close() + continue + } + + _, _ = io.Copy(f, reader) + reader.Close() + } + + // 增加下载次数 + h.batchService.IncrementDownloadCount(batch.ID) +} + +type PickupHandler struct { + batchService *service.BatchService +} + +func NewPickupHandler() *PickupHandler { + return &PickupHandler{ + batchService: service.NewBatchService(), + } +} + +// Pickup 获取批次信息 +// @Summary 获取批次信息 +// @Description 根据取件码获取文件批次详详情和文件列表 +// @Tags Public +// @Produce json +// @Param pickup_code path string true "取件码" +// @Success 200 {object} model.Response{data=PickupResponse} +// @Failure 404 {object} model.Response +// @Router /api/pickup/{pickup_code} [get] +func (h *PickupHandler) Pickup(c *gin.Context) { + code := c.Param("pickup_code") + if code == "" { + c.JSON(http.StatusBadRequest, model.ErrorResponse(model.CodeBadRequest, "pickup code required")) + return + } + + batch, err := h.batchService.GetBatchByPickupCode(code) + if err != nil { + middleware.RecordPickupFailure(c.ClientIP(), code) + c.JSON(http.StatusNotFound, model.ErrorResponse(model.CodeNotFound, "batch not found or expired")) + return + } + + c.JSON(http.StatusOK, model.SuccessResponse(PickupResponse{ + Remark: batch.Remark, + ExpireAt: batch.ExpireAt, + ExpireType: batch.ExpireType, + DownloadCount: batch.DownloadCount, + MaxDownloads: batch.MaxDownloads, + Files: batch.FileItems, + })) +} + +// DownloadFile 下载单个文件 +// @Summary 下载单个文件 +// @Description 根据文件 ID 下载单个文件 +// @Tags Public +// @Param file_id path int true "文件 ID" +// @Produce application/octet-stream +// @Success 200 {file} file +// @Failure 404 {object} model.Response +// @Failure 410 {object} model.Response +// @Router /api/download/file/{file_id} [get] +func (h *PickupHandler) DownloadFile(c *gin.Context) { + fileIDStr := c.Param("file_id") + fileID, _ := strconv.ParseUint(fileIDStr, 10, 32) + + var item model.FileItem + if err := bootstrap.DB.First(&item, fileID).Error; err != nil { + c.JSON(http.StatusNotFound, model.ErrorResponse(model.CodeNotFound, "file not found")) + return + } + + var batch model.FileBatch + if err := bootstrap.DB.First(&batch, item.BatchID).Error; err != nil { + c.JSON(http.StatusNotFound, model.ErrorResponse(model.CodeNotFound, "batch not found")) + return + } + + if h.batchService.IsExpired(&batch) { + h.batchService.MarkAsExpired(&batch) + c.JSON(http.StatusGone, model.ErrorResponse(model.CodeGone, "batch expired")) + return + } + + // 打开文件 + reader, err := storage.GlobalStorage.Open(c.Request.Context(), item.StoragePath) + if err != nil { + c.JSON(http.StatusInternalServerError, model.ErrorResponse(model.CodeInternalError, "failed to open file")) + return + } + defer reader.Close() + + // 增加下载次数 + h.batchService.IncrementDownloadCount(batch.ID) + + c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", item.OriginalName)) + c.Header("Content-Type", item.MimeType) + c.Header("Content-Length", strconv.FormatInt(item.Size, 10)) + + io.Copy(c.Writer, reader) +} diff --git a/internal/api/public/upload.go b/internal/api/public/upload.go new file mode 100644 index 0000000..5d4da1b --- /dev/null +++ b/internal/api/public/upload.go @@ -0,0 +1,96 @@ +package public + +import ( + "FileRelay/internal/config" + "FileRelay/internal/model" + "FileRelay/internal/service" + "net/http" + "strconv" + "time" + + "github.com/gin-gonic/gin" +) + +type UploadHandler struct { + uploadService *service.UploadService +} + +func NewUploadHandler() *UploadHandler { + return &UploadHandler{ + uploadService: service.NewUploadService(), + } +} + +type UploadResponse struct { + PickupCode string `json:"pickup_code"` + ExpireAt *time.Time `json:"expire_at"` + BatchID uint `json:"batch_id"` +} + +// Upload 上传文件并生成取件码 +// @Summary 上传文件 +// @Description 上传一个或多个文件并创建一个提取批次 +// @Tags Public +// @Accept multipart/form-data +// @Produce json +// @Param files formData file true "文件列表" +// @Param remark formData string false "备注" +// @Param expire_type formData string false "过期类型 (time/download/permanent)" +// @Param expire_days formData int false "过期天数 (针对 time 类型)" +// @Param max_downloads formData int false "最大下载次数 (针对 download 类型)" +// @Success 200 {object} model.Response{data=UploadResponse} +// @Failure 400 {object} model.Response +// @Failure 500 {object} model.Response +// @Router /api/upload [post] +func (h *UploadHandler) Upload(c *gin.Context) { + form, err := c.MultipartForm() + if err != nil { + c.JSON(http.StatusBadRequest, model.ErrorResponse(model.CodeBadRequest, "invalid form")) + return + } + + files := form.File["files"] + if len(files) == 0 { + c.JSON(http.StatusBadRequest, model.ErrorResponse(model.CodeBadRequest, "no files uploaded")) + return + } + + if len(files) > config.GlobalConfig.Upload.MaxBatchFiles { + c.JSON(http.StatusBadRequest, model.ErrorResponse(model.CodeBadRequest, "too many files")) + return + } + + remark := c.PostForm("remark") + expireType := c.PostForm("expire_type") // time / download / permanent + if expireType == "" { + expireType = "time" + } + + var expireValue interface{} + switch expireType { + case "time": + days, _ := strconv.Atoi(c.PostForm("expire_days")) + if days <= 0 { + days = config.GlobalConfig.Upload.MaxRetentionDays + } + expireValue = days + case "download": + max, _ := strconv.Atoi(c.PostForm("max_downloads")) + if max <= 0 { + max = 1 + } + expireValue = max + } + + batch, err := h.uploadService.CreateBatch(c.Request.Context(), files, remark, expireType, expireValue) + if err != nil { + c.JSON(http.StatusInternalServerError, model.ErrorResponse(model.CodeInternalError, err.Error())) + return + } + + c.JSON(http.StatusOK, model.SuccessResponse(UploadResponse{ + PickupCode: batch.PickupCode, + ExpireAt: batch.ExpireAt, + BatchID: batch.ID, + })) +} diff --git a/internal/auth/jwt.go b/internal/auth/jwt.go new file mode 100644 index 0000000..40e0924 --- /dev/null +++ b/internal/auth/jwt.go @@ -0,0 +1,42 @@ +package auth + +import ( + "FileRelay/internal/config" + "time" + + "github.com/golang-jwt/jwt/v5" +) + +type Claims struct { + AdminID uint `json:"admin_id"` + jwt.RegisteredClaims +} + +func GenerateToken(adminID uint) (string, error) { + claims := Claims{ + AdminID: adminID, + RegisteredClaims: jwt.RegisteredClaims{ + ExpiresAt: jwt.NewNumericDate(time.Now().Add(24 * time.Hour)), + IssuedAt: jwt.NewNumericDate(time.Now()), + }, + } + + token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) + return token.SignedString([]byte(config.GlobalConfig.Security.JWTSecret)) +} + +func ParseToken(tokenString string) (*Claims, error) { + token, err := jwt.ParseWithClaims(tokenString, &Claims{}, func(token *jwt.Token) (interface{}, error) { + return []byte(config.GlobalConfig.Security.JWTSecret), nil + }) + + if err != nil { + return nil, err + } + + if claims, ok := token.Claims.(*Claims); ok && token.Valid { + return claims, nil + } + + return nil, jwt.ErrSignatureInvalid +} diff --git a/internal/bootstrap/init.go b/internal/bootstrap/init.go new file mode 100644 index 0000000..6a2160f --- /dev/null +++ b/internal/bootstrap/init.go @@ -0,0 +1,112 @@ +package bootstrap + +import ( + "FileRelay/internal/config" + "FileRelay/internal/model" + "FileRelay/internal/storage" + "context" + "crypto/rand" + "fmt" + "log" + "math/big" + + "github.com/glebarez/sqlite" + "golang.org/x/crypto/bcrypt" + "gorm.io/gorm" +) + +var DB *gorm.DB + +func InitDB() { + var err error + dbPath := config.GlobalConfig.Database.Path + if dbPath == "" { + dbPath = "file_relay.db" + } + + DB, err = gorm.Open(sqlite.Open(dbPath), &gorm.Config{}) + if err != nil { + log.Fatalf("Failed to connect to database: %v", err) + } + + // 自动迁移 + err = DB.AutoMigrate( + &model.FileBatch{}, + &model.FileItem{}, + &model.APIToken{}, + &model.Admin{}, + ) + if err != nil { + log.Fatalf("Failed to migrate database: %v", err) + } + + fmt.Println("Database initialized and migrated.") + + // 初始化存储 + initStorage() + + // 初始化管理员 (如果不存在) + initAdmin() +} + +func initStorage() { + storageType := config.GlobalConfig.Storage.Type + switch storageType { + case "local": + storage.GlobalStorage = storage.NewLocalStorage(config.GlobalConfig.Storage.Local.Path) + case "webdav": + cfg := config.GlobalConfig.Storage.WebDAV + storage.GlobalStorage = storage.NewWebDAVStorage(cfg.URL, cfg.Username, cfg.Password, cfg.Root) + case "s3": + cfg := config.GlobalConfig.Storage.S3 + s3Storage, err := storage.NewS3Storage(context.Background(), cfg.Endpoint, cfg.Region, cfg.AccessKey, cfg.SecretKey, cfg.Bucket, cfg.UseSSL) + if err != nil { + log.Fatalf("Failed to initialize S3 storage: %v", err) + } + storage.GlobalStorage = s3Storage + default: + log.Fatalf("Unsupported storage type: %s", storageType) + } + fmt.Printf("Storage initialized with type: %s\n", storageType) +} + +func initAdmin() { + var count int64 + DB.Model(&model.Admin{}).Count(&count) + if count == 0 { + passwordHash := config.GlobalConfig.Security.AdminPasswordHash + if passwordHash == "" { + // 生成随机密码 + password := generateRandomPassword(12) + hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost) + if err != nil { + log.Fatalf("Failed to generate password hash: %v", err) + } + passwordHash = string(hash) + fmt.Printf("**************************************************\n") + fmt.Printf("NO ADMIN PASSWORD CONFIGURED. GENERATED RANDOM PASSWORD:\n") + fmt.Printf("Password: %s\n", password) + fmt.Printf("Please save this password or configure admin_password_hash in config.yaml\n") + fmt.Printf("**************************************************\n") + } + + admin := &model.Admin{ + PasswordHash: passwordHash, + } + DB.Create(admin) + fmt.Println("Admin account initialized.") + } +} + +func generateRandomPassword(length int) string { + const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*" + b := make([]byte, length) + for i := range b { + num, err := rand.Int(rand.Reader, big.NewInt(int64(len(charset)))) + if err != nil { + return "admin123" // 退路 + } + b[i] = charset[num.Int64()] + } + return string(b) +} diff --git a/internal/config/config.go b/internal/config/config.go new file mode 100644 index 0000000..a58d066 --- /dev/null +++ b/internal/config/config.go @@ -0,0 +1,82 @@ +package config + +import ( + "os" + + "gopkg.in/yaml.v3" +) + +type Config struct { + Site SiteConfig `yaml:"site"` + Security SecurityConfig `yaml:"security"` + Upload UploadConfig `yaml:"upload"` + Storage StorageConfig `yaml:"storage"` + APIToken APITokenConfig `yaml:"api_token"` + Database DatabaseConfig `yaml:"database"` +} + +type SiteConfig struct { + Name string `yaml:"name"` + Description string `yaml:"description"` +} + +type SecurityConfig struct { + AdminPasswordHash string `yaml:"admin_password_hash"` + PickupCodeLength int `yaml:"pickup_code_length"` + PickupFailLimit int `yaml:"pickup_fail_limit"` + JWTSecret string `yaml:"jwt_secret"` +} + +type UploadConfig struct { + MaxFileSizeMB int64 `yaml:"max_file_size_mb"` + MaxBatchFiles int `yaml:"max_batch_files"` + MaxRetentionDays int `yaml:"max_retention_days"` +} + +type StorageConfig struct { + Type string `yaml:"type"` + Local struct { + Path string `yaml:"path"` + } `yaml:"local"` + WebDAV struct { + URL string `yaml:"url"` + Username string `yaml:"username"` + Password string `yaml:"password"` + Root string `yaml:"root"` + } `yaml:"webdav"` + S3 struct { + Endpoint string `yaml:"endpoint"` + Region string `yaml:"region"` + AccessKey string `yaml:"access_key"` + SecretKey string `yaml:"secret_key"` + Bucket string `yaml:"bucket"` + UseSSL bool `yaml:"use_ssl"` + } `yaml:"s3"` +} + +type APITokenConfig struct { + Enabled bool `yaml:"enabled"` + AllowAdminAPI bool `yaml:"allow_admin_api"` + MaxTokens int `yaml:"max_tokens"` +} + +type DatabaseConfig struct { + Path string `yaml:"path"` +} + +var GlobalConfig *Config + +func LoadConfig(path string) error { + data, err := os.ReadFile(path) + if err != nil { + return err + } + + var cfg Config + if err := yaml.Unmarshal(data, &cfg); err != nil { + return err + } + + GlobalConfig = &cfg + return nil +} diff --git a/internal/model/admin.go b/internal/model/admin.go new file mode 100644 index 0000000..2e2b597 --- /dev/null +++ b/internal/model/admin.go @@ -0,0 +1,11 @@ +package model + +import ( + "time" +) + +type Admin struct { + ID uint `gorm:"primaryKey" json:"id"` + PasswordHash string `json:"-"` + LastLogin *time.Time `json:"last_login"` +} diff --git a/internal/model/api_token.go b/internal/model/api_token.go new file mode 100644 index 0000000..459dcc8 --- /dev/null +++ b/internal/model/api_token.go @@ -0,0 +1,16 @@ +package model + +import ( + "time" +) + +type APIToken struct { + ID uint `gorm:"primaryKey" json:"id"` + Name string `json:"name"` + TokenHash string `gorm:"uniqueIndex;not null" json:"-"` + Scope string `json:"scope"` + ExpireAt *time.Time `json:"expire_at"` + LastUsedAt *time.Time `json:"last_used_at"` + Revoked bool `gorm:"default:false" json:"revoked"` + CreatedAt time.Time `json:"created_at"` +} diff --git a/internal/model/file_batch.go b/internal/model/file_batch.go new file mode 100644 index 0000000..b723789 --- /dev/null +++ b/internal/model/file_batch.go @@ -0,0 +1,22 @@ +package model + +import ( + "time" + + "gorm.io/gorm" +) + +type FileBatch struct { + ID uint `gorm:"primaryKey" json:"id"` + PickupCode string `gorm:"uniqueIndex;not null" json:"pickup_code"` + Remark string `json:"remark"` + ExpireType string `json:"expire_type"` // time / download / permanent + ExpireAt *time.Time `json:"expire_at"` + MaxDownloads int `json:"max_downloads"` + DownloadCount int `gorm:"default:0" json:"download_count"` + Status string `gorm:"default:'active'" json:"status"` // active / expired / deleted + FileItems []FileItem `gorm:"foreignKey:BatchID" json:"file_items,omitempty"` + CreatedAt time.Time `json:"created_at"` + UpdatedAt time.Time `json:"updated_at"` + DeletedAt gorm.DeletedAt `gorm:"index" json:"-"` +} diff --git a/internal/model/file_item.go b/internal/model/file_item.go new file mode 100644 index 0000000..c766dbe --- /dev/null +++ b/internal/model/file_item.go @@ -0,0 +1,15 @@ +package model + +import ( + "time" +) + +type FileItem struct { + ID uint `gorm:"primaryKey" json:"id"` + BatchID uint `gorm:"index;not null" json:"batch_id"` + OriginalName string `json:"original_name"` + StoragePath string `json:"storage_path"` + Size int64 `json:"size"` + MimeType string `json:"mime_type"` + CreatedAt time.Time `json:"created_at"` +} diff --git a/internal/model/response.go b/internal/model/response.go new file mode 100644 index 0000000..33bc2d4 --- /dev/null +++ b/internal/model/response.go @@ -0,0 +1,45 @@ +package model + +// Response 统一响应模型 +type Response struct { + Code int `json:"code" example:"200"` + Msg string `json:"msg" example:"success"` + Data interface{} `json:"data"` +} + +// 错误码定义 +const ( + CodeSuccess = 200 + CodeBadRequest = 400 + CodeUnauthorized = 401 + CodeForbidden = 403 + CodeNotFound = 404 + CodeGone = 410 + CodeInternalError = 500 +) + +// NewResponse 创建响应 +func NewResponse(code int, msg string, data interface{}) Response { + return Response{ + Code: code, + Msg: msg, + Data: data, + } +} + +// SuccessResponse 成功响应 +func SuccessResponse(data interface{}) Response { + return Response{ + Code: CodeSuccess, + Msg: "success", + Data: data, + } +} + +// ErrorResponse 错误响应 +func ErrorResponse(code int, msg string) Response { + return Response{ + Code: code, + Msg: msg, + } +} diff --git a/internal/service/batch_service.go b/internal/service/batch_service.go new file mode 100644 index 0000000..9957e93 --- /dev/null +++ b/internal/service/batch_service.go @@ -0,0 +1,86 @@ +package service + +import ( + "FileRelay/internal/bootstrap" + "FileRelay/internal/model" + "FileRelay/internal/storage" + "context" + "errors" + "time" + + "gorm.io/gorm" +) + +type BatchService struct { + db *gorm.DB +} + +func NewBatchService() *BatchService { + return &BatchService{db: bootstrap.DB} +} + +func (s *BatchService) GetBatchByPickupCode(code string) (*model.FileBatch, error) { + var batch model.FileBatch + err := s.db.Preload("FileItems").Where("pickup_code = ? AND status = ?", code, "active").First(&batch).Error + if err != nil { + return nil, err + } + + // 检查是否过期 + if s.IsExpired(&batch) { + s.MarkAsExpired(&batch) + return nil, errors.New("batch expired") + } + + return &batch, nil +} + +func (s *BatchService) IsExpired(batch *model.FileBatch) bool { + if batch.Status != "active" { + return true + } + + switch batch.ExpireType { + case "time": + if batch.ExpireAt != nil && time.Now().After(*batch.ExpireAt) { + return true + } + case "download": + if batch.MaxDownloads > 0 && batch.DownloadCount >= batch.MaxDownloads { + return true + } + } + return false +} + +func (s *BatchService) MarkAsExpired(batch *model.FileBatch) error { + return s.db.Model(batch).Update("status", "expired").Error +} + +func (s *BatchService) DeleteBatch(ctx context.Context, batchID uint) error { + var batch model.FileBatch + if err := s.db.Preload("FileItems").First(&batch, batchID).Error; err != nil { + return err + } + + // 删除物理文件 + for _, item := range batch.FileItems { + _ = storage.GlobalStorage.Delete(ctx, item.StoragePath) + } + + // 删除数据库记录 (软删除 Batch) + return s.db.Transaction(func(tx *gorm.DB) error { + if err := tx.Where("batch_id = ?", batch.ID).Delete(&model.FileItem{}).Error; err != nil { + return err + } + if err := tx.Delete(&batch).Error; err != nil { + return err + } + return nil + }) +} + +func (s *BatchService) IncrementDownloadCount(batchID uint) error { + return s.db.Model(&model.FileBatch{}).Where("id = ?", batchID). + UpdateColumn("download_count", gorm.Expr("download_count + ?", 1)).Error +} diff --git a/internal/service/token_service.go b/internal/service/token_service.go new file mode 100644 index 0000000..4258265 --- /dev/null +++ b/internal/service/token_service.go @@ -0,0 +1,83 @@ +package service + +import ( + "FileRelay/internal/bootstrap" + "FileRelay/internal/model" + "crypto/sha256" + "encoding/hex" + "errors" + "strings" + "time" + + "github.com/google/uuid" + "gorm.io/gorm" +) + +type TokenService struct { + db *gorm.DB +} + +func NewTokenService() *TokenService { + return &TokenService{db: bootstrap.DB} +} + +func (s *TokenService) CreateToken(name string, scope string, expireAt *time.Time) (string, *model.APIToken, error) { + rawToken := uuid.New().String() + hash := s.hashToken(rawToken) + + token := &model.APIToken{ + Name: name, + TokenHash: hash, + Scope: scope, + ExpireAt: expireAt, + } + + if err := s.db.Create(token).Error; err != nil { + return "", nil, err + } + + return rawToken, token, nil +} + +func (s *TokenService) ValidateToken(rawToken string, requiredScope string) (*model.APIToken, error) { + hash := s.hashToken(rawToken) + var token model.APIToken + if err := s.db.Where("token_hash = ? AND revoked = ?", hash, false).First(&token).Error; err != nil { + return nil, errors.New("invalid token") + } + + if token.ExpireAt != nil && time.Now().After(*token.ExpireAt) { + return nil, errors.New("token expired") + } + + // 检查 Scope (简单包含判断) + // 在实际应用中可以实现更复杂的逻辑 + if requiredScope != "" && !s.checkScope(token.Scope, requiredScope) { + return nil, errors.New("insufficient scope") + } + + // 更新最后使用时间 + now := time.Now() + s.db.Model(&token).Update("last_used_at", &now) + + return &token, nil +} + +func (s *TokenService) hashToken(token string) string { + h := sha256.New() + h.Write([]byte(token)) + return hex.EncodeToString(h.Sum(nil)) +} + +func (s *TokenService) checkScope(tokenScope, requiredScope string) bool { + if requiredScope == "" { + return true + } + scopes := strings.Split(tokenScope, ",") + for _, s := range scopes { + if strings.TrimSpace(s) == requiredScope { + return true + } + } + return false +} diff --git a/internal/service/upload_service.go b/internal/service/upload_service.go new file mode 100644 index 0000000..b55ab71 --- /dev/null +++ b/internal/service/upload_service.go @@ -0,0 +1,121 @@ +package service + +import ( + "FileRelay/internal/bootstrap" + "FileRelay/internal/config" + "FileRelay/internal/model" + "FileRelay/internal/storage" + "context" + "crypto/rand" + "fmt" + "math/big" + "mime/multipart" + "path/filepath" + "time" + + "github.com/google/uuid" + "gorm.io/gorm" +) + +type UploadService struct { + db *gorm.DB +} + +func NewUploadService() *UploadService { + return &UploadService{db: bootstrap.DB} +} + +func (s *UploadService) CreateBatch(ctx context.Context, files []*multipart.FileHeader, remark string, expireType string, expireValue interface{}) (*model.FileBatch, error) { + // 1. 生成取件码 + pickupCode, err := s.generatePickupCode(config.GlobalConfig.Security.PickupCodeLength) + if err != nil { + return nil, err + } + + // 2. 准备 Batch + batch := &model.FileBatch{ + PickupCode: pickupCode, + Remark: remark, + ExpireType: expireType, + Status: "active", + } + + switch expireType { + case "time": + if days, ok := expireValue.(int); ok { + expireAt := time.Now().Add(time.Duration(days) * 24 * time.Hour) + batch.ExpireAt = &expireAt + } + case "download": + if max, ok := expireValue.(int); ok { + batch.MaxDownloads = max + } + } + + // 3. 处理文件上传 + return batch, s.db.Transaction(func(tx *gorm.DB) error { + if err := tx.Create(batch).Error; err != nil { + return err + } + + for _, fileHeader := range files { + fileItem, err := s.processFile(ctx, tx, batch.ID, fileHeader) + if err != nil { + return err + } + batch.FileItems = append(batch.FileItems, *fileItem) + } + return nil + }) +} + +func (s *UploadService) processFile(ctx context.Context, tx *gorm.DB, batchID uint, fileHeader *multipart.FileHeader) (*model.FileItem, error) { + file, err := fileHeader.Open() + if err != nil { + return nil, err + } + defer file.Close() + + // 生成唯一存储路径 + ext := filepath.Ext(fileHeader.Filename) + storagePath := fmt.Sprintf("%d/%s%s", batchID, uuid.New().String(), ext) + + // 保存到存储层 + if err := storage.GlobalStorage.Save(ctx, storagePath, file); err != nil { + return nil, err + } + + // 创建数据库记录 + item := &model.FileItem{ + BatchID: batchID, + OriginalName: fileHeader.Filename, + StoragePath: storagePath, + Size: fileHeader.Size, + MimeType: fileHeader.Header.Get("Content-Type"), + } + + if err := tx.Create(item).Error; err != nil { + return nil, err + } + + return item, nil +} + +func (s *UploadService) generatePickupCode(length int) (string, error) { + const charset = "0123456789" + b := make([]byte, length) + for i := range b { + num, err := rand.Int(rand.Reader, big.NewInt(int64(len(charset)))) + if err != nil { + return "", err + } + b[i] = charset[num.Int64()] + } + // 检查是否冲突 + var count int64 + s.db.Model(&model.FileBatch{}).Where("pickup_code = ? AND status = ?", string(b), "active").Count(&count) + if count > 0 { + return s.generatePickupCode(length) // 递归生成 + } + return string(b), nil +} diff --git a/internal/task/cleaner.go b/internal/task/cleaner.go new file mode 100644 index 0000000..fe3f310 --- /dev/null +++ b/internal/task/cleaner.go @@ -0,0 +1,59 @@ +package task + +import ( + "FileRelay/internal/bootstrap" + "FileRelay/internal/model" + "FileRelay/internal/service" + "context" + "fmt" + "time" +) + +type Cleaner struct { + batchService *service.BatchService +} + +func NewCleaner() *Cleaner { + return &Cleaner{ + batchService: service.NewBatchService(), + } +} + +func (c *Cleaner) Start(ctx context.Context) { + ticker := time.NewTicker(1 * time.Hour) + defer ticker.Stop() + + for { + select { + case <-ctx.Done(): + return + case <-ticker.C: + c.Clean() + } + } +} + +func (c *Cleaner) Clean() { + fmt.Println("Running cleanup task...") + + // 1. 寻找过期的 Active Batches + var batches []model.FileBatch + now := time.Now() + bootstrap.DB.Where("status = ? AND expire_type = ? AND expire_at < ?", "active", "time", now).Find(&batches) + + for _, batch := range batches { + c.batchService.MarkAsExpired(&batch) + } + + // 2. 寻找标记为 expired 或 deleted 的批次并彻底清理文件和记录 + // 这里可以根据业务需求决定是否立即物理删除,或者等待一段时间 + // 按照需求:扫描 expired / deleted 批次,批量删除文件,清理数据库记录 + + var toDelete []model.FileBatch + bootstrap.DB.Unscoped().Where("status IN ? OR deleted_at IS NOT NULL", []string{"expired", "deleted"}).Find(&toDelete) + + for _, batch := range toDelete { + fmt.Printf("Deep cleaning batch: %d\n", batch.ID) + c.batchService.DeleteBatch(context.Background(), batch.ID) + } +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..6fa2818 --- /dev/null +++ b/main.go @@ -0,0 +1,102 @@ +package main + +import ( + _ "FileRelay/docs" + "FileRelay/internal/api/admin" + "FileRelay/internal/api/middleware" + "FileRelay/internal/api/public" + "FileRelay/internal/bootstrap" + "FileRelay/internal/config" + "FileRelay/internal/task" + "context" + "fmt" + "log" + + "github.com/gin-contrib/cors" + "github.com/gin-gonic/gin" + swaggerFiles "github.com/swaggo/files" + ginSwagger "github.com/swaggo/gin-swagger" +) + +// @title 文件暂存柜 API +// @version 1.0 +// @description 自托管的文件暂存柜后端系统 API 文档 +// @termsOfService http://swagger.io/terms/ + +// @contact.name API Support +// @contact.url http://www.swagger.io/support +// @contact.email support@swagger.io + +// @license.name Apache 2.0 +// @license.url http://www.apache.org/licenses/LICENSE-2.0.html + +// @BasePath / + +// @securityDefinitions.apikey AdminAuth +// @in header +// @name Authorization +// @description Type "Bearer " to authenticate. + +func main() { + // 1. 加载配置 + if err := config.LoadConfig("config/config.yaml"); err != nil { + log.Fatalf("Failed to load config: %v", err) + } + + // 2. 初始化 + bootstrap.InitDB() + + // 3. 启动清理任务 + cleaner := task.NewCleaner() + go cleaner.Start(context.Background()) + + // 4. 设置路由 + r := gin.Default() + + // 配置更完善的 CORS + corsConfig := cors.DefaultConfig() + corsConfig.AllowAllOrigins = true + corsConfig.AllowHeaders = append(corsConfig.AllowHeaders, "Authorization", "Accept", "X-Requested-With") + corsConfig.AllowMethods = append(corsConfig.AllowMethods, "OPTIONS") + r.Use(cors.New(corsConfig)) + + // Swagger 文档 + r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler)) + + // 公共接口 + uploadHandler := public.NewUploadHandler() + pickupHandler := public.NewPickupHandler() + + api := r.Group("/api") + { + api.POST("/upload", uploadHandler.Upload) + api.GET("/pickup/:pickup_code", middleware.PickupRateLimit(), pickupHandler.Pickup) + api.GET("/download/file/:file_id", pickupHandler.DownloadFile) + api.GET("/download/batch/:pickup_code", pickupHandler.DownloadBatch) + } + + // 管理员接口 + authHandler := admin.NewAuthHandler() + batchHandler := admin.NewBatchHandler() + tokenHandler := admin.NewTokenHandler() + + r.POST("/admin/login", authHandler.Login) + + adm := r.Group("/admin") + adm.Use(middleware.AdminAuth()) + { + adm.GET("/batches", batchHandler.ListBatches) + adm.GET("/batch/:batch_id", batchHandler.GetBatch) + adm.PUT("/batch/:batch_id", batchHandler.UpdateBatch) + adm.DELETE("/batch/:batch_id", batchHandler.DeleteBatch) + + adm.GET("/api-tokens", tokenHandler.ListTokens) + adm.POST("/api-tokens", tokenHandler.CreateToken) + adm.DELETE("/api-tokens/:id", tokenHandler.DeleteToken) + } + + // 5. 运行 + port := 8080 + fmt.Printf("Server is running on port %d\n", port) + r.Run(fmt.Sprintf(":%d", port)) +}