Skip to content

Commit 5c0ebb8

Browse files
author
JojiiOfficial
committed
update kingpin
add search in an other namespaces
1 parent 30bd611 commit 5c0ebb8

4 files changed

Lines changed: 22 additions & 4 deletions

File tree

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@ github.com/JojiiOfficial/gaw v1.2.8/go.mod h1:fPm2wG1z8xSCmfkqq9V5iHdlgLUpkRx73t
1111
github.com/JojiiOfficial/shred v1.2.1 h1:658CFVTqcAkYVg815vW+guYnyJTLOIoS15tMyPTYhNo=
1212
github.com/JojiiOfficial/shred v1.2.1/go.mod h1:/OAxd6eYOhrXb3KW+2wmDog2BiFlUld8oJEKa+xblxU=
1313
github.com/PuerkitoBio/goquery v1.5.1/go.mod h1:GsLWisAFVj4WgDibEWF4pvYnkVQBpKBKeU+7zCJoLcc=
14+
github.com/alecthomas/kingpin v0.0.0-20200323085623-b6657d9477a6 h1:0fwkEPHxb5V+KZZLxWmOknl4oHWo60+TnhmKOi4BIkU=
15+
github.com/alecthomas/kingpin v0.0.0-20200323085623-b6657d9477a6/go.mod h1:b6br6/pDFSfMkBgC96TbpOji05q5pa+v5rIlS0Y6XtI=
16+
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
1417
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 h1:JYp7IbQjafoB+tBA3gMyHYHrpOtNuDiK/uB5uXxq5wM=
1518
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
19+
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
1620
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d h1:UQZhZ2O0vMHr2cI+DC1Mbh0TJxzA3RcLoMsFw+aXw7E=
1721
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho=
1822
github.com/andybalholm/cascadia v1.1.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y=

handlers/FileHandler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ func FileHandler(handlerData web.HandlerData, w http.ResponseWriter, r *http.Req
375375
}
376376

377377
// Find files
378-
files, err := models.FindFiles(handlerData.Db, models.File{
378+
files, err := models.FindFiles(handlerData.Db, handlerData.Config, models.File{
379379
Model: gorm.Model{
380380
ID: request.FileID,
381381
},

models/Config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ type configServer struct {
4141
Roles roleConfig
4242
AllowRegistration bool `default:"false"`
4343
DeleteUnusedSessionsAfter time.Duration `default:"10m"`
44+
SearchInOtherNamespaces bool
4445
}
4546

4647
type roleConfig struct {
@@ -125,6 +126,7 @@ func InitConfig(confFile string, createMode bool) (*Config, bool) {
125126
},
126127
AllowRegistration: false,
127128
DeleteUnusedSessionsAfter: 10 * time.Minute,
129+
SearchInOtherNamespaces: true,
128130
Roles: roleConfig{
129131
DefaultRole: 1,
130132
Roles: []Role{

models/File.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ func (file File) IsInGroupList(groups []string) bool {
123123
}
124124

125125
// FindFiles finds file
126-
func FindFiles(db *gorm.DB, file File) ([]File, error) {
126+
func FindFiles(db *gorm.DB, config *Config, file File, ignoreNamespace ...bool) ([]File, error) {
127127
var files []File
128128
a := db.Model(&File{})
129129

@@ -139,7 +139,11 @@ func FindFiles(db *gorm.DB, file File) ([]File, error) {
139139

140140
// Filter by namespace ID and uploader
141141
if file.Namespace != nil {
142-
a = a.Where("namespace_id = ? AND uploader = ?", file.Namespace.ID, file.Namespace.UserID)
142+
a = a.Where("uploader = ?", file.Namespace.UserID)
143+
144+
if len(ignoreNamespace) == 0 {
145+
a = a.Where("namespace_id = ?", file.Namespace.ID)
146+
}
143147
}
144148

145149
// Get file to delete
@@ -153,14 +157,22 @@ func FindFiles(db *gorm.DB, file File) ([]File, error) {
153157
return nil, err
154158
}
155159

160+
// Try to find file without filtering for namespace
161+
if config.Server.SearchInOtherNamespaces &&
162+
len(files) == 0 &&
163+
len(ignoreNamespace) == 0 {
164+
165+
return FindFiles(db, config, file, true)
166+
}
167+
156168
return files, nil
157169
}
158170

159171
// FindFile finds a file
160172
func FindFile(db *gorm.DB, fileID, userID uint) (*File, error) {
161173
a := db.Model(&File{}).Where("uploader = ?", userID)
162174

163-
// Include ID if set. Otherwise use namespace
175+
// Include ID if set
164176
if fileID != 0 {
165177
a = a.Where("id = ?", fileID)
166178
}

0 commit comments

Comments
 (0)