Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion app/services/images/image_transform_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { createHash } from 'node:crypto'
import drive from '@adonisjs/drive/services/main'
import { existsSync } from 'node:fs'
import { join } from 'node:path'
import env from '#start/env'

export type SupportedFormat = 'webp' | 'png' | 'jpeg' | 'avif'
export type SupportedFit = keyof FitEnum
Expand Down Expand Up @@ -42,6 +43,7 @@ const FIT_MAP: Record<string, keyof FitEnum> = {
@inject()
export default class ImageTransformService {
private readonly cacheDir = './storage/.cache/images'
private readonly driverDisk = env.get('DRIVE_DISK') ?? 'secret-key'

async transform(options: TransformOptions): Promise<TransformResult> {
const { src, width, height, quality = 80, format = 'webp', fit = 'cover' } = options
Expand Down Expand Up @@ -76,7 +78,7 @@ export default class ImageTransformService {
}

private async fetchFromDrive(src: string): Promise<Buffer> {
const stream = await drive.use('s3').getStream(src)
const stream = await drive.use(this.driverDisk).getStream(src)
const chunks: Buffer[] = []
for await (const chunk of stream) chunks.push(Buffer.from(chunk))
return Buffer.concat(chunks)
Expand Down
7 changes: 7 additions & 0 deletions config/drive.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import env from '#start/env'
import { defineConfig, services } from '@adonisjs/drive'
import app from '@adonisjs/core/services/app'

const driveConfig = defineConfig({
default: env.get('DRIVE_DISK'),
Expand All @@ -9,6 +10,12 @@ const driveConfig = defineConfig({
* services each using the same or a different driver.
*/
services: {
fs: services.fs({
location: app.makePath('storage'),
serveFiles: true,
routeBasePath: '/uploads',
visibility: 'public',
}),
s3: services.s3({
credentials: {
accessKeyId: env.get('AWS_ACCESS_KEY_ID'),
Expand Down
12 changes: 6 additions & 6 deletions start/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ export default await Env.create(new URL('../', import.meta.url), {
| Variables for configuring the drive package
|----------------------------------------------------------
*/
DRIVE_DISK: Env.schema.enum(['s3'] as const),
AWS_ACCESS_KEY_ID: Env.schema.string(),
AWS_SECRET_ACCESS_KEY: Env.schema.string(),
AWS_REGION: Env.schema.string(),
S3_BUCKET: Env.schema.string(),
S3_FORCE_PATH_STYLE: Env.schema.boolean(),
DRIVE_DISK: Env.schema.enum(['s3', 'fs'] as const),
AWS_ACCESS_KEY_ID: Env.schema.string.optional(),
AWS_SECRET_ACCESS_KEY: Env.schema.string.optional(),
AWS_REGION: Env.schema.string.optional(),
S3_BUCKET: Env.schema.string.optional(),
S3_FORCE_PATH_STYLE: Env.schema.boolean.optional(),
})
Loading