class Object

Public Instance Methods

GenChar(length) click to toggle source

generate random char

# File lib/kamishibai/functions.rb, line 77
def GenChar(length)
        length = length.to_i
        return nil if length < 1

        s = ''
        w = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' # 62 uniq chars

        while s.length < length
                s = s + w[rand(62)]
        end

        return s
end
available_drives() click to toggle source

show list of drives in ms windows environment

# File lib/kamishibai/functions.rb, line 112
def available_drives
        if RbConfig::CONFIG['host_os'] =~ /ming/
                drives = []

                file_system = WIN32OLE.new("Scripting.FileSystemObject")
                fs_drives = file_system.Drives
                fs_drives.each do |drive|
                        next unless drive.IsReady
                        drives << drive.Path
                end

                drives
        else
                ['/']
        end
end
cbz_pages?( zfile ) click to toggle source

returns total number of pages for cbz file

# File lib/kamishibai/functions.rb, line 130
def cbz_pages?( zfile )
        i = 0
        begin
                Zip::File.open( zfile ) { |x|
                        x.each { |zobj|
                                if zobj.ftype == :file and File.basename(zobj.name)[0] != '.' and File.basename( zobj.name ) =~ /\.(jpg|jpeg|png|gif)$/i
                                        i += 1
                                end
                        }
                }
        rescue
                return nil
        end
        return i
end
image_type(magic) click to toggle source

return image type

# File lib/kamishibai/functions.rb, line 92
def image_type(magic)
        magic = magic[0..9]
        case magic
                when /^\xff\xd8/n
                        :jpeg
                when /^\x89PNG/n
                        :png
                when /^GIF8/n
                        :gif
                when /^\x00/n
                        :wbmp
                when /^gd2/n
                        :gd2
                else
                        :unknown
        end
end
img_resize( dat, w, h, options = {} ) click to toggle source

resize image using Java library

# File lib/kamishibai/functions.rb, line 193
def img_resize( dat, w, h, options = {} )
        puts "resizing jimage… #{w} : #{h} : #{options[:quality]} : #{options[:format]}" if $debug

        quality = options[:quality]
        format = options[:format]

        ssimg = ''
        ImageVoodoo.with_bytes(dat) { |img|
                scale = 1280 / img.width

                img.scale( scale ) do |simg|
                        ssimg = simg.bytes( image_type( dat ).to_s )
                end
        }

        return ssimg
end
init_database(extra_dirs=[]) click to toggle source

function for initialize database

# File lib/kamishibai.rb, line 18
def init_database(extra_dirs=[])
        puts 'initializing database... this may take some time to run on first time...'

        # load or create new db
        $db = Kamishibai::Database.new( $settings.db_path, $settings.bookmarks_path )
        
        # add new files to db
        $db.add_books( $settings.srcs )
end
mk_thumb(f_cbz, auto_gen = false) click to toggle source

create image thumbnail and save to cache

# File lib/kamishibai/functions.rb, line 289
def mk_thumb(f_cbz, auto_gen = false)
        unless auto_gen
                # holds the last time the open_cbz is called
                # to pause the Thumbnail Generator thread
                $open_cbz_ltime = Time.now
        end

        quality = 60 #80
        width = 220 #320
        height = 0
        page = 1

        f = $settings.cache_path + '/' + File.basename( f_cbz.delete('ÿ') ).gsub('.cbz','.jpeg') # utf8-mac puts ÿ in filename, need to remove first for cross os support

        if File.exists?( f )
                if File.size( f ) == 0
                        File.delete( f )
                else
                        if auto_gen
                                # no need to return data if this is called from auto thumbnail worker
                                return
                        else
                                # return file if it exists
                                puts "thumbnail found. #{f}" if $debug
                                return File.binread( f )
                        end
                end
        end

        puts "thumbnail generated. #{f_cbz}" if $debug
        image = open_cbz( f_cbz, page )
        image = img_resize(image, width, height, { :quality => quality, :format => :jpeg })

        begin
                # store image to cache dir
                FileUtils.mkdir_p( File.dirname( f ) ) if ! Dir.exists?( File.dirname( f ) )
                File.binwrite( f, image )
        rescue => errmsg
                puts "\nthumbnail storage failed: #{f} >> #{errmsg} … retrying\n"
                retry
        end

        return image
end
open_cbz( zfile, page = 1, options = {} ) click to toggle source

cbz file accessor, give file name and page and you shall receive

# File lib/kamishibai/functions.rb, line 148
def open_cbz( zfile, page = 1, options = {} )
        objs = []
        # begin
                Zip::File.open( zfile ) { |x|
                        x.each { |zobj|
                                if zobj.ftype == :file and File.basename(zobj.name)[0] != '.' and File.basename( zobj.name ) =~ /\.(jpg|jpeg|png|gif)$/i
                                        objs << zobj
                                end
                        }
                        objs.sort!

                        if objs.length == 0
                                puts "error: no image detected. #{zfile}"
                                return nil
                        elsif page > objs.length or page < 1
                                puts "error: no such page #{page} : #{zfile}"
                                return nil
                        else
                                img = objs[page-1].name
                                uimg = img.clone.force_encoding('UTF-8') # unicode version of filename, or it won't print on puts
                                puts "reading image… #{page} : #{uimg} : #{zfile}" if $debug

                                simg = x.file.read(img)

                                begin
                                        # load the image to check if the image is corrupted or not
                                        GD2::Image.load( simg ) if defined?(GD2)
                                rescue => errmsg
                                        puts "error: fail to load image #{page} : #{zfile}"
                                        p errmsg
                                        return nil
                                end

                                return simg
                        end
                }
        # rescue => e
        #      puts "Corrupted zip file."
        #      puts e.exception
        #      puts e.backtrace
        # end
end
restricted_dir?( dir, excludes=[] ) click to toggle source

is dir restricted directory?

# File lib/kamishibai/functions.rb, line 17
def restricted_dir?( dir, excludes=[] )
        # restricted directory list
        restricted_dirs = [
                '.Trash', '.Trashes', '.fseventsd', '.Spotlight-V100', '.DocumentRevisions-V100',
                '.$EXTEND',
                '_SYNCAPP',
                'Corrupted',
                'System Volume Information', 'RECYCLER',
                'backup',
                '.sparsebundle',
                '.tmpdir', '.tmp7z',
                '.AppleDouble'
        ]

        for word in restricted_dirs - excludes
            return true if dir.include?( word )
    end
    return false
end
start_auto_gen_thumbnail() click to toggle source

worker thread for generating thumbnails

# File lib/kamishibai/workers.rb, line 21
def start_auto_gen_thumbnail
        $open_cbz_ltime = Time.now - 61 # holds the last time the open_cbz is called

        Thread.new {
                puts "************* Start Generating Thumbnails *************" 
                
                for bookcode, obj in $db.books
                        # if the call to open_cbz has elasped more than 60 seconds, allow to resume generating
                        if $open_cbz_ltime + 60 < Time.now
                                mk_thumb( obj.fullpath, true )
                        else
                                puts "Thumbnail generating paused."
                                sleep 60
                        end
                end
                
                puts "************* Finish Generating Thumbnails *************"
        }
end
start_auto_save() click to toggle source

worker thread for saving database and bookmarks

# File lib/kamishibai/workers.rb, line 8
def start_auto_save
        # run save every 60 seconds
        Thread.new {
                while true
                        $db.save
                        $db.save_bookmarks
                        sleep 60
                end
        }
end