How to unit test the download file name in Laravel - laravel

I am developing a Laravel application. I have developed a file download feature in my Controller like this.
public function downloadUsagePdf()
{
return Storage::disk('default')->response('path/to/usage.pdf', 'new-file-name.pdf');
}
As you can see above, I am renaming the file. I like to unit test that the file is rendered with the new name. How can I unit test that? I am not downloading. I am just rendering on the browser first.

The way I made it work was by doing this in the ControllerTest file:
$header = $response->headers->get('content-disposition');
$this->assertEquals($header, "attachment; filename=new-file-name.pdf");
Hopefully someone knows an easier way to do this

Related

Laravel response helper file() method downloading, not displaying file

I am trying to display a file stored in storage folder using laravel's response helper. Unfortunately, every time it runs, the file is DOWNLOADED, not DISPLAYED. Below is my code:
public function viewDoc($id)
{
$a = pro_doc::findorfail($id);
$path = storage_path('documents/'.$a->file_name);
if ( ! File::exists($path) ) {
abort(404);
}
return response()->file($path);
}
Im sending an id to the controller, which looks up the record and gets the file name, then attempts to open the file in a new window (this is from a link with a blank target). A new window momentarily opens, then the download begins and the window closes.
The documentation (https://laravel.com/docs/5.4/responses#file-responses) seems to say this shouldn't happen
The file method may be used to display a file, such as an image or PDF, directly in the user's browser instead of initiating a download.
Can anyone help me make this work? Again, Im trying to get it to open in the browser window, not download. Thank you!

How to download files properly using FileSystemResource in Spring?

I have this following code for downloading files :-
#Controller
public class FileController {
#RequestMapping(value = "/files/{file_name:.+}", method = RequestMethod.GET)
#ResponseBody
public FileSystemResource getFile(#PathVariable("file_name") String fileName) {
return new FileSystemResource("C:/Users/sourav/fileServer/"+fileName);
}
}
When I go to the link for the first time nothing is displayed .When I reload only a text file with name f.txt is downloaded instead of the pdf file. I want the pdf file to be displayed in the browser. How to solve this problem ?
I think you need to set the response headers. Otherwise there is no way for the browser to intuit the file format. Something like response.setContentType("application/pdf");.
your code is ok. I think if you try with pdf file it will work as you expected, it will be displayed in browser. I tested it and worked fine in Chrome and Firefox. May be your testing file is corrupted.
If you are using Spring Boot, you can add the MIME types of the files you want to download into spring.mvc.mediaTypes properties in the configuration file. For example:
spring.mvc.mediaTypes.yml=text/yaml
Source: https://github.com/spring-projects/spring-boot/issues/4220

Handling dynamic created files in play 2

I wrote a small app that creates downloadable pdf files with play 2.0
I want to serve them to the public. On my development environment I created a folder in the
/assets/ folder and everything worked nice.
Now, when switching to production, I figured out that play always deployed those files behind my back.
Do I really have to write a own controller to serve those files or what is the way here ?
I've also had problems trying to serve files created dynamically with the assets controller. I don't know if it's for some kind of caching but I ended up writing my own controller, and now it woks perfectly.
I mean, I use the Assets controller for regular public files and for my dynamic generated files I use this one:
public class FileService extends Controller {
static String path = "/public/dynamicfiles/";
public static Result getFile(String file){
File myfile = new File (System.getenv("PWD")+path+file);
return ok(myfile);
}
}
And the routes would be like this:
GET /files/:file controllers.FileService.getFile(file: String)
GET /assets/*file controllers.Assets.at(path="/public", file)
It works great for me

How to upload a file in joomla?

Hi i am making a simple component in joomla having name image detail and i have to upload that image how can i upload image from backend. which one is better using extension or make custom. can you please share any good article for it. i have searched many more but due to lack of idea on joomla cannot find. hope you genius guys help me.
thanks i advance
Joomla Component for the exact scenario of your requirement will be very hard to find out. So you've two options:
1. Make your own component
2. Customize other similar type of component like gallery component
For uploading file from joomla component on admin if you're making your own component:
1. Just use move_uploaded_file php function.
2. copy this code, for joomla's standard fxn :
function upload($src, $dest)
{
jimport('joomla.client.helper');
$FTPOptions = JClientHelper::getCredentials('ftp');
$ret = false;
$dest = JPath::clean($dest);
$baseDir = dirname($dest);
if (!file_exists($baseDir)) {
jimport('joomla.filesystem.folder');
JFolder::create($baseDir);
}
if ($FTPOptions['enabled'] == 1) {
jimport('joomla.client.ftp');
$ftp = & JFTP::getInstance($FTPOptions['host'], $FTPOptions['port'], null, $FTPOptions['user'], $FTPOptions['pass']);
$dest = JPath::clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $dest), '/');
if (is_uploaded_file($src) && $ftp->store($src, $dest))
{
$ret = true;
unlink($src);
} else {
JError::raiseWarning(21, JText::_('WARNFS_ERR02'));
}
} else {
if (is_writeable($baseDir) && move_uploaded_file($src, $dest)) { // Short circuit to prevent file permission errors
if (JPath::setPermissions($dest)) {
$ret = true;
} else {
JError::raiseWarning(21, JText::_('WARNFS_ERR01'));
}
} else {
JError::raiseWarning(21, JText::_('WARNFS_ERR02'));
}
}
return $ret;
}
If you want to use other's component and edit it according to need, download it:
http://prakashgobhaju.com.np/index.php?option=com_showcase_gallery&view=items&catid=1&Itemid=64
Remember it's a gallery component.
Uploading any file be it an image on your Joomla site is something which is so simple, and can be done using either the web based FTP and or the desktop FTP services like filezilla but only when you have saved the file you want to upload. Using the web based way, you need to log in to your host for example 000webhost, locate the file manager option, click on it and enter your domain username and password. Then go to public_html folder , create a new folder for your photos or images and click on upload. Locate your image and click on the tick link to start uploading.
Using the desktop way, you will need to unzip your file to add to joomla, open your FTP client like filezilla, locate the file on local host, input your log in details as provided by your host and once you are logged in to your account through filezilla, locate where you want to add the file and click on upload.
You can find a similar tutorial with regard here http://www.thekonsulthub.com/how-tos/how-to-upload-joomla-with-filezilla-to-your-hosting-servers-cpanel/ {entire thing}
Please please please make sure you use the filtering available in the MediaHelper. Specifically never trust uploaded images, always check first that they are valid file types, then that they are in the list of approved types of files listed in your global configuration, that the names do not contain html or javascript, and that the files themselves do not contain code. In particular I would recommend the MediaHelper::canUpload method which will check the majority of these things for you. If anything you should be checking even more strongly. Also make sure that you are checking whether the user has permission to upload. If anything you should make the checking even more restrictive. Use the APIs that joomla gives you, such as the built in media field.

Monodroid emulator retrieving location problem

I am having difficulty getting my Monodroid application retrieve a location when I run it in an emulator. My code looks something like this:
LocationManager locationsManager = (LocationManager)threadSurfaceView.Context.GetSystemService(Context.LocationService);
location = locationsManager.GetLastKnownLocation(Android.Content.Context.LocationService);
However, this always seems to return null. Do I have to configure the emulator in some way so that it has a locaion service and/or location?
I also tried adding a LocationListener:
locationListener = new MyLocationListener();
locationsManager.RequestLocationUpdates(LocationManager.GpsProvider, 120000, 0, locationListener);
But the problem I had here was to create the MyLocationListener class. I implemented the four public methods (OnLocationChanged(Location) and so on), but Visual Studio complained about a missing Android.Runtime.IJavaObject method - does anyone have a simple example of a class that implements ILocationListener?
Thanks for any help. Martin
I got a kindly link to a sample at
https://github.com/gshackles/Sample-Projects/blob/master/MonoDroid/MonoDroidSamples/MonoDroidSamples/DemoActivities/LocationDemo/LocationActivity.cs
Which works fine on my Nexus S but still will not work on the emulator - so (perhaps) the issue is not a code issue.
I liked the technique in this sample of getting the "Activity" to implement ILocationListener as well as Activity - a neat solution to getting data back out of the "listener" function which had been giving me headaches when it was wrapped into another class.
I downloaded the bundle of samples and if you do the same then you will need to grab an mp3 file - rename it volbeat.mp3 and add it to the "raw" folder within "Resources" as it is missing.
partial answer as I am working on the same problem.
Your listener class that implements ILocationListener needs to be declared like
public class myLocationListener : Java.Lang.Object, ILOcationListener
{
//plus the public functions you identified
}
My attempt looks like this:
Android.Locations.Location iAmHere;
LocationManager myLoc = (LocationManager)GetSystemservice(Context.LocationService);
iAmHere = myLoc.GetLastKnownLocation(Android.Content.Context.LocationService);
but iAmHere is null on the emulator even when I have used TelNet to push a geo fix location through

Resources