From 5a2d2acb1baab9d803f4e1d7d95cafd92e3ed1d1 Mon Sep 17 00:00:00 2001 From: movsb Date: Fri, 21 Jun 2024 15:27:52 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- service/comment_test.go | 11 ++++++++++- service/filesystem.go | 3 --- service/main.go | 2 +- service/post.go | 6 +----- service/service_test.go | 4 ++++ theme/modules/fs/fs.go | 13 +++++++++++++ 6 files changed, 29 insertions(+), 10 deletions(-) diff --git a/service/comment_test.go b/service/comment_test.go index f960beae..7122d058 100644 --- a/service/comment_test.go +++ b/service/comment_test.go @@ -8,7 +8,6 @@ import ( ) func TestPreviewComment(t *testing.T) { - t.SkipNow() initService() rsp, err := blog.PreviewComment(guest, &proto.PreviewCommentRequest{ Markdown: ``, @@ -17,4 +16,14 @@ func TestPreviewComment(t *testing.T) { if err == nil || !strings.Contains(err.Error(), "不能包含") { t.Fatal(rsp, err) } + rsp2, err := blog.CreateComment(guest, &proto.Comment{ + PostId: 1, + Author: `昵称`, + Email: `fake@twofei.com`, + SourceType: `markdown`, + Source: `(🏃逃……`, + }) + if err == nil || !strings.Contains(err.Error(), `不能包含`) { + t.Fatal(rsp2, err) + } } diff --git a/service/filesystem.go b/service/filesystem.go index cc32e2f7..bf1cfb72 100644 --- a/service/filesystem.go +++ b/service/filesystem.go @@ -44,9 +44,6 @@ func (s *Service) FileSystem(srv proto.Management_FileSystemServer) error { } else if initReq != nil { initialized = true if init := initReq.GetPost(); init != nil { - if s.postDataFS == nil { - return errors.New(`对此编号的文件系统不可用。`) - } pfs, err = s.postDataFS.ForPost(int(init.Id)) } if err != nil { diff --git a/service/main.go b/service/main.go index a63bbe85..cc9b8852 100644 --- a/service/main.go +++ b/service/main.go @@ -139,7 +139,7 @@ func newService(ctx context.Context, cancel context.CancelFunc, cfg *config.Conf testing: testing, cfg: cfg, - postDataFS: nil, + postDataFS: &theme_fs.Empty{}, db: db, tdb: taorm.NewDB(db), diff --git a/service/post.go b/service/post.go index 10ff396e..ffbe30a5 100644 --- a/service/post.go +++ b/service/post.go @@ -6,7 +6,6 @@ import ( "html" "log" "net/url" - "os" "path" "slices" "strings" @@ -166,15 +165,12 @@ func (s *Service) setPostLink(p *proto.Post, k proto.LinkKind) { } func (s *Service) OpenAsset(id int64) gold_utils.WebFileSystem { - if s.testing { - panic(`测试服务器不用于本地文件系统。`) - } u := utils.Must1(url.Parse(s.cfg.Site.Home)) if u.Path == "" { u.Path = "/" } return gold_utils.NewWebFileSystem( - os.DirFS(s.cfg.Data.File.Path), + utils.Must1(s.postDataFS.ForPost(int(id))), u.JoinPath(fmt.Sprintf("/%d/", id)), ) } diff --git a/service/service_test.go b/service/service_test.go index d9889bcc..bd8d6393 100644 --- a/service/service_test.go +++ b/service/service_test.go @@ -3,6 +3,7 @@ package service_test import ( "context" "log" + "os" "github.com/movsb/taoblog/cmd/config" "github.com/movsb/taoblog/cmd/server" @@ -18,6 +19,9 @@ var admin context.Context var guest context.Context func initService() { + // 测试环境应该不依赖本地系统。 + os.Setenv(`DEV`, `0`) + cfg := config.DefaultConfig() cfg.Auth.Basic.Username = `test` cfg.Auth.Basic.Password = `test` diff --git a/theme/modules/fs/fs.go b/theme/modules/fs/fs.go index c588fe4c..29af2c9e 100644 --- a/theme/modules/fs/fs.go +++ b/theme/modules/fs/fs.go @@ -1,6 +1,7 @@ package theme_fs import ( + "embed" "io/fs" ) @@ -12,3 +13,15 @@ type FS interface { // 针对单篇文章/评论的文件系统。 ForPost(id int) (fs.FS, error) } + +type Empty struct{} + +var empty embed.FS + +func (Empty) Root() (fs.FS, error) { + return empty, nil +} + +func (Empty) ForPost(id int) (fs.FS, error) { + return empty, nil +}