Class: Pig

Inherits:
Object
  • Object
show all
Defined in:
lib/pig-media-server/model/pig.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(record) ⇒ Pig

Returns a new instance of Pig.



10
11
12
13
14
15
16
# File 'lib/pig-media-server/model/pig.rb', line 10

def initialize record
  unless record.size
    record.size = File::Stat.new(record.path).size.to_s rescue nil
  end
  self.record = record
  self.config = CONFIG
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



9
10
11
# File 'lib/pig-media-server/model/pig.rb', line 9

def config
  @config
end

#recordObject

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

.allObject



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

#checkObject



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

#comicObject



46
47
48
# File 'lib/pig-media-server/model/pig.rb', line 46

def comic
  Comic.new self.record
end

#keyObject



17
# File 'lib/pig-media-server/model/pig.rb', line 17

def key; self.record._key;end

#metadataObject



22
# File 'lib/pig-media-server/model/pig.rb', line 22

def ; self.record.;end

#mtimeObject



19
# File 'lib/pig-media-server/model/pig.rb', line 19

def mtime; Time.at(self.record.mtime);end

#mtime_to_iObject



20
# File 'lib/pig-media-server/model/pig.rb', line 20

def mtime_to_i; self.record.mtime; end

#nameObject



18
# File 'lib/pig-media-server/model/pig.rb', line 18

def name; self.record.path.split('/').last; end

#pathObject



43
44
45
# File 'lib/pig-media-server/model/pig.rb', line 43

def path
  self.record.path.sub(/#{config['path'].sub(/\//, '\/')}/, '')
end

#sizeObject



21
# File 'lib/pig-media-server/model/pig.rb', line 21

def size; self.record.size;end

#srtObject



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

#typeObject



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

#urlObject



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

#webvttObject



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