From 503cb55c554f94b25ffa0a4997ad551ed687ba35 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Tue, 16 May 2023 18:45:32 -0400 Subject: [PATCH] refactor: tus needs to move to its own package --- main.go | 7 ++++--- tus.go => tus/tus.go | 25 ++++++++++++++----------- 2 files changed, 18 insertions(+), 14 deletions(-) rename tus.go => tus/tus.go (87%) diff --git a/main.go b/main.go index 91ebffd..2c482cd 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,7 @@ import ( _ "git.lumeweb.com/LumeWeb/portal/docs" "git.lumeweb.com/LumeWeb/portal/renterd" "git.lumeweb.com/LumeWeb/portal/service/files" + "git.lumeweb.com/LumeWeb/portal/tus" "git.lumeweb.com/LumeWeb/portal/validator" "github.com/iris-contrib/swagger" "github.com/iris-contrib/swagger/swaggerFiles" @@ -76,10 +77,10 @@ func main() { app.Handle(new(controller.FilesController)) }) - tus := initTus() + tusHandler := tus.Init() - v1.Any(TUS_API_PATH+"/{fileparam:path}", iris.FromStd(http.StripPrefix(v1.GetRelPath()+TUS_API_PATH+"/", tus))) - v1.Post(TUS_API_PATH, iris.FromStd(http.StripPrefix(v1.GetRelPath()+TUS_API_PATH, tus))) + v1.Any(tus.TUS_API_PATH+"/{fileparam:path}", iris.FromStd(http.StripPrefix(v1.GetRelPath()+tus.TUS_API_PATH+"/", tusHandler))) + v1.Post(tus.TUS_API_PATH, iris.FromStd(http.StripPrefix(v1.GetRelPath()+tus.TUS_API_PATH, tusHandler))) swaggerConfig := swagger.Config{ // The url pointing to API definition. diff --git a/tus.go b/tus/tus.go similarity index 87% rename from tus.go rename to tus/tus.go index ad83442..76877a0 100644 --- a/tus.go +++ b/tus/tus.go @@ -1,4 +1,4 @@ -package main +package tus import ( "context" @@ -9,6 +9,7 @@ import ( "git.lumeweb.com/LumeWeb/portal/db" "git.lumeweb.com/LumeWeb/portal/model" "git.lumeweb.com/LumeWeb/portal/service/files" + "git.lumeweb.com/LumeWeb/portal/shared" "github.com/golang-queue/queue" "github.com/tus/tusd/pkg/filestore" tusd "github.com/tus/tusd/pkg/handler" @@ -21,15 +22,13 @@ const TUS_API_PATH = "/files/tus" const HASH_META_HEADER = "blake3-hash" -var tusQueue *queue.Queue -var store *filestore.FileStore -var composer *tusd.StoreComposer - -func initTus() *tusd.Handler { - store = &filestore.FileStore{ +func Init() *tusd.Handler { + store := &filestore.FileStore{ Path: "/tmp", } + shared.SetTusStore(store) + composer := tusd.NewStoreComposer() composer.UseCore(store) composer.UseConcater(store) @@ -73,7 +72,7 @@ func initTus() *tusd.Handler { }, PreFinishResponseCallback: func(hook tusd.HookEvent) error { tusEntry := &model.Tus{ - Path: hook.Upload.Storage["Path"], + Id: hook.Upload.ID, Hash: hook.Upload.MetaData[HASH_META_HEADER], } @@ -81,7 +80,7 @@ func initTus() *tusd.Handler { return err } - if err := tusQueue.QueueTask(func(ctx context.Context) error { + if err := shared.GetTusQueue().QueueTask(func(ctx context.Context) error { upload, err := store.GetUpload(nil, hook.Upload.ID) if err != nil { return err @@ -97,7 +96,8 @@ func initTus() *tusd.Handler { if err != nil { panic(err) } - tusQueue = queue.NewPool(5) + + shared.SetTusQueue(queue.NewPool(5)) go tusStartup() @@ -108,6 +108,9 @@ func tusStartup() { result := map[int]model.Tus{} db.Get().Table("tus").Take(&result) + tusQueue := shared.GetTusQueue() + store := shared.GetTusStore() + for _, item := range result { if err := tusQueue.QueueTask(func(ctx context.Context) error { upload, err := store.GetUpload(nil, item.UploadID) @@ -150,7 +153,7 @@ func tusWorker(upload *tusd.Upload) error { ret = db.Get().Delete(&tusUpload) - err = composer.Terminater.AsTerminatableUpload(*upload).Terminate(context.Background()) + err = shared.GetTusComposer().Terminater.AsTerminatableUpload(*upload).Terminate(context.Background()) if err != nil { log.Print(err)