fix: ignore count query for gorm

This commit is contained in:
zhuyasen
2025-11-23 20:39:29 +08:00
parent f721317a67
commit 209e6b8363
3 changed files with 29 additions and 6 deletions

View File

@@ -21,6 +21,7 @@ type listDirOptions struct {
prefixPath string
enableDownload bool // default: false
enableFilter bool // default: true
middlewares []gin.HandlerFunc
}
func (o *listDirOptions) apply(opts ...ListDirOption) {
@@ -70,6 +71,13 @@ func WithListDirDirsFilter(filters ...string) ListDirOption {
}
}
// WithListDirMiddlewares sets middlewares.
func WithListDirMiddlewares(middlewares ...gin.HandlerFunc) ListDirOption {
return func(o *listDirOptions) {
o.middlewares = append(o.middlewares, middlewares...)
}
}
// -------------------------------------------------------------------------------------------
// FileInfo is a struct that represents a file or directory in the file system.
@@ -317,11 +325,20 @@ func ListDir(r *gin.Engine, opts ...ListDirOption) {
prefixPath = ""
}
r.GET(prefixPath+"/dir/list", handleList(prefixPath, o))
if o.enableDownload {
r.GET(prefixPath+"/dir/file/download", handleDownload)
if len(o.middlewares) > 0 {
group := r.Group("", o.middlewares...)
group.GET(prefixPath+"/dir/list", handleList(prefixPath, o))
if o.enableDownload {
group.GET(prefixPath+"/dir/file/download", handleDownload)
}
group.GET(prefixPath+"/dir/list/api", handleAPIList(o.enableFilter))
} else {
r.GET(prefixPath+"/dir/list", handleList(prefixPath, o))
if o.enableDownload {
r.GET(prefixPath+"/dir/file/download", handleDownload)
}
r.GET(prefixPath+"/dir/list/api", handleAPIList(o.enableFilter))
}
r.GET(prefixPath+"/dir/list/api", handleAPIList(o.enableFilter))
}
// nolint

View File

@@ -391,7 +391,13 @@ func TestListDirRouterSetup(t *testing.T) {
})
t.Run("WithDownloadEnabled", func(t *testing.T) {
r, tmpDir := setupTestServer(t, WithListDirDownload())
middleware := func(c *gin.Context) {
c.Header("X-Test-Header", "test-value")
c.Next()
}
r, tmpDir := setupTestServer(t,
WithListDirDownload(),
WithListDirMiddlewares(middleware))
filePath := filepath.Join(tmpDir, "file1.txt")
url := fmt.Sprintf("/dir/file/download?path=%s", filePath)
w := newRequest(t, r, "GET", url)

View File

@@ -84,7 +84,7 @@ func NewPage(page int, limit int, columnNames string) *Page {
// columnNames="-name,-age" means sort by name descending before sorting by age descending.
func getSort(columnNames string) string {
columnNames = strings.Replace(columnNames, " ", "", -1)
if columnNames == "" {
if columnNames == "" || columnNames == "ignorecount" {
return "id DESC"
}