feat: add a status endpoint and move cid validation to a utility method
This commit is contained in:
parent
1f195cf328
commit
38b76155af
|
@ -17,6 +17,10 @@ type UploadResponse struct {
|
|||
Cid string `json:"cid"`
|
||||
}
|
||||
|
||||
type StatusResponse struct {
|
||||
Status string `json:"status"`
|
||||
}
|
||||
|
||||
func (f *FilesController) PostUpload() {
|
||||
ctx := f.Ctx
|
||||
|
||||
|
@ -50,14 +54,12 @@ func (f *FilesController) PostUpload() {
|
|||
func (f *FilesController) GetDownloadBy(cidString string) {
|
||||
ctx := f.Ctx
|
||||
|
||||
_, err := cid.Valid(cidString)
|
||||
if sendError(ctx, err, iris.StatusBadRequest) {
|
||||
logger.Get().Debug("invalid cid", zap.Error(err))
|
||||
hashHex, valid := validateCid(cidString, true, ctx)
|
||||
|
||||
if !valid {
|
||||
return
|
||||
}
|
||||
|
||||
cidObject, _ := cid.Decode(cidString)
|
||||
hashHex := cidObject.StringHash()
|
||||
download, err := files.Download(hashHex)
|
||||
if internalError(ctx, err) {
|
||||
logger.Get().Debug("failed fetching file", zap.Error(err))
|
||||
|
@ -74,6 +76,39 @@ func (f *FilesController) GetDownloadBy(cidString string) {
|
|||
}
|
||||
}
|
||||
|
||||
func (f *FilesController) GetStatusBy(cidString string) {
|
||||
ctx := f.Ctx
|
||||
|
||||
hashHex, valid := validateCid(cidString, false, ctx)
|
||||
|
||||
if !valid {
|
||||
return
|
||||
}
|
||||
|
||||
status := files.Status(hashHex)
|
||||
|
||||
var statusCode string
|
||||
|
||||
switch status {
|
||||
case files.STATUS_UPLOADED:
|
||||
statusCode = "uploaded"
|
||||
break
|
||||
case files.STATUS_UPLOADING:
|
||||
statusCode = "uploading"
|
||||
break
|
||||
case files.STATUS_NOT_FOUND:
|
||||
statusCode = "uploading"
|
||||
break
|
||||
}
|
||||
|
||||
err := ctx.JSON(&StatusResponse{Status: statusCode})
|
||||
|
||||
if err != nil {
|
||||
logger.Get().Error("failed to create response", zap.Error(err))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func sendErrorCustom(ctx iris.Context, err error, customError error, irisError int) bool {
|
||||
if err != nil {
|
||||
if customError != nil {
|
||||
|
@ -94,3 +129,27 @@ func internalErrorCustom(ctx iris.Context, err error, customError error) bool {
|
|||
func sendError(ctx iris.Context, err error, irisError int) bool {
|
||||
return sendErrorCustom(ctx, err, nil, irisError)
|
||||
}
|
||||
|
||||
func validateCid(cidString string, validateStatus bool, ctx iris.Context) (string, bool) {
|
||||
_, err := cid.Valid(cidString)
|
||||
if sendError(ctx, err, iris.StatusBadRequest) {
|
||||
logger.Get().Debug("invalid cid", zap.Error(err))
|
||||
return "", false
|
||||
}
|
||||
|
||||
cidObject, _ := cid.Decode(cidString)
|
||||
hashHex := cidObject.StringHash()
|
||||
|
||||
if validateStatus {
|
||||
status := files.Status(hashHex)
|
||||
|
||||
if status == files.STATUS_NOT_FOUND {
|
||||
err := errors.New("cid not found")
|
||||
sendError(ctx, errors.New("cid not found"), iris.StatusNotFound)
|
||||
logger.Get().Debug("cid not found", zap.Error(err))
|
||||
return "", false
|
||||
}
|
||||
}
|
||||
|
||||
return hashHex, true
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue