Laravel image cache using Dropbox as disk - laravel

I'm using this package (https://github.com/spatie/flysystem-dropbox) to storage and get images from Dropbox.
This works fine but images have to load everytime the page is refreshed. I wonder if you know any image cache solution that works in this case and if can please provide with a minimal working example.
Thanks.

One way to solve this would be to create your own caching system. If the image doesn't exist on your local file system, pull it from Dropbox and then save it to the local file system and serve it. If it already exists in the local file system just serve it from the local file system.
1 Route
Serve the images from their own route.
Route::get('images/{filename}', [
'uses' => 'ImageController#getImage'
]);
2 Controller
Check the local file system to see if the file already exists, otherwise pull it from dropbox and store it in the local file system.
<?php
namespace App\Http\Controllers;
class ImageController extends Controller
public function __construct()
{
parent::__construct();
}
public function getImage($filename)
{
// If the file doesn't exist
if(!file_exists('/path/to/' . $filename)) {
// 1. Get the image from dropbox
// 2. Save the image to local storage
}
// 3. Serve the image from local storage
}
}

Related

Path for images for Laravel project from cpanel

I have a problem with displaying pictures in View for my project in Laravel..destination path in my application where the photos were saved is: public_path (/ images) but recently I put the project on the server in cpanel, and to get rid of / public from the url, I used a google article, and I made a folder called laravel outside the public_html folder, in which I keep the application files and in public_html I keep only the files that were in the public folder of the application. In View I display the pictures as follows: "{{asset ('images /'. $ Img-> file_name)}}" .. Only it is no longer displayed. Pictures are saved but no longer displayed. I mention that it is saved in the Laravel folder which is outside the public_html folder..and I think that public / images was created automatically there..I would like to know if anyone has faced such a thing and what is the solution. thank you
I solved the problem like this:
In AppServiceProvider in register() I put this code:
$this->app->bind('path.public', function () {
return base_path('../public_html');
});
and now I have public_html/images and there my images are saved..and the rest of myy app, outside public_html folder in a different folder named laravel_project
In Laravel 8, I solved it adding this to the /app/Providers/AppServiceProvider.php in register method:
public function register()
{
$this->app->bind('path.public', function () {
return base_path('../public_html');
});
}
But in order to create the symlink from the "php artisan storage:link" command, I also had to add this code to /bootstrap/app.php, just after the last singleton:
$app->bind('path.public', function () {
return base_path("../public_html");
});
Finally I used this code to get the image that will be sent to my blade template:
$image=Storage::disk('public')->url('path_to_image');

Laravel deployed to shared hosting not displaying image

I deployed a laravel app to shared hosting (hostinger). Everything is working fine except the images which are not showing up.
I have created a symlink of my storage folder with my public_html folder. Files uploaded enters the public folder but when I link the images they still do not show up.
I have created a symlink to the public_html since I cannot access the public folder. I need help on this please
I need the image to be displayed on the browser
In default Laravel deployment the symlink is created in public folder as storage and links to ../storage/app/public.
In order to debug your problem, please have a look at the developer tools of your browser to find out whether it is a 404 or 403 problem (or even another).
Please check for correct permissions. Otherwise you may change the physical storage path to something inside the public folder. This avoids the requirement to create a symlink. Keep in mind to protect non-public storage via .htaccess for example.
You have to the path to your public assets directory if it is not public directory in the root folder. In your case, the public assets directory is public_html. So please bind the path to it from the root folder.
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* #return void
*/
public function register()
{
if (App::environment('prod')) {
$this->app->bind('path.public', function() {
return base_path('../../public_html');
});
}
}
}
had the same problem, remember to run 'php artisan config:cache' as per Laravel Deployment guidelines.
Running the command creates a general configuration file
in app/bootstrap/cache/config.php with all settings including directories and links that you should update

Understanding file storage and protecting contents Laravel 5

