              "key=#{apiKey}&" +
              "url=#{url}"
 
-  filename = "#{imagePath}/#{url.split('/').join('_')}"
+  # split the ext, md5 just the url and write md5+ext to disk
+  # md5 since some urls are too big and cause 'ENAMETOOLONG' error
+  splitUrl = url.split(".")
+  ext      = splitUrl.pop()
+  noExt    = splitUrl.join(".")
+
+  md5      = crypto.createHash("md5").update(noExt).digest("hex")
+  filename = "#{imagePath}/#{md5}.#{ext}"
 
   serveFile = (filename, res)->
     fileStream = fs.createReadStream filename


    mimeType = mime.lookup filename

    res.writeHead(200, {'Content-Type': mimeType });

  if fs.existsSync filename
    return serveFile filename, res

fs     = require "fs"
http   = require "https"
mime   = require "mime"
crypto = require "crypto"

{
  embedly : {
