Class: Attachment

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Elasticsearch::Model, Elasticsearch::Model::Callbacks
Defined in:
app/models/attachment.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create_from_uploaded_file(file, user, options = {}) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/models/attachment.rb', line 36

def self.create_from_uploaded_file(file, user, options = {})
  filename = file.original_filename
  attachment = Attachment.create( options.merge(
    filename: filename,
    content_type: file.content_type,
    size: file.size,
  ))

  if attachment.errors.none?
    attachment.save_to_gollum(
      file, user, 
      "uploading file: #{filename}"
    )
    attachment.__elasticsearch__.index_document
  end

  attachment
end

Instance Method Details

#as_indexed_json(options = {}) ⇒ Object



100
101
102
103
104
105
106
# File 'app/models/attachment.rb', line 100

def as_indexed_json(options = {})
  json = self.as_json
  json[:type] = self.class.to_s.underscore
  json[:body] = self.extracted_text

  json
end

#file_dataObject



73
74
75
# File 'app/models/attachment.rb', line 73

def file_data
  wiki_file.raw_data
end

#save_and_destroy(deleted_by) ⇒ Object



140
141
142
# File 'app/models/attachment.rb', line 140

def save_and_destroy(deleted_by)
  DeletedItem.save_and_destroy(self, deleted_by)
end

#save_to_gollum(file, user, message) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/models/attachment.rb', line 85

def save_to_gollum(file, user, message)
  commit = { 
    message:  message,
    name:     user.name,
    email:    user.email
  }

  data_file = file.try(:tempfile) || file
  if wiki_page.present?
    $wiki.update_page(wiki_page, wiki_page.name, wiki_page.format, data_file, commit)
  else
    $wiki.write_page(wiki_attachment_data, :textile, data_file, commit, wiki_dir)
  end
end

#set_extracted_textObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'app/models/attachment.rb', line 108

def set_extracted_text
  tmp_dir = File.join Rails.root, "tmp", "#{id}_#{Time.now.to_i}"
  text_dir = File.join tmp_dir, "text_dump"
  tmp_filename = File.join tmp_dir, filename

  FileUtils.mkdir_p tmp_dir

  File.open tmp_filename, "wb" do |file|
    file.write self.file_data
  end

  docs = Dir[File.join(tmp_dir, "*")]
  Docsplit.extract_text(docs, output: text_dir)

  texts = []
  Dir[File.join(text_dir, "*")].each do |file|
    texts << File.read(file)
  end

  text = texts.join("\n")

  encoding_options = {
    :invalid           => :replace,  # Replace invalid byte sequences
    :undef             => :replace,  # Replace anything not defined in ASCII
    :replace           => '',        # Use a blank for those replacements
    :universal_newline => true       # Always break lines with \n
  }
  self.extracted_text = text.encode(Encoding.find('ASCII'), encoding_options)
rescue
  Rails.logger.info "document not extractable."
end

#update(file, user) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/models/attachment.rb', line 55

def update(file, user)
  filename = file.original_filename
  if update_attributes(
    filename: filename,
    content_type: file.content_type,
    size: file.size)

    save_to_gollum(
      file, user, 
      "uploading file: #{filename}"
    )
  end
end

#versionsObject



81
82
83
# File 'app/models/attachment.rb', line 81

def versions
  wiki_page.versions
end

#wiki_fileObject



69
70
71
# File 'app/models/attachment.rb', line 69

def wiki_file
  $wiki.file(wiki_page.path)
end

#wiki_pageObject



77
78
79
# File 'app/models/attachment.rb', line 77

def wiki_page
  $wiki.page(wiki_attachment_data, nil, wiki_dir)
end