CodamAIDocs
Topicdone

Replacing and renaming

What an update with and without a new file does: replace the content, only rename, or both.

Variants
with new file → content replacedwithout file → content staysrename onlyrename and replacePUT and PATCHconcurrently → 409

What this is about

You change a file model with PUT or PATCH, like any model. Whether the content is swapped as well depends on one question: does the request carry a file whose name matches the file object?

The four cases

Update of a file model
name changed?matching file present?Result
nonoonly own fields changed; content, size, type stay
yesnorenamed; content, size and mimeType stay
noyescontent replaced; fileSize, mimeType, fileVersion new
yesyesrenamed and replaced; the file part must carry the new name

Replacing the content

Upload a new edition
Request
PATCH /api/rest/fileasset/update/f1…/upload
data:  { "data": { "id": "f1…", "name": "Contract.pdf" },
         "response": ["id", "name", "fileSize", "fileVersion"] }
files: Contract.pdf   (new edition)
Response
{ "data": { "id": "f1…", "name": "Contract.pdf",
            "fileSize": 51877, "fileVersion": 1790000123456 },
  "meta": { "error": false } }

The fileId stays the same, and the new content lives in the same place. What happens to the old content is decided by the auditing of the model:

The old content
Audited model
  • the old content is kept as a version
  • can be restored through a rollback
  • see File versions
Non-audited model
  • the old content is overwritten
  • cannot be restored through CDMS

Renaming only

Rename without a new file
Request
PATCH /api/rest/fileasset/update/f1…
{ "data": { "id": "f1…", "name": "Framework contract 2026.pdf" },
  "response": ["id", "name", "mimeType"] }
Response
{ "data": { "id": "f1…", "name": "Framework contract 2026.pdf",
            "mimeType": "application/pdf" },
  "meta": { "error": false } }

The content lives under the fileId, not under the name. So renaming only changes one column in the database. mimeType stays as it was determined at the last upload, even if the new extension suggests something else.

PUT and PATCH

Which verb?

When: PATCH /update/{id} or /update/{id}/upload

You send only what changes. Without name in the patch, the stored name applies, and a file must then carry that name. With a new name, the file must carry the new name.

Result: Usually the best choice for files: rename, replace or both, without knowing the other fields.

When: PUT /update/{id} or /update/{id}/upload

As with every PUT, you replace the whole record. So send name and all your own fields along. The file must carry the name from the request. Without a file, the content stays.

Result: See Replacing with PUT.

Instead of multipart, you can also send the new content as Base64 in the field content. See Uploading.

Replacing concurrently

Two requests that upload new content for the same record at the same time would overwrite each other in the storage. That is why CDMS locks the record before it writes the content. The second request gets 409. Then read the record again and upload again. See Concurrent changes.

Pitfalls

What comes next

Sources in the code and the knowledge base
  • CDMS/cdms-system-layer – AbstractLayer.recursivePrepare (update: fileId from the stored record, lockForUpdate, without upload keep the file fields), recursivePatch (file branch: name from the patch, lockForUpdate)
  • CDMS/cdms-localfs-storage – LocalFSFileController.saveFile (retainCurrentContent, publish, applyStoredMetadata); docs/adr/ADR-014-path-is-the-file-id-alone.md
  • CDMS/cdms-integrationtest – AbstractFileRollbackTest (anUploadReplacesInsteadOfAddingAFile, renaming)
Search