Skip to content

Unable to update Media content #12934

Closed
Closed

Description

@rjay-hb

Describe the Bug

Hi,

We cannot update the Media content (alt, resize image). We are using Azure Storage cloud for storing the media. We have implemented a select file for folder selection and uploaded the media to the selected folder. The first upload is working fine, but when we try to update the media, we get "Something went wrong" on the UI and "There was a problem while uploading the file. fetch failed" in the console.

Here are the hooks that we are using to manage the media folder:

hooks: {
    beforeOperation: [
      ({ req, context, operation }) => {
        if (['update'].includes(operation) && req?.file) {
          context.newFileName = true;
          context.fileName = req?.file?.name;
        }
      },
    ],
    beforeChange: [
      async ({ data, req, operation, collection, originalDoc, context }) => {
        const payload = await getPayload({ config });
        if (!['create', 'update'].includes(operation)) return data;
        const folderPath = data.folder
          ? await getFullFolderPath(data.folder, req, MediaFolders.slug)
          : 'Project';
        const fileName: string = originalDoc?.filename ?? data.filename;
        const relativePath =folderPath + '/' + fileName;

        //Extract the extension (e.g., "png")
        const extension = path.extname(fileName).replace('.', '').toLowerCase();
        data.extension = null;
        if (extension) {
          data.extension = extension; // Save fileType in the collection field
        }

        //check if media exist at same location
        const existingFile = await payload.find({
          collection: collection.slug,
          where: {
            filename: { equals: relativePath },
          },
          limit: 1,
        });

        //if there throw error
        if (existingFile.docs.length > 0 && operation === 'create') {
          // throw new Error('File with this name already exists')
          throw new APIError(`File with ${fileName} name already exists`, 400, [
            {
              message: `File with name ${fileName} already exists`,
            },
          ]);
        }

        if (operation === 'update') {
          const fileSize = originalDoc?.filesize ?? data?.filesize;
          data.url = fileName;
          data.thumbnailURL = fileName;
          if (context?.newFileName) {
            data.filename = `${folderPath}/${context.fileName}`;
            data.filesize = fileSize;
          } else {
            data.filename = fileName;
            data.filesize = fileSize;
          }
        } else {
          data.filename = relativePath;
          data.url = relativePath;
          data.thumbnailURL = relativePath;
        }
        return data;
      },
    ],
  },

And the strange thing is, this setup is working fine on local, but failing on production. Even when I'm trying to upload new media with the URL, it's failing.

Link to the code that reproduces this issue

pnpx create-payload-app@latest -t blank

Reproduction Steps

  1. Create a Payload Application.
  2. Link Azure Storage for media upload.
  3. Implemented a collection for the media folder and linked it to the media collection for selection.
  4. Upload a media, it will work fine.
  5. Try to edit the media by updating alt and publish; this will fail.

Which area(s) are affected? (Select all that apply)

area: core

Environment Info

Binaries:
  Node: 20.18.2
  npm: N/A
  Yarn: N/A
  pnpm: N/A
Relevant Packages:
  payload: 3.33.0
  next: 15.2.2
  @payloadcms/db-postgres: 3.33.0
  @payloadcms/email-nodemailer: 3.33.0
  @payloadcms/graphql: 3.33.0
  @payloadcms/next/utilities: 3.33.0
  @payloadcms/payload-cloud: 3.33.0
  @payloadcms/plugin-cloud-storage: 3.33.0
  @payloadcms/richtext-lexical: 3.33.0
  @payloadcms/storage-azure: 3.33.0
  @payloadcms/storage-s3: 3.33.0
  @payloadcms/translations: 3.33.0
  @payloadcms/ui/shared: 3.33.0
  react: 19.0.0
  react-dom: 19.0.0
Operating System:
  Platform: win32
  Arch: x64
  Version: Windows 11 Pro
  Available memory (MB): 16159
  Available CPU cores: 8

Activity

  1. added
    status: needs-triage
    validate-reproduction
    on Jun 25, 2025
  2. github-actions commented on Jun 25, 2025

    @github-actions
    Contributor

    Please add a reproduction in order for us to be able to investigate.

    Depending on the quality of reproduction steps, this issue may be closed if no reproduction is provided.

    Why was this issue marked with the invalid-reproduction label?

    To be able to investigate, we need access to a reproduction to identify what triggered the issue. We prefer a link to a public GitHub repository created with create-payload-app@latest -t blank or a forked/branched version of this repository with tests added (more info in the reproduction-guide).

    To make sure the issue is resolved as quickly as possible, please make sure that the reproduction is as minimal as possible. This means that you should remove unnecessary code, files, and dependencies that do not contribute to the issue. Ensure your reproduction does not depend on secrets, 3rd party registries, private dependencies, or any other data that cannot be made public. Avoid a reproduction including a whole monorepo (unless relevant to the issue). The easier it is to reproduce the issue, the quicker we can help.

    Please test your reproduction against the latest version of Payload to make sure your issue has not already been fixed.

    I added a link, why was it still marked?

    Ensure the link is pointing to a codebase that is accessible (e.g. not a private repository). "example.com", "n/a", "will add later", etc. are not acceptable links -- we need to see a public codebase. See the above section for accepted links.

    Useful Resources

  3. removed
    validate-reproduction
    on Jun 25, 2025
  4. lukebennett commented on Jun 25, 2025

    @lukebennett
    Contributor

    It's probably #12876

  5. rjay-hb commented on Jun 25, 2025

    @rjay-hb
    Author

    @lukebennett But I'm using old version v3.33.0

  6. lukebennett commented on Jun 25, 2025

    @lukebennett
    Contributor

    Good shout, missed that. There's been a couple of issues about the latest bug and I though this was another. Ignore me then, sorry - as you were!

  7. removed
    status: needs-triage
    on Jun 26, 2025
  8. github-actions commented on Jul 4, 2025

    @github-actions
    Contributor

    This issue has been automatically locked.
    Please open a new issue if this issue persists with any additional detail.

  9. locked as resolved and limited conversation to collaborators on Jul 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions