laravel local storage 404 error on images - laravel

I have made a youtube style website, some people may click the video to be private, I keep these videos in storage/app/vidoes/{channelname}, the public images and videos I have working not a problem but local storage I am having a problem with, any ideas?
just getting a 404 on the thumbnails
thanks
view
<img src="{{ route('home.image', ['file_path' => 'videos/' . $video->channel_name . '/thumbnails/' . $video->video_thumbnail_name]) }}" alt="" class="img-responsive">
route
Route::get('{file_path}', [
'uses' => 'HomeController#getImage',
'as' => 'home.image'
]);
controller
public function getImage($filepath)
{
$fileContents = \Storage::disk('local')->get($filepath);
$response = \Response::make($fileContents, 200);
$response->header('Content-Type', "image/jpg");
return $response;
}

Laravel 5.8 local storage working for images
view
<img class="home-video" src="{{ route('getImage', ['thumbnail' => $video->video_thumbnail_name, 'channel_name' => $video->channel_name]) }}" alt="" >
Route
Route::get('get-image/{channel_name}/{thumbnail}','HomeController#getImage')->name('getImage');
Controller
public function getImage($channel_name,$thumbnail)
{
$fileContents = \Storage::disk('local')->get("videos/$channel_name/thumbnails/$thumbnail");
return \Image::make($fileContents)->response();
}
see how the thumbnail is important for me, I was passing the thumbnail name eg photo.jpg if you are using route model binding you would want to pass the id of the image, hope this saves someone a few hours

Related

Laravel protect audio file from direct url and show on blade view

I have a form with uploading audio mp3.
I need to protect these audio files from direct access from url, accessible only logged users.
Added a new storage disk in > filesystem.php
'audio' => [
'driver' => 'local',
'root' => storage_path('app/private/audio/'),
],
Added a new route in > web.php
Route::get('/private/audio/{audio}', [AudioController::class, 'index']);
AudioController
public function __construct()
{
$this->middleware('auth');
}
public function index($audio)
{
$path = "/private/audio/{$audio}";
if(Storage::exists($path)) {
return Storage::download($path);
}
abort(401);
}
Now if i get on follow url, my audio will download only for logged users, so works fine.
https://example.com/private/audio/my_audio_file.mp3
So how can I access to audio file in a blade view for logged users only.
#foreach($files as $file)
<audio preload="none" src="{{ asset('audio/private/') }}/{{ $file->file_name }}.mp3" controlslist="nodownload" type="audio/mp3">
</audio>
#endoforeach
Is there a way for get this?
It works fine after doing the optimize command:
php artisan optimize
#foreach($files as $file)
#if(Auth::check())
<audio preload="none" src="{{ asset('audio/private/') }}/{{ $file->file_name }}.mp3" controlslist="nodownload" type="audio/mp3"></audio>
#else
Login to access audio
#endif
#endforeach

Simple qrcode to generate in DOMPDF

Hi i cant generate simpleqrcode in dompdf
Here is my blade.php
<img src="data:image/png;base64, {!! base64_encode(QrCode::format('svg')->size(200)->errorCorrection('H')->generate('string')) !!}">
in my controller
public function printpdf($isbn)
{
$data = [
'isbn' => $isbn
];
$pdf = PDF::loadView('main.inventory.view_pdf ', $data);
return $pdf->stream($isbn.'.pdf');
}
i tried this image and successfully render
<img src="{{ public_path('/uploads/image/1578635297.jpg')}}" style="width:30%;height:50%;">
Dont know why i cant generate qrcode to dompdf but when i generate in other blade its working but not in dompdf
I got mine working by initializing the base64_encode(QrCode::format('svg')->size(200)->errorCorrection('H')->generate('string')) in a variable within a controller, and passing it to the view.
Controller:
public function foobar() {
$qrcode = base64_encode(QrCode::format('svg')->size(200)->errorCorrection('H')->generate('string'));
$pdf = PDF::loadView('main.inventory.view_pdf', compact('qrcode'));
return $pdf->stream();
}
View:
<img src="data:image/png;base64, {!! $qrcode !!}">
Used laravel-snappy instead of laravel-dompdf. https://github.com/barryvdh/laravel-snappy
If you're still unable to generate a pdf file, then temporarily change the read-write permissions of the folder with chmod -R 777 <folder_path>.

Using Laravel, how to get an image from an s3 private bucket via controller action

