Class: Snap::SnapFile

Inherits:
Object
  • Object
show all
Defined in:
lib/snap/snap_file.rb

Instance Method Summary collapse

Constructor Details

#initialize(file_name) ⇒ SnapFile

Returns a new instance of SnapFile.



3
4
5
# File 'lib/snap/snap_file.rb', line 3

def initialize(file_name)
  @path = file_name.sub("//", "/")
end

Instance Method Details

#directory?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/snap/snap_file.rb', line 59

def directory?
  type.match("directory")
end

#iconObject

TODO: Get this HTML outta here



49
50
51
52
53
# File 'lib/snap/snap_file.rb', line 49

def icon
  return "<img src=\"/__snap__/icons/dir.png\"/>" if directory?
  return "<img src=\"/__snap__/icons/image.png\"/>" if image?
  return "<img src=\"/__snap__/icons/text.png\"/>"
end

#image?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/snap/snap_file.rb', line 55

def image?
  type.match("image")
end

#mtimeObject



27
28
29
30
31
32
33
34
35
# File 'lib/snap/snap_file.rb', line 27

def mtime
  return if File.symlink?(@path)
  mtime = File.mtime(@path)
  if (mtime.to_i > Time.now.to_i - 24*60*60)
    mtime.strftime("%I:%M:%S %p")
  else
    mtime.strftime("%d-%m-%Y")      
  end
end

#nameObject



7
8
9
10
11
12
13
# File 'lib/snap/snap_file.rb', line 7

def name
  if @path && @path.match("/")
    @path.split("/").last
  else
    @path
  end
end

#pathObject



15
16
17
# File 'lib/snap/snap_file.rb', line 15

def path
  @path
end

#relative_to(dir) ⇒ Object



19
20
21
# File 'lib/snap/snap_file.rb', line 19

def relative_to(dir)
  rel = @path.sub(dir, "")
end

#sizeObject



23
24
25
# File 'lib/snap/snap_file.rb', line 23

def size
  File.size(@path)
end

#typeObject



37
38
39
40
41
42
43
44
45
46
# File 'lib/snap/snap_file.rb', line 37

def type
  type = Rack::Mime::MIME_TYPES[File.extname(path)]
       
  if type.nil? 
    escaped_path = @path.gsub(" ", "\\ ")
    type = `file #{escaped_path}`.strip.sub(/.*:\s*/, "")
    type = "executable"       if type.match("executable")
  end
  type
end