Skip to content

Commit b4e6ab1

Browse files
authored
refactor: FilterReadMeScripts (#8154 close #8150)
* refactor: FilterReadMeScripts * .
1 parent 3499c4d commit b4e6ab1

File tree

7 files changed

+51
-98
lines changed

7 files changed

+51
-98
lines changed

drivers/quqi/driver.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ func (d *Quqi) Put(ctx context.Context, dstDir model.Obj, stream model.FileStrea
316316
// if the file already exists in Quqi server, there is no need to actually upload it
317317
if uploadInitResp.Data.Exist {
318318
// the file name returned by Quqi does not include the extension name
319-
nodeName, nodeExt := uploadInitResp.Data.NodeName, rawExt(stream.GetName())
319+
nodeName, nodeExt := uploadInitResp.Data.NodeName, utils.Ext(stream.GetName())
320320
if nodeExt != "" {
321321
nodeName = nodeName + "." + nodeExt
322322
}
@@ -432,7 +432,7 @@ func (d *Quqi) Put(ctx context.Context, dstDir model.Obj, stream model.FileStrea
432432
return nil, err
433433
}
434434
// the file name returned by Quqi does not include the extension name
435-
nodeName, nodeExt := uploadFinishResp.Data.NodeName, rawExt(stream.GetName())
435+
nodeName, nodeExt := uploadFinishResp.Data.NodeName, utils.Ext(stream.GetName())
436436
if nodeExt != "" {
437437
nodeName = nodeName + "." + nodeExt
438438
}

drivers/quqi/util.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"io"
1010
"net/http"
1111
"net/url"
12-
stdpath "path"
1312
"strings"
1413
"time"
1514

@@ -115,16 +114,6 @@ func (d *Quqi) checkLogin() bool {
115114
return true
116115
}
117116

118-
// rawExt 保留扩展名大小写
119-
func rawExt(name string) string {
120-
ext := stdpath.Ext(name)
121-
if strings.HasPrefix(ext, ".") {
122-
ext = ext[1:]
123-
}
124-
125-
return ext
126-
}
127-
128117
// decryptKey 获取密码
129118
func decryptKey(encodeKey string) []byte {
130119
// 移除非法字符

drivers/vtencent/util.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ import (
88
"fmt"
99
"io"
1010
"net/http"
11-
"path"
1211
"strconv"
13-
"strings"
1412

1513
"github.com/alist-org/alist/v3/drivers/base"
1614
"github.com/alist-org/alist/v3/internal/driver"
@@ -151,7 +149,7 @@ func (d *Vtencent) ApplyUploadUGC(signature string, stream model.FileStreamer) (
151149
form := base.Json{
152150
"signature": signature,
153151
"videoName": stream.GetName(),
154-
"videoType": strings.ReplaceAll(path.Ext(stream.GetName()), ".", ""),
152+
"videoType": utils.Ext(stream.GetName()),
155153
"videoSize": stream.GetSize(),
156154
}
157155
var resps RspApplyUploadUGC

internal/net/request.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -619,10 +619,9 @@ type Buf struct {
619619
// NewBuf is a buffer that can have 1 read & 1 write at the same time.
620620
// when read is faster write, immediately feed data to read after written
621621
func NewBuf(ctx context.Context, maxSize int) *Buf {
622-
d := make([]byte, 0, maxSize)
623622
return &Buf{
624623
ctx: ctx,
625-
buffer: bytes.NewBuffer(d),
624+
buffer: bytes.NewBuffer(make([]byte, 0, maxSize)),
626625
size: maxSize,
627626
}
628627
}
@@ -677,5 +676,5 @@ func (br *Buf) Write(p []byte) (n int, err error) {
677676
}
678677

679678
func (br *Buf) Close() {
680-
br.buffer.Reset()
679+
br.buffer = nil
681680
}

pkg/utils/path.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func IsSubPath(path string, subPath string) bool {
4545

4646
func Ext(path string) string {
4747
ext := stdpath.Ext(path)
48-
if strings.HasPrefix(ext, ".") {
48+
if len(ext) > 0 && ext[0] == '.' {
4949
ext = ext[1:]
5050
}
5151
return strings.ToLower(ext)

server/common/proxy.go

Lines changed: 3 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,25 @@
11
package common
22

33
import (
4-
"bytes"
54
"context"
65
"fmt"
76
"io"
87
"net/http"
98
"net/url"
109
"os"
11-
"strconv"
1210
"strings"
1311

14-
"github.com/alist-org/alist/v3/internal/conf"
12+
"maps"
13+
1514
"github.com/alist-org/alist/v3/internal/model"
1615
"github.com/alist-org/alist/v3/internal/net"
17-
"github.com/alist-org/alist/v3/internal/setting"
1816
"github.com/alist-org/alist/v3/internal/stream"
1917
"github.com/alist-org/alist/v3/pkg/http_range"
2018
"github.com/alist-org/alist/v3/pkg/utils"
21-
"github.com/microcosm-cc/bluemonday"
2219
log "github.com/sirupsen/logrus"
23-
"github.com/yuin/goldmark"
2420
)
2521

26-
func processMarkdown(content []byte) ([]byte, error) {
27-
var buf bytes.Buffer
28-
if err := goldmark.New().Convert(content, &buf); err != nil {
29-
return nil, fmt.Errorf("markdown conversion failed: %w", err)
30-
}
31-
return bluemonday.UGCPolicy().SanitizeBytes(buf.Bytes()), nil
32-
}
33-
3422
func Proxy(w http.ResponseWriter, r *http.Request, link *model.Link, file model.Obj) error {
35-
36-
//优先处理md文件
37-
if utils.Ext(file.GetName()) == "md" && setting.GetBool(conf.FilterReadMeScripts) {
38-
var markdownContent []byte
39-
var err error
40-
41-
if link.MFile != nil {
42-
defer link.MFile.Close()
43-
attachHeader(w, file)
44-
markdownContent, err = io.ReadAll(link.MFile)
45-
if err != nil {
46-
return fmt.Errorf("failed to read markdown content: %w", err)
47-
}
48-
} else if link.RangeReadCloser != nil {
49-
attachHeader(w, file)
50-
rrc, err := link.RangeReadCloser.RangeRead(r.Context(), http_range.Range{Start: 0, Length: -1})
51-
if err != nil {
52-
return err
53-
}
54-
defer rrc.Close()
55-
markdownContent, err = io.ReadAll(rrc)
56-
if err != nil {
57-
return fmt.Errorf("failed to read markdown content: %w", err)
58-
}
59-
} else {
60-
header := net.ProcessHeader(r.Header, link.Header)
61-
res, err := net.RequestHttp(r.Context(), r.Method, header, link.URL)
62-
if err != nil {
63-
return err
64-
}
65-
defer res.Body.Close()
66-
for h, v := range res.Header {
67-
w.Header()[h] = v
68-
}
69-
w.WriteHeader(res.StatusCode)
70-
if r.Method == http.MethodHead {
71-
return nil
72-
}
73-
markdownContent, err = io.ReadAll(res.Body)
74-
if err != nil {
75-
return fmt.Errorf("failed to read markdown content: %w", err)
76-
}
77-
78-
}
79-
80-
safeHTML, err := processMarkdown(markdownContent)
81-
if err != nil {
82-
return err
83-
}
84-
85-
safeHTMLReader := bytes.NewReader(safeHTML)
86-
w.Header().Set("Content-Length", strconv.FormatInt(int64(len(safeHTML)), 10))
87-
w.Header().Set("Content-Type", "text/html; charset=utf-8")
88-
_, err = utils.CopyWithBuffer(w, safeHTMLReader)
89-
if err != nil {
90-
return err
91-
}
92-
return nil
93-
}
94-
9523
if link.MFile != nil {
9624
defer link.MFile.Close()
9725
attachHeader(w, file)
@@ -152,9 +80,7 @@ func Proxy(w http.ResponseWriter, r *http.Request, link *model.Link, file model.
15280
}
15381
defer res.Body.Close()
15482

155-
for h, v := range res.Header {
156-
w.Header()[h] = v
157-
}
83+
maps.Copy(w.Header(), res.Header)
15884
w.WriteHeader(res.StatusCode)
15985
if r.Method == http.MethodHead {
16086
return nil

server/handles/down.go

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package handles
22

33
import (
4+
"bytes"
45
"fmt"
56
"io"
7+
"net/http"
68
stdpath "path"
9+
"strconv"
710
"strings"
811

912
"github.com/alist-org/alist/v3/internal/conf"
@@ -15,7 +18,9 @@ import (
1518
"github.com/alist-org/alist/v3/pkg/utils"
1619
"github.com/alist-org/alist/v3/server/common"
1720
"github.com/gin-gonic/gin"
21+
"github.com/microcosm-cc/bluemonday"
1822
log "github.com/sirupsen/logrus"
23+
"github.com/yuin/goldmark"
1924
)
2025

2126
func Down(c *gin.Context) {
@@ -124,7 +129,34 @@ func localProxy(c *gin.Context, link *model.Link, file model.Obj, proxyRange boo
124129
if proxyRange {
125130
common.ProxyRange(link, file.GetSize())
126131
}
127-
err = common.Proxy(c.Writer, c.Request, link, file)
132+
133+
//优先处理md文件
134+
if utils.Ext(file.GetName()) == "md" && setting.GetBool(conf.FilterReadMeScripts) {
135+
w := c.Writer
136+
buf := bytes.NewBuffer(make([]byte, 0, file.GetSize()))
137+
err = common.Proxy(&proxyResponseWriter{ResponseWriter: w, Writer: buf}, c.Request, link, file)
138+
if err == nil && buf.Len() > 0 {
139+
if w.Status() < 200 || w.Status() > 300 {
140+
w.Write(buf.Bytes())
141+
return
142+
}
143+
144+
var html bytes.Buffer
145+
if err = goldmark.Convert(buf.Bytes(), &html); err != nil {
146+
err = fmt.Errorf("markdown conversion failed: %w", err)
147+
} else {
148+
buf.Reset()
149+
err = bluemonday.UGCPolicy().SanitizeReaderToWriter(&html, buf)
150+
if err == nil {
151+
w.Header().Set("Content-Length", strconv.FormatInt(int64(buf.Len()), 10))
152+
w.Header().Set("Content-Type", "text/html; charset=utf-8")
153+
_, err = utils.CopyWithBuffer(c.Writer, buf)
154+
}
155+
}
156+
}
157+
} else {
158+
err = common.Proxy(c.Writer, c.Request, link, file)
159+
}
128160
if err != nil {
129161
common.ErrorResp(c, err, 500, true)
130162
return
@@ -150,3 +182,12 @@ func canProxy(storage driver.Driver, filename string) bool {
150182
}
151183
return false
152184
}
185+
186+
type proxyResponseWriter struct {
187+
http.ResponseWriter
188+
io.Writer
189+
}
190+
191+
func (pw *proxyResponseWriter) Write(p []byte) (int, error) {
192+
return pw.Writer.Write(p)
193+
}

0 commit comments

Comments
 (0)