Search code examples
phplaravellaravel-9

Make a file visible at view but not downloadable


I am showing a .stl file at my Blade view like:

var stl_viewer=new StlViewer(document.getElementById("stl_cont"), {
    auto_rotate:true,
    auto_resize: true,
    cameray: 30,
    allow_drag_and_drop:true,

    models: [ {
        id:0,
        //It can be direct link or something it doesn't matter.
        filename:"{{Storage::url($file->path)}}"
    } ]
});

HTML Code is:

<div id="stl_cont"  style="min-height: 400px;"></div>

When user inspects the page they can find the link and they can download the file directly.

I don't want this to happen.


Solution

  • Ok, I solved my issue with base64_encode function.

    First, I send the URL encrypted:

    $encrypted = base64_encode($file->path);
        return view('product', compact('product','encrypted'));
    

    Then I get data into a variable on-page javascript.

    var tboe  = '{{$encrypted}}';
    

    This variable becomes global and I get the variable from separated .js file like:

       //example
       var file = atob(tboe);
    

    I believe this makes the user reach the file hard. I can't find another way.