mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-09-29 22:06:38 +08:00
530a9dd3ee
BREAKING: pkg/migrator/ deleted entirely. Migration SQL files are now owned by each plugin in its own migrations/ directory. Architecture: - Delete pkg/migrator/ (26 global SQL files + ClickHouse migration) - Move global SQL to per-plugin migrations/ with go:embed + Register() - Rewrite cmd/app.go gooseEngine: uses Inject[DBService] for DB, iterates all plugin-registered MigrationEntry, runs goose.Up per entry - core.MigrationEngine.Migrate signature changed: *Context instead of context.Context, so engine can resolve services via IoC Per-plugin migration ownership: auth/ → w_access_tokens, w_auth_sources, w_external_accounts user/ → w_users (seed system user) admin/ → w_system_configs, w_templates (seeds) upload/ → w_uploads, w_upload_stats message_gateway/ → w_push_*, w_message_* risk_control/ → w_user_access_logs (PG + ClickHouse) driver_asynq_cron/ → w_schedules driver_asynq_worker/ → w_task_executions Dependencies: - cmd/banner.go: removed migration report display (migrations are automatic) - cmd/reset_passwd.go: removed PreRun migrator.Migrate() call - go.mod: clickhouse-go kept (used by plugins/infra/database/clickhouse.go)
133 lines
4.3 KiB
Go
133 lines
4.3 KiB
Go
// Copyright 2026 Arctel.net
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
// Package upload provides file uploading, storage abstraction, image transcoding, and caching domain plugin for Cordis.
|
|
package upload
|
|
|
|
import (
|
|
"context"
|
|
"embed"
|
|
|
|
"github.com/Rain-kl/Wavelet/core"
|
|
"github.com/Rain-kl/Wavelet/core/contracts"
|
|
"github.com/Rain-kl/Wavelet/core/extpoints"
|
|
"github.com/Rain-kl/Wavelet/plugins/domain/upload/filesrv"
|
|
"github.com/Rain-kl/Wavelet/plugins/domain/upload/handler"
|
|
"github.com/Rain-kl/Wavelet/plugins/domain/upload/task"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/hibiken/asynq"
|
|
)
|
|
|
|
//go:embed migrations/*.sql
|
|
var uploadMigrations embed.FS
|
|
|
|
// Plugin implements core.Plugin to provide file upload and media serving domain services.
|
|
type Plugin struct{}
|
|
|
|
// New creates a new upload domain plugin.
|
|
func New() *Plugin {
|
|
return &Plugin{}
|
|
}
|
|
|
|
// Name returns the unique identifier for the upload domain plugin.
|
|
func (p *Plugin) Name() string {
|
|
return "upload"
|
|
}
|
|
|
|
// Manifest returns the plugin metadata.
|
|
func (p *Plugin) Manifest() core.Manifest {
|
|
return core.Manifest{
|
|
Name: "upload",
|
|
Version: "1.0.0",
|
|
Description: "File upload, secure delivery, image transcoding, and storage management domain plugin",
|
|
Author: "Wavelet Team",
|
|
}
|
|
}
|
|
|
|
// Apply registers upload routes, tasks, and settings into the Context.
|
|
func (p *Plugin) Apply(ctx *core.Context) error {
|
|
// 0. Resolve auth service for middleware (via IoC, not direct import)
|
|
var authSvc contracts.AuthService
|
|
if err := core.Using[contracts.AuthService](ctx, func(svc contracts.AuthService) { authSvc = svc }); err != nil {
|
|
return err
|
|
}
|
|
loginMW := authSvc.RequireAuthMiddleware().(gin.HandlerFunc)
|
|
|
|
// 0a. Register migrations
|
|
ctx.Migrations().Register("upload", uploadMigrations)
|
|
|
|
// 1. Register File Server Routes
|
|
ctx.Router().GET("/f/:id", filesrv.ServeFileByID)
|
|
|
|
// 2. Register User/Admin Upload HTTP Routes
|
|
uploadGroup := ctx.Router().Group("/api/v1/upload", loginMW)
|
|
{
|
|
uploadGroup.POST("", handler.UploadFile)
|
|
uploadGroup.GET("", handler.ListFiles)
|
|
uploadGroup.DELETE("/:id", handler.DeleteFile)
|
|
uploadGroup.POST("/batch-download", handler.BatchDownloadFiles)
|
|
}
|
|
|
|
adminUploadGroup := ctx.Router().Group("/api/v1/admin/uploads", loginMW)
|
|
{
|
|
adminUploadGroup.GET("", handler.ListFiles)
|
|
adminUploadGroup.GET("/stats", handler.GetFileStats)
|
|
adminUploadGroup.DELETE("/:id", handler.DeleteFile)
|
|
adminUploadGroup.GET("/download/:id", handler.DownloadFile)
|
|
adminUploadGroup.POST("/download/batch", handler.BatchDownloadFiles)
|
|
adminUploadGroup.GET("/types", handler.GetDistinctUploadTypes)
|
|
}
|
|
|
|
const (
|
|
defaultCleanupRetry = 3
|
|
defaultStatsRetry = 2
|
|
defaultSingleRetry = 1
|
|
)
|
|
|
|
// 3. Register Asynq tasks
|
|
cleanupHandler := &task.SystemCleanupHandler{}
|
|
ctx.Task().Register(task.SystemCleanupTask, func(c context.Context, t *asynq.Task) error {
|
|
_, err := cleanupHandler.Execute(c, t.Payload())
|
|
return err
|
|
}, extpoints.WithTaskRetry(defaultCleanupRetry))
|
|
|
|
rebuildStatsHandler := &task.RebuildUploadStatsHandler{}
|
|
ctx.Task().Register(task.RebuildUploadStatsTask, func(c context.Context, t *asynq.Task) error {
|
|
_, err := rebuildStatsHandler.Execute(c, t.Payload())
|
|
return err
|
|
}, extpoints.WithTaskRetry(defaultStatsRetry))
|
|
|
|
migrationHandler := &task.MigrationHandler{}
|
|
ctx.Task().Register(task.StorageMigrationTask, func(c context.Context, t *asynq.Task) error {
|
|
_, err := migrationHandler.Execute(c, t.Payload())
|
|
return err
|
|
}, extpoints.WithTaskRetry(defaultSingleRetry))
|
|
|
|
warmHandler := &task.WarmImageCacheHandler{}
|
|
ctx.Task().Register(task.WarmImageCacheTask, func(c context.Context, t *asynq.Task) error {
|
|
_, err := warmHandler.Execute(c, t.Payload())
|
|
return err
|
|
}, extpoints.WithTaskRetry(1))
|
|
|
|
// 4. Register Cron Schedule
|
|
ctx.Schedule().RegisterCron("0 3 * * *", task.SystemCleanupTask, nil)
|
|
|
|
// 5. Register Settings Schemas
|
|
ctx.Settings().Register(extpoints.SettingSchema{
|
|
Key: "upload.max_file_size_mb",
|
|
Default: 100,
|
|
Description: "Maximum upload file size limit in MB",
|
|
Type: "integer",
|
|
Category: "storage",
|
|
})
|
|
ctx.Settings().Register(extpoints.SettingSchema{
|
|
Key: "upload.allowed_extensions",
|
|
Default: "png,jpg,jpeg,gif,webp,svg,pdf,zip,tar,gz",
|
|
Description: "Allowed upload file extensions separated by commas",
|
|
Type: "string",
|
|
Category: "storage",
|
|
})
|
|
|
|
return nil
|
|
}
|