Browse Source

Added previews, load pdf with selected page.

master
Nils Dittberner 9 years ago
parent
commit
9186bbdede
12 changed files with 126 additions and 18 deletions
  1. +41
    -9
      app/jobs/content_generation_job.rb
  2. +1
    -0
      app/models/content.rb
  3. +11
    -0
      app/models/page_preview.rb
  4. +3
    -5
      app/views/documents/index.html.erb
  5. +10
    -1
      app/views/documents/show.html.erb
  6. +5
    -0
      db/migrate/20161126215021_add_pagecount_to_content.rb
  7. +9
    -0
      db/migrate/20161126222233_create_page_previews.rb
  8. +11
    -0
      db/migrate/20161126222301_add_attachment_preview_to_page_previews.rb
  9. +5
    -0
      db/migrate/20161126234039_add_pagenumber_to_page_preview.rb
  10. +16
    -3
      db/schema.rb
  11. +7
    -0
      test/fixtures/page_previews.yml
  12. +7
    -0
      test/models/page_preview_test.rb

+ 41
- 9
app/jobs/content_generation_job.rb View File

@@ -2,25 +2,57 @@ class ContentGenerationJob < ApplicationJob
require 'docsplit'

@document_id = nil
@document = nil
@content = nil

queue_as :default

after_perform :generate_tags
# after_perform :generate_tags

def perform(document_id)
@document_id = document_id
document = Document.find(document_id)
Docsplit.extract_text(document.doc.path, output: 'tmp/raw_content')
file_path = 'tmp/raw_content/' + File.basename(document.doc.path, 'pdf') + 'txt'
text = IO.read(file_path)
@document = Document.find(document_id)
@content = @document.build_content

content = document.build_content(text: text)
content.save!

File.delete(file_path) if File.exist?(file_path)
generate_metadata
generate_text
generate_page_previews
end

private
def generate_metadata
length = Docsplit.extract_length(@document.doc.path, output: 'tmp/raw_content')
@content.pagecount = length
@content.save
end

def generate_text
Docsplit.extract_text(@document.doc.path, output: 'tmp/raw_content')
file_path = 'tmp/raw_content/' + File.basename(@document.doc.path, 'pdf') + 'txt'
text = IO.read(file_path)
@content.text = text
@content.save!

File.delete(file_path) if File.exist?(file_path)
end

def generate_page_previews
file_basename = File.basename(@document.doc.path, '.pdf')
folder_path = 'tmp/raw_content/' + file_basename
Docsplit.extract_images(@document.doc.path, output: folder_path, size: '200x', format: :png)
@content.pagecount.times do |page|
i = page + 1
page_preview = @content.page_previews.build
puts 'build fine'
file = File.open(folder_path + '/' + File.basename(@document.doc.path, '.pdf') + '_' + i.to_s + '.png', 'rb')
puts 'opened fine'
page_preview.preview = file
page_preview.pagenumber = i
file.close
page_preview.save
end
end

def generate_tags
TagGenerationJob.perform_now @document_id
end


+ 1
- 0
app/models/content.rb View File

@@ -1,3 +1,4 @@
class Content < ApplicationRecord
belongs_to :document
has_many :page_previews, dependent: :destroy
end

+ 11
- 0
app/models/page_preview.rb View File

@@ -0,0 +1,11 @@
class PagePreview < ApplicationRecord
belongs_to :content

has_attached_file :preview,
{
adapter_options: { hash_digest: Digest::SHA256 },
url: "/system/:class/:attachment/:id_partition/:style/:hash.:extension",
hash_secret: Rails.application.secrets.secret_key_base
}
validates_attachment_content_type :preview, content_type: 'image/png'
end

+ 3
- 5
app/views/documents/index.html.erb View File

@@ -7,7 +7,7 @@
<tr>
<th>Category</th>
<th>Name</th>
<th colspan="3"></th>
<th>Created</th>
</tr>
</thead>

