Sopamo / laravel-filepond

Laravel backend module for filepond uploads
MIT License
202 stars 56 forks source link

Improve support for deleting files #42

Closed rivalex closed 2 years ago

rivalex commented 3 years ago

Change the delete method to correctly remove the uploaded file, eventually remove the empty folder and retrieve a comprehensive error code in case of server error.

public function delete(Request $request)
{
    try {
        $filePath = $this->filepond->getPathFromServerId($request->getContent());
        $path = pathinfo($filePath, PATHINFO_DIRNAME);
        File::delete($filePath);
        if (count(glob($path . '/*')) === 0) {
            rmdir($path);
        }
        return Response::make('', 200, [
            'Content-Type' => 'text/plain',
        ]);
    } catch (Throwable $e) {
        return Response::make($e->getMessage(), 500, [
            'Content-Type' => 'text/plain',
        ]);
    }
}
LTroya commented 3 years ago

Can be please merge this PR? I am getting a error 500 on the way it is now.

Because the upload method is returning the full path for the file, like var/www/project/storage/app/filepond/SOME_HASH/file.jpg.

Storage can only delete file with the path relative to the app project.

abrardev99 commented 3 years ago

This PR should merge

abrardev99 commented 3 years ago

It will through File Class not found Exception. Modify code like below:

public function delete(Request $request)
{
    try {
        $filePath = $this->filepond->getPathFromServerId($request->getContent());
        $path = pathinfo($filePath, PATHINFO_DIRNAME);
        \File::delete($filePath);
        if (count(glob($path . '/*')) === 0) {
            rmdir($path);
        }
        return Response::make('', 200, [
            'Content-Type' => 'text/plain',
        ]);
    } catch (Throwable $e) {
        return Response::make($e->getMessage(), 500, [
            'Content-Type' => 'text/plain',
        ]);
    }

}

Sopamo commented 3 years ago

@rivalex thanks for your contribution! We'd have to support different disks though for this to be able to be merged.

Right now it would only work with the local disk, could you change the implementation to use Laravel's Storage feature?

Sopamo commented 2 years ago

Closing this in favour of https://github.com/Sopamo/laravel-filepond/pull/53