Class: File

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

Class Method Summary collapse

Class Method Details

.date_taken(file) ⇒ Object



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

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



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

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



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

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)


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

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



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

def File.human_readable_size file_size
  if file_size < 1024 ** 1
    (file_size / 1024 ** 0).to_s + " bytes"
  elsif file_size < 1024 ** 2
    (file_size / 1024 ** 1).to_s + "KB"
  elsif file_size < 1024 ** 3
    (file_size / 1024 ** 2).to_s + "MB"
  else
    (file_size / 1024.0 ** 3).round.to_s + "GB" # with float division
  end
end

.image?(file) ⇒ Boolean

Returns:

  • (Boolean)


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

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

.video?(file) ⇒ Boolean

Returns:

  • (Boolean)


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

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