On this page

Uploads

The uploads collection is a multi-entry collection that manages file uploads and image optimization.


Definition

The collection includes the following fields, with required fields indicated by the R symbol:

FieldTypeDescription

directory R

The uploaded file's directory path. Nested directories are separated by a slash (/). The root directory is represented by an empty string. The input value is normalized to ensure that non-root directories do not start with a slash, but always end with one (e.g., subdirectory/, nested/directory/, etc.).

filename R

The name of the file. When updating this field, you must also provide the directory in the input data.

type

The file's MIME type is automatically generated based on the filename.

size

The file size in bytes. The field value is generated automatically.

width

The image width in pixels (applies only to image files). The field value is generated automatically.

height

The image height in pixels (applies only to image files). The field value is generated automatically.

description

A brief description of the file. The description is used as the default alt attribute in image fields.

createdAt

An automatically generated timestamp indicating when the upload was created.

updatedAt

An automatically generated timestamp indicating when the upload was last updated.

id

The unique identifier of the record.

Check out the collection definition on GitHub for more details.

File input

The uploads collection has a special $file field that is only accessible during record creation. You can use this field to upload a File and associate it with the record. Here's an example of how to do this using the query builder:

# server/api/upload-file.ts

import { query, readInputData } from '#pruvious/server'

export default defineEventHandler(async (event) => {
  const input = await readInputData(event, 'uploads')

  if (input.errors.length) {
    setResponseStatus(event, 400)
    return input.errors.join('\n')
  }

  if (Array.isArray(input.data)) {
    const response = await query('uploads').createMany(input.data as any)

    if (response.success) {
      return response.records
    } else if (response.message) {
      setResponseStatus(event, 400)
      return response.message
    } else {
      setResponseStatus(event, 422)
      return response.errors
    }
  } else {
    const response = await query('uploads').create(input.data as any)

    if (response.success) {
      return response.record
    } else if (response.message) {
      setResponseStatus(event, 400)
      return response.message
    } else {
      setResponseStatus(event, 422)
      return response.errors
    }
  }
})

Images

Pruvious utilizes a database table named _optimized_images to store optimized images. These images are generated using the image field or the getOptimizedImage utility. They are associated with the uploads collection through a foreign key and are deleted when the original upload record is removed.

For further information, refer to the Image Optimization section in the File Management documentation page.

Last updated on January 8, 2024 at 18:57