Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add spatie media library to demo #456

Merged
merged 23 commits into from
Jun 28, 2023
Merged

Add spatie media library to demo #456

merged 23 commits into from
Jun 28, 2023

Conversation

pxpm
Copy link
Contributor

@pxpm pxpm commented Jan 16, 2023

WHY

BEFORE - What was wrong? What was happening before this PR?

No working examples on how to use Spatie Media Library with our fields.

Installation steps:

  1. You should do a composer update first to get media library installed, I already added it to composer.json
  2. There is a new disk on filesystems.php along with links. If you are caching your configs, make sure to php artisan config:clear.
  3. Migrations are added/modified, so do a php artisan migrate:fresh --seed
  4. setup the folder link: php artisan storage:link

You should be good to go. Navigate to Products Crud, you should see something like:
image

AFTER - What is happening after this PR?

We should now evaluate the steps taken to use Spatie Media Library and brainstorm a way to make developers life easier. I see there is a lot of "repetitive" work that we can automate.

app/Models/Product.php Outdated Show resolved Hide resolved
@pxpm pxpm assigned pxpm and unassigned jcastroa87 Feb 10, 2023
@pxpm
Copy link
Contributor Author

pxpm commented Feb 10, 2023

Note: we should review the final syntax so that I can update this branch with the correct definitions to work in demo.

@tabacitu
Copy link
Member

Let's finish this PR to use the latest media syntax 💪

@pxpm
Copy link
Contributor Author

pxpm commented Feb 22, 2023

@tabacitu testing notes.

This requires: Laravel-Backpack/CRUD#4921

Everything should be configured in this PR, just don't forget to php artisan migrate, and clear configs in case you are caching them: php artisan config:clear.

This should work out-of-the-box! 🙏

@pxpm pxpm assigned tabacitu and unassigned pxpm Feb 22, 2023
pxpm and others added 5 commits March 7, 2023 12:11
Bumps [backpack/backupmanager](https://github.com/Laravel-Backpack/BackupManager) from 3.0.9 to 4.0.2.
- [Release notes](https://github.com/Laravel-Backpack/BackupManager/releases)
- [Changelog](https://github.com/Laravel-Backpack/BackupManager/blob/master/CHANGELOG.md)
- [Commits](Laravel-Backpack/BackupManager@v3.0.9...v4.0.2)

---
updated-dependencies:
- dependency-name: backpack/backupmanager
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <[email protected]>
…kpack/backupmanager-4.0.2

Bump backpack/backupmanager from 3.0.9 to 4.0.2
@tabacitu tabacitu changed the base branch from main to v6 May 31, 2023 11:53
@tabacitu
Copy link
Member

I've just gone through this PR and "refreshed" it:

  • pointed PR to v6 instead of main
  • merged v6 into it
  • change the syntax etc, because the package had suffered some changes since it was first submitted;
  • ran composer update

And I can confirm it is working WONDERFULLY:
CleanShot 2023-05-31 at 15 12 09@2x

There's only one thing I didn't do, that's preventing me from merging this is. We don't have a stopping mechanism for the online demo 👀 @pxpm could you please prevent people from actually uploading, when this is in production? We can probably bind/intercept all uploaders in production, to prevent the uploads... right?! 🤷‍♂️

@tabacitu tabacitu assigned pxpm and unassigned tabacitu May 31, 2023
@pxpm
Copy link
Contributor Author

pxpm commented May 31, 2023

Thanks for moving this foward, was my next on line 🙏

We can probably bind/intercept all uploaders in production, to prevent the uploads... right?!

Will investigate the best course of action here.

@pxpm
Copy link
Contributor Author

pxpm commented Jun 13, 2023

This is pending Laravel-Backpack/medialibrary-uploaders#17

As soon as that gets merged I think we are good to go here.

@tabacitu can you give this a double check in the production constrains to don't upload files ?

Cheers

@pxpm pxpm assigned tabacitu and unassigned pxpm Jun 13, 2023
config/filesystems.php Outdated Show resolved Hide resolved
config/filesystems.php Outdated Show resolved Hide resolved
Copy link
Member

@tabacitu tabacitu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For UX and another layer of protection, I've made the upload fields disabled in production. For the repeatable I've chosen to hide it entirely.

I only want one extra thing out of this - for the mutator overrides to actually work, to prevent uploads instead of triggering an error.

I've added other ideas here, but those aren't urgent.

app/Models/Product.php Outdated Show resolved Hide resolved
Comment on lines +73 to +84
public function specifications(): Attribute
{
return Attribute::make(
set: function ($item) {
if (app('env') === 'production') {
return null;
}

return json_encode($item);
},
);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering... if the actual upload now... is being done with Uploaders, not Mutators... why are we using Mutators to catch and stop the upload process?

Why don't we bind to all our uploaders... and prevent the upload process from happening... for ALL uploaders, if the env is production? Wouldn't that be cleaner, and more secure? That way nobody can introduce a vulnerability in our demo, all Uploaders are prevented from working in production.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or maybe we can catch the ->withMedia() and ->withFiles() helper and do that there?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't urgent though. Just a thought. What I want right now is to get this merged and out of the way, ASAP.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can catch the withMedia and withFiles, with that we prevent the uploader events from beeing registered, so we wouldn't even need to bind into the uploader classes as they wouldn't be registered/called.

That wouldn't prevent much, just the upload from happening, but anything you submit in the input would be saved to the database, except it didn't go through the upload process.

At the moment we can't bind to our uploaders as they aren't resolved from the container, they are statically initialized.
We can work that out, or use the mutator as we are doing now. Everything is "working", except we always send empty values to the uploaders 🤷

Probably it's like you said, it's more future proof, but at the moment it's not a requirement.

Comment on lines +289 to +296
public function dropzoneUpload()
{
if (app('env') === 'production') {
return response()->json([]);
}

return $this->traitDropzoneUpload();
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same with dropzone. Can't we bind on the DropzoneOperation trait, and prevent all dropzone uploads in production? It's too broad in a normal app, but in our case that's what we want, to prevent all uploads.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not urgent - just a thought. Right now I just want to merge this ASAP.

Copy link
Contributor Author

@pxpm pxpm Jun 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you thinking something like this:

<?php

namespace App\Http\Controllers\Operations;

if (! env('APP_ENV') === 'production') {
    trait DropzoneOperation
    {
        public function dropzoneUpload()
        {
            return response()->json([]);
        }
    }
} else {
    trait DropzoneOperation
    {
        use \Backpack\Pro\Http\Controllers\Operations\DropzoneOperation;
    }
}

And then in demo we use App\Http\Controllers\Operations\DropzoneOperation instead of the one in the package ?

If you agree I can push this change.

Cheers

@tabacitu tabacitu assigned pxpm and unassigned tabacitu Jun 20, 2023
@tabacitu tabacitu added Size: S 1 day Size: M 1 week labels Jun 20, 2023
@tabacitu
Copy link
Member

Tested, works, merging 🎉

Let's postpone the other 2 suggestions, they're COULDs/SHOULDs. Added issues for them for later in the year:

@tabacitu tabacitu merged commit 2809e2d into v6 Jun 28, 2023
@tabacitu tabacitu deleted the add-spatie-media-library branch June 28, 2023 15:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

4 participants