Displaying a S3 Bucket as Nested Directories in Rails

The component of the app I am currently working on for WCAI, researchers need to be able to download data files. Many of these files are more than a gigabyte in length. After deciding to store the files on S3, it was important to create a secure and easy to use interface to access and download these files.

Here is the files model I created to treat files stored on aws the same as things stored in the database.

models/project_file.rbDownload
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
class ProjectFile
  attr_accessor :children, :size, :path

  EXT_MAP =  {
      'xls'  => :xls,
      'xlsx' => :xls,
      'doc'  => :doc,
      'docx' => :doc,
      'zip'  => :zip,
      'txt'  => :txt,
      'pdf'  => :pdf,
      'sql'  => :sql,
  }

  def self.find_by_project_name(name)
    project_files = self.files.all({:prefix => name})
    output_files = Hash.new
    file_lookup = Hash.new
    root = nil

    project_files.each do |project_file|
      file_lookup[project_file.key] = convert(project_file)
    end

     file_lookup.values.each do |project_file|
      if project_file.parent_name.nil?
        root = project_file
      else
        parent = file_lookup[project_file.parent_name]
        parent.children ||= []
        parent.children  << project_file
      end
     end
    return root
  end

  def self.find_link_by_name(name)
    fog_file = self.files.get(name)
    expiration = Time.now + 60.seconds
    fog_file.url(expiration)
  end

  def self.convert(fog_file)
    file = ProjectFile.new
    file.size = fog_file.content_length
    file.path = fog_file.key
    file.children = nil
    return file
  end

  def self.files
    FOG_STORAGE.directories.get(Settings.aws_bucket).files
  end

  def extension
    ext = self.path[path.rindex(/\./) + 1..-1]
    EXT_MAP[ext]
  end

  def extension_css
    unless extension.nil?
      extension.to_s
    else
      "default"
    end
  end

  def file_name
    parent_index = path.rindex(/\//,-2)
    if parent_index.nil?
      path
    else
      path[parent_index +1..-1]
    end
  end

  def parent_name
    parent_index = path.rindex(/\//,-2)
    if parent_index.nil?
      nil
    else
      path[0..parent_index]
    end
  end

  def is_directory?
    not children.nil?
  end

  def str_size
      if size > 1024 * 1024 * 1024
        "%0.0f GB" % (size / (1024 * 1024 * 1024))
      elsif size > 1024 * 1024
        "%0.0f MB" % (size / (1024 * 1024 ))
      else
        "%0.0f KB" % (size / 1024)
      end
  end
end

Files are displayed to uses using a tutorial on collapse tree for file structures. A simple helper function converts the data structure to the proper nested list explained in this example.

Comments