| @@ -2,25 +2,57 @@ class ContentGenerationJob < ApplicationJob | |||||
| require 'docsplit' | require 'docsplit' | ||||
| @document_id = nil | @document_id = nil | ||||
| @document = nil | |||||
| @content = nil | |||||
| queue_as :default | queue_as :default | ||||
| after_perform :generate_tags | |||||
| # after_perform :generate_tags | |||||
| def perform(document_id) | def perform(document_id) | ||||
| @document_id = 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 | end | ||||
| private | 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 | def generate_tags | ||||
| TagGenerationJob.perform_now @document_id | TagGenerationJob.perform_now @document_id | ||||
| end | end | ||||
| @@ -1,3 +1,4 @@ | |||||
| class Content < ApplicationRecord | class Content < ApplicationRecord | ||||
| belongs_to :document | belongs_to :document | ||||
| has_many :page_previews, dependent: :destroy | |||||
| end | end | ||||
| @@ -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 | |||||
| @@ -7,7 +7,7 @@ | |||||
| <tr> | <tr> | ||||
| <th>Category</th> | <th>Category</th> | ||||
| <th>Name</th> | <th>Name</th> | ||||
| <th colspan="3"></th> | |||||
| <th>Created</th> | |||||
| </tr> | </tr> | ||||
| </thead> | </thead> | ||||
| @@ -15,10 +15,8 @@ | |||||
| <% @documents.each do |document| %> | <% @documents.each do |document| %> | ||||
| <tr> | <tr> | ||||
| <td><%= document.category.get_fqcn %></td> | <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> | </tr> | ||||
| <% end %> | <% end %> | ||||
| </tbody> | </tbody> | ||||
| @@ -10,7 +10,11 @@ | |||||
| <%= @document.category.get_fqcn %> | <%= @document.category.get_fqcn %> | ||||
| </p> | </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> | <p> | ||||
| <%= link_to image_tag(@document.doc.url(:thumb), alt: 'Thumbnail of PDF'), @document.doc.url %> | <%= link_to image_tag(@document.doc.url(:thumb), alt: 'Thumbnail of PDF'), @document.doc.url %> | ||||
| </p> | </p> | ||||
| @@ -22,6 +26,10 @@ | |||||
| <% if @document.content %> | <% if @document.content %> | ||||
| <p> | <p> | ||||
| <strong>Pagecount:</strong> | |||||
| <%= @document.content.pagecount %> | |||||
| </p> | |||||
| <p> | |||||
| <strong>Content:</strong> | <strong>Content:</strong> | ||||
| <%= @document.content.text.truncate_words(30) %> | <%= @document.content.text.truncate_words(30) %> | ||||
| </p> | </p> | ||||
| @@ -33,4 +41,5 @@ | |||||
| <%= link_to 'Edit', edit_document_path(@document) %> | | <%= link_to 'Edit', edit_document_path(@document) %> | | ||||
| <%= link_to 'Destroy', @document, method: :delete, data: { confirm: 'Are you sure?' } %> | | |||||
| <%= link_to 'Back', documents_path %> | <%= link_to 'Back', documents_path %> | ||||
| @@ -0,0 +1,5 @@ | |||||
| class AddPagecountToContent < ActiveRecord::Migration[5.0] | |||||
| def change | |||||
| add_column :contents, :pagecount, :integer, default: -1 | |||||
| end | |||||
| end | |||||
| @@ -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 | |||||
| @@ -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 | |||||
| @@ -0,0 +1,5 @@ | |||||
| class AddPagenumberToPagePreview < ActiveRecord::Migration[5.0] | |||||
| def change | |||||
| add_column :page_previews, :pagenumber, :integer | |||||
| end | |||||
| end | |||||
| @@ -10,7 +10,7 @@ | |||||
| # | # | ||||
| # It's strongly recommended that you check this file into your version control system. | # 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| | create_table "categories", force: :cascade do |t| | ||||
| t.string "name" | t.string "name" | ||||
| @@ -25,8 +25,9 @@ ActiveRecord::Schema.define(version: 20161125201757) do | |||||
| create_table "contents", force: :cascade do |t| | create_table "contents", force: :cascade do |t| | ||||
| t.text "text" | t.text "text" | ||||
| t.integer "document_id" | 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" | t.index ["document_id"], name: "index_contents_on_document_id" | ||||
| end | end | ||||
| @@ -51,6 +52,18 @@ ActiveRecord::Schema.define(version: 20161125201757) do | |||||
| t.integer "tag_id", null: false | t.integer "tag_id", null: false | ||||
| end | 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| | create_table "tags", force: :cascade do |t| | ||||
| t.string "value" | t.string "value" | ||||
| t.boolean "automatically_asigned" | t.boolean "automatically_asigned" | ||||
| @@ -0,0 +1,7 @@ | |||||
| # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html | |||||
| one: | |||||
| content: one | |||||
| two: | |||||
| content: two | |||||
| @@ -0,0 +1,7 @@ | |||||
| require 'test_helper' | |||||
| class PagePreviewTest < ActiveSupport::TestCase | |||||
| # test "the truth" do | |||||
| # assert true | |||||
| # end | |||||
| end | |||||