How can i get a file from storage without symlink? - laravel

I have a problem with storage.
How can I get a file from storage without symlink ?
When I try to get a video from file storage, to HTML video tag, I got a "forbidden" or "Not allowed to load local resource".
Controller :
$url = Storage::url($file);
return view('video')->with('url',$url);
HTML tag :
<video width="600" height="400" controls>
<source src="{{url($url)}}" type="video/mp4">
Your browser does not support the video tag.
Votre navigateur ne supporte pas le lecteur vidéo.
</video>

The idea of the storage is that it may reside in a place not accessible by your webserver, or at least nooit publicly accessible. It could be a folder, it could be an S3 storage, could be something else.
What you need to do is define a route to specifically download the video file. You can generate a temporary signed URL to that route in your controller code: https://laravel.com/docs/7.x/urls#signed-urls
Sent that signed URL to the view. Now the browser will load the video from the signed URL. You need to create the route and controller function to load the file from storage and sent it to the browser: https://laravel.com/docs/7.x/filesystem#downloading-files

Related

Heroku file upload success, but error when download

Hi i am building a laravel-livewire app on heroku
the app requires file upload and I'm using FileUpload of livewire, it works fine on local, but when I try it on heroku, it says upload successful, but when I download the file it gets "No file" message. I don't know where the error lies.
here is my source code:
_ In controller:
public function updatedFile()
{
// $this->validate();
$fileUpload = new File();
$fileUpload->url = $this->file->storeAs('public/files/' . auth()->id(), $this->file->getFilename());
$fileUpload->size_file = $this->getFileSize($this->file);
$fileUpload->file_name = $this->file->getClientOriginalName();
$fileUpload->model_name = $this->model_name;
$fileUpload->model_id = $this->model_id;
$fileUpload->admin_id = auth()->check() ? auth()->id() : null;
$fileUpload->save();
if ($this->model_id == null)
$this->list[] = $fileUpload->id;
}
_ In view
<a href="{{ $canDownload ? asset('storage/' . substr($val['url'], 7, strlen($val['url']) - 7)) : '#' }}" download>
<span class="d-block mb-0" style="word-break: break-all;">{{ $val['file_name'] }}</span>
<small class="kb">{{ $val['size_file'] }}</small>
</a>
```
The immediate issue may just be with relative vs. absolute paths.
But even once you resolve that you'll find that your uploads disappear frequently and unpredictably. This is due to Heroku's ephemeral filesystem.
To store uploads long-term you'll need to use a third-party service like Amazon S3 or Azure Blob Storage. It looks like Livewire supports this directly:
The previous example demonstrates the most basic storage scenario: Moving the temporarily uploaded file to the "photos" directory on the app's default filesystem disk.
However, you may want to customize the file name of the stored file, or even specify a specific storage "disk" to store the file on (maybe in an S3 bucket for example).
It also provides the following example:
// Store in the "photos" directory in a configured "s3" bucket.
$this->photo->store('photos', 's3');
And links to the relevant Laravel documentation, saying that Livewire uses the same API. Just make sure to configure an S3 bucket.
You can also completely bypass your server, having uploads go directly from the user's browser to your S3 bucket. This is particularly useful with large uploads.
Make sure to use the correct disk when building your download URL.

VPS Squid proxy, how to access a remote video from HTML

I've seen the video below on how to install Squid , but in the official doc I can't see how do I access a remote video in HTML using Squid.
I suppose I need to create a node but not sure how, and how do I reference it in my script:
HTML
<video id="fplayer" class="video-js vjs-default-skin"
class="video-js"
controls
preload="auto"
width="640"
height="264"
data-setup="{}"
>
<p class="vjs-no-js">
Please enable JavaScript ..</a>
</p>
<source src="my/vps/path/to/squid/node/myfile.mp4" type="video/mp4" />
// myfiles.mp4 is actually hosted here: mediafire.com/files/myfile.mp4
</video>
https://wiki.squid-cache.org/ConfigExamples ** Squid Doc
https://www.youtube.com/watch?v=C7BobNN4UJg ** SSH Install
Many thanks
I think Squid is not the right proxy to achieve what I need.
Express JS is a better tool, and can route the request to stream a video available in the server.
To stream a video from a remote server (eg.mediafire.com) we need to have access to the HTTP Headers (eg. mediafire.com) , which they're not willing to provide.

Sending file in source element for file in Azure Storage

I have an email template which uses
<P style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt"><IMG border=0 hspace=0 alt="" src="file://some image file path"></SPAN></P>
, however, now using a cloud based solution with the file to be stored in Blob storage.
If try to copy the path just get the following:
<Error>
<Code>ResourceNotFound</Code>
<Message>
The specified resource does not exist. RequestId:78b69839-201e-00ac-7c8f-e81311000000 Time:2019-04-01T13:35:28.8100122Z
</Message>
</Error>
Could download from storage using C# for an attachment, but don't know how can send the file by specifying in img element within the HTML template.
If the access level of your container is Blob or Container, just use the src like src="https://storageaccountname.blob.core.windows.net/containername/123.PNG".
If the access level is Private, you could not use the src above directly, otherwise, you will get a 404 error.
To fix the issue, you could generate a SAS token for the blob. Navigate to the azure portal -> find your image -> click ... -> Generate SAS -> specific the parameter and click Generate blob SAS token and URL. The Blob SAS URL is the src you need, it should be like src="https://storageaccountname.blob.core.windows.net/containername/123.PNG?sp=r&st=2019-04-02T04:09:45Z&se=2019-04-02T12:09:45Z&spr=https&sv=2018-03-28&sig=xxxxxxZA0g%3D&sr=b".
Result:
For more details about shared access signatures (SAS), you could refer to this link.

display image from database in react in laravel

How can i display image form database ? in react ? i am using laravel as backend .this is my table with image
i am using {this.props.obj.image} and this is showing the file as shown below
as you can see it is showing the file it is not displying image .searched web but it says to import path but all the tutorial are importing statically
my image location is Assets/Admin/Image .
How can i resolve this
When you run a laravel project, you are already in the public folder so you don't have to specify the public folder in the path and "assets" is just a laravel-defined method for accessing the contents of the "public" directory so you shouldn't specify that in the path too.
So you should do something like this:
<img src="http://localhost:<your_port>/images/{this.props.obj.image}" alt="hello image" height="200" />
You are storing just the name of the file in the database, to load the image you need to complete the full path of the image.
If you are storing your files in:
{laravel_root_folder}/public/assets/images/
Then you should do something like this (btw I know nothing about react but this should kinda work). I'm asuming you are running your server in localhost in the 8000 port:
<img src="http://localhost:8000/assets/images/{this.props.obj.image}" alt="hello image" height="200"/>
You can not have directly access to public/storage/images from react (front end side ) for that you have to use following method to generate the URL of image and then just return that url to react side while requesting and use that url in < img src={url} /> to display the picture
https://laravel.com/docs/5.4/filesystem#file-urls

the server responded with a status of 404 (Not Found) Failed to load resource

I am facing issue in accessing public folder of laravel.
I am on ubuntu 17.10.
I am trying to run the app on localhost.
I am accessing like this:
<img src="{{URL::asset('public/assets/img/person.jpg')}}"/>
Also when checking the its source , its path is shown correct :
<img src="http://localhost:8000/public/assets/img/person.jpg"/>
It may seem duplicate but I've tried most of the solutions present on net.
You are calling the wrong directory
Instead of
<img src="{{URL::asset('public/assets/img/person.jpg')}}"/>
Use this instead
<img src="{{asset('assets/img/person.jpg')}}"/>
Considering the assets folder is existing in public folder.
I hope this will solve you :)
Just in case file had cached on your browser try to add query string on your file by doing this so browser will treat it as a new file
You shoud use Storage::get() to retrive the file url. See the docs
anyway, you don't need that public in the path:
Switch
<img src="{{URL::asset('public/assets/img/person.jpg')}}"/>
to
<img src="{{URL::asset('assets/img/person.jpg')}}"/>

Resources