Handling dynamic created files in play 2 - image

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

Related

Save image from webservice call in in local PCL folder Xamarin Forms

I have looked for many examples of this and I believe they are trying to do something different than what want to do.
I have a set of images stored a folder in my pcl. See Image attached.
PCL Image
Currently I have those images set to embedded resource and they all display in my List View when their name is called. However if an update is made on the server like an image is changed or replaced or added I would like the new image to get saved in the AllImages folder as shown in the image. Is this possible? I've seen a lot of people doing separate dependencies to store the images in the platforms resources/ drawable folder but I don't want I don't want to do that. If i have to change where i am storing my images I can do that as well, but I do need to be able to store the images from the server on my app locally.
I currently have a class dedicated to embedded images, so it would be nice to do it this, but I am not married to the idea.
[ContentProperty("ResourceID")]
public class EmbeddedImage : IMarkupExtension
{
public string ResourceID { get; set; }
public object ProvideValue(IServiceProvider serviceProvider)
{
if (String.IsNullOrWhiteSpace(ResourceID))
return null;
return ImageSource.FromResource(ResourceID);
}
}

How to access file stored in shared project in Xamarin Forms?

I have a folder called Documentation inside the shared project, named App2 in this case. How do I access the files stored inside the Documentation folder? Attached image below shows the project structure.
Visual Studio Solution Page
I have tried out following commands but they aren't working :
System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
AppDomain.CurrentDomain.BaseDirectory
System.Reflection.Assembly.GetExecutingAssembly().CodeBase;
If it's troublesome to access the file in that folder, I'm open to hearing other alternatives.
This is how I have done it for JSON files in my shared project (using PCL). As Jason pointed out in the comments, if you are using .NET Standard, you can simply define the GetSharedFile method in your shared project instead of creating platform specific references.
Add the file to the shared project and set it as Embedded Resource
Create an IFileHelper interface in your shared project
public interface IFileHelper {
Stream GetSharedFile(string fileResourceName);
}
Create a new FileHelper class in each project (Android and iOS) with the following
public class FileHelper : IFileHelper {
public Stream GetSharedFile(string fileResourceName) {
Type type = typeof(IFileHelper); // We could use any type here, just needs to be something in your shared project so we get the correct Assembly below
return type.Assembly.GetManifestResourceStream(fileResourceName);
}
}
Add a documentation handler class in your shared project. Something like below (make sure to change the App namespace to match yours):
public class Documentation {
private const string ResourcePath = "App.Documentation.index.html"; // App would be your application's namespace, you may need to play with the Documentation path part to get it working
public string GetDocs() {
IFileHelper helper = DependencyService.Get<IFileHelper>(); // Your platform specific helper will be filled in here
Stream stream = helper.GetSharedFile(ResourcePath);
using (stream)
using (StreamReader reader = new StreamReader(stream)) {
return reader.ReadToEnd(); // This should be the file contents, you could serialize/process it further
}
}
}
I wrote this mostly by hand so let me know if something is not working. If you cannot load your file, I suggest trying to put it into the root of your shared project and then changing ResourcePath in the code above to the following (again using your app's namespace instead of App):
private const string ResourcePath = "App.index.html";

Azure (C#) System.IO.FileNotFoundException: Could not find file

I've created a ML model with Visual Studio. I uploaded the web app to Azure with Visual Studio too. However, when I fill the fields for my ML model and click "run" on the website, I get this error which I copied directly from Azure App Service Editor.
I only get this error while trying to run the ML model on Azure website, if I run the web app on my computer I have no errors at all.
Thank you :)
The error:
2020-07-18 01:12:59.138 +00:00 [Error] Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware: An unhandled exception has occurred while executing the request.
System.IO.FileNotFoundException: Could not find file 'C:\Users\X\X\X\fileML.Model\MLModel.zip'.
File name: 'C:\Users\X\X\X\fileML.Model\MLModel.zip'
____________________
My code:
// This file was auto-generated by ML.NET Model Builder.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.ML;
using fileML.Model;
namespace fileML.Model
{
public class ConsumeModel
{
private static readonly Lazy<PredictionEngine<ModelInput, ModelOutput>> PredictionEngine = new Lazy<PredictionEngine<ModelInput, ModelOutput>>(CreatePredictionEngine);
// For more info on consuming ML.NET models, visit https://aka.ms/mlnet-consume
// Method for consuming model in your app
public static ModelOutput Predict(ModelInput input)
{
ModelOutput result = PredictionEngine.Value.Predict(input);
return result;
}
public static PredictionEngine<ModelInput, ModelOutput> CreatePredictionEngine()
{
// Create new MLContext
MLContext mlContext = new MLContext();
// Load model & create prediction engine
string modelPath = #"C:\Users\X\X\X\fileML.Model\MLModel.zip";
ITransformer mlModel = mlContext.Model.Load(modelPath, out _);
var predEngine = mlContext.Model.CreatePredictionEngine<ModelInput, ModelOutput>(mlModel);
return predEngine;
}
}
}
Nathan, welcome to the stackoverflow. Here is thing you are missing:
You are trying to access local path from your computer but on Azure there is no local machine so whenever you code tries to access the same path which you have hard coded it's resulting in error.
My recommendation would be to add your zip file to your project, once added right click on that file and mark Copy to Output Directory - Copy always. Please see below
This will help to get the local file path from output directory.
Now it's time to change your code to get file dynamically.
You can use
string directoryPath = Directory.GetCurrentDirectory();
string modelPath= Path.Combine(directoryPath ,"MLModel.zip");
This will get you the file path. Just do a test your code on your local and deploy the app.
The good thing is now your model file will get deployed along with your code. Every time you change your model just replace the file and deploy code again.
Hint to make it more dynamic:- You can also use Azure Blob Storage to keep your zip file, by using this you do not need to deploy your code again and again . Just need to replace the file in side blob.
you are trying to load file MLModel.zip from C:\Users\X\X\X\fileML.Model. Now that's your local computer path. That path not exists into Azure Web App.
There are 2 ways you can do if you really want to store in local directory:
HOMe environment variable in your Azure Web App that resolves to the
equivalent of inetpub for your site. Your app data folder is located
at %HOME%\site\wwwroot\AppData.
TEMP environment both on Azure Web Apps and on your local machine.
public static PredictionEngine<ModelInput, ModelOutput> CreatePredictionEngine()
{
// Create new MLContext
MLContext mlContext = new MLContext();
// Load model & create prediction engine
string directoryPath = Directory.GetCurrentDirectory();
string modelPath = Path.Combine(#"C:\Users\Admin\source\repos\ShanuASPMLNETML.Model\MLModel.zip","MLModel.zip");
ITransformer mlModel = mlContext.Model.Load(modelPath, out _);
var predEngine = mlContext.Model.CreatePredictionEngine<ModelInput, ModelOutput>(mlModel);
return predEngine;
}
}

How to unit test the download file name in 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

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

Resources