|
1 | 1 | package common |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bytes" |
4 | 5 | "context" |
5 | 6 | "fmt" |
6 | 7 | "io" |
7 | 8 | "net/http" |
8 | 9 | "net/url" |
| 10 | + "strconv" |
9 | 11 |
|
10 | 12 | "github.com/alist-org/alist/v3/internal/model" |
11 | 13 | "github.com/alist-org/alist/v3/internal/net" |
12 | 14 | "github.com/alist-org/alist/v3/internal/stream" |
13 | 15 | "github.com/alist-org/alist/v3/pkg/http_range" |
14 | 16 | "github.com/alist-org/alist/v3/pkg/utils" |
| 17 | + "github.com/microcosm-cc/bluemonday" |
15 | 18 | log "github.com/sirupsen/logrus" |
| 19 | + "github.com/yuin/goldmark" |
16 | 20 | ) |
17 | 21 |
|
| 22 | +func processMarkdown(content []byte) ([]byte, error) { |
| 23 | + var buf bytes.Buffer |
| 24 | + if err := goldmark.New().Convert(content, &buf); err != nil { |
| 25 | + return nil, fmt.Errorf("markdown conversion failed: %w", err) |
| 26 | + } |
| 27 | + return bluemonday.UGCPolicy().SanitizeBytes(buf.Bytes()), nil |
| 28 | +} |
| 29 | + |
18 | 30 | func Proxy(w http.ResponseWriter, r *http.Request, link *model.Link, file model.Obj) error { |
| 31 | + |
| 32 | + //优先处理md文件 |
| 33 | + if utils.Ext(file.GetName()) == "md" { |
| 34 | + var markdownContent []byte |
| 35 | + var err error |
| 36 | + |
| 37 | + if link.MFile != nil { |
| 38 | + defer link.MFile.Close() |
| 39 | + attachFileName(w, file) |
| 40 | + markdownContent, err = io.ReadAll(link.MFile) |
| 41 | + if err != nil { |
| 42 | + return fmt.Errorf("failed to read markdown content: %w", err) |
| 43 | + } |
| 44 | + |
| 45 | + } else { |
| 46 | + header := net.ProcessHeader(r.Header, link.Header) |
| 47 | + res, err := net.RequestHttp(r.Context(), r.Method, header, link.URL) |
| 48 | + if err != nil { |
| 49 | + return err |
| 50 | + } |
| 51 | + defer res.Body.Close() |
| 52 | + for h, v := range res.Header { |
| 53 | + w.Header()[h] = v |
| 54 | + } |
| 55 | + w.WriteHeader(res.StatusCode) |
| 56 | + if r.Method == http.MethodHead { |
| 57 | + return nil |
| 58 | + } |
| 59 | + markdownContent, err = io.ReadAll(res.Body) |
| 60 | + if err != nil { |
| 61 | + return fmt.Errorf("failed to read markdown content: %w", err) |
| 62 | + } |
| 63 | + |
| 64 | + } |
| 65 | + |
| 66 | + safeHTML, err := processMarkdown(markdownContent) |
| 67 | + if err != nil { |
| 68 | + return err |
| 69 | + } |
| 70 | + |
| 71 | + safeHTMLReader := bytes.NewReader(safeHTML) |
| 72 | + w.Header().Set("Content-Length", strconv.FormatInt(int64(len(safeHTML)), 10)) |
| 73 | + w.Header().Set("Content-Type", "text/html; charset=utf-8") |
| 74 | + _, err = utils.CopyWithBuffer(w, safeHTMLReader) |
| 75 | + if err != nil { |
| 76 | + return err |
| 77 | + } |
| 78 | + return nil |
| 79 | + } |
| 80 | + |
19 | 81 | if link.MFile != nil { |
20 | 82 | defer link.MFile.Close() |
21 | 83 | attachFileName(w, file) |
@@ -72,6 +134,7 @@ func Proxy(w http.ResponseWriter, r *http.Request, link *model.Link, file model. |
72 | 134 | return nil |
73 | 135 | } |
74 | 136 | } |
| 137 | + |
75 | 138 | func attachFileName(w http.ResponseWriter, file model.Obj) { |
76 | 139 | fileName := file.GetName() |
77 | 140 | w.Header().Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"; filename*=UTF-8''%s`, fileName, url.PathEscape(fileName))) |
|
0 commit comments