# Need feedback on a small change to the nginx-gridfs module

**URL:** <https://www.ruby-forum.com/t/need-feedback-on-a-small-change-to-the-nginx-gridfs-module/217551>\
**Category:** NGINX\
**Created:** [March 4, 2012, 10:05pm UTC](https://www.ruby-forum.com/t/need-feedback-on-a-small-change-to-the-nginx-gridfs-module/217551 "2012-03-04T22:05:48Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [March 4, 2012, 10:05pm UTC](https://www.ruby-forum.com/t/need-feedback-on-a-small-change-to-the-nginx-gridfs-module/217551/1 "2012-03-04T22:05:48Z")

</div>

Hi,

I have made a small tweak to the nginx-gridfs module (  
[GitHub - mdirolf/nginx-gridfs: Nginx module for serving files from MongoDB's GridFS](https://github.com/mdirolf/nginx-gridfs)) to serve my requirement. I am  
not  
sure if it is generic enough to raise a pull request. So I thought of  
posting it here and asking for some feedback.

The code changes are related more to MongoDB and GridFS but still it  
would  
be very helpful if the nginx gurus here can review the code from the  
nginx  
module angle and give me some feedback.

Here is a description about my requirement and my change. Or you can  
skip  
right ahead and go look at the code at :

> <https://github.com/brahmana/nginx-gridfs/commit/5f32a63678b9f997bcd4539b89f357dc98be65d4>

Setup :

- Rails app accepts user uploaded images and stores them in GridFS,  
with  
unique file names
- Nginx uses the nginx-gridfs module to serve these files from GridFS  
directly.

Requirement

- Multiple versions (sizes) of every uploaded image needs to exist -  
each  
addressed by the same (or similar) url with a query parameter specifying  
the version.
- Creating versions during the upload request results in bad user  
experience (too much time) and hence needs to be done by a background  
process - using a queuing server. This means additional versions are not  
available right away.
- The clients would not know when these versions are available and  
would  
get 404 when they try to fetch a non-default version of the image.
- So the nginx-gridfs module needs to be smart enough to fallback to  
the  
default version when the requested version is not available.  
i.e. Look for the specified version. If present serve it. Else fetch  
the default version and serve that.

I have a very rudimentary implementation of this. It compiles and works  
fine - no crashes, doesn’t seem to leak memory. I haven’t done any  
benchmarking, but it seems do just fine under simple loads.

Here is how the different versions of the image files are stored and  
addressed :

The value of the ‘type’ query parameter is added as a suffix to the  
filename before searching.  
Ex : original filename - ‘images/4f4292ff1d41c80be4000013’ and is  
requested as ‘[http://hostname/files/images/4f4292ff1d41c80be4000013](http://hostname/files/images/4f4292ff1d41c80be4000013)’  
Small version of the same image will be at :  
‘images/4f4292ff1d41c80be4000013\_small’ or  
‘images/4f4292ff1d41c80be4000013\_s’  
and is requested as ’  
[http://hostname/files/images/4f4292ff1d41c80be4000013?type=small](http://hostname/files/images/4f4292ff1d41c80be4000013?type=small)’ or ’  
[http://hostname/files/images/4f4292ff1d41c80be4000013?type=s](http://hostname/files/images/4f4292ff1d41c80be4000013?type=s)’

If the requested version is not found, then we fallback to the default  
version, i.e. the one without any suffixes (a.k.a  
‘images/4f4292ff1d41c80be4000013’) and serve that one.

Few notes about the changed code :

1. It assumes that the field type to be queried against on a String type
2. It assumes that the gridfs and gridfile are reusable types - i.e same  
object can be used across multiple queries.
3. It is probably not super optimized, but that is ok for now.

And here is the code commit on github.cm for this change -

> <https://github.com/brahmana/nginx-gridfs/commit/5f32a63678b9f997bcd4539b89f357dc98be65d4>

It would be very helpful to get some feedback from you guys.

–  
Regards,  
Srirang G Doddihal  
Brahmana.  
@sribrahmana - [http://www.twitter.com/sribrahmana](http://www.twitter.com/sribrahmana)

The LIGHT shows the way.  
The WISE see it.  
The BRAVE walk it.  
The PERSISTENT endure and complete it.

I want to do it all ALONE.
