Class: Pig
- Inherits:
-
Object
- Object
- Pig
- Defined in:
- lib/pig-media-server/model/pig.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
Returns the value of attribute config.
-
#record ⇒ Object
Returns the value of attribute record.
Class Method Summary collapse
Instance Method Summary collapse
- #change_aspect_rate(rate) ⇒ Object
- #check ⇒ Object
- #comic ⇒ Object
-
#initialize(record) ⇒ Pig
constructor
A new instance of Pig.
- #key ⇒ Object
- #metadata ⇒ Object
- #mtime ⇒ Object
- #mtime_to_i ⇒ Object
- #name ⇒ Object
- #path ⇒ Object
- #size ⇒ Object
- #srt ⇒ Object
- #to_hash(opts = {}) ⇒ Object
- #to_kindle(user_id) ⇒ Object
- #type ⇒ Object
- #url ⇒ Object
- #webvtt ⇒ Object
Constructor Details
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
9 10 11 |
# File 'lib/pig-media-server/model/pig.rb', line 9 def config @config end |
#record ⇒ Object
Returns the value of attribute record.
9 10 11 |
# File 'lib/pig-media-server/model/pig.rb', line 9 def record @record end |
Class Method Details
.all ⇒ Object
115 116 117 |
# File 'lib/pig-media-server/model/pig.rb', line 115 def self.all Groonga['Files'].select.map{|x| Pig.new x} end |
.find(key) ⇒ Object
71 72 73 74 75 76 77 78 |
# File 'lib/pig-media-server/model/pig.rb', line 71 def self.find key case key.class.to_s when 'Array' return key.map{|x| Groonga['Files'][x]}.select{|x| x}.map{|x| self.new x} when 'String' return self.new Groonga['Files'][key] end end |
.find_or_create_by_path(path) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/pig-media-server/model/pig.rb', line 103 def self.find_or_create_by_path path key = Digest::MD5.hexdigest(path).to_s unless Groonga['Files'][key] stat = File::Stat.new path Groonga["Files"].add(key) Groonga["Files"][key].path = path Groonga["Files"][key].mtime = stat.mtime.to_i Groonga['Files'][key].size = stat.size.to_s end return Pig.new Groonga['Files'][key] end |
.search(opts) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/pig-media-server/model/pig.rb', line 80 def self.search opts query = Sewell.generate opts[:query].to_s, %w{metadata path srt} limit = opts[:limit] ? opts[:limit].to_i : 50 page = opts[:page].to_i < 1 ? 1 : opts[:page].to_i order = (opts[:order] == 'ascending' or opts[:order] == 'descending') ? opts[:order] : 'descending' result = Groonga['Files'].select(query) if result.count == 0 begin result = Groonga['Files'].select{|x| x.path.match str} rescue end end if opts[:sort] == 'name' list = result.paginate([key: 'path', order: order], size: 2000, page: page).map{|x| Pig.new x}.sort{|x,y| x.path.split('/').last.sub(/^\d\d*_/, '') <=> y.path.split('/').last.sub(/^\d\d*_/, '') }.reverse rescue list = [] list.reverse! if order == 'descending' else list = result.paginate([key: 'mtime', order: order], size: limit, page: page).map{|x| Pig.new x} rescue list = [] end list end |
Instance Method Details
#change_aspect_rate(rate) ⇒ Object
61 62 63 64 |
# File 'lib/pig-media-server/model/pig.rb', line 61 def change_aspect_rate rate FileUtils.mkdir_p "#{self.config['user_data_path']}/rate" open("#{self.config['user_data_path']}/rate/queue.txt", "a"){|x| x.puts "#{self.key} #{rate}"} end |
#check ⇒ Object
119 120 121 122 |
# File 'lib/pig-media-server/model/pig.rb', line 119 def check return false unless self.record Groonga['Files'].delete self.record._key unless File.exist? self.record.path end |
#comic ⇒ Object
46 47 48 |
# File 'lib/pig-media-server/model/pig.rb', line 46 def comic Comic.new self.record end |
#key ⇒ Object
17 |
# File 'lib/pig-media-server/model/pig.rb', line 17 def key; self.record._key;end |
#metadata ⇒ Object
22 |
# File 'lib/pig-media-server/model/pig.rb', line 22 def ; self.record.;end |
#mtime ⇒ Object
19 |
# File 'lib/pig-media-server/model/pig.rb', line 19 def mtime; Time.at(self.record.mtime);end |
#mtime_to_i ⇒ Object
20 |
# File 'lib/pig-media-server/model/pig.rb', line 20 def mtime_to_i; self.record.mtime; end |
#name ⇒ Object
18 |
# File 'lib/pig-media-server/model/pig.rb', line 18 def name; self.record.path.split('/').last; end |
#path ⇒ Object
43 44 45 |
# File 'lib/pig-media-server/model/pig.rb', line 43 def path self.record.path.sub(/#{config['path'].sub(/\//, '\/')}/, '') end |
#size ⇒ Object
21 |
# File 'lib/pig-media-server/model/pig.rb', line 21 def size; self.record.size;end |
#srt ⇒ Object
23 |
# File 'lib/pig-media-server/model/pig.rb', line 23 def srt; self.record.srt;end |
#to_hash(opts = {}) ⇒ Object
66 67 68 69 |
# File 'lib/pig-media-server/model/pig.rb', line 66 def to_hash(opts = {}) hash = {key: self.key, name: self.name, mtime: self.mtime, size: self.size, url: self.url, type: self.type, mtime_to_i: self.mtime_to_i} hash end |
#to_kindle(user_id) ⇒ Object
57 58 59 60 |
# File 'lib/pig-media-server/model/pig.rb', line 57 def to_kindle user_id FileUtils.mkdir_p "#{self.config['user_data_path']}/kindle/queue" open("#{self.config['user_data_path']}/kindle/queue/#{self.key}_#{user_id}", 'w'){|x| x.puts ''} end |
#type ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/pig-media-server/model/pig.rb', line 25 def type case self.name.split('.').last when 'mp4', 'MP4', 'webm', 'm4v' 'video' when 'zip', 'ZIP' 'read' when 'pdf' 'pdf' when 'txt', 'TXT' 'txt' when 'mobi' 'mobi' when 'flv' 'flv' else 'other' end end |
#url ⇒ Object
24 |
# File 'lib/pig-media-server/model/pig.rb', line 24 def url; 'http://'+self.config['hostname']+ '/volume' + URI.encode(self.record.path.sub(/#{config['path'].sub(/\//, '\/')}/, ''));end |
#webvtt ⇒ Object
49 50 51 52 53 54 55 56 |
# File 'lib/pig-media-server/model/pig.rb', line 49 def webvtt str = <<EOS WEBVTT #{self.srt.split("\n").delete_if{|x| x.to_i.to_s == x.chomp}.map{|x| if x =~ /\d\d:\d\d/; x.gsub!(/,/, '.');else; x = x.gsub(/</, '<').gsub(/>/, '>');end; x}.join("\n")} EOS str.chomp.chomp end |