Class: FileCache

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

Overview

creates and manages a local cache of files, so an URL can be requested, if it is in the cache, the local copy is used, otherwise the file is downloaded to the local cache

Constant Summary collapse

@@file_system_image_urls =
nil

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url_cache_dir, native_file_cache_dir) ⇒ FileCache

Returns a new instance of FileCache.



44
45
46
47
48
49
50
51
52
# File 'lib/FileCache.rb', line 44

def initialize(url_cache_dir,native_file_cache_dir)
  @url_cache_dir=url_cache_dir
  @native_file_cache_dir=native_file_cache_dir
  @url_cache_list="#{url_cache_dir}/cache.urls"
  Dir.mkdir(url_cache_dir) unless File.exists?(url_cache_dir)
  raise "cache directory #{url_cache_dir} invalid" unless File.directory?(url_cache_dir)
  Dir.mkdir(native_file_cache_dir) unless File.exists?(native_file_cache_dir)
  raise "cache directory #{native_file_cache_dir} invalid" unless File.directory?(native_file_cache_dir)
end

Instance Attribute Details

#native_file_cache_dirObject

Returns the value of attribute native_file_cache_dir.



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

def native_file_cache_dir
  @native_file_cache_dir
end

#url_cache_dirObject

Returns the value of attribute url_cache_dir.



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

def url_cache_dir
  @url_cache_dir
end

#url_cache_listObject

Returns the value of attribute url_cache_list.



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

def url_cache_list
  @url_cache_list
end

Instance Method Details

#cache_name_for_icon(native_file) ⇒ Object



40
41
42
# File 'lib/FileCache.rb', line 40

def cache_name_for_icon(native_file)
  "#{native_file_cache_dir}/#{native_file.icon_tag}.icon.#{native_file.icon_format.to_s}"
end

#cache_name_for_native_file(native_file, extension) ⇒ Object



34
35
36
37
38
# File 'lib/FileCache.rb', line 34

def cache_name_for_native_file(native_file,extension)
  image=native_file.file_system_image
  index=image.files.index(native_file)
  "#{native_file_cache_dir}/#{Digest::MD5.hexdigest(image.filename)}-#{index}.#{extension}"
end

#cache_name_for_url(url) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/FileCache.rb', line 22

def cache_name_for_url(url)
  if ( url=~/^file:/) then
    local_name=CGI.unescape(url.sub(/file:\/+/,""))
    local_name="#{File::Separator}#{local_name}" unless ((local_name[1].chr==':') || (local_name[0].chr==File::Separator))
#    puts "#{url}->#{local_name}"
    local_name
    
  else
    "#{url_cache_dir}/#{Digest::MD5.hexdigest(url)}.cached"
  end
end

#file_system_image_urls_in_cacheObject



88
89
90
91
92
93
94
# File 'lib/FileCache.rb', line 88

def file_system_image_urls_in_cache
  if @@file_system_image_urls.nil? then
    make_file_system_image_list unless File.exists?(@url_cache_list)
    @@file_system_image_urls=File.open(@url_cache_list,"r").readlines.collect{|url| url.chomp}
  end
  @@file_system_image_urls
end

#force_icon_to_cache(native_file) ⇒ Object



129
130
131
132
133
134
135
136
137
# File 'lib/FileCache.rb', line 129

def force_icon_to_cache(native_file)
  cache_filename=cache_name_for_icon(native_file)
  if ! File.exists?(cache_filename) then    
      local_file=File.open(cache_filename,"wb")      
      local_file<<native_file.to_icon
      local_file.close        
    end
    return cache_filename
end

#force_native_file_to_cache(native_file, method_to_cache_on_native_file, extension) ⇒ Object



119
120
121
122
123
124
125
126
127
# File 'lib/FileCache.rb', line 119

def force_native_file_to_cache(native_file,method_to_cache_on_native_file,extension)
  cache_filename=cache_name_for_native_file(native_file,extension)
  if ! File.exists?(cache_filename) then    
      local_file=File.open(cache_filename,"wb")      
      local_file<<native_file.send(method_to_cache_on_native_file)
      local_file.close        
    end
    return cache_filename
end

#get_file_system_image(url) ⇒ Object



80
81
82
83
84
85
# File 'lib/FileCache.rb', line 80

def get_file_system_image(url)
  file=get_url(url)    
  image=RipXplore.best_fit_from_file(file)
  @@file_system_image_urls<<url if (!@@file_system_image_urls.nil? && !@@file_system_image_urls.include?(url))    
  return image
end

#get_url(url) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/FileCache.rb', line 62

def get_url(url)
  local_file_name=cache_name_for_url(url)
  if !File.exists?(local_file_name) then
    RipXploreLog.log "downloading #{url}"
    remote_file=open(url,"rb")
    local_file=File.open(local_file_name,"wb")      
    local_file<<remote_file.read
    local_file.close
    local_file_info_filename="#{local_file_name}.info"
    local_file_info_file=File.open(local_file_info_filename,"w")
    local_file_info_file<<url
    local_file_info_file.close
  end
  file=FileCacheItem.open(local_file_name,"rb")
  file.set_path(url)
  file
end

#make_file_system_image_listObject



96
97
98
99
100
101
102
103
# File 'lib/FileCache.rb', line 96

def make_file_system_image_list
  urls=[]
  Dir.glob("#{url_cache_dir}/*.cached.info").each do |info_file|
    url=File.open(info_file,"r").read.chomp
    urls<<url if (FileSystemImage.is_file_system_image_filename?(url))
  end
  File.open(@url_cache_list,"w") {|f| f<< urls.join("\n")}
end

#purge_icon_from_cache(native_file) ⇒ Object



146
147
148
149
150
# File 'lib/FileCache.rb', line 146

def purge_icon_from_cache(native_file)
	filename=cache_name_for_icon(native_file)
	File.delete(filename) if File.exists?(filename)
	
end

#purge_native_file_from_cache(native_file) ⇒ Object



139
140
141
142
143
144
# File 'lib/FileCache.rb', line 139

def purge_native_file_from_cache(native_file)
  Dir.glob(cache_name_for_native_file(native_file,'*')).each do |filename|
    RipXploreLog.log "purging local #{filename}"
    File.delete(filename)  
  end
end

#purge_url(url) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/FileCache.rb', line 54

def purge_url(url)
  local_file_name=cache_name_for_url(url)
  if File.exists?(local_file_name) then
    RipXploreLog.log "purging local copy of #{url}"
    File.delete(local_file_name)
  end
end

#random_file_system_image(collection_type = nil) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/FileCache.rb', line 106

def random_file_system_image(collection_type=nil)
  all_image_urls=file_system_image_urls_in_cache    
  all_image_urls.length.times do
    begin
      index=rand(all_image_urls.length)
      image=get_file_system_image(all_image_urls[index%all_image_urls.length])
      return image if ((collection_type==nil) || (image.send(collection_type).length>0))
    rescue #silently swallow any exception
    end    
  end
  raise 'no suitable image found in cache'
end