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:
Field | Type | Description |
---|---|---|
| The uploaded file's directory path. Nested directories are separated by a slash ( | |
| The name of the file. When updating this field, you must also provide the | |
| The file's MIME type is automatically generated based on the | |
| The file size in bytes. The field value is generated automatically. | |
| The image width in pixels (applies only to image files). The field value is generated automatically. | |
| The image height in pixels (applies only to image files). The field value is generated automatically. | |
| A brief description of the file. The description is used as the default | |
| An automatically generated timestamp indicating when the upload was created. | |
| An automatically generated timestamp indicating when the upload was last updated. | |
| 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