CodamAIDocs
Topicdone

What a file model is

Metadata in the database, content in the file storage. This page explains who manages which part.

Variants
standalone file modelfile as a child of another modelfields the server setsendpoints

What this is about

A file model is a model that has exactly one file: an attachment, a logo, a contract as a PDF. It has two parts that live in two different places:

  • the record in the database: file name, size, file type and your own fields, such as title or category,
  • the content, that is the bytes of the file, in the file storage.

For the client, both are one object. It reads and searches the record like any other object and fetches the content through an endpoint of its own.

Two storage locations, one object

flowchart LR
    C["Client"] -->|"read, search record"| D["CDMS"]
    C -->|"GET /{id}/file"| D
    D --> DB[("Database<br/>id, name, mimeType,<br/>fileSize, fileId, fileVersion,<br/>own fields")]
    D --> F[("File storage<br/>content under the fileId")]
    DB -. "fileId" .-> F

The fields of a file model

Besides the system fields id, _createdOn and _updatedOn, every file model has these fields:

FieldMeaningWho sets it?
namefile name as the user sees it, e.g. Contract.pdfthe client
mimeTypefile type, e.g. application/pdf; derived from the extension of namethe server when it stores the content
fileSizesize in bytesthe server when it stores the content
fileIdidentifier of the content in the file storagethe server, once at the first save
fileVersionidentifier of the current content; changes with every new uploadthe server when it stores the content

On top of that come your own fields from the model. When you create the model in the hub, name and the file fields are already taken, and the hub rejects an own field with that name. See Modeling in the hub.

Who manages which part

Record and content
Record
database
  • read, search, filter like any model
  • permissions, row filters, validation, hooks
  • history if the model is audited
  • part of the request's transaction
Content
file storage

Standalone or as a child

A file model can stand on its own or hang on another model:

Where a file model occurs

When: The file itself is the object, such as a document in a filing system.

You create it directly: POST /document/create/upload. It has its own endpoints for reading, searching, changing, deleting and downloading.

When: The file belongs to another object, such as Company.logo (1:1) or Dossier.attachments (1:n).

You write it nested with the parent: POST /company/create/upload with the logo in data and its file in the same request. For this, models without a file of their own also get the /upload variants. Downloading goes through the endpoints of the file model.

Result: Whether the file goes along when the parent is deleted is decided by the DELETE flag. See Deleting file models.

The endpoints

Endpointpurpose
POST {basis}/create/uploadcreate with file, multipart
PUT {basis}/update/{id}/uploadreplace with file, multipart
PATCH {basis}/update/{id}/uploadchange with file, multipart
POST /create, PUT and PATCH /update/{id}also possible, the file is then Base64 in the JSON
GET {basis}/{id}/file?access_token=…download the content

All other endpoints (read, search, delete, history) work with the record like for any model. See Which endpoints a model has.

Prerequisite

The application needs a file storage: the local file system (FILESYSTEM), a directory on the server or on a volume. Without a storage, every request that writes or reads content fails with file-storage-not-configured. See Storage backends.

What comes next

Sources in the code and the knowledge base
  • CDMS/cdms-generator – DtoMetaProcessor (fields name, mimeType, fileSize, fileId, fileVersion), EntityProcessor, RestPayloadProcessor (name, content), AbstractProcessor.isDownloadExposed/isUploadExposed
  • CDMS/cdms-system-layer – AbstractLayer.recursivePrepare, recursivePatch (file branch), downloadFile
  • CDMS/cdms-localfs-storage – LocalFSFileController, docs/02-path-layout-and-tenant-separation.md, docs/04-file-operations.md
  • CDMS/cdms-integrationtest – AbstractRecursiveFileDeleteTest (Company.logo, Dossier.attachments)
Search