Controller setup
From file
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)
await user.save()
}
}From files
Now you can create an attachments from the user uploaded files as follows.
import { attachmentManager } from '@jrmc/adonis-attachment'
class UsersController {
public store({ request }: HttpContext) {
const files = request.files('files')!
const user = new User()
user.files = await attachmentManager.createFromFiles(files)
await user.save()
}
}From Buffer
import { attachmentManager } from '@jrmc/adonis-attachment'
import app from '@adonisjs/core/services/app'
class UsersController {
public store({ request }: HttpContext) {
const user = new User()
const buffer = await readFile(app.makePath('me.jpg'))
user.avatar = await attachmentManager.createFromBuffer(buffer, 'photo.jpg')
await user.save()
}
}From Base64
import { attachmentManager } from '@jrmc/adonis-attachment'
class UsersController {
public store({ request }: HttpContext) {
const user = new User()
const b64 = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAApgAAAKYB3X3/OAAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAAANCSURBVEiJtZZPbBtFFMZ/M7ubXdtdb1xSFyeilBapySVU8h8OoFaooFSqiihIVIpQBKci6KEg9Q6H9kovIHoCIVQJJCKE1ENFjnAgcaSGC6rEnxBwA04Tx43t2FnvDAfjkNibxgHxnWb2e/u992bee7tCa00YFsffekFY+nUzFtjW0LrvjRXrCDIAaPLlW0nHL0SsZtVoaF98mLrx3pdhOqLtYPHChahZcYYO7KvPFxvRl5XPp1sN3adWiD1ZAqD6XYK1b/dvE5IWryTt2udLFedwc1+9kLp+vbbpoDh+6TklxBeAi9TL0taeWpdmZzQDry0AcO+jQ12RyohqqoYoo8RDwJrU+qXkjWtfi8Xxt58BdQuwQs9qC/afLwCw8tnQbqYAPsgxE1S6F3EAIXux2oQFKm0ihMsOF71dHYx+f3NND68ghCu1YIoePPQN1pGRABkJ6Bus96CutRZMydTl+TvuiRW1m3n0eDl0vRPcEysqdXn+jsQPsrHMquGeXEaY4Yk4wxWcY5V/9scqOMOVUFthatyTy8QyqwZ+kDURKoMWxNKr2EeqVKcTNOajqKoBgOE28U4tdQl5p5bwCw7BWquaZSzAPlwjlithJtp3pTImSqQRrb2Z8PHGigD4RZuNX6JYj6wj7O4TFLbCO/Mn/m8R+h6rYSUb3ekokRY6f/YukArN979jcW+V/S8g0eT/N3VN3kTqWbQ428m9/8k0P/1aIhF36PccEl6EhOcAUCrXKZXXWS3XKd2vc/TRBG9O5ELC17MmWubD2nKhUKZa26Ba2+D3P+4/MNCFwg59oWVeYhkzgN/JDR8deKBoD7Y+ljEjGZ0sosXVTvbc6RHirr2reNy1OXd6pJsQ+gqjk8VWFYmHrwBzW/n+uMPFiRwHB2I7ih8ciHFxIkd/3Omk5tCDV1t+2nNu5sxxpDFNx+huNhVT3/zMDz8usXC3ddaHBj1GHj/As08fwTS7Kt1HBTmyN29vdwAw+/wbwLVOJ3uAD1wi/dUH7Qei66PfyuRj4Ik9is+hglfbkbfR3cnZm7chlUWLdwmprtCohX4HUtlOcQjLYCu+fzGJH2QRKvP3UNz8bWk1qMxjGTOMThZ3kvgLI5AzFfo379UAAAAASUVORK5CYII="
user.avatar = await attachmentManager.createFromBase64(b64, 'photo.jpg')
await user.save()
}
}From Path
import { attachmentManager } from '@jrmc/adonis-attachment'
import app from '@adonisjs/core/services/app'
class UsersController {
public store({ request }: HttpContext) {
const user = new User()
const path = app.makePath('me.jpg')
user.avatar = await attachmentManager.createFromPath(path, 'photo.jpg')
await user.save()
}
}From Url
import { attachmentManager } from '@jrmc/adonis-attachment'
class UsersController {
public store({ request }: HttpContext) {
const user = new User()
const url = new URL('https://site.com/picture.jpg')
user.avatar = await attachmentManager.createFromUrl(url, 'photo.jpg')
await user.save()
}
}From Stream
import { attachmentManager } from '@jrmc/adonis-attachment'
import fs from 'node:fs'
class UsersController {
public store({ request }: HttpContext) {
const user = new User()
const videoStream = fs.createReadStream(app.makePath('path/video.mkv'))
user.avatar = await attachmentManager.createFromStream(videoStream, 'name.mkv')
await user.save()
}
}Delete Attachment
class User extends BaseModel {
@attachment()
declare avatar: Attachment | null
}user.avatar = null
await user.save()Temporary files
Files created internally by createFromUrl() and createFromStream() are removed after the attachment is written to storage, including when writing fails. Variant generation then reads from storage when needed. Temporary conversion inputs and outputs are cleaned up after each conversion, including failed conversions.
Files supplied through createFromPath() or multipart uploads remain owned by the caller or the upload framework and are not deleted by this cleanup. An attachment created from a URL or stream must be persisted to complete its temporary file lifecycle. Existing temporary files left by older package versions are not automatically removed.