@@ -15,10 +15,8 @@
<% @documents.each do |document| %>
<tr>
<td><%= document.category.get_fqcn %></td>
<td><%= document.name %></td>
<td><%= link_to 'Show', document %></td>
<td><%= link_to 'Edit', edit_document_path(document) %></td>
<td><%= link_to 'Destroy', document, method: :delete, data: { confirm: 'Are you sure?' } %></td>
<td><%= link_to document.name, document %></td>
<td><%= document.created_at.localtime %></td>
</tr>
<% end %>
</tbody>


+ 10
- 1
app/views/documents/show.html.erb View File

@@ -10,7 +10,11 @@
<%= @document.category.get_fqcn %>
</p>

<% if File.exists?(@document.doc.path(:thumb)) %>
<% if @document.content.page_previews.any? %>
<% @document.content.page_previews.each do |page_preview| %>
<%= link_to image_tag(page_preview.preview.url, alt: 'Page #' + page_preview.pagenumber.to_s), @document.doc.url + '#page=' + page_preview.pagenumber.to_s %>
<% end %>
<% elsif File.exists?(@document.doc.path(:thumb)) %>
<p>
<%= link_to image_tag(@document.doc.url(:thumb), alt: 'Thumbnail of PDF'), @document.doc.url %>
</p>
@@ -22,6 +26,10 @@

<% if @document.content %>
<p>
<strong>Pagecount:</strong>
<%= @document.content.pagecount %>
</p>
<p>
<strong>Content:</strong>
<%= @document.content.text.truncate_words(30) %>
</p>
@@ -33,4 +41,5 @@


<%= link_to 'Edit', edit_document_path(@document) %> |
<%= link_to 'Destroy', @document, method: :delete, data: { confirm: 'Are you sure?' } %> |
<%= link_to 'Back', documents_path %>

+ 5
- 0
db/migrate/20161126215021_add_pagecount_to_content.rb View File

@@ -0,0 +1,5 @@
class AddPagecountToContent < ActiveRecord::Migration[5.0]
def change
add_column :contents, :pagecount, :integer, default: -1
end
end

+ 9
- 0
db/migrate/20161126222233_create_page_previews.rb View File

@@ -0,0 +1,9 @@
class CreatePagePreviews < ActiveRecord::Migration[5.0]
def change
create_table :page_previews do |t|
t.references :content, foreign_key: true

t.timestamps
end
end
end

+ 11
- 0
db/migrate/20161126222301_add_attachment_preview_to_page_previews.rb View File

@@ -0,0 +1,11 @@
class AddAttachmentPreviewToPagePreviews < ActiveRecord::Migration
def self.up
change_table :page_previews do |t|
t.attachment :preview
end
end

def self.down
remove_attachment :page_previews, :preview
end
end

+ 5
- 0
db/migrate/20161126234039_add_pagenumber_to_page_preview.rb View File

@@ -0,0 +1,5 @@
class AddPagenumberToPagePreview < ActiveRecord::Migration[5.0]
def change
add_column :page_previews, :pagenumber, :integer
end
end

+ 16
- 3
db/schema.rb View File

@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20161125201757) do
ActiveRecord::Schema.define(version: 20161126234039) do

create_table "categories", force: :cascade do |t|
t.string "name"
@@ -25,8 +25,9 @@ ActiveRecord::Schema.define(version: 20161125201757) do
create_table "contents", force: :cascade do |t|
t.text "text"
t.integer "document_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "pagecount", default: -1
t.index ["document_id"], name: "index_contents_on_document_id"
end

@@ -51,6 +52,18 @@ ActiveRecord::Schema.define(version: 20161125201757) do
t.integer "tag_id", null: false
end

create_table "page_previews", force: :cascade do |t|
t.integer "content_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "preview_file_name"
t.string "preview_content_type"
t.integer "preview_file_size"
t.datetime "preview_updated_at"
t.integer "pagenumber"
t.index ["content_id"], name: "index_page_previews_on_content_id"
end

create_table "tags", force: :cascade do |t|
t.string "value"
t.boolean "automatically_asigned"


+ 7
- 0
test/fixtures/page_previews.yml View File

@@ -0,0 +1,7 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one:
content: one

two:
content: two

+ 7
- 0
test/models/page_preview_test.rb View File

@@ -0,0 +1,7 @@
require 'test_helper'

class PagePreviewTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

Loading…
Cancel
Save