Created
          March 20, 2012 09:17 
        
      - 
      
- 
        Save perusio/2133228 to your computer and use it in GitHub Desktop. 
    Get the size of an image file using Nginx Embedded Lua module
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | ## Get the size of the image file using Lua. | |
| ## Cf. http://wiki.nginx.org/HttpLuaModule#set_by_lua. | |
| location ~* \.(?:gif|jpe?g|png)$ { | |
| set_by_lua $img_file_size ' | |
| function fsize (file) | |
| local current = file:seek() -- get current position | |
| local size = file:seek("end") -- get file size | |
| file:seek("set", current) -- restore position | |
| return size | |
| end | |
| fsize(ngx.var.request_filename)'; | |
| } | 
That gave me error failed to run set_by_lua*: set_by_lua:3: attempt to call method 'seek' (a nil value), and I fixed by using this:
set_by_lua $file_size '
  function fsize (file_name)
    local file = assert(io.open(file_name, "r"))   -- open file
    local current = file:seek()                    -- get current position
    local size = file:seek("end")                  -- get file size
    file:seek("set", current)                      -- restore position
    assert(file:close())                           -- close file
    return size
  end
  return fsize(ngx.var.request_filename);
';
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
            
thanks