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

Inconsistent file naming behaviour local vs s3 #3751

Open
ryanmortier opened this issue Dec 19, 2024 · 2 comments
Open

Inconsistent file naming behaviour local vs s3 #3751

ryanmortier opened this issue Dec 19, 2024 · 2 comments

Comments

@ryanmortier
Copy link

ryanmortier commented Dec 19, 2024

I've built a simple file uploading example to learn the intricacies of AWS/S3.

I'm using Livewire and while I'm storing my media in S3, I've toyed with both local and S3 for the temporary upload location.

In doing so, I've noticed that when using the local file system for the temporary upload that the file name after storing the file in the media disk is the actual name of the file rather than some random string/hash. However, when using S3 as the temporary upload location, the file name is a random string/hash.

I know there are security implications when storing files as the original file names on local storage but perhaps this isn't an issue since it's still being stored on S3 but I'm curious why this behaviour is happening still.

For example when using S3 for both temporary uploads and the media disk:

Image

Image

Image

    public function save(): void
    {
        $this->validate();

        Auth::user()
            ->addMediaFromDisk($this->photo->getRealPath(), 's3')
            ->usingName($this->photo->getClientOriginalName())
            ->toMediaCollection('photos');

        $this->reset('photo');
    }

For example when using local for the temporary uploads and S3 as the media disk:

Image

Image

Image

    public function save(): void
    {
        $this->validate();

        Auth::user()
            ->addMedia($this->photo)
            ->toMediaCollection('photos');

        $this->reset('photo');
    }
@ryanmortier
Copy link
Author

ryanmortier commented Dec 19, 2024

After looking into this more, it appears that storing the file as its original name is the default behaviour and it's because the addMediaFromDisk() method is now taking the temporary file name and assuming it's the uploaded file's original name.

The problem though is that using addMedia() method doesn't work if using S3 as the temporary upload location and alternatively doing ->addMediaFromDisk($this->photo, 's3') doesn't work either.

Do you suggest any workarounds?

@timgavin
Copy link
Contributor

Why not just rename the photo when adding it? I use a random 12 character string and it works great.

auth()->user()
    ->addMedia($this->photo)
    ->usingName(str()->random(12))
    ->toMediaCollection('photos');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants