diff --git a/pkg/schemas/file.go b/pkg/schemas/file.go index c7098b9..3345623 100644 --- a/pkg/schemas/file.go +++ b/pkg/schemas/file.go @@ -81,8 +81,8 @@ type FileOperation struct { } type DirMove struct { - Source string `json:"source"` - Destination string `json:"destination"` + Source string `json:"source" binding:"required"` + Destination string `json:"destination" binding:"required"` } type MkDir struct { diff --git a/pkg/schemas/upload.go b/pkg/schemas/upload.go index 24379c9..305526d 100644 --- a/pkg/schemas/upload.go +++ b/pkg/schemas/upload.go @@ -7,7 +7,6 @@ type UploadQuery struct { } type UploadPartOut struct { - ID string `json:"id"` Name string `json:"name"` PartId int `json:"partId"` PartNo int `json:"partNo"` diff --git a/pkg/services/auth.go b/pkg/services/auth.go index 6a98e7a..0e0e618 100644 --- a/pkg/services/auth.go +++ b/pkg/services/auth.go @@ -159,13 +159,11 @@ func (as *AuthService) LogIn(c *gin.Context) (*schemas.Message, *types.AppError) if err := as.Db.Model(&models.User{}).Where("user_id = ?", session.UserID). Find(&result).Error; err != nil { - return nil, &types.AppError{Error: errors.New("failed to find user"), - Code: http.StatusInternalServerError} + return nil, &types.AppError{Error: err, Code: http.StatusInternalServerError} } if len(result) == 0 { if err := as.Db.Create(&user).Error; err != nil { - return nil, &types.AppError{Error: errors.New("failed to create or update user"), - Code: http.StatusInternalServerError} + return nil, &types.AppError{Error: err, Code: http.StatusInternalServerError} } //Create root folder on first login @@ -180,8 +178,7 @@ func (as *AuthService) LogIn(c *gin.Context) (*schemas.Message, *types.AppError) ParentID: "root", } if err := as.Db.Create(file).Error; err != nil { - return nil, &types.AppError{Error: errors.New("failed to create root folder"), - Code: http.StatusInternalServerError} + return nil, &types.AppError{Error: err, Code: http.StatusInternalServerError} } } @@ -190,8 +187,7 @@ func (as *AuthService) LogIn(c *gin.Context) (*schemas.Message, *types.AppError) //create session if err := as.Db.Create(&models.Session{UserId: session.UserID, Hash: hexToken, Session: session.Sesssion}).Error; err != nil { - return nil, &types.AppError{Error: errors.New("failed to create user session"), - Code: http.StatusInternalServerError} + return nil, &types.AppError{Error: err, Code: http.StatusInternalServerError} } return &schemas.Message{Message: "login success"}, nil diff --git a/pkg/services/file.go b/pkg/services/file.go index 98db585..ce0b8f0 100644 --- a/pkg/services/file.go +++ b/pkg/services/file.go @@ -54,7 +54,7 @@ func (fs *FileService) CreateFile(c *gin.Context) (*schemas.FileOut, *types.AppE if fileIn.Path != "" { var parent models.File if err := fs.Db.Where("type = ? AND path = ?", "folder", fileIn.Path).First(&parent).Error; err != nil { - return nil, &types.AppError{Error: errors.New("parent directory not found"), Code: http.StatusNotFound} + return nil, &types.AppError{Error: err, Code: http.StatusNotFound} } fileDB.ParentID = parent.ID } @@ -125,11 +125,11 @@ func (fs *FileService) UpdateFile(c *gin.Context) (*schemas.FileOut, *types.AppE if fileUpdate.Type == "folder" && fileUpdate.Name != "" { if err := fs.Db.Raw("select * from teldrive.update_folder(?, ?)", fileID, fileUpdate.Name).Scan(&files).Error; err != nil { - return nil, &types.AppError{Error: errors.New("failed to update the folder"), Code: http.StatusInternalServerError} + return nil, &types.AppError{Error: err, Code: http.StatusInternalServerError} } } else { if err := fs.Db.Model(&files).Clauses(clause.Returning{}).Where("id = ?", fileID).Updates(fileUpdate).Error; err != nil { - return nil, &types.AppError{Error: errors.New("failed to update the file"), Code: http.StatusInternalServerError} + return nil, &types.AppError{Error: err, Code: http.StatusInternalServerError} } } @@ -286,8 +286,7 @@ func (fs *FileService) MakeDirectory(c *gin.Context) (*schemas.FileOut, *types.A userId, _ := getUserAuth(c) if err := fs.Db.Raw("select * from teldrive.create_directories(?, ?)", userId, payload.Path). Scan(&files).Error; err != nil { - return nil, &types.AppError{Error: errors.New("failed to create directory"), - Code: http.StatusInternalServerError} + return nil, &types.AppError{Error: err, Code: http.StatusInternalServerError} } file := mapper.ToFileOut(files[0]) @@ -370,7 +369,7 @@ func (fs *FileService) CopyFile(c *gin.Context) (*schemas.FileOut, *types.AppErr var destRes []models.File if err := fs.Db.Raw("select * from teldrive.create_directories(?, ?)", userId, payload.Destination).Scan(&destRes).Error; err != nil { - return nil, &types.AppError{Error: errors.New("failed to create destination"), Code: http.StatusInternalServerError} + return nil, &types.AppError{Error: err, Code: http.StatusInternalServerError} } dest := destRes[0] @@ -389,7 +388,7 @@ func (fs *FileService) CopyFile(c *gin.Context) (*schemas.FileOut, *types.AppErr dbFile.ChannelID = &file.ChannelID if err := fs.Db.Create(&dbFile).Error; err != nil { - return nil, &types.AppError{Error: errors.New("failed to copy file"), Code: http.StatusInternalServerError} + return nil, &types.AppError{Error: err, Code: http.StatusInternalServerError} } @@ -410,12 +409,12 @@ func (fs *FileService) MoveFiles(c *gin.Context) (*schemas.Message, *types.AppEr var destination models.File if err := fs.Db.Model(&models.File{}).Select("id").Where("path = ?", payload.Destination).First(&destination).Error; errors.Is(err, gorm.ErrRecordNotFound) { - return nil, &types.AppError{Error: errors.New("destination not found"), Code: http.StatusNotFound} + return nil, &types.AppError{Error: err, Code: http.StatusNotFound} } if err := fs.Db.Model(&models.File{}).Where("id IN ?", payload.Files).UpdateColumn("parent_id", destination.ID).Error; err != nil { - return nil, &types.AppError{Error: errors.New("move failed"), Code: http.StatusInternalServerError} + return nil, &types.AppError{Error: err, Code: http.StatusInternalServerError} } return &schemas.Message{Message: "files moved"}, nil @@ -426,11 +425,11 @@ func (fs *FileService) DeleteFiles(c *gin.Context) (*schemas.Message, *types.App var payload schemas.FileOperation if err := c.ShouldBindJSON(&payload); err != nil { - return nil, &types.AppError{Error: errors.New("invalid request payload"), Code: http.StatusBadRequest} + return nil, &types.AppError{Error: err, Code: http.StatusBadRequest} } if err := fs.Db.Exec("call teldrive.delete_files($1)", payload.Files).Error; err != nil { - return nil, &types.AppError{Error: errors.New("failed to delete files"), Code: http.StatusInternalServerError} + return nil, &types.AppError{Error: err, Code: http.StatusInternalServerError} } return &schemas.Message{Message: "files deleted"}, nil @@ -441,13 +440,13 @@ func (fs *FileService) MoveDirectory(c *gin.Context) (*schemas.Message, *types.A var payload schemas.DirMove if err := c.ShouldBindJSON(&payload); err != nil { - return nil, &types.AppError{Error: errors.New("invalid request payload"), Code: http.StatusBadRequest} + return nil, &types.AppError{Error: err, Code: http.StatusBadRequest} } userId, _ := getUserAuth(c) if err := fs.Db.Exec("select * from teldrive.move_directory(? , ? , ?)", payload.Source, payload.Destination, userId).Error; err != nil { - return nil, &types.AppError{Error: errors.New("failed to move directory"), Code: http.StatusInternalServerError} + return nil, &types.AppError{Error: err, Code: http.StatusInternalServerError} } return &schemas.Message{Message: "directory moved"}, nil diff --git a/pkg/services/upload.go b/pkg/services/upload.go index 3bd9589..d1dfe8d 100644 --- a/pkg/services/upload.go +++ b/pkg/services/upload.go @@ -39,7 +39,7 @@ func (us *UploadService) GetUploadFileById(c *gin.Context) (*schemas.UploadOut, if err := us.Db.Model(&models.Upload{}).Order("part_no").Where("upload_id = ?", uploadId). Where("created_at >= ?", time.Now().UTC().AddDate(0, 0, -config.UploadRetention)). Find(&parts).Error; err != nil { - return nil, &types.AppError{Error: errors.New("failed to fetch from db"), Code: http.StatusInternalServerError} + return nil, &types.AppError{Error: err, Code: http.StatusInternalServerError} } return &schemas.UploadOut{Parts: parts}, nil @@ -48,7 +48,7 @@ func (us *UploadService) GetUploadFileById(c *gin.Context) (*schemas.UploadOut, func (us *UploadService) DeleteUploadFile(c *gin.Context) (*schemas.Message, *types.AppError) { uploadId := c.Param("id") if err := us.Db.Where("upload_id = ?", uploadId).Delete(&models.Upload{}).Error; err != nil { - return nil, &types.AppError{Error: errors.New("failed to delete upload"), Code: http.StatusInternalServerError} + return nil, &types.AppError{Error: err, Code: http.StatusInternalServerError} } return &schemas.Message{Message: "upload deleted"}, nil @@ -61,7 +61,7 @@ func (us *UploadService) CreateUploadPart(c *gin.Context) (*schemas.UploadPartOu var payload schemas.UploadPart if err := c.ShouldBindJSON(&payload); err != nil { - return nil, &types.AppError{Error: errors.New("invalid request payload"), Code: http.StatusBadRequest} + return nil, &types.AppError{Error: err, Code: http.StatusBadRequest} } partUpload := &models.Upload{ @@ -101,10 +101,6 @@ func (us *UploadService) UploadFile(c *gin.Context) (*schemas.UploadPartOut, *ty return nil, &types.AppError{Error: err, Code: http.StatusBadRequest} } - if uploadQuery.Filename == "" { - return nil, &types.AppError{Error: errors.New("filename missing"), Code: http.StatusBadRequest} - } - userId, session := getUserAuth(c) uploadId := c.Param("id") @@ -127,7 +123,7 @@ func (us *UploadService) UploadFile(c *gin.Context) (*schemas.UploadPartOut, *ty tokens, err := getBotsToken(c, userId, channelId) if err != nil { - return nil, &types.AppError{Error: errors.New("failed to fetch bots"), Code: http.StatusInternalServerError} + return nil, &types.AppError{Error: err, Code: http.StatusInternalServerError} } if len(tokens) == 0 { @@ -199,7 +195,7 @@ func (us *UploadService) UploadFile(c *gin.Context) (*schemas.UploadPartOut, *ty } if err := us.Db.Create(partUpload).Error; err != nil { - return errors.New("failed to upload part") + return err } out = mapper.ToUploadOut(partUpload) diff --git a/pkg/services/user.go b/pkg/services/user.go index 2522f17..7ad4f9e 100644 --- a/pkg/services/user.go +++ b/pkg/services/user.go @@ -78,7 +78,7 @@ func (us *UserService) GetStats(c *gin.Context) (*schemas.AccountStats, *types.A userId, _ := getUserAuth(c) var res []schemas.AccountStats if err := us.Db.Raw("select * from teldrive.account_stats(?);", userId).Scan(&res).Error; err != nil { - return nil, &types.AppError{Error: errors.New("failed to get stats"), Code: http.StatusInternalServerError} + return nil, &types.AppError{Error: err, Code: http.StatusInternalServerError} } return &res[0], nil } @@ -112,7 +112,7 @@ func (us *UserService) UpdateChannel(c *gin.Context) (*schemas.Message, *types.A var payload schemas.Channel if err := c.ShouldBindJSON(&payload); err != nil { - return nil, &types.AppError{Error: errors.New("invalid request payload"), Code: http.StatusBadRequest} + return nil, &types.AppError{Error: err, Code: http.StatusBadRequest} } channel := &models.Channel{ChannelID: payload.ChannelID, ChannelName: payload.ChannelName, UserID: userId, @@ -167,7 +167,7 @@ func (us *UserService) AddBots(c *gin.Context) (*schemas.Message, *types.AppErro var botsTokens []string if err := c.ShouldBindJSON(&botsTokens); err != nil { - return nil, &types.AppError{Error: errors.New("invalid request payload"), Code: http.StatusBadRequest} + return nil, &types.AppError{Error: err, Code: http.StatusBadRequest} } if len(botsTokens) == 0 { @@ -195,7 +195,7 @@ func (us *UserService) RemoveBots(c *gin.Context) (*schemas.Message, *types.AppE if err := us.Db.Where("user_id = ?", userID).Where("channel_id = ?", channelId). Delete(&models.Bot{}).Error; err != nil { - return nil, &types.AppError{Error: errors.New("failed to delete bots"), Code: http.StatusInternalServerError} + return nil, &types.AppError{Error: err, Code: http.StatusInternalServerError} } cache.GetCache().Delete(fmt.Sprintf("users:bots:%d:%d", userID, channelId)) @@ -301,7 +301,7 @@ func (us *UserService) addBots(c context.Context, client *telegram.Client, userI cache.GetCache().Delete(fmt.Sprintf("users:bots:%d:%d", userId, channelId)) if err := us.Db.Clauses(clause.OnConflict{DoNothing: true}).Create(&payload).Error; err != nil { - return nil, &types.AppError{Error: errors.New("failed to add bots"), Code: http.StatusInternalServerError} + return nil, &types.AppError{Error: err, Code: http.StatusInternalServerError} } return &schemas.Message{Message: "bots added"}, nil