Class: FileProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/file_processor.rb

Overview

Process the incorporation of a file to Buhos Could handle upload files, and other types of files

Defined Under Namespace

Classes: NoUploadedFilesType

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, basedir = nil) ⇒ FileProcessor

Returns a new instance of FileProcessor.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/file_processor.rb', line 41

def initialize(file,basedir=nil)
  @basedir=basedir
  @filetype="application/octet-stream"
  @file_id=nil
  if file.is_a? String and File.exist? file # Ok, is a filepath
    require 'mimemagic'
    @filetype=MimeMagic.by_magic(File.open(file)).type
    @filename=File.basename(file)
    @filepath=file
  elsif file.respond_to?("[]") and !file[:tempfile].nil? # Is uploaded
    @filetype=file[:type]
    @filename=file[:filename]
    @filepath=file[:tempfile]
  elsif file.is_a? IFile
    @filetype=file[:typetype]
    @filename=file[:filename]
    @filepath="#{basedir}/#{file[:file_path]}"
    @file_id=file[:id]
  else
    raise "I don't know what type of file is it"
  end
  create_file_on_system unless @file_id
  @filename=@filename.gsub(/[^A-Za-z0-9\.-_]/,"")
end

Instance Attribute Details

#basedirObject (readonly)

Returns the value of attribute basedir.



36
37
38
# File 'lib/file_processor.rb', line 36

def basedir
  @basedir
end

#file_idObject (readonly)

Returns the value of attribute file_id.



35
36
37
# File 'lib/file_processor.rb', line 35

def file_id
  @file_id
end

#filenameObject (readonly)

Returns the value of attribute filename.



33
34
35
# File 'lib/file_processor.rb', line 33

def filename
  @filename
end

#filepathObject (readonly)

Returns the value of attribute filepath.



34
35
36
# File 'lib/file_processor.rb', line 34

def filepath
  @filepath
end

#filetypeObject (readonly)

Returns the value of attribute filetype.



32
33
34
# File 'lib/file_processor.rb', line 32

def filetype
  @filetype
end

Instance Method Details

#add_to_cd(cd) ⇒ Object



77
78
79
80
81
82
83
84
# File 'lib/file_processor.rb', line 77

def add_to_cd(cd)
  $db.transaction do
    archivo_cd_o=FileCd.where(:file_id=>file_id, :canonical_document_id=>cd[:id])
    if archivo_cd_o.empty?
      FileCd.insert(:file_id=>file_id, :canonical_document_id=>cd[:id])
    end
  end
end

#add_to_record_search(search, record) ⇒ Object



86
87
88
89
90
91
92
93
94
# File 'lib/file_processor.rb', line 86

def add_to_record_search(search,record)
  $db.transaction do
    raise NoUploadedFilesType, "Search should be uploaded_files type" unless search.is_type?(:uploaded_files)
    rec_sec=RecordsSearch[:record_id=>record[:id], :search_id=>search[:id]]
    if rec_sec
      rec_sec.update(:file_id=>file_id)
    end
  end
end

#add_to_sr(systematic_review) ⇒ Object



68
69
70
71
72
73
74
75
# File 'lib/file_processor.rb', line 68

def add_to_sr(systematic_review)
  $db.transaction do
    archivo_rs_o=FileSr.where(:file_id=>file_id,:systematic_review_id=>systematic_review[:id])
    if archivo_rs_o.empty?
       FileSr.insert(:file_id=>file_id,:systematic_review_id=>systematic_review[:id])
    end
  end
end