Designing a service for reading files - asp.net-mvc

Conceptual Overview
Store templated text files on the file system. Read text file into a templating service, get the string, send the email.
I have 2 services so far:
ITemplateService - this is the templating engine (accepts model and the string for templating).
IEmailService - Send email.
Can I have an example of how a file reader service would look, how I can mock or unit test it. Thanks.
I basically want to pass a path/filename to a method and let it read the file and return a string.

System.IO.File.ReadAllText(string filePath) returns the contents.

You may also checkout Postal.

Related

Umbraco - Encode URL following the same rules as defined in the request handler settings

In Umbraco, i want to generate URL encoded string as it was a genuine Umbraco path.
So if i have the following relative path:
/products/4/drinking glass
I want it encoded like this:
/products/4/drinking-glass/
As it would if i used NiceUrl on a published content object.
Following the same rules as described in the request handler part of umbracoSettings.config.
Is there a way to do that?
I found the answer after searching a bit more.
Umbraco made an extension method for this purpose:
"drinking glass".ToUrlSegment()
Which provides a safe way to generate custom url encoded strings.

How to validate a file as image on the server before uploading to S3?

The flow is:
The user selects an image on the client.
Only filename, content-type and size are sent to the server. (E.g. "file.png", "image/png", "123123")
The response are fields and policies for upload directly to S3. (E.g. "key: xxx, "alc": ...)
The case is that if I change the extension of "file.pdf" to "file.png" and then uploads it, the data sent to the server before uploads to S3 are:
"file.png"
"image/png"
The servers says "ok" and return the S3 fields for upload .
But the content type sent is not a real content type. But how I can validate this on the server?
Thanks!
Example:
Testing Redactorjs server side code (https://github.com/dybskiy/redactor-js/blob/master/demo/scripts/image_upload.php) it checks the file content type. But trying upload fake image (test here: http://imperavi.com/redactor/), it not allows the fake image. Like I want!
But how it's possible? Look at the request params: (It sends as image/jpeg, that should be valid)
When I was dealing with this question at work I found a solution using Mechanize.
Say you have an image url, url = "http://my.image.com"
Then you can use img = Mechanize.new.get(url)[:body]
The way to test whether img is really an image is by issuing the following test:
img.is_a?(Mechanize::Image)
If the image is not legitimate, this will return false.
There may be a way to load the image from file instead of URL, I am not sure, but I recommend looking at the mechanize docs to check.
With older browsers there's nothing you can do, since there is no way for you to access the file contents or any metadata beyond its name.
With the HTML5 file api you can do better. For example,
document.getElementById("uploadInput").files[0].type
Returns the mime type of the first file. I don't believe that the method used to perform this identification is mandated by the standard.
If this is insufficient then you could read the file locally with the FileReader apis and do whatever tests you require. This could be as simple as checking for the magic bytes present at the start of various file formats to fully validating that the file conforms to the relevant specification. MDN has a great article that shows how to use various bits of these apis.
Ultimately none of this would stop a malicious attempt.

Passing values from C# to VBA code?

I am pretty new to VBA, sorry if this is simple question or already answered... I tried by searching on MSDN but did not get any thing to implement this.
I am in need of sending a string (Host name in URL, e.g. xyzserver:4500/Home/GetExcelData) to VBA code before downloading the Excel to User.
I am calling MVC action method from Excel VBA code, getting some data and displaying in Excel.
My problem is Host name (xyzserver:4500) is different in different servers, I need to update the URL dynamically based on the server URL that the user is accessing from.
Basically i need to send server name to VBA code to update the URL in module. Is there any way to send and maintain values from C# (MVC Action) to VBA?
Session/ Application Cache kind of things are available in VBA?
Thanks in advance
You can pass a parameter from c# to VBA in this manner:
Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
Excel.Workbook wb = app.Workbooks.Open("your file here which contains the macro");
string url = "blah blah";
app.Run("name of macro here", url);
If you want to edit the vba code programmatically, then you would need to reference the VBIDE. Have a look at Chip Pearson's excellent website: http://www.cpearson.com/Excel/vbe.aspx

ASP.NET MVC Read files from server

I have 3 questions:
Where is the correct place to put some template files? I'll be using these templates to render emails with DotLiquid. I'm thinking about just having it at ~/Templates/.
How do I unit test this? Should I even unit test reading files from the file system?
Best way to read the file into a string?
I would make a views folder for them /Views/Emails perhaps
Unit test code that you write, not code from the .NET framework imo
string s = System.IO.File.ReadAllText( path );
Check out this blog entry, which talks about how to send emails using a view as a template: ASP.NET MVC 2 Render Template to String.
In short, you create a method that renders the View into a string and then call that method from an action to generate the email body content.
Placing the code in ~/Content/Templates/ and downloading the content using a web client worked best for me.
var welcomeMailTemplatePath = "yourPath";
var webClient = new WebClient();
string html = webClient.DownloadString(WelcomeMailTemplatePath);
This way, you don't need to deal with controllers/views and can directly grab the contents of the template file.

asp.net MVC Read from a http URL

i am trying to use this code:
<%= File.ReadAllText(Server.MapPath("Members/newsletters/welcome.html"))%>
which works great but now the welcome.html file has moved onto another server so i need to read it from an external URL .
any suggestions?
Have a look at the HttpWebRequest class in .NET (there's an example at that URL or have a look at this blog).
try this:
WebClient WebClient = new WebClient();
string YourContent = WebClient.DownloadString(YourUrl);
You need to be more specific. Are you trying to read it from an UNC path or over the web? Is the other on your network?
If UNC path, probably easiest to map a drive on your local server so it looks like a local file.
Of course, I'm wondering why you're doing it this way at all. Why not replicate the content to all web servers? Reading over network is expensive. Also, that logic should probably be encapsulated in a helper method which can handle caching. You might consider moving the logic to retrieve that content to your controller. A view should render the model data given to it.

Resources