Class: File

Inherits:
Object
  • Object
show all
Defined in:
lib/flickru/file.rb

Class Method Summary collapse

Class Method Details

.date_taken(file) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/flickru/file.rb', line 49

def File.date_taken file
  attempt = 1
  begin
     case attempt
     when 1 then EXIFR::JPEG.new(file).date_time_original.strftime "%y-%m-%d %H:%M:%S"
     when 2 then EXIFR::TIFF.new(file).date_time_original.strftime "%y-%m-%d %H:%M:%S"
     else nil
     end
  rescue
     attempt += 1
     retry
  end
end

.duration(video) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/flickru/file.rb', line 35

def File.duration video
  s = `#{Escape.shell_command ["ffmpeg", "-i", video]} 2>&1`
  if s =~ /Duration: ([\d][\d]):([\d][\d]):([\d][\d]).([\d]+)/
    hours     = $1.to_i
    mins      = $2.to_i
    seconds   = $3.to_i
    # fractions = ("." + $4).to_f

    hours * 60 * 60 + mins * 60 + seconds
  else
    0
  end
end

.find(dir) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/flickru/file.rb', line 9

def File.find dir
  found = Array.new
  Find.find dir do |file|
    if File.directory? file
      if File.basename(file)[0] == ?.
        Find.prune # don't look any further into this directory
      else
        next
      end
    else
      if yield file
        found << file
      end
    end
  end
  found
end

.geotagged?(file) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/flickru/file.rb', line 63

def File.geotagged? file
  attempt = 1
  begin
     case attempt
     when 1 then
        hash = EXIFR::JPEG.new(file).to_hash
     when 2 then
        hash = EXIFR::TIFF.new(file).to_hash
     else return false
     end
  rescue
     puts $!
     attempt += 1
     retry
  end

  lat     = hash.key? :gps_latitude
  lon     = hash.key? :gps_longitude
  lat_ref = hash.key? :gps_latitude_ref
  lon_ref = hash.key? :gps_longitude_ref

  lat and lon and lat_ref and lon_ref
end

.human_readable_size(file_size) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/flickru/file.rb', line 87

def File.human_readable_size file_size
  if file_size < 1000
    file_size.to_s + " bytes"
  elsif file_size < 1000000
    (file_size / 1000).to_s + "KB"
  elsif file_size < 1000000000
    (file_size / 1000000).to_s + "MB"
  else
    (file_size / 1000000000).to_s + "GB"
  end
end

.image?(file) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/flickru/file.rb', line 27

def File.image? file
  IMAGE_EXTENSIONS.include? File.extname(file).downcase
end

.video?(file) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/flickru/file.rb', line 31

def File.video? file
  VIDEO_EXTENSIONS.include? File.extname(file).downcase
end