I need help understanding Laravel's file storage system. I'm creating an app where I need to upload a users drivers license photo. This is obviously sensitive information. I would like to keep this image off of the internet unless an admin is viewing it. My understanding is that i have to save things to the public directory in the storage>app>public and create a symlink to the public>storage folder. I have this done and it's working fine. I save the path to the database and the program can display it, but I don't want things to be in the public folder if I can avoid it.
Is there any way to save an image/file to a private folder in the storage system then access it through the view? If I do this, does it keep that image "private" so that it's not stored in a cache somewhere beings that it's not in a "public" folder? Does the symlink protect files in the storage folder in the way I'm wanting or does it truly make all files available to the public?
Any help in understanding this would be appreciated.
What you've said is correct the files in storage/app/public is public. This is why you have to create a private directory, lets say storage/app/private, then upload your sensitive files here.
You may want to add a disks in your config/filesystems.php:
'private' => [
'driver' => 'local',
'root' => storage_path('app/private'),
],
If you want to access your private files. Create a route for this:
Route::get('/private-files/{file?}','FileController#get')->where('file', '(.*)');
Then in the FileController.php, you will have something like this (this is just an example, edit the code here to check if the user is admin):
<?php
namespace App\Http\Controllers;
use Auth;
use Storage;
use App\Http\Controllers\Controller;
class FileController extends Controller {
public function __construct()
{
$this->middleware('auth');
}
public function get($file)
{
return Storage::disk('private')->get($file);
}
}

How to control access to files at another server in Laravel

I have a host for my Laravel website and another (non-laravel) for stored files. Direct access to my files are blocked completely by default and I want to control access to them by creating temporary links in my Laravel site. I know how to code, just want to know the idea of how to do it (not details).
From the Laravel docs
Temporary URLs For files stored using the s3 or rackspace driver, you
may create a temporary URL to a given file using the temporaryUrl
method. This methods accepts a path and a DateTime instance specifying
when the URL should expire:
$url = Storage::temporaryUrl(
'file.jpg', now()->addMinutes(5)
);
You could also make your own solution by directing all image request through your own server and making sure the file visibility is set to private.
Here is an example of how a controller could return image from your storage
public function get($path)
{
$file = Storage::disk('s3')->get($path);
// Do your temp link solution here
return response($file, 200)->header('Content-Type', 'image/png');
}
What i am using right now is Flysystem provided in laravel.Laravel Flysystem integration use simple drivers for working with local filesystems, Amazon S3 and other some space provide also. So for this doesn't matter whether is a server is laravel server or not.
Even better, it's very simple in this to switch between server by just changing server configuration in API.
As far as I know we can create temporary Url for s3 and rackspace in this also by calling temporaryUrl method. Caching is already in this.
That's the thing.
If your files are uploaded on an AWS S3 server
then,
use Storage;
$file_path = "4/1563454594.mp4";
if( Storage::disk('s3')->exists($file_path) ) {
// link expiration time
$urlExpires = Carbon::now()->addMinutes(1);
try {
$tempUrl = Storage::disk('s3')->temporaryUrl($file_path, $urlExpires);
} catch ( \Exception $e ) {
// Unable to test temporaryUrl, its giving driver dont support it issue.
return response($e->getMessage());
}
}
Your temporary URL will be generated, After given expiration time (1 minute). It will expire.

Laravel Route to Standalone WebApp

I am trying to build a portal in Laravel to serve some other, standalone web apps (not built in Laravel), but I am struggling to find out how to route to these apps if I want to place them outside the public folder.
In the past, I would use (temporary) symlinks for this kind of things, but I was wondering if Laravel provides another solution.
So, I have a folder:
module
module/index.php
module/js/whatever
module/css/whatever
module/img/whatever
and I want a route /modules/1 to link to index.php in the module-folder in such a way that the resources in this folder (js/css/img) are also accessible.
Any suggestions?
You can include other PHP files with require_once.
web.php
Route::any('/webapp/{assets?}', 'WebAppController#index');
WebAppController
class WebAppController {
public function index(Request $request) {
require_once '../module/index.php';
if ($request->assets) {
// check from session if user is logged in
// require asset as well
// (or download them https://laravel.com/docs/5.5/responses#file-downloads)
}
}
}
http://php.net/manual/en/function.require-once.php

Resources