Hi everybody,

when sharing a file between multiple Drupal websites, it would be nice if they all share the same UUID.

In jsonapi_file it is possible to set the file UUID along with the file data. The following proposed request shows an example of how to achieve this when creating a file entity from binary data via JSON API requests.

Request:

POST /jsonapi/entity_type/bundle/file_field?id={uuid}

Content-Type: application/octet-stream
Content-Disposition: file; filename=image.jpg; resource-id={uuid}
Accept: application/vnd.api+json

// binary data

Comments

hedeshy created an issue. See original summary.

wim leers’s picture

Title: Support client-generated UUIDs on file uploads » [PP-1] Support client-generated UUIDs on file uploads
Issue tags: -file upload +API-First Initiative
Related issues: +#2934386: Accept client-generated IDs (UUIDs)

Thanks for reporting this!

Did some preparatory investigating:

  1. Related: commit d3bb4d69ea8a3948bd8f5e1c720d64a8cbeb09f7 added explicit support for 409 responses when an entity already exists.
  2. More related: #2934386: Accept client-generated IDs (UUIDs) made this explicitly supported for resources in general. But files are a special case.
  3. This is blocked on #2958554: Allow creation of file entities from binary data via JSON API requests. If I apply #2958554 and check where a File entity is created, it's in the core infrastructure that #2940383: [META] Unify file upload logic of REST and JSON:API is adding. Which means #2940383 would need to change to accommodate this, to allow a UUID to be specified and not just a filename.
wim leers’s picture

Title: [PP-1] Support client-generated UUIDs on file uploads » [PP-1] Accept client-generated IDs (UUIDs) for file uploads too

Updating title to match that of #2934386: Accept client-generated IDs (UUIDs).

wim leers’s picture

wim leers’s picture

Title: [PP-1] Accept client-generated IDs (UUIDs) for file uploads too » Accept client-generated IDs (UUIDs) for file uploads too
Status: Postponed » Active
shenzhuxi’s picture

StatusFileSize
new5.48 KB

I would like to have this feature now, so I patched TemporaryJsonapiFileFieldUploader.php.

shenzhuxi’s picture

Project: JSON:API » Drupal core
Version: 8.x-2.x-dev » 8.7.x-dev
Component: Code » jsonapi.module
Status: Active » Needs review
Issue tags: -API-First Initiative +file entity, +UUID
StatusFileSize
new5.23 KB

Here is the patch for Drupal core 8.7.x.

Status: Needs review » Needs work

The last submitted patch, 7: client-generated-id-3021155-7.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

shenzhuxi’s picture

StatusFileSize
new10.99 KB

Updated FileUploadTest.php to use the disposition-type in content-disposition following RFC 6266 (Support "attachment" and "file").

wim leers’s picture

Issue tags: +API-First Initiative

@shenzhuxi came to talk to me at Drupal Dev Days Cluj. I explained that:

  • this is a new feature, so probably can't land in 8.7 — new features go into the next minor release
  • this is blocked on #2940383: [META] Unify file upload logic of REST and JSON:API (which I apparently already explained in #2.3)
  • if we're going to do this without waiting for #2940383, this will have to update both rest and jsonapi, to ensure that #2940383 doesn't become more complex

I wish I had better news for @shenzhuxi — I understand that it's frustrating that so very few lines of additional code would solve his needs :/ We've all felt this pain of course 😩

And finally, the test coverage fixes in #9 (changing Content-Disposition: filename=… to Content-Disposition: file; filename=…) is an independent bugfix that merits its own issue. But it also points to some missing validation. @shenzhuxi, please create a separate issue for that.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.9 was released on November 6 and is the final full bugfix release for the Drupal 8.7.x series. Drupal 8.7.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.8.0 on December 4, 2019. (Drupal 8.8.0-beta1 is available for testing.)

Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.