Daravel

daravel_medialibrary

English ยท Bahasa Indonesia

Files and images attached to Daravel models, the way spatie/laravel-medialibrary v11 does it: one media table, collections, and image conversions made from the original. Rows and files follow Spatie's layout, so a Laravel application on the same database and disk shares them. Images are converted in pure Dart, with no GD or ImageMagick to install.

dartisan pub:add media
import 'package:daravel_medialibrary/daravel_medialibrary.dart';

final class Post extends Model with InteractsWithMedia {
  static final ModelDefinition<Post> definition = ModelDefinition<Post>(
    table: 'posts',
    factory: Post.new,
    observers: [MediaObserver<Post>()],
  );

  @override
  ModelDefinition<Post> get modelDefinition => Post.definition;

  @override
  void registerMediaCollections() => addMediaCollection('cover').singleFile();

  @override
  void registerMediaConversions([Media? media]) {
    addMediaConversion('thumb').fit(Fit.crop, 300, 200).nonQueued();
  }
}

final media = await post
    .addMedia((await request.file('cover'))!)
    .toMediaCollection('cover');
final thumb = await post.getFirstMediaUrl('cover', 'thumb');
  • Spatie's media table, <id>/<file> and <id>/conversions/<file>-<conversion>.jpg: Laravel and Daravel read, serve and delete each other's media.
  • Conversions with Spatie's resize rules (width, height, fit, crop) in a separate isolate, JPEG by default, EXIF rotation applied; queued ones run as a job.
  • Single-file collections, accepted MIME types, size limit, and Spatie's file name cleaning and refused extensions (shell.php.jpg).
  • media:regenerate and media:clean.

Documentation: github.com/daravel-dev/daravel/blob/main/docs/guide/medialibrary.md.

License and trademarks

MIT. "Laravel" is a trademark of Laravel Holdings Inc.; "Dart" is a trademark of Google LLC. Daravel is an independent project, not affiliated with or endorsed by either.

Libraries

commands
The media:regenerate and media:clean commands, for bootstrap/commands.dart:
daravel_medialibrary
Files and images attached to models (spatie/laravel-medialibrary v11): the media table and file layout Spatie uses, so a Laravel application on the same database and disk shares them, and image conversions in pure Dart.