room reservation model, controller, seeder and migration files added
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
import type { HttpContext } from '@adonisjs/core/http'
|
||||
import app from '@adonisjs/core/services/app'
|
||||
import drive from '@adonisjs/drive/services/main'
|
||||
import logger from '@adonisjs/core/services/logger'
|
||||
import { cuid } from '@adonisjs/core/helpers'
|
||||
import { createReadStream } from 'node:fs'
|
||||
import { promises as fs } from 'node:fs'
|
||||
import { randomUUID } from 'node:crypto'
|
||||
import { extname } from 'node:path'
|
||||
import { Jimp } from 'jimp'
|
||||
import fs from 'node:fs/promises'
|
||||
|
||||
const allowedExtnames = ['jpg', 'jpeg', 'png', 'webp', 'gif']
|
||||
|
||||
@@ -32,36 +32,49 @@ export default class ImagesController {
|
||||
return response.badRequest({ message: message || 'Invalid image file' })
|
||||
}
|
||||
|
||||
const uploadsDir = app.makePath('storage', 'uploads')
|
||||
await fs.mkdir(uploadsDir, { recursive: true })
|
||||
|
||||
const extension = image.extname || 'bin'
|
||||
const filename = `${cuid()}.${extension}`
|
||||
await image.move(uploadsDir, { name: filename, overwrite: false })
|
||||
const filename = `${randomUUID()}.${extension}`
|
||||
|
||||
if (image.state !== 'moved') {
|
||||
logger.error('Image upload failed: %s', image.errors)
|
||||
return response.internalServerError({ message: 'Image upload failed' })
|
||||
await drive.use().put(filename, await fs.readFile(image.tmpPath!))
|
||||
|
||||
// Generate thumbnail
|
||||
try {
|
||||
const jimpImage = await Jimp.read(image.tmpPath!)
|
||||
const buffer = await jimpImage
|
||||
.resize({ w: 200 }) // Resize to width 200px, maintain aspect ratio
|
||||
.getBuffer('image/jpeg')
|
||||
|
||||
await drive.use().put(`thumbnails/${filename}`, buffer)
|
||||
} catch (error) {
|
||||
logger.error('Thumbnail generation failed: %s', error.message)
|
||||
// We don't fail the upload if thumbnail generation fails, but we log it
|
||||
}
|
||||
|
||||
return response.ok({
|
||||
filename,
|
||||
url: `/api/images/${filename}`,
|
||||
thumbnailUrl: `/api/images/${filename}?type=thumbnail`,
|
||||
})
|
||||
}
|
||||
|
||||
async show({ params, response }: HttpContext) {
|
||||
async show({ params, request, response }: HttpContext) {
|
||||
const filename = String(params.filename || '')
|
||||
const type = request.input('type')
|
||||
|
||||
if (!filename || filename.includes('/') || filename.includes('\\')) {
|
||||
return response.badRequest({ message: 'Invalid image name' })
|
||||
}
|
||||
|
||||
const filePath = app.makePath('storage', 'uploads', filename)
|
||||
let key = filename
|
||||
|
||||
try {
|
||||
await fs.access(filePath)
|
||||
} catch {
|
||||
if (type === 'thumbnail') {
|
||||
const thumbnailKey = `thumbnails/${filename}`
|
||||
if (await drive.use().exists(thumbnailKey)) {
|
||||
key = thumbnailKey
|
||||
}
|
||||
}
|
||||
|
||||
if (!(await drive.use().exists(key))) {
|
||||
return response.notFound({ message: 'Image not found' })
|
||||
}
|
||||
|
||||
@@ -69,6 +82,26 @@ export default class ImagesController {
|
||||
response.header('Content-Type', mimeType)
|
||||
response.header('Cache-Control', 'public, max-age=86400')
|
||||
|
||||
return response.stream(createReadStream(filePath))
|
||||
return response.stream(await drive.use().getStream(key))
|
||||
}
|
||||
|
||||
async list() {
|
||||
const { objects } = await drive.use().listAll('')
|
||||
const images = []
|
||||
|
||||
for (const file of objects) {
|
||||
if (file.isFile) {
|
||||
images.push({
|
||||
filename: file.key,
|
||||
url: `/api/images/${file.key}`,
|
||||
thumbnailUrl: `/api/images/${file.key}?type=thumbnail`,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return images
|
||||
}
|
||||
async delete() {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user