chore: 初始化仓库,导入项目基线代码
Academy v0.8.3 完整代码基线:Go 后端(internal/cmd/cli/mcp-server/docreader)、 Vue 前端(frontend)、数据库迁移(migrations)、部署编排(docker/helm/deploy)与文档。 今日开发的培训推送管理改动随后单独提交。
This commit is contained in:
commit
7ec21f35b0
5767 files changed
+1436788
No files matched your search
@@ -0,0 +1,70 @@
|
||||
// Package filetransport streams already-authorized files. It performs no
|
||||
// resource lookup or permission inference; callers own those boundaries.
|
||||
package filetransport
|
||||
|
||||
import (
|
||||
"io"
|
||||
"mime"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
secutils "academy/internal/utils"
|
||||
)
|
||||
|
||||
// Options defines response metadata for an already-authorized reader.
|
||||
type Options struct {
|
||||
Filename string
|
||||
Download bool
|
||||
ContentType string
|
||||
Disposition string
|
||||
CacheControl string
|
||||
Size int64
|
||||
}
|
||||
|
||||
// Serve closes reader on every path. Seekable backends support standard Range
|
||||
// and HEAD via ServeContent; streaming-only backends return the full response
|
||||
// without buffering the object just to implement seeking (RFC 9110 section 14.2).
|
||||
func Serve(w http.ResponseWriter, r *http.Request, reader io.ReadCloser, options Options) error {
|
||||
defer func() { _ = reader.Close() }()
|
||||
contentType, inline := secutils.SafeContentTypeByFilename(options.Filename)
|
||||
if options.Download {
|
||||
inline = false
|
||||
}
|
||||
if options.ContentType != "" {
|
||||
contentType = options.ContentType
|
||||
}
|
||||
w.Header().Set("Content-Type", contentType)
|
||||
w.Header().Set("X-Content-Type-Options", "nosniff")
|
||||
disposition := "attachment"
|
||||
if inline {
|
||||
disposition = "inline"
|
||||
}
|
||||
if options.Disposition != "" {
|
||||
w.Header().Set("Content-Disposition", options.Disposition)
|
||||
} else if options.Filename != "" {
|
||||
w.Header().Set("Content-Disposition",
|
||||
mime.FormatMediaType(disposition,
|
||||
map[string]string{"filename": filepath.Base(options.Filename)}))
|
||||
} else {
|
||||
w.Header().Set("Content-Disposition", disposition)
|
||||
}
|
||||
if options.CacheControl != "" {
|
||||
w.Header().Set("Cache-Control", options.CacheControl)
|
||||
}
|
||||
if seeker, ok := reader.(io.ReadSeeker); ok {
|
||||
http.ServeContent(w, r, options.Filename, time.Time{}, seeker)
|
||||
return nil
|
||||
}
|
||||
w.Header().Set("Accept-Ranges", "none")
|
||||
if options.Size > 0 {
|
||||
w.Header().Set("Content-Length", strconv.FormatInt(options.Size, 10))
|
||||
}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
if r.Method == http.MethodHead {
|
||||
return nil
|
||||
}
|
||||
_, err := io.Copy(w, reader)
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package filetransport
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
type seekFile struct {
|
||||
*bytes.Reader
|
||||
closed bool
|
||||
}
|
||||
|
||||
func (f *seekFile) Close() error { f.closed = true; return nil }
|
||||
|
||||
func TestServeSeekableFile(t *testing.T) {
|
||||
for _, tt := range []struct {
|
||||
name, method, rangeHeader, body string
|
||||
status int
|
||||
}{
|
||||
{"whole", "GET", "", "0123456789", 200},
|
||||
{"range", "GET", "bytes=2-5", "2345", 206},
|
||||
{"suffix", "GET", "bytes=-3", "789", 206},
|
||||
{"head", "HEAD", "", "", 200},
|
||||
{"unsatisfiable", "GET", "bytes=20-30", "", 416},
|
||||
} {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
reader := &seekFile{Reader: bytes.NewReader([]byte("0123456789"))}
|
||||
req := httptest.NewRequest(tt.method, "/file", nil)
|
||||
req.Header.Set("Range", tt.rangeHeader)
|
||||
w := httptest.NewRecorder()
|
||||
require.NoError(t, Serve(w, req, reader, Options{Filename: "image.png", CacheControl: "private, no-store"}))
|
||||
require.Equal(t, tt.status, w.Code)
|
||||
if tt.status != 416 {
|
||||
require.Equal(t, tt.body, w.Body.String())
|
||||
}
|
||||
require.True(t, reader.closed)
|
||||
require.Equal(t, "nosniff", w.Header().Get("X-Content-Type-Options"))
|
||||
if tt.status == 206 {
|
||||
require.Contains(t, w.Header().Get("Content-Range"), "/10")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
type streamFile struct{ read, closed bool }
|
||||
|
||||
func (f *streamFile) Read([]byte) (int, error) { f.read = true; return 0, io.EOF }
|
||||
func (f *streamFile) Close() error { f.closed = true; return nil }
|
||||
|
||||
func TestServeStreamingHEADClosesWithoutReading(t *testing.T) {
|
||||
reader := &streamFile{}
|
||||
w := httptest.NewRecorder()
|
||||
require.NoError(
|
||||
t,
|
||||
Serve(w, httptest.NewRequest("HEAD", "/file", nil), reader, Options{Filename: "a.pdf", Size: 123}),
|
||||
)
|
||||
require.False(t, reader.read)
|
||||
require.True(t, reader.closed)
|
||||
require.Equal(t, "123", w.Header().Get("Content-Length"))
|
||||
}
|
||||
|
||||
func TestServeActiveContentAndStreamingRange(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodGet, "/file", nil)
|
||||
req.Header.Set("Range", "bytes=0-1")
|
||||
require.NoError(t, Serve(w, req, io.NopCloser(strings.NewReader("<svg/>")), Options{Filename: "../payload.svg"}))
|
||||
require.Equal(t, http.StatusOK, w.Code, "non-seekable readers return the complete body")
|
||||
require.Equal(t, "<svg/>", w.Body.String())
|
||||
require.Equal(t, "none", w.Header().Get("Accept-Ranges"))
|
||||
require.Equal(t, "attachment; filename=payload.svg", w.Header().Get("Content-Disposition"))
|
||||
}
|
||||
Reference in new issue
Block a user