2023-05-16 22:45:32 +00:00
|
|
|
package tus
|
2023-05-15 16:36:00 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"git.lumeweb.com/LumeWeb/portal/cid"
|
|
|
|
"git.lumeweb.com/LumeWeb/portal/db"
|
|
|
|
"git.lumeweb.com/LumeWeb/portal/model"
|
|
|
|
"git.lumeweb.com/LumeWeb/portal/service/files"
|
2023-05-16 22:45:32 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/portal/shared"
|
2023-05-15 16:36:00 +00:00
|
|
|
"github.com/golang-queue/queue"
|
|
|
|
"github.com/tus/tusd/pkg/filestore"
|
|
|
|
tusd "github.com/tus/tusd/pkg/handler"
|
|
|
|
"github.com/tus/tusd/pkg/memorylocker"
|
2023-05-17 13:52:25 +00:00
|
|
|
"io"
|
2023-05-15 16:36:00 +00:00
|
|
|
"log"
|
|
|
|
)
|
|
|
|
|
|
|
|
const TUS_API_PATH = "/files/tus"
|
|
|
|
|
|
|
|
const HASH_META_HEADER = "blake3-hash"
|
|
|
|
|
2023-05-16 22:45:32 +00:00
|
|
|
func Init() *tusd.Handler {
|
|
|
|
store := &filestore.FileStore{
|
2023-05-15 16:36:00 +00:00
|
|
|
Path: "/tmp",
|
|
|
|
}
|
|
|
|
|
2023-05-16 22:45:32 +00:00
|
|
|
shared.SetTusStore(store)
|
|
|
|
|
2023-05-15 16:36:00 +00:00
|
|
|
composer := tusd.NewStoreComposer()
|
|
|
|
composer.UseCore(store)
|
|
|
|
composer.UseConcater(store)
|
|
|
|
composer.UseLocker(memorylocker.New())
|
|
|
|
composer.UseTerminater(store)
|
|
|
|
|
|
|
|
handler, err := tusd.NewHandler(tusd.Config{
|
|
|
|
BasePath: "/api/v1" + TUS_API_PATH,
|
|
|
|
StoreComposer: composer,
|
|
|
|
PreUploadCreateCallback: func(hook tusd.HookEvent) error {
|
|
|
|
hash := hook.Upload.MetaData[HASH_META_HEADER]
|
|
|
|
|
|
|
|
if len(hash) == 0 {
|
|
|
|
return errors.New("missing blake3-hash metadata")
|
|
|
|
}
|
|
|
|
|
|
|
|
var upload model.Upload
|
|
|
|
result := db.Get().Where(&model.Upload{Hash: hash}).First(&upload)
|
|
|
|
if (result.Error != nil && result.Error.Error() != "record not found") || result.RowsAffected > 0 {
|
|
|
|
hashBytes, err := hex.DecodeString(hash)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
cidString, err := cid.Encode(hashBytes, uint64(hook.Upload.Size))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := json.Marshal(UploadResponse{Cid: cidString})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return tusd.NewHTTPError(errors.New(string(resp)), 304)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
PreFinishResponseCallback: func(hook tusd.HookEvent) error {
|
|
|
|
tusEntry := &model.Tus{
|
2023-05-16 22:45:32 +00:00
|
|
|
Id: hook.Upload.ID,
|
2023-05-15 16:36:00 +00:00
|
|
|
Hash: hook.Upload.MetaData[HASH_META_HEADER],
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := db.Get().Create(tusEntry).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-16 22:45:32 +00:00
|
|
|
if err := shared.GetTusQueue().QueueTask(func(ctx context.Context) error {
|
2023-05-15 16:36:00 +00:00
|
|
|
upload, err := store.GetUpload(nil, hook.Upload.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return tusWorker(&upload)
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2023-05-16 22:45:32 +00:00
|
|
|
|
|
|
|
shared.SetTusQueue(queue.NewPool(5))
|
2023-05-15 16:36:00 +00:00
|
|
|
|
|
|
|
go tusStartup()
|
|
|
|
|
|
|
|
return handler
|
|
|
|
}
|
|
|
|
|
|
|
|
func tusStartup() {
|
|
|
|
result := map[int]model.Tus{}
|
|
|
|
db.Get().Table("tus").Take(&result)
|
|
|
|
|
2023-05-16 22:45:32 +00:00
|
|
|
tusQueue := shared.GetTusQueue()
|
|
|
|
store := shared.GetTusStore()
|
|
|
|
|
2023-05-15 16:36:00 +00:00
|
|
|
for _, item := range result {
|
|
|
|
if err := tusQueue.QueueTask(func(ctx context.Context) error {
|
|
|
|
upload, err := store.GetUpload(nil, item.UploadID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return tusWorker(&upload)
|
|
|
|
}); err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func tusWorker(upload *tusd.Upload) error {
|
|
|
|
info, err := (*upload).GetInfo(nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
file, err := (*upload).GetReader(nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-17 13:52:25 +00:00
|
|
|
_, err = files.Upload(file.(io.ReadSeeker), info.Size)
|
2023-05-15 16:36:00 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
hash := info.MetaData[HASH_META_HEADER]
|
|
|
|
|
|
|
|
var tusUpload model.Tus
|
|
|
|
ret := db.Get().Where(&model.Tus{Hash: hash}).First(&tusUpload)
|
|
|
|
if ret.Error != nil && ret.Error.Error() != "record not found" {
|
|
|
|
log.Print(ret.Error)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = db.Get().Delete(&tusUpload)
|
|
|
|
|
2023-05-16 22:45:32 +00:00
|
|
|
err = shared.GetTusComposer().Terminater.AsTerminatableUpload(*upload).Terminate(context.Background())
|
2023-05-15 16:36:00 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type UploadResponse struct {
|
|
|
|
Cid string `json:"cid"`
|
|
|
|
}
|