github.com/ngocphuongnb/tetua@v0.0.7-alpha/app/entities/file.go (about) 1 package entities 2 3 import ( 4 "context" 5 "errors" 6 "net/url" 7 "strconv" 8 "time" 9 10 "github.com/ngocphuongnb/tetua/app/fs" 11 "github.com/ngocphuongnb/tetua/app/utils" 12 ) 13 14 type File struct { 15 ID int `json:"id,omitempty"` 16 Disk string `json:"disk,omitempty"` 17 Path string `json:"path,omitempty"` 18 Type string `json:"type,omitempty"` 19 Size int `json:"size,omitempty"` 20 UserID int `json:"user_id,omitempty"` 21 User *User `json:"user,omitempty"` 22 Posts []*Post `json:"post,omitempty"` 23 CreatedAt *time.Time `json:"created_at,omitempty"` 24 UpdatedAt *time.Time `json:"updated_at,omitempty"` 25 DeletedAt *time.Time `json:"deleted_at,omitempty"` 26 } 27 28 func (f *File) Url() string { 29 if f.Disk == "" || f.Path == "" { 30 return "" 31 } 32 33 fileDisk := fs.Disk(f.Disk) 34 if fileDisk == nil { 35 return "" 36 } 37 38 return fileDisk.Url(f.Path) 39 } 40 41 func (f *File) Delete(ctx context.Context) error { 42 if f.Disk == "" || f.Path == "" { 43 return errors.New("disk or path is empty") 44 } 45 46 fileDisk := fs.Disk(f.Disk) 47 if fileDisk == nil { 48 return errors.New("disk not found") 49 } 50 51 return fileDisk.Delete(ctx, f.Path) 52 } 53 54 type FileFilter struct { 55 *Filter 56 UserIDs []int `form:"user_ids" json:"user_ids"` 57 } 58 59 func (p *FileFilter) Base() string { 60 q := url.Values{} 61 if !utils.SliceContains(p.IgnoreUrlParams, "search") && p.Search != "" { 62 q.Add("q", p.Search) 63 } 64 if !utils.SliceContains(p.IgnoreUrlParams, "user") && len(p.UserIDs) > 0 { 65 q.Add("user", strconv.Itoa(p.UserIDs[0])) 66 } 67 68 if queryString := q.Encode(); queryString != "" { 69 return p.FilterBaseUrl() + "?" + q.Encode() 70 } 71 72 return p.FilterBaseUrl() 73 }