<commit_before>require File.dirname(__FILE__) + '/support/setup'
require 'fileutils'

class DocumentRemovePages < CloudCrowd::Action
  
  def process
    begin
      @pdf = document.slug + '.pdf'
      File.open(@pdf, 'w+') {|f| f.write(asset_store.read_pdf(document)) }
      remove_page options['pages']
      FileUtils.rm @pdf
      FileUtils.mv @pdf + '_temp', @pdf
    rescue Exception => e
      LifecycleMailer.deliver_exception_notification(e)
      raise e
    end
    document.id
  end
  
  def merge
    document.update_attributes :access => access
    document.id
  end
  
  private
  
  def remove_page(delete_pages)
    # Which pages to keep
    delete_pages.map! {|p| p.to_i }
    keep_pages = ((1..document.page_count).to_a - delete_pages)
    
    # Rename pages with pdftk, keeping only unremoved pages
    cmd = "pdftk #{@pdf} cat #{keep_pages.join(' ')} output #{document.slug}.pdf_temp"
    `#{cmd}`
    asset_store.save_pdf(document, "#{@pdf}_temp")
    
    # Pull images from S3, delete old images, then upload renamed images
    keep_pages.each_with_index do |p, i|
      sizes = {}
      Page::IMAGE_SIZES.keys.each do |size|
        page = document.page_image_path(p, size)
        new_file = "#{document.slug}-p#{i+1}-#{size}.gif"
        sizes[size] = new_file
        File.open(new_file, 'w+') do |f| <commit_msg>Renaming rebundled PDF during page removal.<commit_after>require File.dirname(__FILE__) + '/support/setup'
require 'fileutils'

class DocumentRemovePages < CloudCrowd::Action
  
  def process
    begin
      @pdf = document.slug + '.pdf'
      File.open(@pdf, 'w+') {|f| f.write(asset_store.read_pdf(document)) }
      remove_page options['pages']
      FileUtils.rm @pdf
      FileUtils.mv @pdf + '_temp', @pdf
    rescue Exception => e
      LifecycleMailer.deliver_exception_notification(e)
      raise e
    end
    document.id
  end
  
  def merge
    document.update_attributes :access => access
    document.id
  end
  
  private
  
  def remove_page(delete_pages)
    # Which pages to keep
    delete_pages.map! {|p| p.to_i }
    keep_pages = ((1..document.page_count).to_a - delete_pages)
    
    # Rename pages with pdftk, keeping only unremoved pages
    cmd = "pdftk #{@pdf} cat #{keep_pages.join(' ')} output #{document.slug}.pdf_temp"
    `#{cmd}`
    asset_store.save_pdf(document, "#{document.slug}.pdf_temp")
    
    # Pull images from S3, delete old images, then upload renamed images
    keep_pages.each_with_index do |p, i|
      sizes = {}
      Page::IMAGE_SIZES.keys.each do |size|
        page = document.page_image_path(p, size)
        new_file = "#{document.slug}-p#{i+1}-#{size}.gif"
        sizes[size] = new_file
        File.open(new_file, 'w+') do |f| 
{'fraction_non_alphanumeric': 0.12678636863319898, 'fraction_numerical': 0.0021986075485525836, 'mean_word_length': 2.9737991266375547, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4211067', 'n_tokens_mistral': 918, 'n_tokens_neox': 937, 'n_words': 211}