dmitrybubyakin / nova-medialibrary-field

Laravel Nova field for managing the Spatie media library
MIT License
262 stars 61 forks source link

Implementing `previewUsing` to work on `attachExisting` #163

Open marlocorridor opened 2 years ago

marlocorridor commented 2 years ago

We are using previewUsing to use a conversion file for preview. image

But on "use existing" button, preview does not work. image

We can't seem to find on documents and we are still looking at the source code. We reviewed the closed issues but still couldn't find a reference.

marlocorridor commented 2 years ago

MediaUploadingListItem.vue

  methods: {
    loadPreview() {
      if (this.media.isImage) { // could we change this
        this.preview = this.media.previewUrl
      } else {
        return
      }
    ...
      ...
      if (this.media.previewUrl) { // to this?
        this.preview = this.media.previewUrl
      } else {
        return
      }
    ...
marlocorridor commented 2 years ago

As of inspecting, previewUrl for attacheExisting is not using ->previewUsing('preview')

{
    "0": {
        "id": ...,
        "size": 13133,
        "mimeType": "application/pdf",
        "fileName": "sample-2.pdf",
        "collectionName": "",
        "extension": "pdf",
        "previewUrl": "http://127.0.0.1:9000/.../media/16fc12ce-e916-4e0f-bf65-21fd71b2f3bc/sample-2.pdf"
    }
}
marlocorridor commented 2 years ago
class AttachableMediaPresenter implements Arrayable
{
    private $media;

    public function __construct(Media $media)
    {
        $this->media = $media;
    }

    public function toArray(): array
    {
        return array_merge([
            'id' => $this->media->id,
            'size' => $this->media->size,
            'mimeType' => $this->media->mime_type,
            'fileName' => $this->media->file_name,
            'collectionName' => $this->media->collection_name,
            'extension' => $this->media->extension,
            'previewUrl' => $this->media->getFullUrl('preview'), // how could we pass the conversion value :)
        ]);
    }
}
marlocorridor commented 2 years ago
class AttachableMediaPresenter implements Arrayable
{
    ...
    public function toArray(): array
    {
        return array_merge([
            ...
            'previewUrl' => $this->media->getFullUrl('preview'), // how could we pass the conversion value :)
        ]);
    }
}

This seems to work, we just need to find where to get the conversion value

marlocorridor commented 2 years ago

image

marlocorridor commented 2 years ago

Preparing the pull request