Using Laravel, I want a unified get url for all images. The images are stored in a private s3 bucket.
My current controller action:
public function getImage(Request $request) {
$file = Storage::disk('s3')->temporaryUrl($request->filename, now()->addMinutes(5));
return response()->file($file);
}
In my view I am referencing this like:
{{ route('image', ['filename' => $user->meta->profile_image_url]) }}
The error I am get is: Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException: ...
The problem is that if I just drop the url access directly into the view it works fine. I have done things like:
{{ Storage::disk('s3')->temporaryUrl($filePathFromDB, now()->addMinutes(5)) }}
Again, trying to unify my access to files.
--
I have worked on this some more and I have landed on an updated controller action:
public function getImage(Request $request) {
$adapter = Storage::disk('s3')->getDriver()->getAdapter();
$command = $adapter->getClient()->getCommand('GetObject', [
'Bucket' => $adapter->getBucket(),
'Key' => $adapter->getPathPrefix(). '' . $request->filename
]);
$img = $adapter->getClient()->createPresignedRequest($command, '+20 minute');
return response()->file($img->getUri());
}
the issue is that this returns an image url that has & and "
I have tried htmlspecialchars_decode
How do I get the url to respond clean?
You can easily access the S3 private content from the controller. You can find the solution here:
Access s3 bucket private content
In your view
{{ Html::image("$user->meta->profile_image_url", '', array('class' => 'img-responsive')) }}
in your user model
public function getProfileImageUrlAttribue {
if(Storage::disk('s3')->exists($this->meta->profile_image_url)) {
return $this->meta->profile_image_url;
}else {
//....
}
}
I know you want to do it in the controller, but still the same logic use Storage::disk('s3')->exists->
Laravel filesystem

Laravel Dropzone.js some photos cannot be saved into the folder

I am using dropzone.js to allow the users to upload multiple photos to the database. Before I set up the database for the image uploader feature, I wanted to see if the image can be saved into my desired folder. However, after successfully uploading 3 photos(since the tick logo was shown right after the image was uploaded, I assumed the image has been successfully uploaded), only 2 of them were present in the folder.
In the following codes I have listed the view(including javascript and html codes), route and controller file:
CSS ->
{{html::style('https://cdnjs.cloudflare.com/ajax/libs/dropzone/5.1.1/min/dropzone.min.css')}}
HTML ->
{!! Form::open(array('route' => 'gallery.images.upload', 'class' => 'dropzone', 'enctype' => 'multipart/form-data','files' => true, 'id' => 'addImages', 'method' => "POST")) !!}
{{ Form::hidden('gallery_id', $gallery->id) }}
{!! Form::close() !!}
Javascript ->
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/dropzone/5.1.1/min/dropzone.min.js"></script>
<script type="text/javascript">
Dropzone.options.addImages = {
maxFilesize: 5,
acceptedFiles: 'image/jpeg,image/png,image/gif',
paramName: "file",
}
</script>
Route ->
Route::prefix('gallery')->group(function(){
Route::post('/images/upload', 'GalleryController#uploadGalleryImages')->name('gallery.images.upload');
});
Controller ->
public function uploadGalleryImages(Request $request)
{
if ($request->hasFile('file')) {
// Get the file from the file request
$file = $request->file('file');
// set my file name
$originalName = $file->getClientOriginalName();
$filename = uniqid() . $originalName;
$file->move('galleries/images/', $filename);
}
else{
return redirect()->route('gallery.view');
}
}
I have been asking around for several weeks but couldn't get any answer. I really need your help!
Screenshot of images being successfully uploaded
Screenshot of images present in the folder
This happened to me once because the server when going through the images it overwrites some images because they get the same filename, to avoid this use something like this to make sure you have unique filenames:
$random = mt_rand(1,1000);
$filename = time().''.$random. '.' . $file->getClientOriginalExtension();

Laravel5.2 delete doesn't work

I developed website with CRUD on products table .this is the structure of the table.
Create and update works fine But delete not work.
This is the form in blade to delete product
{{ Form::open(array('url' => 'admin/products/' . $product->id, 'class' => 'pull-right')) }}
{{ Form::hidden('_method', 'DELETE') }}
{{ Form::submit('Delete ', array('class' => 'btn btn-warning')) }}
{{ Form::close() }}
And this the destroy function in controller
public function destroy($id)
{
$product = Product::find($id);
$product->delete();
// Product::destroy($id);
return redirect('admin/products')->with('message', 'Successfully deleted the product!');
}
And This is my routes
Route::group(['middleware' =>'App\Http\Middleware\AdminMiddleware'], function () {
//resource
Route::resource('admin/products','AdminFront');
});
When I click delete button it enter the destroy function and dd($id) correct
But when write
$product = Product::find($id);
$product->delete();
Or
Product::destroy($id);
I get this error
The localhost page isn’t working
localhost is currently unable to handle this request.
This error tired me . I developed delete fun with resource API in another table and work fine.I don't know are the problem in the db or where. please any one help me ,
What does your routes.php look like?
You may need to include the resource route in routes.php.
Route::resource('admin/products/', 'TheNameOfYourController');
But make sure the route is protected either in the controller or routes.php.
Here is somewhat the same setup you have:
https://github.com/jeremykenedy/laravel-material-design/blob/master/app/Http/routes.php LINE 119
https://github.com/jeremykenedy/laravel-material-design/blob/master/app/Http/Controllers/UsersManagementController.php LINES 369-376
https://github.com/jeremykenedy/laravel-material-design/blob/master/resources/views/admin/edit-user.blade.php LINES 243-246
Cheers!

Resources