10 @@DC_NS_URL = 'http://purl.org/dc/elements/1.1/'
14 @classification_id = nil
24 def load_from_file!(fileName)
26 parse_file_name!(fileName)
29 def self.can_handle?(fileName)
34 #puts "Filename: " + fileName.to_s
35 lowerName = fileName.downcase()
37 if lowerName.end_with?(".epub")
41 if lowerName.end_with?(".pdf")
60 def classification_id=(value)
61 @classification_id = value
76 def description=(value)
84 result.push('<b>' + @title + '</b>')
86 result.push('<i>(Unknown title)</i>')
89 result.push('<i>by ' + @author.reading_order + '</i>')
93 series = @store.load_series(@series_id)
94 if nil != series and nil != series.descr
95 seriesInfo.push(series.descr.to_s)
98 seriesInfo.push(@volume.to_s)
100 if seriesInfo.length > 0
101 result.push(seriesInfo.join(' '))
104 return result.join('<br/>')
110 data.push('author="' + @author.inspect + '"')
113 data.push('series_id="' + @series_id.to_s() + '"')
116 data.push('volume="' + @volume + '"')
119 data.push('title="' + @title + '"')
122 data.push(@cover.inspect())
125 data.push('path="' + @path + '"')
127 return '(Book:' + data.join(',') + ')'
142 def series_id=(value)
163 return File.basename(@path, '.*')
176 return /[[:upper:]]/.match(c)
180 def massage_author(input)
186 input.each_char do |c|
187 if isUpper?(c) and (reading_order.length > 0)
196 # Returns (series, volumeNo, titleText)
198 def processTitle(input)
203 arr = input.split('_')
209 matchData = (arr[0]).match(/^([A-Z]+)([0-9]+)$/)
211 capt = matchData.captures
217 pos = arr[-1].rindex('.')
219 arr[-1] = arr[-1].slice(0, pos)
222 title = arr.join(' ')
224 return series, vol, title
228 def parse_file_name!(file_name)
229 parts = file_name.split('/')
230 (series_code, @volume, @title) = processTitle(parts[-1])
233 reading_order = massage_author(grouping)
235 @author = Author.new(grouping, reading_order, sort_order)
236 @series_id = @store.get_series(grouping, series_code)
239 lc_file_name = file_name.downcase
240 if lc_file_name.end_with?(".epub")
242 elsif lc_file_name.end_with?(".pdf")
246 @classification_id = @store.find_classification(@author.grouping, File.basename(file_name, '.*'))
250 def scanEpub!(fileName)
251 #puts 'Scanning "' + fileName.to_s + '"...'
253 Zip::File.open(fileName) do |zipfile|
254 entry = zipfile.find_entry('META-INF/container.xml')
256 puts 'No META-INF/container.xml, skipping book ' + fileName
259 contXml = zipfile.read('META-INF/container.xml')
260 contDoc = Nokogiri::XML(contXml)
261 opfPath = contDoc.css("container rootfiles rootfile")[0]['full-path']
263 scanOpf!(zipfile, opfPath)
265 rescue Zip::Error => exc
266 puts 'ERROR processing file "' + fileName + '":'
273 def scan_pdf!(file_name)
274 #puts 'Scanning "' + file_name.to_s + '"...'
276 pdf_path = File.expand_path(file_name).to_s
277 if ! pdf_path.end_with?('.pdf')
278 puts 'Unexpected internal error: path "' + file_name.to_s + '" does not end with ".pdf".'
282 jpeg_path = pdf_path[0..-5] + '.jpeg'
283 if File.file?(jpeg_path)
284 File.open(jpeg_path, 'r') do |is|
285 @cover = Cover.new(is, jpeg_path, 'image/jpeg')
292 def scanOpf!(zipfile, opfPath)
295 opfXml = zipfile.read(opfPath)
296 opfDoc = Nokogiri::XML(opfXml)
301 grouping = @author.grouping
302 reading_order = @author.reading_order
303 sort_order = @author.sort_order
305 creators = opfDoc.css('dc|creator', 'dc' => @@DC_NS_URL)
306 if (creators.length > 0)
307 creator = creators[0]
309 role = creator['opf:role']
311 reading_order = creator.content
313 file_as = creator['opf:file-as']
319 @author = Author.new(grouping, reading_order, sort_order)
323 #---------------------------------------
326 titles = opfDoc.css('dc|title', 'dc' => @@DC_NS_URL)
330 @title = title.content
334 #---------------------------------------
337 descrNodes = opfDoc.css('dc|description', 'dc' => @@DC_NS_URL)
338 if (descrNodes.length > 0)
339 descrNode = descrNodes[0]
341 @description = descrNode.content
345 #---------------------------------------
346 # Other metadata: series, volume, cover
348 metas = opfDoc.css('package metadata meta')
351 content = m['content']
353 if 'calibre:series' == name
354 # TODO: Dynamically create a new series?
355 # @series_id = content
356 elsif 'calibre:series-index' == name
358 elsif 'cover' == name
360 #puts 'File ' + @path + ' coverId ' + coverId
367 @cover = load_cover(zipfile, opfPath, opfDoc, coverId)
371 def load_cover(zipfile, opfPath, opfDoc, coverId)
374 coverId = "cover-image"
377 items = opfDoc.css('package manifest item')
381 mimeType = i['media-type']
384 entry = zipfile.find_entry(href)
387 # Although the epub standard requires the path to be relative
388 # to the base of the epub (zip), some books encountered in the
389 # wild have been found to use a bath relative to the location
391 parts = opfPath.split('/')
392 opfBasePath = opfPath.split('/')[0..-2].join('/')
393 coverPath = opfBasePath + '/' + href
394 entry = zipfile.find_entry(coverPath)
398 puts 'WARNING! Cover image "' + href + '" not found in file "' + @path + '".'
401 entry.get_input_stream() do |is|
402 return Cover.new(is, href, mimeType)