@@ -12,26 +12,33 @@ import (
1212 "path/filepath"
1313 "slices"
1414 "strconv"
15+ "strings"
1516 "time"
1617
1718 "gopkg.d7z.net/gitea-pages/pkg/core"
1819)
1920
2021type LocalProvider struct {
21- graph map [string ][]string
22- path string
22+ graph map [string ][]string
23+ path string
24+ overlay map [string ][]byte
2325}
2426
2527func NewLocalProvider (
2628 graph map [string ][]string ,
2729 path string ,
2830) * LocalProvider {
2931 return & LocalProvider {
30- graph : graph ,
31- path : path ,
32+ graph : graph ,
33+ path : path ,
34+ overlay : map [string ][]byte {},
3235 }
3336}
3437
38+ func (l * LocalProvider ) AddOverlay (path string , overlay []byte ) {
39+ l .overlay [strings .Trim (path , "/" )] = overlay
40+ }
41+
3542func (l * LocalProvider ) Close () error {
3643 return nil
3744}
@@ -65,20 +72,28 @@ func (l *LocalProvider) Branches(_ context.Context, owner, repo string) (map[str
6572}
6673
6774func (l * LocalProvider ) Open (_ context.Context , _ , _ , _ , path string , _ http.Header ) (* http.Response , error ) {
68- open , err := os .Open (filepath .Join (l .path , path ))
69- if err != nil {
70- return nil , errors .Join (err , os .ErrNotExist )
71- }
72- defer open .Close ()
73- all , err := io .ReadAll (open )
74- if err != nil {
75- return nil , errors .Join (err , os .ErrNotExist )
76- }
75+ var all []byte
7776 recorder := httptest .NewRecorder ()
77+ if data , ok := l .overlay [strings .Trim (path , "/" )]; ok {
78+ all = data
79+ recorder .Header ().Add ("Content-Length" , strconv .FormatInt (int64 (len (data )), 10 ))
80+ } else {
81+ open , err := os .Open (filepath .Join (l .path , path ))
82+ if err != nil {
83+ return nil , errors .Join (err , os .ErrNotExist )
84+ }
85+ defer open .Close ()
86+ all , err = io .ReadAll (open )
87+ if err != nil {
88+ return nil , errors .Join (err , os .ErrNotExist )
89+ }
90+ stat , _ := open .Stat ()
91+ recorder .Header ().Add ("Content-Length" , strconv .FormatInt (stat .Size (), 10 ))
92+ recorder .Header ().Add ("Last-Modified" , stat .ModTime ().Format (http .TimeFormat ))
93+ }
94+
7895 recorder .Body = bytes .NewBuffer (all )
7996 recorder .Header ().Add ("Content-Type" , mime .TypeByExtension (filepath .Ext (path )))
80- stat , _ := open .Stat ()
81- recorder .Header ().Add ("Content-Length" , strconv .FormatInt (stat .Size (), 10 ))
82- recorder .Header ().Add ("Last-Modified" , stat .ModTime ().Format (http .TimeFormat ))
97+
8398 return recorder .Result (), nil
8499}
0 commit comments