Laravel-Backpack / medialibrary-uploaders

MIT License
10 stars 4 forks source link

[Feature Request] Documentation for how to preserve/sanitize the original file name and custom alt text in repeatable field #32

Open aerostah opened 4 months ago

aerostah commented 4 months ago

Feature Request

Thank you for putting together this integration, it will be very helpful to my use case once I can overcome these obstacles.

What I am trying to achieve

I'd like more control over how file names are stored in the media library, so I can reference them in the frontend. In my PageCrudController I've setup a repeatable field that uses withMedia, for the most part it works, I can successfully upload files, but my troubles start where your documentation stops -- I cannot get the certain values stored in the media table.

file_name and alt_text I'd like the file_name column to take the original file name of what I uploaded, ideally after sanitizating it, shown here in the "sanitize the filename using a callable" example. If this cannot easily be achieved, then I'd defer to using a subfield to store a manually typed file name, so I can pass that value into the usingFileName method. Secondly, I need to associate image alt_text with each uploaded image, I plan to key in that data in another subfield named alt_text and use the customProperties method to store it.

Is there a clean way get the subfield data associated with each image into the whenSaving function?

collection_name For my web app, I set the collection to the slug, this way I can filter the media in the frontend controller. Is there a better way to set the collection name because the slug doesn't exist until after the record is created, requiring to user to save once before uploading media?

name This currently uses the of the repeater field's name. This makes sense for situations where there are multiple repeatables w/ images.

How I see it implemented

'withMedia' => [
   'collection' => $this->crud->getCurrentEntry()->slug,
   'whenSaving' => function($spatieMedia, $backpackMediaObject, ?????) {
   return $spatieMedia->usingFileName(?????->getClientOriginalName())
   ->withCustomProperties([
       'alt_text' => ?????
   ])
   ->withResponsiveImages();
   }
],

As you can see, the documentation got me most of the way there, it could use a strong finish! Hopefully I explained this clearly, I look forward to seeing what you suggest.