Skip to content

Media library "All Files" count shows the page size (24), not the library total #888

Description

@PlusA2M

Version: 2.19.0 (also present on main)

Describe the bug

The Media sidebar always shows All Files (24) regardless of how many media
files exist. The per-folder and per-type counts next to it are correct, and
pagination is also affected (the "Next" control appears/disappears based on the
page length rather than the real total).

Root cause

In packages/core/src/routes/admin-media.ts, the GET / handler runs a
paginated query (… ORDER BY uploaded_at DESC LIMIT 24 OFFSET …) into
results, then reports the total as that page's length:

totalFiles: results.length,                 // ≤ 24 (the page size)
hasNextPage: results.length === limit,

The handler already issues separate aggregate queries for the folder and type
sidebars, so the total should likewise be a COUNT(*) over the same WHERE
filters (folder/type), without LIMIT/OFFSET.

To reproduce

  1. Have more than 24 media files.
  2. Open /admin/media.
  3. The sidebar reads All Files (24).

Expected

The sidebar shows the true library total, and hasNextPage is computed against
that total. Applying a folder/type filter should reflect the filtered total.

Proposed fix

Add a COUNT(*) over the same conditions/params the list query already
builds, and derive totalFiles and hasNextPage from it:

const countQuery = `SELECT COUNT(*) as total FROM media WHERE ${conditions.join(' AND ')}`
const countRow = await db.prepare(countQuery).bind(...params).first<{ total: number }>()
const totalFiles = countRow?.total ?? 0
// …
totalFiles,
hasNextPage: offset + results.length < totalFiles,

I have a working fix with a regression test and can open a PR.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions