File Upload: Validating Extensions

Posted by anup.narkhede on January 05, 2008

I was searching for a way to validate a file against an allowed set of extensions before uploading. My application uses file_column plugin to store video files and I am using mongrel_upload_progress to track the upload. The maximum file size allowed is 50Mb so there was a need to have validation at both client as well as server side.

Model Validation

Client Side javascript validation

mongrel_upload_progress plugin uses following code to submit the form in a hidden iframe. Modify submitUploadForm method as shown:

The advantages of this approach are:
File is validated at both sides to avoid unnecessary uploading of file before model validation. Good for big files.
In case JS is disabled, the file is still validated.

Ajax Tree Plugin

Posted by anup.narkhede on December 19, 2007

Introduction

This is a helper plugin to generate ajax based tree structure (two level). It can be used for two models having n:m or 1:n relationships. A leaf from any branch can be dragged and dropped in another branch. A scaffolding arrangement is also done to create a new branch, add leaf etc.

Sceenshot

For example:

Installation:

Here we take the previous example of Category and Article models. Generate the ajax controller for the two models.

Usage

Example

Copy static files

Copy add.png, delete.png from /vendor/plugins/ajax_tree/ directory to /public/images directory.

Copy ajax_tree.css file from /vendor/plugins/ajax_tree/ directory to /public/stylesheets/ directory.
Include css in your application layout

Call the helpers

Specify model names for branch model and leaf model in /views/home/_show_ajax_tree.rhtml partial.

Then, call this partial in any rhtml, where you want to display the ajax tree. In this case, we have done it in /views/home/index.rhtml

home_controller.rb (Generated automatically)

index.rhtml

Contributions:

This project is hosted at http://code.google.com/p/ajax-tree/. Please leave your feedback.

FlickrGallery - Papervision3D

Posted by anup.narkhede on November 27, 2007

FlickrGallery 0.1 is ready to go :) It pulls out the latest 12 images from your Flickr album. You just need to pass nsid of the flickr account.

To get Flickr nsid, see the rss feed url of your photos. For example:
http://api.flickr.com/services/feeds/photos_public.gne?id=75426038@N00&lang=en-us&format=rss_200

Here 75426038@N00 is the nsid for my flickr account. Embed the flash file http://anup.info/FlickrGallery.swf?userid=your-nsid like any other swf control in your webpage.

SecuritySandbox Errors and PaperVision3D

Posted by anup.narkhede on November 27, 2007

I have been banging my head since past one week to resolve the Security Sandbox error issue with ActionScript 3.0 and Papervision 3D. This security thingy was needed badly to get my FlickrGallery working.

Error Description:

     SecurityError: Error #2122: Security sandbox violation: LoaderInfo.content:
     file:///C:/Documents and Settings/anup/My Documents/app/Final/bin/Final.swf
     cannot access http://farm3.static.flickr.com/2263/2043746131_8e36900f5d_t.jpg.
     A policy file is required, but the checkPolicyFile flag was not set when this media
     was loaded.
	at flash.display::LoaderInfo/get content()
	at Final/::imageLoaded()

Anyhow the flash application was not getting the LoaderContext’s checkPolicyFile flag and domain’s crossdomain.xml file. I searched for solutions all over the web including pv3d mailing list and then arrived at a solution for this problem.

My ActionScript 3.0 loader code looks like this:

     imageURL = string;
     var loaderContext:LoaderContext = new LoaderContext(true);
     var imageLoader:Loader = new Loader();
     imageLoader.load( new URLRequest(imageURL),loaderContext);
     imageLoader.contentLoaderInfo.addEventListener( Event.COMPLETE, imageLoaded );

     private function imageLoaded(e:Event):void{
	var loadedBmp:Bitmap = e.target.content as Bitmap;
	var bmp:BitmapData = loadedBmp.bitmapData;
	var material:BitmapMaterial = new BitmapMaterial(bmp);
	material.doubleSided = true;
	images[imgCounter] = new Plane( material, 40, 40, 1, 1);
	rootNode.addChild(images[imgCounter]);
     }

I created a proxy script for my domain http://anup.info to route all flickr images through my domain. Found a cool php script at http://www.abdulqabiz.com/blog/archives/general/php_proxy_script_for.php

Then finally added one crossdomain.xml file at http://anup.info

     < ?xml version="1.0"?>
        < !DOCTYPE cross-domain-policy SYSTEM
          "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">
        <cross -domain-policy>
            <allow -access-from domain="*" />
        </cross>>

Bravo! it works! Finally my FlickrGallery is ready to go live on external web sites.

yaml_form_builder

Posted by anup.narkhede on October 13, 2007

This plugin provides a simple way to generate dynamic forms in view. The forms are configured with the help of a yaml configuration file.

1) Installation

2) Configuration
The example below demonstrates a form generated for the following schema.

3) Configuration
Create a file named formconfig.yml in /config directory.

4) Controller
Fetch records in the controller.

5) Finally the helper in View
(views/form/index.rhtml)

The url /form/index shows a form, generated dynamically with the help of above yml configuration file.