Can you use a URL to embed an image inline in MVCMailer - image

In looking at the MVCMailer step by step, I see that you can embed images inline using cid in the HTML.
https://github.com/smsohan/MvcMailer/wiki/MvcMailer-Step-by-Step-Guide
I see the code that talks about embedding:
#Html.InlineImage("logo", "Company Logo")
var resources = new Dictionary<string, string>();
resources["logo"] = logoPath;
PopulateBody(mailMessage, "WelcomeMessage", resources);
My question is rather than use a path within the site like:
resources["logo"] = Server.MapPath("~/Content/images/logo.png");
Can I somehow get at images I have in the Azure Cloud or S3 Cloud where I have plenty of space?
Please let me know if there is a way to get the image from some other source than the server the MVC mailer is running on.
Thanks,
Victor

In Server.MapPath(my_path), my_path, specifies the relative or virtual path to map to a physical directory. Server.MapPath is related with local FileSystemObject so in that case it will look for the file (you pass as parameter) in a location within local web server. More info is described here:
http://msdn.microsoft.com/en-us/library/ms524632(v=VS.90).aspx
So if you image is located at http://azure_storage_name.blob.core.windows.net/your_public_container/image_name.jpg, the passing it to Server.MapPath() will not work as this is not a location in local file system. I tried and found that it will not accept and I also try to tweak a few way but no success. IF you can change the code behind and which can access the resources from a URL then it will work otherwise not.

Related

Can I serve files stored in Google Cloud Storage via a http.FileServer in golang?

I have developed a small web application that runs a web server in golang.
Each user can login, view the list of their docs (previously uploaded) and click on an item to view an html page that shows some fields of the document plus an tag with a src attribute
The src attribute includes an url like "mydocuments/download/123-456-789.pdf"
On the server side I handle the URL ("mydocuments/download/*") via an http Handler
mymux.HandleFunc(pat.Get("/mydocuments/download/:docname"), DocDownloadHandler)
where:
I check that the user has the rights to view the document in the url
Then I create a fileserver that obviously re-maps the url to the real path of the folder where the files are stored on the filesystem of the server
fileServer := http.StripPrefix("/mydocs/download/",http.FileServer(http.Dir("/the-real-path-to-documents-folder/user-specific-folder/)))
and of course I serve the files
fileServer.ServeHTTP(w, r)
IMPORTANT: the directory where the documents are stored is not the static-files directory I sue for the website but a directory where all files end after being uploaded by users.
My QUESTION
As I am trying to convert the code for it to work also on Google Cloud, I am trying to change the code so that files are stored in a bucket (or, better in "sub-directories" -as they do not properly exist- of a bucket).
How can I modify the code so to map the real document url as available via the cloud storage bucket?
Can I still use the http.FileServer technique above (if so what should I use instead of http.Dir to map the bucket "sub-folder" path where the documents are stored)?
I hope I was enough clear to explain my issue...
I apologise in advance for any unclear point...
Some options are:
Give the user direct access to the resource using a signed URL.
Write code to proxy the request to GCS.
Use http.FS with an fs.FS backed by GCS.
It's possible that a fs.FS for GCS already exists, but you may need to write one.
You can use http.FileSystem since it is an interface and can be implemented however you like.

Jar file's images are not displaying in the web

I made a jar file, and I have a problem with images when I run it in the web. (I'm using Amazon EC2)
Currently I think I'm lost at setting the path.
It works well on my local host when I set the path to something like img src= "/img/1.jpg".
So, with URL URL = getClass().getClassLoader().getResource("img/1.jpg"); I get a path that starts with jar:file~~ when I run it.
But on the web, it doesn't display the image. I just see a small square icon where the image is supposed to be.
Would this be a path related issue? Or something else?
How should I set the path to resolve the problem?
I'm a beginner and I'd appreciate any help. :)
see this.
Example
ClassPathResource resource = new ClassPathResource("application.yml");
InputStream inputStream = resource.getInputStream();

OWIN WEB API self host resolve path to folder

Form inside an ApiController. I need to read content of a file embedded inside the project. But I can't resolve the correct path
[HttpPost]
public HttpResponseMessage DoSomething()
{
String content = System.IO.File.ReadAllText(#"~\SomeFolder\file.txt");
}
Doing like this the resolved path point to:
C:...\bin\Debug\~\SomeFolder\file.txt
instead of
C:...\SomeFolder\file.txt
Does anyone have any idea how to solve this under OWIN Self Host?
In general, it does not make sense to try to access a file inside a project. All the files necessary for deployment (dlls, assets, txts,...) should be copied to a separated folder so that when we need to deploy, we just need to copy that folder.
You should set the file as Copy To Output and try:
String content = System.IO.File.ReadAllText(#"SomeFolder\file.txt");
which is resolved to
C:...\bin\Debug\SomeFolder\file.txt
To make the code work with OWIN, you could take a look at this answer How do you resolve a virtual path to a file under an OWIN host?. It suggests using HostingEnvironment.MapPath and falling back to manipulating file paths manually in self host scenario.

iText Add Image to PDF in a web application

I'm posting this for a friend. He is not able to access Stackoverflow from work (Third Party Cookies Appear To Be Disabled) :)
Ok here goes:
They have a Web application (JSP/Servlets/Custom Framework) and he is trying to generate a PDF on the fly. Now he wants to add images to that PDF. But it ain't working. Here is the piece of code:
Image image = Image.getInstance("../graphics/caution_sign.gif");
The graphics folder is on the parent project (webcontent/graphics/) and this is how they access the images from that folder in all other places (in the JSPs).
Now I read on another post that we need to use the real absolute path to access the Images. But the problem here is this is a POJO and there is no access to the servletContext in this class.
The PDF is generated fine, but the Image does not show and the error is:
C:\Program Files\IBM\SDP\runtimes\base_v7\profiles\was70profile1\..\graphics\caution_sign.gif (The system cannot find the path specified.)
It is trying to look for the "Graphics" folder in a different location instead of looking within the webcontent folder.
Hope my question is clear and would appreciate a lot if someone can help with this and help in resolving this issue
Thanks so much
Harish
He was able to solve it using this piece of code. Hope this helps someone else.
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
String path = classLoader.getResource("/graphics/caution_sign.gif").getPath();
Image image = Image.getInstance(path);
Thanks
Harish
Following code can be used to access image path inside a java class.
URL imageUrl = getClass().getProtectionDomain().getCodeSource().getLocation();
Image img=Image.getInstance(imageurl+"../../../some/images/pic.gif");
Small change from the previous solutions :
Step1: Below code will return the class path including class file name
URL classURL = getClass().getProtectionDomain().getCodeSource().getLocation();
Step2: Get the base path by removing class name
String basePath = classURL.getPath().replaceAll("<classname>.class","");
Step3: Navigate to the image location based on your project
Image headerLogo = Image.getInstance(basePath+"../../../../../../../images/header_logo.gif");

Provide url and store image from the url in a file in file system in groovy

I am doing a project in grails using groovy to upload an image. I have been able to fetch a file from my-computer and store it in a location and display it from there. I wish to accept a url from the user during run time and store the image in the locaion and display from there. Help please
Please consider Gregg's comment, but if you stay with your idea, this is the way I would do it:
new File("path/to/local/image.jpg").bytes = new java.net.Url("http://url.from.user/image.jpg").bytes

Resources