$:.unshift(File.dirname(__FILE__)) unless $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__))) require 'open-uri' require 'digest/md5' require 'RipXploreLog' require 'RipXplore' class FileCacheItem < File def set_path(path) @path=path end def path @path end end #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 class FileCache attr_accessor :url_cache_dir,:url_cache_list,:native_file_cache_dir def cache_name_for_url(url) "#{url_cache_dir}/#{Digest::MD5.hexdigest(url)}.cached" end 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 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 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 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<0)) rescue #silently swallow any exception end end raise 'no suitable image found in cache' end 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<