support tusd extention check

This commit is contained in:
sjqzhang 2022-04-10 16:21:28 +08:00
parent f2da2d39a8
commit 66fd10c3fe
2 changed files with 42 additions and 11 deletions

View File

@ -59,6 +59,7 @@ func (c *Server) initTus() {
bigDir = fmt.Sprintf("/%s%s", Config().Group, CONST_BIG_UPLOAD_PATH_SUFFIX)
}
composer := tusd.NewStoreComposer()
composer.UsesTerminater = true
// support raw tus upload and download
store.GetReaderExt = func(id string) (io.Reader, error) {
var (
@ -231,6 +232,18 @@ func (c *Server) initTus() {
}
}
}
h.AddMiddleWare(func() http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodPost {
meta := tusd.ParseMetadataHeader(r.Header.Get("Upload-Metadata"))
if name, ok := meta["filename"]; ok {
if len(Config().Extensions) > 0 && !c.util.Contains(path.Ext(strings.ToLower(name)), Config().Extensions) {
r.Header.Set("Suspend", "True")
}
}
}
})
}())
go notify(h)
if err != nil {
log.Error(err)

View File

@ -112,7 +112,8 @@ type UnroutedHandler struct {
// true in the Config structure.
CreatedUploads chan FileInfo
// Metrics provides numbers of the usage for this handler.
Metrics Metrics
Metrics Metrics
Handlers []http.HandlerFunc
}
// NewUnroutedHandler creates a new handler without routing using the given
@ -148,6 +149,7 @@ func NewUnroutedHandler(config Config) (*UnroutedHandler, error) {
logger: config.Logger,
extensions: extensions,
Metrics: newMetrics(),
Handlers: make([]http.HandlerFunc, 10),
}
return handler, nil
@ -158,8 +160,29 @@ func NewUnroutedHandler(config Config) (*UnroutedHandler, error) {
// cannot make PATCH AND DELETE requests. If you are using the tusd handlers
// directly you will need to wrap at least the POST and PATCH endpoints in
// this middleware.
func (handler *UnroutedHandler) AddMiddleWare(h http.HandlerFunc) {
handler.Handlers = append(handler.Handlers, h)
}
func (handler *UnroutedHandler) Middleware(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if len(handler.Handlers) > 0 {
for i := 0; i < len(handler.Handlers); i++ {
if handler.Handlers[i] != nil {
handler.Handlers[i].ServeHTTP(w, r)
if r.Header.Get("Suspend") != "" {
//handler.log("Suspend", "被用户中止")
w.WriteHeader(http.StatusForbidden)
return
}
}
}
}
// Allow overriding the HTTP method. The reason for this is
// that some libraries/environments to not support PATCH and
// DELETE requests, e.g. Flash in a browser and parts of Java
@ -590,13 +613,12 @@ func (handler *UnroutedHandler) finishUploadIfComplete(info FileInfo) error {
func (handler *UnroutedHandler) GetFile(w http.ResponseWriter, r *http.Request) {
var (
id string
err error
src io.Reader
id string
err error
src io.Reader
info FileInfo
)
if !handler.composer.UsesGetReader {
handler.sendError(w, r, ErrNotImplemented)
return
@ -610,7 +632,6 @@ func (handler *UnroutedHandler) GetFile(w http.ResponseWriter, r *http.Request)
return
}
//add by sjqzhang
src, err = handler.composer.GetReader.GetReader(id)
@ -622,14 +643,11 @@ func (handler *UnroutedHandler) GetFile(w http.ResponseWriter, r *http.Request)
handler.sendResp(w, r, http.StatusOK)
io.Copy(w, src)
if closer, ok := src.(io.Closer); ok {
closer.Close()
}
return
// end
// end
if handler.composer.UsesLocker {
locker := handler.composer.Locker
@ -660,7 +678,7 @@ func (handler *UnroutedHandler) GetFile(w http.ResponseWriter, r *http.Request)
return
}
src, err= handler.composer.GetReader.GetReader(id)
src, err = handler.composer.GetReader.GetReader(id)
if err != nil {
handler.sendError(w, r, err)
return