rahulhaque / laravel-filepond

Use FilePond the Laravel way.
MIT License
185 stars 31 forks source link

Unable to manually delete temporary files. #21

Closed jameswong3388 closed 2 years ago

jameswong3388 commented 2 years ago

I had a job to create a "post", after the post is created all the temporary files should be deleted. But weird things happened, pls look at the codes.

public function handle(): void
    {
        $showcase = new Showcase([
            "uuid" => $this->uuid->toString(),
            "title" => $this->title,
            "description" => $this->description,
            "slug" => Str::slug($this->title),
        ]);

        $showcase->authoredBy($this->author);
        $showcase->save();

        $showcase_cover_image = Filepond::field($this->showcase_cover_image); // single upload
        $showcase_images = Filepond::field($this->showcase_images); // chunk upload 

        $showcase_cover_image->delete(); // this is not deleted 
        $showcase_images->delete(); // this is deleted
    }
rahulhaque commented 2 years ago

@jameswong3388 Set soft_delete to false in config/filepond.php

Use moveTo() or copyTo() method as shown in the example.

There is no need to manually call the delete method if you're not saving the file using API.

jameswong3388 commented 2 years ago

@rahulhaque, but I want to use spatie's laravel-medialibrary to handle the medias.

How can I integrate with that?

jameswong3388 commented 2 years ago

@rahulhaque, I have disabled soft delete in the config file and nothing changed.

And I updated the handle method

public function handle(): void
    {
        $showcase = new Showcase([
            "uuid" => $this->uuid->toString(),
            "title" => $this->title,
            "description" => $this->description,
            "slug" => Str::slug($this->title),
        ]);

        $showcase->authoredBy($this->author);
        $showcase->save();

        $showcase_cover_image = Filepond::field($this->showcase_cover_image)->getModel();
        $showcase_images = Filepond::field($this->showcase_images)->getModel();

        dump($showcase_cover_image->filepath);

        if ($this->showcase_cover_image) { // request->showcase_cover_image
            try {
                $showcase->addMedia($showcase_cover_image->filepath)->toMediaCollection("showcase_cover_image");
            } catch (\Exception $e) {
                dump($e);
            }
            $showcase_cover_image->delete();
        }

        if ($this->showcase_images) { // request->showcase_images
            try {
                $showcase->addMedia($showcase_cover_image->filepath)->toMediaCollection("showcase_images");

            } catch (\Exception $e) {
                dump($e);
            }
            $showcase_images->delete();
        }
    }
rahulhaque commented 2 years ago

@jameswong3388 Filepond::field($this->showcase_cover_image)->delete() and Filepond::field($this->showcase_images)->delete(). This method is for instantly deleting the temporary file. However, you can make use of filepond:clear command in schedule.

jameswong3388 commented 2 years ago

@rahulhaque, no problem I have got the problem solved 😂

rahulhaque commented 2 years ago

@jameswong3388 people tend to skip reading documentation. For spatie’s media library that works with laravel file object, getFile() method of this package should be useful but least used. Close the issue if done. Attach your implementation of media library for others if possible.

jameswong3388 commented 2 years ago

@rahulhaque, sure !

jameswong3388 commented 2 years ago

For those who want to integrate spatie's laravel-medialibrary, here is an example.

public function handle(): void
{
    $showcase = new Showcase([
        "uuid" => $this->uuid->toString(),
        "title" => $this->title,
        "description" => $this->description,
        "slug" => Str::slug($this->title),
    ]);

    $showcase->authoredBy($this->author);
    $showcase->save();

    $showcase_cover_image = Filepond::field($this->showcase_cover_image)->getModel(); // single upload
    $showcase_images = Filepond::field($this->showcase_images)->getModel(); // chunk upload

    // request()->showcase_cover_image
    if ($this->showcase_cover_image) {
        try {
            $showcase->addMediaFromDisk($showcase_cover_image->filepath, 'local')
                ->toMediaCollection("showcase_cover_image");
        } catch (\Exception $e) {
            dump($e);
        }

        Storage::disk('local')->delete($showcase_cover_image->filepath);
        $showcase_cover_image->forceDelete();
    }
    // request()->showcase_images
    if ($this->showcase_images) {
        try {
            foreach ($showcase_images as $showcase_image) {
                $showcase->addMediaFromDisk($showcase_image->filepath, 'local')
                    ->toMediaCollection("showcase_images");
            }
        } catch (\Exception $e) {
            dump($e);
        }

        foreach ($showcase_images as $showcase_image) {
            Storage::disk('local')->delete($showcase_image->filepath);
            $showcase_image->forceDelete();
        }
    }
}