Working with encrypted files in Laravel (how to download decrypted file)

前端 未结 2 779
广开言路
广开言路 2021-02-02 04:19

In my webapp, users can upload files. Before being saved and stored, the contents of the file are encrypted using something like this:

Crypt::encrypt(file_get_co         


        
相关标签:
2条回答
  • 2021-02-02 04:45

    Laravel 5.6 allows you to use streams for downloads: https://laravel.com/docs/5.6/responses#file-downloads

    So in your case:

    return $response()->streamDownload(function() use $decryptedContents {
        echo $decryptedContents;
    }, $fileName);
    
    0 讨论(0)
  • 2021-02-02 04:55

    You could manually create the response like so:

    $encryptedContents = Storage::get($fileRecord->file_path);
    $decryptedContents = Crypt::decrypt($encryptedContents);
    
    return response()->make($decryptedContents, 200, array(
        'Content-Type' => (new finfo(FILEINFO_MIME))->buffer($decryptedContents),
        'Content-Disposition' => 'attachment; filename="' . pathinfo($fileRecord->file_path, PATHINFO_BASENAME) . '"'
    ));
    

    You can check out the Laravel API for more info on what the parameters of the make method are. The pathinfo function is also used to extract the filename from the path so it sends the correct filename with the response.

    0 讨论(0)
提交回复
热议问题