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

Cant delete files after using a customer FileNamer #3702

Open
innoflash opened this issue Aug 29, 2024 · 0 comments · May be fixed by #3717
Open

Cant delete files after using a customer FileNamer #3702

innoflash opened this issue Aug 29, 2024 · 0 comments · May be fixed by #3717

Comments

@innoflash
Copy link

Hello @spatie, great package you have here.

Its probably documented somewhere but I am having issues deleting obsolete files when I add new ones.
I defined a collection to keep only 3 files.

    public function registerMediaCollections(): void
    {
        $this->addMediaCollection('cover')
            ->onlyKeepLatest(3)
            ->useDisk('s3')
            ->acceptsFile(function (File $file) {
                return Str::of($file->mimeType)->contains('image');
            });
    }

I then created a custom file namer, I do not want my image and its conversions to share a name. A user can reverse engineer and I am trying to avoid that.

class FileNamer extends SpatieFileNamer
{
    public function conversionFileName(string $fileName, Conversion $conversion): string {
        $strippedFileName = pathinfo($fileName, PATHINFO_FILENAME);

        return md5($strippedFileName)."-{$conversion->getName()}";
    }

    public function originalFileName(string $fileName): string
    {
        $extLength = strlen(pathinfo($fileName, PATHINFO_EXTENSION));

        $baseName = substr($fileName, 0, strlen($fileName) - ($extLength ? $extLength + 1 : 0));

        return md5($baseName);
    }
}

Basically I am md5ing the original file name and the resultant file name so they cant be similar.
Examples are shown below.
image
image

The furthest I could go investigating was upto these lines below when deleting the conversions, still to inspect the other removers.

$imagePaths = array_filter(
$allFilePaths,
function (string $path) use ($conversions, $media) {
foreach ($conversions as $conversion) {
if (Str::contains($path, pathinfo($media->file_name, PATHINFO_FILENAME).'-'.$conversion)) {
return true;
}
}
return false;
}
);

According to L72, only deletable conversations are files with a naming pattern like filename-conversionname which is not whats not set on my new file namer.

One can write a new file_remover_class or wire an extra media delete observer but cant we have this natively in the package.

Thanks

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