Remove "public/" from URL - ruby

The scenario
I have a script to upload a file to a directory and I need to return its path but without public/ prefix.
The upload destination is: public/uploads/, but I just need the uploads/ part.
The puzzle
I need public/ to upload the image because it is the physical directory which the image will be placed. So, how can I remove it (with a great approach) when I retrieve the path?
In practice
My final upload → /public/uploads/1.jpg
I want to retrieve → /uploads/1.jpg
Thoughts
I think I can remove the first part of the URL with the following snippet:
final_directory = directory.split('public/')[1]
But I don't feel comfortable by doing this.
Do you have anything better?

I think at the time of upload all files will be uploaded to the /public/uploads/ path.
When you fetch the file and show it, you can simply use the /uploads/ path. This is working for me.

Related

Is there some assistance with AJAX and PHP?

I am trying to use AJAX to return high-level calculation results. However, it appears as though the PHP file that I am pointing the AJAX call to is in the '/' root directory. Here is a snapshot of how my structure is: c:\webserver\test\webroot (this is the root directory), and then c:\webserver\test\code is where the HTML and PHP files are stored - looking to use an AJAX call to point to a PHP file in the non-root folder (which is one folder up, then one folder down). I tried a few different things, such as '../code' to move up and then over, but that doesn't work. Any suggestions?
Referencing a file location via relative paths should work - you'll just need to be sure that you are navigating FROM the correct folder TO the correct folder.
This can be a bit tricky if your javascript file is located in another folder - if memory serves you must navigate from the folder containing the javascript file, not from the folder containing the index file.
Alternately, you can use FQDM http://example.com/folder/folder/file.php to reference the file. Note that if you are on localhost, you can use a hosts file to fake-out the webserver.

Laravel Create Download Link To Storage

I have a file in /storage/excel/exports/abc.xls and I want to create a download link to it, so I wrote:
<a href="{{asset('storage/excel/exports/'.$file)}}" download>{{$file}}</a>
But when I click on the link, it cannot find the file. then I looked at href in Inspect Element and the link was http://localhost/[my-project]/public/storage/excel/exports/abc.xls
And when I cleaned public/ using Inspect Element and changed href value, the link worked fine and it downloaded the file.
I have different ways to solve this issue but i'm sure none of them is best practice.
Whats the best way to create a link to a file in storage folder in laravel?
If you're on Laravel 5.5 you can use the Storage class. You can then use Storage::url($file) and it should get the proper URL for your file.
However, as per the docs:
Remember, if you are using the local driver, all files that should be publicly accessible should be placed in the storage/app/public directory. Furthermore, you should create a symbolic link at public/storage which points to the storage/app/public directory.

Secure upload files in Laravel

I have a Laravel 5 project in which I am uploading files in database in Medium Blob format.
But uploading files in database takes some extra time to execute.
Uploading files in database is a secured way to keep files safe from crawlers or some bots.
I have tried to Upload files to the Public folder. But the crawlers can open these files.
Is there any possible way to upload files in the file system?
So that the Crawlers cannot open these files.
I want these files to be Secured
you can upload them outside of the public scope. For example, storage/ folder is a good place. Also, you can grab them using the file system manager. Take a look:
$image = \Storage::get('file.jpg');
Edit
A correct laravel installation just allow the content of public/ to be accesible via web browser. If other directories as storage/ or resources/ are public too, then you installation is really incorrect.
Said that, once you upload the files in storage/ folder nobody can access them except by you using the \Storage facade. When you call for example \Storage::get('file.jpg'); it returns an stream of bits that you can allocate them in a temporary folder and then display it in the webside. Once the request has finished, the image will disappear again from public domain.
No need to change the directory this can be achieved by two ways
LazyOne Answer using .htaccess
AND
Using robots.txt
I will suggest to implement both .htaccess and robots.txt as some cheap crawlers ignore robots.txt but they can't ignore .htaccess
You can follow this method
image-accessibility-for-authenticated-users-only
As this only allows authorized uses to view image

Read image files from non-default directory in PrestaShop 1.6

Hello Stack Overflow community, I have installed PrestaShop 1.6.0.14 in a Xampp server a few weeks ago for the company I work for. They have a web application that handles all the image files from their websites, for example, when a user opens an image with right click->open image in different tab or similar, the url shown is something like:
http://www.example.net/FileViewer/File.aspx?Type=3&File=WebsiteFileDirectory/img/logo.jpg&Width=138&Height=50
As you can see, after the &File= part comes a directory structure and some parameters to handle the image at the end. This directory (WebsiteFileDirectory) is located in C:/StorageFiles/Volume1/, where the web app for image handling looks for file sources.
My PrestaShop /img/ folder needs to be in that location with a directory structure similar to C:/StorageFolder/Volume1/PrestashopFileDirectory/img. So far, I've moved /img/ to that location and managed to make Prestashop write into the new location by editing the constant values _PS_IMG_, _PS_IMG_DIR_ and _PS_CORE_IMG_DIR_ in config/defines.inc.php and config/defines_uri.inc.php
But after that I've hit a brick wall when trying to make PrestaShop read from that location, I've even tried editing the LinkCore::getImageLink() method by hardcoding a uri_path value pointing to the directory mentioned before, with no results whatsoever, PS will try to read from the default location (something like myshop/16-small_default/image_filename.jpg)
How can I make PrestaShop read from that location? Is there even a way to do that?
Thanks in advance.
P.S.: Just to make sure I get a straight forward answer I want to let you know that by now I figured how the image file system in PS 1.6 works, if the image id is 25, PS will store the file in img/p/2/5/ for a product image, I have no problem with anything related to that.
Well, it turns out that getImageLink must be fed a URL (from the image handling webapp I mentioned before), not a filesystem path. So, the uri_path variable in this method would look like:
$uri_path = 'http://www.example.net/FileViewer/File.aspx?Type=3&File='.$my_img_directory.'&Width=138&Height=50';
The methods I have figured so far that need similar tweaking are getMediaLink and getCatImageLink, from the same Link class in the classes/Link.php file.

how to build image service website with sails.js

I am using sails.js to build an image service website. There are lots of images need be shown on the webpage. And the website will allow users to upload images as well. If I put it all images under assets folder, they will be copied to .tmp/public everytime when i restart the sails server . it will be very slow. Does anyone have an idea where should i put .Should I write a route rule to handle this? thanks.
/assets are for your static contents (like css, frontendjs, ...).
If you have a lots of images you should place them in one folder of your server and write a route for showing it.
Or better: Use some Cloud-Hosting like amazonS3 or imageShack.
Example for middleware:
1.) create a file "express.js" in config/
2.) Put this code into the file and change to the right path (routing + your local folder):
module.exports.express = {
customMiddleware: function(app){
app.use('/images', require('../node_modules/sails/node_modules/express').static('/User/yourfolder'));
}
}

Resources