From 1f195cf328ee176be9283ab0cc40e65bb6c40948 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Tue, 6 Jun 2023 16:33:14 -0400 Subject: [PATCH] feat: add a Status method for uploads --- service/files/files.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/service/files/files.go b/service/files/files.go index f73303c..059b151 100644 --- a/service/files/files.go +++ b/service/files/files.go @@ -20,6 +20,12 @@ import ( "strings" ) +const ( + STATUS_UPLOADED = iota + STATUS_UPLOADING = iota + STATUS_NOT_FOUND = iota +) + var client *resty.Client func Init() { @@ -176,6 +182,32 @@ func Download(hash string) (io.Reader, error) { } } +func Status(hash string) int { + var count int64 + + uploadItem := db.Get().Table("uploads").Where(&model.Upload{Hash: hash}).Count(&count) + + if uploadItem.Error != nil { + logger.Get().Error("Failed querying upload from db", zap.Error(uploadItem.Error)) + } + + if count > 0 { + return STATUS_UPLOADED + } + + tusItem := db.Get().Table("tus").Where(&model.Tus{Hash: hash}).Count(&count) + + if tusItem.Error != nil { + logger.Get().Error("Failed querying upload from db", zap.Error(tusItem.Error)) + } + + if count > 0 { + return STATUS_UPLOADING + } + + return STATUS_NOT_FOUND +} + func objectUrlBuilder(hash string, bus bool, proof bool) string { path := []string{} if bus {