Class: Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/sinatra/directory_listing/resource.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, page) ⇒ Resource

Returns a new instance of Resource.



13
14
15
16
17
18
19
20
# File 'lib/sinatra/directory_listing/resource.rb', line 13

def initialize(file, page)
  @page = page
  @file = file
  @sort_name = file.gsub(/^[Tt]he /,"")
  @name_html = set_name(file)
  @mtime, @mtime_html = set_mtime(file)
  @size, @size_html = set_size(file)
end

Instance Attribute Details

#fileObject

Class definition for a single resource to be listed. Each resource object has accessors for its file name, regular name, sortable name (removing “[Tt]he”) size and mtime, as well as those components wrapped in html.



11
12
13
# File 'lib/sinatra/directory_listing/resource.rb', line 11

def file
  @file
end

#mtimeObject

Class definition for a single resource to be listed. Each resource object has accessors for its file name, regular name, sortable name (removing “[Tt]he”) size and mtime, as well as those components wrapped in html.



11
12
13
# File 'lib/sinatra/directory_listing/resource.rb', line 11

def mtime
  @mtime
end

#mtime_htmlObject

Class definition for a single resource to be listed. Each resource object has accessors for its file name, regular name, sortable name (removing “[Tt]he”) size and mtime, as well as those components wrapped in html.



11
12
13
# File 'lib/sinatra/directory_listing/resource.rb', line 11

def mtime_html
  @mtime_html
end

#name_htmlObject

Class definition for a single resource to be listed. Each resource object has accessors for its file name, regular name, sortable name (removing “[Tt]he”) size and mtime, as well as those components wrapped in html.



11
12
13
# File 'lib/sinatra/directory_listing/resource.rb', line 11

def name_html
  @name_html
end

#pageObject

Class definition for a single resource to be listed. Each resource object has accessors for its file name, regular name, sortable name (removing “[Tt]he”) size and mtime, as well as those components wrapped in html.



11
12
13
# File 'lib/sinatra/directory_listing/resource.rb', line 11

def page
  @page
end

#sizeObject

Class definition for a single resource to be listed. Each resource object has accessors for its file name, regular name, sortable name (removing “[Tt]he”) size and mtime, as well as those components wrapped in html.



11
12
13
# File 'lib/sinatra/directory_listing/resource.rb', line 11

def size
  @size
end

#size_htmlObject

Class definition for a single resource to be listed. Each resource object has accessors for its file name, regular name, sortable name (removing “[Tt]he”) size and mtime, as well as those components wrapped in html.



11
12
13
# File 'lib/sinatra/directory_listing/resource.rb', line 11

def size_html
  @size_html
end

#sort_nameObject

Class definition for a single resource to be listed. Each resource object has accessors for its file name, regular name, sortable name (removing “[Tt]he”) size and mtime, as well as those components wrapped in html.



11
12
13
# File 'lib/sinatra/directory_listing/resource.rb', line 11

def sort_name
  @sort_name
end

Class Method Details

.sort(resource_array, sortby, direction) ⇒ Object

Sort an array of resources by name, mtime, or size. Direction should be “ascending” or “descending”



140
141
142
143
144
145
# File 'lib/sinatra/directory_listing/resource.rb', line 140

def self.sort(resource_array, sortby, direction)
  sortby = "sort_name" if resource_array[0].page.smart_sort == true #and sortby = "file"
  new_array = resource_array.sort_by {|a| a.send(sortby)}
  new_array.reverse! if direction == "descending"
  new_array
end

Instance Method Details

#set_mtime(file) ⇒ Object

Set the mtime for a file.

Returns the mtime as a Time object so it can be sorted.



27
28
29
30
31
# File 'lib/sinatra/directory_listing/resource.rb', line 27

def set_mtime(file)
  f = File.join(File.join(@page.public_folder, URI.unescape(@page.request_path)), file)
  html = "\t<td>#{File.mtime(f).strftime(@page.last_modified_format)}</td>"
  return [File.mtime(f), html]
end

#set_name(file) ⇒ Object

Set the name of the file and its link.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/sinatra/directory_listing/resource.rb', line 56

def set_name(file)

  ##
  # Make sure we're working with an unescaped file name.
  # Remove the extension if neccesary and truncate it.
  # URI.unescape seems to work best to decode uris. 

  file = URI.unescape(file)
  file_noext = file.gsub(File.extname(file), "") if @page.should_show_file_exts == false
  if file_noext
    file_truncated = file_noext.truncate(@page.filename_truncate_length, '...')
  else
    file_truncated = file.truncate(@page.filename_truncate_length, '...')
  end

  ##
  # If the requested resource is in the root public directory, the link is 
  # just the resource itself without the public directory path as well. 

  requested = Pathname.new(URI.unescape(@page.request_path)).cleanpath
  pub_folder = Pathname.new(@page.public_folder).cleanpath
  if requested.eql?(pub_folder)
    link = file
  else
    link = File.join(@page.request_path, file)
  end

  ##
  # Add a class of "dir" to directories and "file" to files.

  html = ""
  if File.directory?(URI.unescape(File.join(@page.public_folder, link)))
    html << "\t<td class='dir'>"
    
    ##
    # Append the sorting information if the current directory is sorted
    
    if @page.request_params["sortby"] && @page.request_params["direction"]
      link << "?sortby=" + @page.request_params["sortby"] + "&direction=" + @page.request_params["direction"]
    end
  else
    html << "\t<td class='file'>"
  end

  ##
  # Append the rest of the html. 
  # 
  # I haven't found a URI escaping library that will handle this
  # gracefully, so for now, we're going to just take care of spaces and 
  # apostrophes ourselves. 

  link = link.gsub(" ", "%20").gsub("'", "%27")
  html << "<a href='#{link}'>#{file_truncated}</a></td>"
  
  return html
end

#set_size(file) ⇒ Object

Set the size for a file.

Returns the size as number.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/sinatra/directory_listing/resource.rb', line 38

def set_size(file)
  html = ""
  size = ''
  f = File.join(File.join(@page.public_folder, URI.unescape(@page.request_path)), file)
  if File.directory?(f)
    size = 0
    html = "\t<td>-</td>"
  else
    size = File.stat(f).size
    converted = Filesize.from("#{File.stat(f).size} B").pretty
    html = "\t<td>#{converted}</td>"
  end
  return [size, html]
end

#wrapObject

Generate html for a resource.



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/sinatra/directory_listing/resource.rb', line 116

def wrap
  html = ""
  if @page.should_list_invisibles == true
    html << "\n\t<tr>
    #{@name_html}
    #{@mtime_html}
    #{@size_html}
    \t</tr>"
  else
    if @file[0] != "."
      html << "\n\t<tr>
      #{@name_html}
      #{@mtime_html}
      #{@size_html}
      </tr>"
    end
  end
  html
end