feat: re-use adapter logic for upload updates #13069
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Context:
I'm running Payload in a private subnet with a cookie-based auth gateway (which federates to an SSO provider).
Problem:
In a deployed setting, updating the focus point or crop area for an image and re-saving it results in a generic 500.
I tracked it down to this code section:
payload/packages/payload/src/uploads/generateFileData.ts
Line 99 in 2650eb7
The codepath re-uses the http machinery to fetch the image from wherever it may be stored. The issue here is that upload URLs are based on server hostname, which is correct for users/browsers, but with my setup Payload cannot send requests to itself that way.
Architecturally, I'm guessing that the fetch call is to accommodate the side loading feature where you can paste the direct asset URL. However, I'm wondering if it's unnecessary to invoke the whole http flow to re-fetch an asset that was already uploaded.
Changes:
With the above in mind, I refactored
getFile.ts
to split up the logic such that it's reusable internally without making a full request. This enables payload to use the configured upload adapters directly, using the same code path as the regular REST endpoint.Then, if we're doing an update operation, we can go directly to the cloud storage plugin instead of making a round trip to the API endpoint.