UNPKG

@adonisjs/drive

Version:

A thin wrapper on top of Flydrive to work seamlessly with AdonisJS

219 lines (218 loc) 7.61 kB
import { DriveManager } from 'flydrive'; import type { ApplicationService } from '@adonisjs/core/types'; import type { DriveDisks, DriveService, WriteOptions } from '../src/types.ts'; /** * Extending the AdonisJS container with the drive manager service. * This allows the drive service to be resolved from the IoC container * using dependency injection. * * @example * ```js * // Inject drive manager in controllers/services * export default class FileController { * constructor(@inject('drive.manager') private drive: DriveService) {} * } * * // Or resolve manually * const drive = await app.container.make('drive.manager') * ``` */ declare module '@adonisjs/core/types' { interface ContainerBindings { 'drive.manager': DriveService; } } /** * Extending BodyParser MultipartFile with "moveToDisk" method * to move uploaded files from the local filesystem to a drive disk. * Supports both stream and buffer modes for different cloud providers. * * @example * ```js * // In route handlers or controllers * export default class UploadController { * async store({ request }) { * const file = request.file('document') * * // Simple usage - move to default disk * await file.moveToDisk('documents/report.pdf') * * // With specific disk and options * await file.moveToDisk('avatars/user.jpg', 'uploads', { * moveAs: 'stream', * contentType: 'image/jpeg' * }) * * // Access the file URL after moving * console.log(file.meta.url) // Generated URL * } * } * ``` */ declare module '@adonisjs/core/bodyparser' { interface MultipartFile { /** * Move user uploaded file from the tmp directory * to a Drive disk. File URL is automatically set in meta.url. * * @param key - The destination file path/key * @param disk - The disk name to use (optional, uses default if not specified) * @param options - Write options and move configuration * @param options.moveAs - Upload mode: 'stream' (default) or 'buffer' */ moveToDisk(key: string, disk?: keyof DriveDisks, options?: WriteOptions & { /** * When using "stream", the file from the tmpPath will be read * as a stream and written to the cloud provider. * * Whereas, in case of "buffer", the entire file will be first * read into the memory and then sent to the cloud provider. Some * cloud providers like supabase cannot work with the "stream" option. */ moveAs?: 'stream' | 'buffer'; }): Promise<void>; /** * Move user uploaded file from the tmp directory * to a Drive disk using the default disk. * * @param key - The destination file path/key * @param options - Write options and move configuration * @param options.moveAs - Upload mode: 'stream' (default) or 'buffer' */ moveToDisk(key: string, options?: WriteOptions & { /** * When using "stream", the file from the tmpPath will be read * as a stream and written to the cloud provider. * * Whereas, in case of "buffer", the entire file will be first * read into the memory and then sent to the cloud provider. Some * cloud providers like supabase cannot work with the "stream" option. */ moveAs?: 'stream' | 'buffer'; }): Promise<void>; } } /** * Drive Provider registers a singleton drive manager service * to the IoC container and wires up the routing to serve * files from the "fs" driver. * * @example * ```js * // Automatically registered by AdonisJS when package is installed * // Access drive service in your application: * * export default class FileController { * async upload({ request }) { * const drive = await app.container.make('drive.manager') * const file = request.file('document') * * await file.moveToDisk('documents/doc.pdf') * return { url: file.meta.url } * } * } * ``` */ export default class DriveProvider { #private; protected app: ApplicationService; /** * Creates a new DriveProvider instance. * * @param app - The AdonisJS application service instance */ constructor(app: ApplicationService); /** * Registers Edge.js template helpers for generating drive URLs. * Adds global helpers `driveUrl` and `driveSignedUrl` to Edge templates. * * @param drive - The drive manager instance * * @example * ```js * // In Edge templates after registration: * // Generate public URL * <img src="{{ driveUrl('profile/avatar.jpg') }}" /> * * // Generate signed URL with expiration * <a href="{{ await driveSignedUrl('documents/secret.pdf', { expiresIn: '1h' }) }}"> * Download Document * </a> * * // Use specific disk * <img src="{{ driveUrl('thumb.jpg', 'thumbnails') }}" /> * ``` */ protected registerViewHelpers(drive: DriveManager<any>): Promise<void>; /** * Extends BodyParser MultipartFile class with "moveToDisk" * method to move uploaded files from the local filesystem to * a drive disk. Supports both stream and buffer upload modes. * * @param drive - The drive manager instance * * @example * ```js * // In a controller after this extension is registered: * export default class UploadController { * async store({ request }) { * const avatar = request.file('avatar') * * // Move to default disk * await avatar.moveToDisk('avatars/user-123.jpg') * * // Move to specific disk with options * await avatar.moveToDisk('documents/file.pdf', 's3', { * moveAs: 'buffer', * contentType: 'application/pdf' * }) * * // Access the generated URL * return { url: avatar.meta.url } * } * } * ``` */ protected extendMultipartFile(drive: DriveManager<any>): Promise<void>; /** * Registers the drive manager singleton and Disk binding * in the IoC container. This method is called during the * AdonisJS application registration phase. * * @example * ```js * // The registered services can be injected: * export default class FileService { * constructor( * @inject('drive.manager') private drive: DriveService, * @inject(Disk) private defaultDisk: Disk * ) {} * } * ``` */ register(): void; /** * The boot method resolves drive and router to register * the routes for the locally served services. Also registers * view helpers and extends MultipartFile with drive methods. * * The routes must be defined before the application has * started. This method is called during the AdonisJS * application boot phase. * * @example * ```js * // After boot, these routes are automatically available: * // GET /uploads/* -> serves files from 'uploads' disk * // GET /documents/* -> serves files from 'documents' disk * * // View helpers are available in Edge templates: * // {{ driveUrl('file.jpg') }} * // {{ await driveSignedUrl('private.pdf', { expiresIn: '1h' }) }} * * // MultipartFile extension is available: * // await file.moveToDisk('uploads/document.pdf') * ``` */ boot(): Promise<void>; }