reym
CRUD

Files

How to Handle Files with CRUD.

Upload Files

Files can be uploaded to crud using the /file endpoint. A POST request with a FormData object containing the file in a field called file and a Tenant-ID header is required.

As an alternative to the Tenant-ID header you can also add a tenantId field to the FormData object
CRUD will automatically update the filename in case it contains non ASCII characters.
const headers = new Headers();
myHeaders.append("Tenant-ID", "");

const formData = new FormData();
formdata.append("file", fileInput.files[0], "fileName.jpeg");

const newFile = await fetch(`${crudUrl}/file`, {
  method: "POST",
  headers: headers,
  body: formData,
}).then(response => response.json());

The response will look like this for images:

{
    "id": "092adad3-9755-48ee-9e63-87316a7eccad",
    "mimeType": "image/jpeg",
    "name": "fileName.jpeg",
    "size": 117606,
    "width": 734,
	"height": 742
}

For other file types the response will look like this:

{
	"id": "0cce31e0-47d2-43e4-9520-b77f07c2e5e5",
	"mimeType": "application/pdf",
	"name": "fileName.pdf",
	"size": 12502
}

Image Optimization

The CRUD service provides an endpoint that does image optimization: /file/image/{tenantId}/{fileId}/{optimizationParams}.{fileExtension}.

ParameterDescription
tenantIdThe ID of the tenant.
fileIdThe ID of the file.
optimizationParamsOptional, see processing options of imageproxy for details
fileExtensionThe target file extension (one of: png, jpeg, jpg, webp, avif, gif, ico, bmp, tiff)

Download Images

Images can be requested using the /file/image/{tenantId}/{fileId} endpoint.

Download Files

Files can be downloaded using the /file/file/{tenantId}/{fileId} endpoint.

Reference Files in CRUD Data

Using the File type in a CRUD type allows you to reference files in your CRUD data.

Example:

type User @crudType {
	avatar: File
}

You can pass the id of your file as value for the avatar field. When querying the User type, the avatar field will automatically be resolved to the file data.

Delete Files

CRUD automatically deletes files that are not referenced in any CRUD type after 30 days. To prevent broken references you cannot delete files manually.