Product
Introducing License Enforcement in Socket
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
@jrmc/adonis-attachment
Advanced tools
Turn any field on your Lucid model to an attachment data type
This package is currently development and will replace attachment-advanced for AdonisJS 6.
Upload file in public folder.
Configure differents images sizes and formats
Regenerate variantes files
Use drive for private file and cloud services
Couple with a job queue (recommended, optional)
Install and configure the package:
node ace add @jrmc/adonis-attachment
Or:
npm i @jrmc/adonis-attachment
node ace configure @jrmc/adonis-attachment
Often times, the size of the image metadata could exceed the allowable length of an SQL String
data type. So, it is recommended to create/modify the column which will hold the metadata to use a JSON
data type.
If you are creating the column for the first time, make sure that you use the JSON data type. Example:
// Within the migration file
protected tableName = 'users'
public async up() {
this.schema.createTable(this.tableName, (table) => {
table.increments()
table.json('avatar') // <-- Use a JSON data type
})
}
If you already have a column for storing image paths/URLs, you need to create a new migration and alter the column definition to a JSON data type. Example:
# Create a new migration file
node ace make:migration change_avatar_column_to_json --table=users
// Within the migration file
protected tableName = 'users'
public async up() {
this.schema.alterTable(this.tableName, (table) => {
table.json('avatar').alter() // <-- Alter the column definition
})
}
Next, in the model, import the attachment
decorator, Attachmentable
mixin and the Attachment
type from the package.
Make sure NOT to use the
@column
decorator when using the@attachment
decorator.
import { BaseModel } from '@adonisjs/lucid/orm'
import { compose } from '@adonisjs/core/helpers'
import { attachment, Attachmentable } from '@jrmc/adonis-attachment'
import type { Attachment } from '@jrmc/adonis-attachment/types/attachment'
class User extends compose(BaseModel, Attachmentable) {
@attachment()
declare avatar: Attachment
}
Now you can create an attachment from the user uploaded file as follows.
import { attachmentManager } from '@jrmc/adonis-attachment'
class UsersController {
public store({ request }: HttpContext) {
const avatar = request.file('avatar')!
const user = new User()
user.avatar = await attachmentManager.createFromFile(avatar)
// user.avatar = await attachmentManager.createFromBuffer(buffer, 'photo.jpg')
await user.save()
}
}
You can also store files inside the subfolder by defining the folder
property as follows.
class User extends BaseModel {
@attachment({ folder: 'uploads/avatars' })
declare avatar: Attachment
}
It is possible to limit the variants on an attachment
class User extends BaseModel {
@attachment({
variants: ['thumbnail', 'medium', 'large']
})
declare avatar: Attachment
}
user.avatar.getUrl()
user.avatar.getUrl('thumbnail')
// or await user.avatar.getVariant('thumbnail').getUrl()
<img src="{{ user.avatar.getUrl('thumbnail') }}" loading="lazy" alt="" />
Configuration for variants files (config/attachment.ts)
import { defineConfig } from '@jrmc/adonis-attachment'
import app from '@adonisjs/core/services/app'
import sharp from 'sharp'
export default defineConfig({
basePath: app.publicPath(),
converters: [
{
key: 'thumbnail',
converter: () => import('@jrmc/adonis-attachment/converters/image_converter'),
options: {
resize: 300,
format: 'webp',
}
},
{
key: 'medium',
converter: () => import('@jrmc/adonis-attachment/converters/image_converter'),
options: {
format: 'jpeg',
resize: { // https://sharp.pixelplumbing.com/api-resize
width: 400,
height: 400,
fit: sharp.fit.cover,
position: 'top'
},
}
},
{
key: 'large',
converter: () => import('@jrmc/adonis-attachment/converters/image_converter'),
options: {
resize: 1024,
format: {
format: 'png',
options: {
quality: 80
}
}
}
},
]
})
Variant image is generate by sharp module
Options resize is number
or object
(options) details in documentation : sharp api resize
Options format is string
or array
[ format, options ] details in documentation : sharp api outpout
FAQs
Turn any field on your Lucid model to an attachment data type
The npm package @jrmc/adonis-attachment receives a total of 88 weekly downloads. As such, @jrmc/adonis-attachment popularity was classified as not popular.
We found that @jrmc/adonis-attachment demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.