Import media #1150
-
I have some xml files which I import using a custom command this contains content and images I am looking to hook into the handlesMedia trait, can someone point me in the right direction? I basically need to pass a file url to be uploaded and build the mediables relation I can populate the media and mediables tables with the required info but it would be nice to hook into the existing functionality. Many thanks in advance |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 1 reply
-
Hi @futurewebsites, I have a quick artisan command I adapted from Standard disclaimer — this was done very quickly and was only tested with the Glide media service. Your mileage may vary :) // file: app/Console/Commands/TwillAddMedia.php
<?php
namespace App\Console\Commands;
use A17\Twill\Repositories\MediaRepository;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;
class TwillAddMedia extends Command
{
protected $signature = 'twill:add-media {path}';
protected $description = 'Add media';
public function handle()
{
$path = $this->argument('path');
if (!file_exists($path)) {
$this->error("File $path does not exist.");
return 1;
}
$originalFilename = basename($path);
$filename = sanitizeFilename($originalFilename);
$fileDirectory = uniqid();
$uuid = $fileDirectory . '/' . $filename;
if (config('twill.media_library.prefix_uuid_with_local_path', false)) {
$prefix = trim(config('twill.media_library.local_path'), '/ ') . '/';
$fileDirectory = $prefix . $fileDirectory;
$uuid = $prefix . $uuid;
}
$disk = config('twill.media_library.disk');
Storage::disk($disk)->put($uuid, file_get_contents($path));
$filePath = Storage::disk($disk)->path($uuid);
list($w, $h) = getimagesize($filePath);
$fields = [
'uuid' => $uuid,
'filename' => $originalFilename,
'width' => $w,
'height' => $h,
];
app(MediaRepository::class)->create($fields);
return 0;
}
} |
Beta Was this translation helpful? Give feedback.
-
Is there any way to have additional media library folders or categories for organising images? |
Beta Was this translation helpful? Give feedback.
-
@futurewebsites at the moment only tags can serve that need, but folders are highly requested, they will come! |
Beta Was this translation helpful? Give feedback.
-
Can attach this to another model ? i.e create the morph relationship something like:
|
Beta Was this translation helpful? Give feedback.
-
Hi @futurewebsites, yes this should be possible. This could get a bit tricky if you have complex crop configurations but for a single role and crop, I believe this would do:
You would need to add It's getting quite experimental at this point, but I hope this can help :) |
Beta Was this translation helpful? Give feedback.
Hi @futurewebsites,
I have a quick artisan command I adapted from
MediaLibraryController::storeFile()
to import a local image into the Media Library. It takes a file path but it's passed tofile_get_contents()
, which I believe can work with URLs.Standard disclaimer — this was done very quickly and was only tested with the Glide media service. Your mileage may vary :)