Skip to content

Commit c67e8bc

Browse files
committed
Implement media management: Add User model media interfaces and create media migration table
1 parent 55fc830 commit c67e8bc

File tree

3 files changed

+321
-2
lines changed

3 files changed

+321
-2
lines changed

app/Models/User.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66
use Illuminate\Database\Eloquent\Factories\HasFactory;
77
use Illuminate\Foundation\Auth\User as Authenticatable;
88
use Illuminate\Notifications\Notifiable;
9+
use Spatie\MediaLibrary\HasMedia;
10+
use Spatie\MediaLibrary\InteractsWithMedia;
911

10-
class User extends Authenticatable
12+
class User extends Authenticatable implements HasMedia
1113
{
1214
/** @use HasFactory<\Database\Factories\UserFactory> */
13-
use HasFactory, Notifiable;
15+
use HasFactory, Notifiable, InteractsWithMedia;
1416

1517
/**
1618
* The attributes that are mass assignable.

config/media-library.php

Lines changed: 285 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,285 @@
1+
<?php
2+
3+
return [
4+
5+
/*
6+
* The disk on which to store added files and derived images by default. Choose
7+
* one or more of the disks you've configured in config/filesystems.php.
8+
*/
9+
'disk_name' => env('MEDIA_DISK', 's3'),
10+
11+
/*
12+
* The maximum file size of an item in bytes.
13+
* Adding a larger file will result in an exception.
14+
*/
15+
'max_file_size' => 1024 * 1024 * 10, // 10MB
16+
17+
/*
18+
* This queue connection will be used to generate derived and responsive images.
19+
* Leave empty to use the default queue connection.
20+
*/
21+
'queue_connection_name' => env('QUEUE_CONNECTION', 'sync'),
22+
23+
/*
24+
* This queue will be used to generate derived and responsive images.
25+
* Leave empty to use the default queue.
26+
*/
27+
'queue_name' => env('MEDIA_QUEUE', ''),
28+
29+
/*
30+
* By default all conversions will be performed on a queue.
31+
*/
32+
'queue_conversions_by_default' => env('QUEUE_CONVERSIONS_BY_DEFAULT', true),
33+
34+
/*
35+
* Should database transactions be run after database commits?
36+
*/
37+
'queue_conversions_after_database_commit' => env('QUEUE_CONVERSIONS_AFTER_DB_COMMIT', true),
38+
39+
/*
40+
* The fully qualified class name of the media model.
41+
*/
42+
'media_model' => Spatie\MediaLibrary\MediaCollections\Models\Media::class,
43+
44+
/*
45+
* The fully qualified class name of the media observer.
46+
*/
47+
'media_observer' => Spatie\MediaLibrary\MediaCollections\Models\Observers\MediaObserver::class,
48+
49+
/*
50+
* When enabled, media collections will be serialised using the default
51+
* laravel model serialization behaviour.
52+
*
53+
* Keep this option disabled if using Media Library Pro components (https://medialibrary.pro)
54+
*/
55+
'use_default_collection_serialization' => false,
56+
57+
/*
58+
* The fully qualified class name of the model used for temporary uploads.
59+
*
60+
* This model is only used in Media Library Pro (https://medialibrary.pro)
61+
*/
62+
// 'temporary_upload_model' => Spatie\MediaLibraryPro\Models\TemporaryUpload::class,
63+
64+
/*
65+
* When enabled, Media Library Pro will only process temporary uploads that were uploaded
66+
* in the same session. You can opt to disable this for stateless usage of
67+
* the pro components.
68+
*/
69+
'enable_temporary_uploads_session_affinity' => true,
70+
71+
/*
72+
* When enabled, Media Library pro will generate thumbnails for uploaded file.
73+
*/
74+
'generate_thumbnails_for_temporary_uploads' => true,
75+
76+
/*
77+
* This is the class that is responsible for naming generated files.
78+
*/
79+
'file_namer' => Spatie\MediaLibrary\Support\FileNamer\DefaultFileNamer::class,
80+
81+
/*
82+
* The class that contains the strategy for determining a media file's path.
83+
*/
84+
'path_generator' => Spatie\MediaLibrary\Support\PathGenerator\DefaultPathGenerator::class,
85+
86+
/*
87+
* The class that contains the strategy for determining how to remove files.
88+
*/
89+
'file_remover_class' => Spatie\MediaLibrary\Support\FileRemover\DefaultFileRemover::class,
90+
91+
/*
92+
* Here you can specify which path generator should be used for the given class.
93+
*/
94+
'custom_path_generators' => [
95+
// Model::class => PathGenerator::class
96+
// or
97+
// 'model_morph_alias' => PathGenerator::class
98+
],
99+
100+
/*
101+
* When urls to files get generated, this class will be called. Use the default
102+
* if your files are stored locally above the site root or on s3.
103+
*/
104+
'url_generator' => Spatie\MediaLibrary\Support\UrlGenerator\DefaultUrlGenerator::class,
105+
106+
/*
107+
* Moves media on updating to keep path consistent. Enable it only with a custom
108+
* PathGenerator that uses, for example, the media UUID.
109+
*/
110+
'moves_media_on_update' => false,
111+
112+
/*
113+
* Whether to activate versioning when urls to files get generated.
114+
* When activated, this attaches a ?v=xx query string to the URL.
115+
*/
116+
'version_urls' => false,
117+
118+
/*
119+
* The media library will try to optimize all converted images by removing
120+
* metadata and applying a little bit of compression. These are
121+
* the optimizers that will be used by default.
122+
*/
123+
'image_optimizers' => [
124+
Spatie\ImageOptimizer\Optimizers\Jpegoptim::class => [
125+
'-m85', // set maximum quality to 85%
126+
'--force', // ensure that progressive generation is always done also if a little bigger
127+
'--strip-all', // this strips out all text information such as comments and EXIF data
128+
'--all-progressive', // this will make sure the resulting image is a progressive one
129+
],
130+
Spatie\ImageOptimizer\Optimizers\Pngquant::class => [
131+
'--force', // required parameter for this package
132+
],
133+
Spatie\ImageOptimizer\Optimizers\Optipng::class => [
134+
'-i0', // this will result in a non-interlaced, progressive scanned image
135+
'-o2', // this set the optimization level to two (multiple IDAT compression trials)
136+
'-quiet', // required parameter for this package
137+
],
138+
Spatie\ImageOptimizer\Optimizers\Svgo::class => [
139+
'--disable=cleanupIDs', // disabling because it is known to cause troubles
140+
],
141+
Spatie\ImageOptimizer\Optimizers\Gifsicle::class => [
142+
'-b', // required parameter for this package
143+
'-O3', // this produces the slowest but best results
144+
],
145+
Spatie\ImageOptimizer\Optimizers\Cwebp::class => [
146+
'-m 6', // for the slowest compression method in order to get the best compression.
147+
'-pass 10', // for maximizing the amount of analysis pass.
148+
'-mt', // multithreading for some speed improvements.
149+
'-q 90', // quality factor that brings the least noticeable changes.
150+
],
151+
Spatie\ImageOptimizer\Optimizers\Avifenc::class => [
152+
'-a cq-level=23', // constant quality level, lower values mean better quality and greater file size (0-63).
153+
'-j all', // number of jobs (worker threads, "all" uses all available cores).
154+
'--min 0', // min quantizer for color (0-63).
155+
'--max 63', // max quantizer for color (0-63).
156+
'--minalpha 0', // min quantizer for alpha (0-63).
157+
'--maxalpha 63', // max quantizer for alpha (0-63).
158+
'-a end-usage=q', // rate control mode set to Constant Quality mode.
159+
'-a tune=ssim', // SSIM as tune the encoder for distortion metric.
160+
],
161+
],
162+
163+
/*
164+
* These generators will be used to create an image of media files.
165+
*/
166+
'image_generators' => [
167+
Spatie\MediaLibrary\Conversions\ImageGenerators\Image::class,
168+
Spatie\MediaLibrary\Conversions\ImageGenerators\Webp::class,
169+
Spatie\MediaLibrary\Conversions\ImageGenerators\Avif::class,
170+
Spatie\MediaLibrary\Conversions\ImageGenerators\Pdf::class,
171+
Spatie\MediaLibrary\Conversions\ImageGenerators\Svg::class,
172+
Spatie\MediaLibrary\Conversions\ImageGenerators\Video::class,
173+
],
174+
175+
/*
176+
* The path where to store temporary files while performing image conversions.
177+
* If set to null, storage_path('media-library/temp') will be used.
178+
*/
179+
'temporary_directory_path' => null,
180+
181+
/*
182+
* The engine that should perform the image conversions.
183+
* Should be either `gd` or `imagick`.
184+
*/
185+
'image_driver' => env('IMAGE_DRIVER', 'gd'),
186+
187+
/*
188+
* FFMPEG & FFProbe binaries paths, only used if you try to generate video
189+
* thumbnails and have installed the php-ffmpeg/php-ffmpeg composer
190+
* dependency.
191+
*/
192+
'ffmpeg_path' => env('FFMPEG_PATH', '/usr/bin/ffmpeg'),
193+
'ffprobe_path' => env('FFPROBE_PATH', '/usr/bin/ffprobe'),
194+
195+
/*
196+
* Here you can override the class names of the jobs used by this package. Make sure
197+
* your custom jobs extend the ones provided by the package.
198+
*/
199+
'jobs' => [
200+
'perform_conversions' => Spatie\MediaLibrary\Conversions\Jobs\PerformConversionsJob::class,
201+
'generate_responsive_images' => Spatie\MediaLibrary\ResponsiveImages\Jobs\GenerateResponsiveImagesJob::class,
202+
],
203+
204+
/*
205+
* When using the addMediaFromUrl method you may want to replace the default downloader.
206+
* This is particularly useful when the url of the image is behind a firewall and
207+
* need to add additional flags, possibly using curl.
208+
*/
209+
'media_downloader' => Spatie\MediaLibrary\Downloaders\DefaultDownloader::class,
210+
211+
/*
212+
* When using the addMediaFromUrl method the SSL is verified by default.
213+
* This is option disables SSL verification when downloading remote media.
214+
* Please note that this is a security risk and should only be false in a local environment.
215+
*/
216+
'media_downloader_ssl' => env('MEDIA_DOWNLOADER_SSL', true),
217+
218+
'remote' => [
219+
/*
220+
* Any extra headers that should be included when uploading media to
221+
* a remote disk. Even though supported headers may vary between
222+
* different drivers, a sensible default has been provided.
223+
*
224+
* Supported by S3: CacheControl, Expires, StorageClass,
225+
* ServerSideEncryption, Metadata, ACL, ContentEncoding
226+
*/
227+
'extra_headers' => [
228+
'CacheControl' => 'max-age=604800',
229+
],
230+
],
231+
232+
'responsive_images' => [
233+
/*
234+
* This class is responsible for calculating the target widths of the responsive
235+
* images. By default we optimize for filesize and create variations that each are 30%
236+
* smaller than the previous one. More info in the documentation.
237+
*
238+
* https://docs.spatie.be/laravel-medialibrary/v9/advanced-usage/generating-responsive-images
239+
*/
240+
'width_calculator' => Spatie\MediaLibrary\ResponsiveImages\WidthCalculator\FileSizeOptimizedWidthCalculator::class,
241+
242+
/*
243+
* By default rendering media to a responsive image will add some javascript and a tiny placeholder.
244+
* This ensures that the browser can already determine the correct layout.
245+
* When disabled, no tiny placeholder is generated.
246+
*/
247+
'use_tiny_placeholders' => true,
248+
249+
/*
250+
* This class will generate the tiny placeholder used for progressive image loading. By default
251+
* the media library will use a tiny blurred jpg image.
252+
*/
253+
'tiny_placeholder_generator' => Spatie\MediaLibrary\ResponsiveImages\TinyPlaceholderGenerator\Blurred::class,
254+
],
255+
256+
/*
257+
* When enabling this option, a route will be registered that will enable
258+
* the Media Library Pro Vue and React components to move uploaded files
259+
* in a S3 bucket to their right place.
260+
*/
261+
'enable_vapor_uploads' => env('ENABLE_MEDIA_LIBRARY_VAPOR_UPLOADS', false),
262+
263+
/*
264+
* When converting Media instances to response the media library will add
265+
* a `loading` attribute to the `img` tag. Here you can set the default
266+
* value of that attribute.
267+
*
268+
* Possible values: 'lazy', 'eager', 'auto' or null if you don't want to set any loading instruction.
269+
*
270+
* More info: https://css-tricks.com/native-lazy-loading/
271+
*/
272+
'default_loading_attribute_value' => null,
273+
274+
/*
275+
* You can specify a prefix for that is used for storing all media.
276+
* If you set this to `/my-subdir`, all your media will be stored in a `/my-subdir` directory.
277+
*/
278+
'prefix' => env('MEDIA_PREFIX', ''),
279+
280+
/*
281+
* When forcing lazy loading, media will be loaded even if you don't eager load media and you have
282+
* disabled lazy loading globally in the service provider.
283+
*/
284+
'force_lazy_loading' => env('FORCE_MEDIA_LIBRARY_LAZY_LOADING', true),
285+
];
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
public function up(): void
10+
{
11+
Schema::create('media', function (Blueprint $table) {
12+
$table->id();
13+
14+
$table->morphs('model');
15+
$table->uuid()->nullable()->unique();
16+
$table->string('collection_name');
17+
$table->string('name');
18+
$table->string('file_name');
19+
$table->string('mime_type')->nullable();
20+
$table->string('disk');
21+
$table->string('conversions_disk')->nullable();
22+
$table->unsignedBigInteger('size');
23+
$table->json('manipulations');
24+
$table->json('custom_properties');
25+
$table->json('generated_conversions');
26+
$table->json('responsive_images');
27+
$table->unsignedInteger('order_column')->nullable()->index();
28+
29+
$table->nullableTimestamps();
30+
});
31+
}
32+
};

0 commit comments

Comments
 (0)