Class: Audiobank::Client::LocalFile

Inherits:
Object
  • Object
show all
Defined in:
lib/audiobank/client/local_file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ LocalFile

Returns a new instance of LocalFile.



9
10
11
# File 'lib/audiobank/client/local_file.rb', line 9

def initialize(filename)
  @filename = @uploaded_file = filename
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



7
8
9
# File 'lib/audiobank/client/local_file.rb', line 7

def filename
  @filename
end

#uploaded_fileObject (readonly)

Returns the value of attribute uploaded_file.



7
8
9
# File 'lib/audiobank/client/local_file.rb', line 7

def uploaded_file
  @uploaded_file
end

Instance Method Details

#basenameObject



17
18
19
# File 'lib/audiobank/client/local_file.rb', line 17

def basename
  @basename ||= File.basename(filename)
end

#convertObject



45
46
47
48
49
50
51
52
53
54
# File 'lib/audiobank/client/local_file.rb', line 45

def convert
  if extension == "wav"
    # unless FileUtils.uptodate? flac_file, filename
      Audiobank::Client.logger.info "Create flac version"
      system "flac -f -o #{flac_file} '#{filename}'" or raise "Can't convert file to wav"
    # end
    Audiobank::Client.logger.info "Use flac version"
    @uploaded_file = flac_file
  end
end

#default_titleObject



35
36
37
# File 'lib/audiobank/client/local_file.rb', line 35

def default_title
  File.basename(filename, File.extname(filename))
end

#document_idObject



25
26
27
28
29
# File 'lib/audiobank/client/local_file.rb', line 25

def document_id
  if basename =~ /^([0-9]+)-/
    $1.to_i
  end
end

#extensionObject



21
22
23
# File 'lib/audiobank/client/local_file.rb', line 21

def extension
  @extension ||= File.extname(filename)[1..-1]
end

#file_uuidObject



31
32
33
# File 'lib/audiobank/client/local_file.rb', line 31

def file_uuid
  document_id or Digest::SHA256.hexdigest(filename)
end

#filename_without_extensionObject



13
14
15
# File 'lib/audiobank/client/local_file.rb', line 13

def filename_without_extension
  @filename_without_extension ||= filename.gsub(/\.#{extension}$/, "")
end

#flac_fileObject



39
40
41
42
43
# File 'lib/audiobank/client/local_file.rb', line 39

def flac_file
  # "#{filename_without_extension}.flac"
  # (@flac_tempfile ||= Tempfile.new(['audiobank', '.flac'])).path
  "/tmp/#{file_uuid}.flac"
end

#import(account) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/audiobank/client/local_file.rb', line 60

def import()
  unless FileUtils.uptodate? uploaded_file, [report_file]
    Audiobank::Client.logger.info "File already imported"
    return
  end

  convert

  imported = false
  if document_id
    Audiobank::Client.logger.debug "Found document id : #{document_id}"
    document = .document(document_id)
    if document
      imported = document.import uploaded_file, import_options
    else
      raise "Document #{document_id} not found"
    end
  else
    imported = .documents.import uploaded_file, { :title => default_title }, import_options
  end

  if imported
    Audiobank::Client.logger.debug "Create report #{report_file}"
    system "touch '#{report_file}'"
    # FileUtils.touch report_file
  else
    Audiobank::Client.logger.info "Failed to import #{filename}"
  end
end

#import_optionsObject



56
57
58
# File 'lib/audiobank/client/local_file.rb', line 56

def import_options
  { :resume => true, :debug => true, :retries => 10, :progress => true }
end

#report_fileObject



90
91
92
# File 'lib/audiobank/client/local_file.rb', line 90

def report_file
  "#{filename}.audiobank"
end