Pagination not working for "api/upload/files" endpoint

System Information
  • Strapi Version: v4:
  • Operating System: Mac:
  • Database: MySQL:
  • Node Version: v14:
  • NPM Version: 8.3.1:

The upload plugin does not support pagination. Anyone know a work around for this?

Files endpoint


Pagination works fine for normal APIs I create. example:
custom folders api - working fine


Anyone know how to fix?

I got it to work by using

{
  start: 10,
  limit: 15,
}

Any answer to this problem please?

Just an extra for anyone who can edit their backend, you can make the files endpoint more like other api endpoints:

Inside: src/index.js

// make the register block look like this:
  register({ strapi }) {
    strapi.plugin("upload").controllers["content-api"].find = async function (ctx) {
      const { start, limit } = ctx.query
      const firstPageAdd = start > limit ? 0 : 1
      const files = await strapi.plugin("upload").services.upload.findMany(ctx.query);
      const total = await strapi.db.query('plugin::upload.file').count(ctx.query)
      const data = await sanitizeOutput(files, ctx)
      ctx.body = {
        data, pagination: { pageCount: files.length, pageSize: limit, total, page: Math.ceil(((start - firstPageAdd) / limit) + 1) }
      };
    }
  },

remember to import sanitizeOutput

Thanks, this works for fetching the items but there is no additional pagination meta sent in the response, like the total number of records.

Hi guys,

I’m facing the same issue and needed to use pagination[page]&pagination[pageSize].
Here is how I could make it work. The important part is using findPage() instead of findMany().
Hopefully this helps someone.
BTW, I found a related issue for listing users. It would be great to have those plugins following the same standard.

# extensions/upload/strapi-server.js

const { sanitize, validate } = require('@strapi/utils');
const FILE_MODEL_UID = "plugin::upload.file";

const sanitizeOutput = async (data, ctx) => {
  const schema = strapi.getModel(FILE_MODEL_UID);
  const { auth } = ctx.state;

  return sanitize.contentAPI.output(data, schema, { auth });
};

const validateQuery = async (data, ctx) => {
  const schema = strapi.getModel(FILE_MODEL_UID);
  const { auth } = ctx.state;

  return validate.contentAPI.query(data, schema, { auth });
};

const sanitizeQuery = async (data, ctx) => {
  const schema = strapi.getModel(FILE_MODEL_UID);
  const { auth } = ctx.state;

  return sanitize.contentAPI.query(data, schema, { auth });
};

module.exports = (plugin) => {
  plugin.controllers["content-api"].find = async (ctx) => {
    ctx.query = {...ctx.query, ...ctx.query?.pagination}

    await validateQuery(ctx.query, ctx);
    const sanitizedQuery = await sanitizeQuery(ctx.query, ctx);
    const data = await strapi.plugin("upload").services.upload.findPage(sanitizedQuery);
    const {results, pagination} = await sanitizeOutput(data, ctx);
    
    ctx.body = {
        data: results,
        meta: {
            pagination: pagination,
        },
    };
  }

  return plugin;
};