Clone entity with media

Hi

I want to clone an entity with media (entity.images) in such a way, that the strapi based backend re-uploads the images. My current controller methods creates a clone but the links to the same files. This is problematic the images could go out of sync between original and cloned entity.

  async copy(ctx) {
    const user = ctx.state.user;
    const { id } = ctx.params;
    try {
      let data = await strapi.entityService.findOne('api::project.project', id, {
        populate: '*',
      });

      if (data === undefined)
        throw new Error('Entry not found');

      data['state'] = 'draft';
      data = _.omit(data, ['releasedAt']);

      const entity = await strapi.entityService.clone('api::project.project', id, { data });

      return await strapi.entityService.update('api::project.project', entity.id, {
        data: {
          owner: user.id
        }
      });
    } catch (err) {
      console.log(err);
      return ctx.notFound('Entry not found');
    }
  },
System Information
  • Strapi Version: 4.14.4

1 Like