How to embed images in gorilla-repl? - clojure

Is it possible to embed image in markdown cell in gorilla-repl?
I would like to keep images in src folder in the root of leiningen project and be able to embed images into my worksheet with:
![plot 1](img/plot_1.png)

This does not look possible using the envisioned incantation.
The markdown implementation looks to be done in the browser in javascript, using codemirror, which for security reasons does not have the ability to load resources from the local file system when the origin is http://localhost.
It can, however, load resources over http, so using an http url for the resource in the markdown expression works.
One could conceivably teach gorilla to listen on a route that resolved to the src/ or resources/ directory on the local file system, but that would break if the worksheet were shared.
There would need to be a mechanism to turn the image data into a data:// url (with all the requisite limitations that entails) so it could be packaged with rendered worksheet html.

Related

Content negotiation with Django staticfiles in runserver

I'm managing static files (JS, CSS, images, etc) in my Django application using staticfiles. This works fine, but I'd like to start dynamically serving pre-compressed sources when the user's browser is capable.
I went through the linked tutorial and, in production (on Apache) this works fine. I can include files using
<script src="/static/js/my-site"></script>
and it will load my-site.js in older browsers and my-site.js.gz when GZip encoding is supported. Great! But: this breaks local development using runserver. Of course, the staticfiles default view has no idea how to turn /js/my-site into /js/my-site.js (or .gz). To get runserver working, I need to specify the extension, which breaks content negotiation.
Is there a better way to configure Apache, so that I can always request .js (or .css, etc) and get served the compressed version transparently? Or can I tell Django how to find the requested resource without specifying an extension? I wouldn't think I'm the only one trying to do this...
there is no simple resolution. mostly because you are using something that was designed for the apache web server only (afaik).
i my opinion there are 3 solutions:
keep source .{js,css} files in separate directory, in development you can serve them from source dir or compressed one - simple, transparent and you can hide your uncompressed and non-obfuscated sources far from the reach
compress files with the .min.{js,css} ending - no need for separate directory, you can hide sources in apache (mod_rewrite)
write your own small middleware that will be simulating what apache does (it is few lines to select and rewrite path, you can even have different behavior depending on DEBUG config var)
use some dynamic solution e.g Django Compressor which will compile those files on-demand
(I'm using option 4 :) )

Sharing files from google cloud storage to GAE

I have one Django application running GAE.The application uses content folder which contains images and html snippets.The content folder was uploaded in google cloud storage.I would like to render a image in static file using img tag.For using img tag I want to know the url of that image.I have seen that when we set the permission to share publicly it will give us a url.But I don't want to share that files publicly.If I share an another application can use my files.I don't want that.I there any way to do that with out log in a user
Sharing it publicly is the best way to go.
You could also base64 encode the image data when you render out the template, which means the url of the image will not be shown to the public on your page. Then you can obfuscate the image names in the GCS. This way it's still public but hard to reach.

S3 Static Website Only Displays Index.html (but not other dependent files)

I've been messing around with AWS lately and it definitely great. As a first test I'm trying to host the most basic static website via S3. The site is simply just one html file and a few javascript, css and image files.
Whenever I load the static URL the only thing that loads is the index.html file, its contents and for some strange reason the only image that loads is my avatar, yet all the images are stored in the same folder. All of the css, js and image files are also written as relative links too of course.
I've made sure all the files and folders permissions are set to "world" multiple times.
I also looked at the network tab in dev tools and its giving me 200's on every GET request.
I'm completely stumped as to why this is happening. Does anyone have an idea of what I'm missing?
The url is available at http://www.mikefisher.io.s3-website-us-east-1.amazonaws.com/
I should add that the site works perfectly locally as well as on a traditional web server.
I checked my browser console and it gives me this error which I think might have something to do with it.
Resource interpreted as Stylesheet but transferred with MIME type binary/octet-stream:
Fixed it!
The issue I was having is the metadata for the CSS files in Amazon S3 were set to 'binary/octet-stream' by default.
The way I fixed this was selecting the individual files in the bucket, clicking the properties tab, then in the meta-data section typing in 'text/css' as the value.

Accessing images on production, from javascript, in Rails 4

It appears that now in Rails 4 using asset pipeline and the sprocket-rails gem, when images are processed, their filename is appended with an md5 fingerprint like css and javascript. While this makes sense because md5 fingerprints are awesome, it makes it increasingly difficult to access that image from javascript. In rails 3.2, I could access the image with /assets/image_name.jpg and it would serve properly, but in rails 4 that asset doesn't exist, it only exists with the md5 fingerprint in the name.
I know that rails provides helpers to access the image via erb <%= asset-url("image_name.jpg") %> but that is less ideal in javascript, because I am not using erb in my js. There are plenty of ways I could hack this with data-attributes serving in the views or using a script tag in my view and setting some globals, but I am looking for a nice solution to this problem, if it exists.
Any help is appreciated, thanks.
Another option to consider (although I wouldn't recommend it) is to use a custom route in your application controller to grab the asset path for you in the controller and either return the url to the asset with the md5 hash or possibly just render the raw binary data of the asset (although this will add processing overhead to your application).
For example, you make a AJAX get request to
http://yourapp.com/images?file=my_image.jpg
Then in your controller your action method would look like this:
def images
ActionController::Base.helpers.asset_url(params[:file])
end
This would then return the url path to the asset. The downside to this method is that it requires that you make two requests on the JS side. The first to get the path to the asset and the second to actually load that asset with the returned path.
To reduce this down to one request you could have the application read the image from the file system and return the proper headers so the browser thinks it is an image being returned and therefor will render the url provided. However, this would be a lot more work for the application and a lot more unneeded disk IO on your server.
It may take two requests for each image on the client to achieve what you want but you have to sacrifice somewhere...
Why do you need to use the asset pipeline for images? I get the hashing behavior. But normally the assets would be preprocessed. If you put the images in the public hierarchy as in olden times, you would get normal path routing.
Here's a quote from the Asset Pipleline guide that I think might be germane.
"Assets can still be placed in the public hierarchy. Any assets under public will be served as static files by the application or web server. You should use app/assets for files that must undergo some pre-processing before they are served."
Unfortunately, I think that you are stuck either adding an ERB extension to your JS and using the asset helpers, or else not using the asset pipeline for the assets.
When you say "I am not using erb in my js", do you mean you don't want to, or simply that you aren't? Because you can!
If you rename the relevant JS files with the extension .js.erb then you can use the asset_url helper in these files like so:
var src = "<%= asset_url('photo.jpg') %>";

Django: how to open local html files directly in the browser with links like href="file:///C:/path/file.html"

I'm making a django app to index my collection of local files (html, text, pdf, ... ) that I keep in diferent partitions and directories so I can search easily based on the name, date, title, etc of the files. It's like a advance locate, the unix utility. It generates a dynamic page with links for the files and in the case of the html files I should click and load the local file in the brower. The generated page contains links like:
Title of local file</li>
The problem is that when I click it does nothing, not even error messages. If I save this generated html page and open it in the brower directly it works fine. I think it doesn't work for security issues but I do not pretend to use it as a web app over the internet but as a local app. I am using the django development server. I know that django can serve static files putting them in a specific directory but this isn't what I need (the files are in multiple locations); I want to load the files in the browser as local files, not through the server. Can this be done?
Is there a way in django to make the "file:///C:/path/file.html" scheme work in the generated dynamic pages?
The problem had nothing to do with django but the browser (in my case firefox 4). Firefox doesn't allow to link to local files from remote sites for security reasons. I have to disable this security check for http://localhost:8000 and it worked. As the change only affects localhost it shouldn't be a security issue.This link explain how to do it:
http://kb.mozillazine.org/Links_to_local_pages_don%27t_work
Basically all you need to do is create a user.js file in your firefox profile folder with this:
user_pref("capability.policy.policynames", "localfilelinks");
user_pref("capability.policy.localfilelinks.sites", "http://localhost:8000");
user_pref("capability.policy.localfilelinks.checkloaduri.enabled", "allAccess");
If you use noscript you also have to change some configuration: NoScript Options ("Advanced -> Trusted -> "Allow local links").
Other browsers may have this security checks so you will have to do diferent changes accoding to the operating system and browser you use.
: is wrong. Use | instead.
Title of local file