- Added new endpoints for listing trash, recent, and starred files. - Implemented chunked file uploads to support large file handling. - Introduced copy and rename functionalities for file management. - Enhanced error handling with specific drive-related error responses. - Updated validation for copy and rename requests. - Improved service methods to handle new functionalities and ensure quota checks. - Updated project checklist to reflect completion of file management features.
115 lines
3.2 KiB
Go
115 lines
3.2 KiB
Go
package drive
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"github.com/ultisuite/ulti-backend/internal/api/apivalidate"
|
|
)
|
|
|
|
const maxJSONRequestBody = 32 << 10
|
|
|
|
type moveRequest struct {
|
|
Source string `json:"source"`
|
|
Destination string `json:"destination"`
|
|
}
|
|
|
|
type copyRequest struct {
|
|
Source string `json:"source"`
|
|
Destination string `json:"destination"`
|
|
}
|
|
|
|
type renameRequest struct {
|
|
Path string `json:"path"`
|
|
NewName string `json:"new_name"`
|
|
}
|
|
|
|
func validateMoveRequest(req *moveRequest) *apivalidate.ValidationError {
|
|
var details []apivalidate.FieldDetail
|
|
if strings.TrimSpace(req.Source) == "" {
|
|
details = append(details, apivalidate.FieldDetail{Field: "source", Message: "required"})
|
|
}
|
|
if strings.TrimSpace(req.Destination) == "" {
|
|
details = append(details, apivalidate.FieldDetail{Field: "destination", Message: "required"})
|
|
}
|
|
if len(details) == 0 {
|
|
return nil
|
|
}
|
|
return apivalidate.NewValidationError(details...)
|
|
}
|
|
|
|
func validateCopyRequest(req *copyRequest) *apivalidate.ValidationError {
|
|
var details []apivalidate.FieldDetail
|
|
if strings.TrimSpace(req.Source) == "" {
|
|
details = append(details, apivalidate.FieldDetail{Field: "source", Message: "required"})
|
|
}
|
|
if strings.TrimSpace(req.Destination) == "" {
|
|
details = append(details, apivalidate.FieldDetail{Field: "destination", Message: "required"})
|
|
}
|
|
if len(details) == 0 {
|
|
return nil
|
|
}
|
|
return apivalidate.NewValidationError(details...)
|
|
}
|
|
|
|
func validateRenameRequest(req *renameRequest) *apivalidate.ValidationError {
|
|
var details []apivalidate.FieldDetail
|
|
if strings.TrimSpace(req.Path) == "" {
|
|
details = append(details, apivalidate.FieldDetail{Field: "path", Message: "required"})
|
|
}
|
|
newName := strings.TrimSpace(req.NewName)
|
|
if newName == "" {
|
|
details = append(details, apivalidate.FieldDetail{Field: "new_name", Message: "required"})
|
|
} else if strings.Contains(newName, "/") {
|
|
details = append(details, apivalidate.FieldDetail{Field: "new_name", Message: "invalid"})
|
|
}
|
|
if len(details) == 0 {
|
|
return nil
|
|
}
|
|
return apivalidate.NewValidationError(details...)
|
|
}
|
|
|
|
type createShareRequest struct {
|
|
Path string `json:"path"`
|
|
ShareType int `json:"share_type"`
|
|
Permissions int `json:"permissions"`
|
|
Role string `json:"role"`
|
|
}
|
|
|
|
func sharePermissionsForRole(role string) (int, bool) {
|
|
switch strings.TrimSpace(strings.ToLower(role)) {
|
|
case "owner":
|
|
return 31, true
|
|
case "editor":
|
|
return 15, true
|
|
case "viewer":
|
|
return 1, true
|
|
default:
|
|
return 0, false
|
|
}
|
|
}
|
|
|
|
func validateCreateShareRequest(req *createShareRequest) *apivalidate.ValidationError {
|
|
var details []apivalidate.FieldDetail
|
|
if strings.TrimSpace(req.Path) == "" {
|
|
details = append(details, apivalidate.FieldDetail{Field: "path", Message: "required"})
|
|
}
|
|
if role := strings.TrimSpace(req.Role); role != "" {
|
|
if _, ok := sharePermissionsForRole(role); !ok {
|
|
details = append(details, apivalidate.FieldDetail{Field: "role", Message: "invalid"})
|
|
}
|
|
}
|
|
if len(details) == 0 {
|
|
return nil
|
|
}
|
|
return apivalidate.NewValidationError(details...)
|
|
}
|
|
|
|
func validatePath(path string) *apivalidate.ValidationError {
|
|
if strings.TrimSpace(path) == "" {
|
|
return apivalidate.NewValidationError(apivalidate.FieldDetail{
|
|
Field: "path", Message: "required",
|
|
})
|
|
}
|
|
return nil
|
|
}
|