Class: Paperbin::Handler

Inherits:
Struct
  • Object
show all
Defined in:
lib/paperbin/handler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#idObject

Returns the value of attribute id

Returns:

  • (Object)

    the current value of id



1
2
3
# File 'lib/paperbin/handler.rb', line 1

def id
  @id
end

#typeObject

Returns the value of attribute type

Returns:

  • (Object)

    the current value of type



1
2
3
# File 'lib/paperbin/handler.rb', line 1

def type
  @type
end

Instance Method Details

#archive_pathObject



44
45
46
# File 'lib/paperbin/handler.rb', line 44

def archive_path
  File.join(directory_path, "#{archive_split_id}.zip")
end

#archive_split_idObject



17
18
19
# File 'lib/paperbin/handler.rb', line 17

def archive_split_id
  split.last
end

#check_versionsObject



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/paperbin/handler.rb', line 152

def check_versions
  versions.each_with_index do |version, index|
    # check both file exist or not
    next unless files_exist?(md5_file_path(version), json_file_path(version))

    if md5_valid?(version)
      process_valid_records(version, versions.last)
    else
      # remove both files
      [json_file_path(version), md5_file_path(version)].each do |f|
        File.delete(f)
      end
      raise Errno::ENOENT
    end
  end
rescue Errno::ENOENT
  # lodge worker unless valid
  Paperbin::WriteWorker.perform_async(id, type)
end

#create_archive_fileObject



48
49
50
51
52
53
54
55
# File 'lib/paperbin/handler.rb', line 48

def create_archive_file
  unless File.exists?(archive_path)
    Zippy.create archive_path do |z|
      z["created_at.txt"] = Date.today.to_s
      handle_old_files(z)
    end
  end
end

#create_directoryObject



81
82
83
# File 'lib/paperbin/handler.rb', line 81

def create_directory
  FileUtils.mkdir_p(directory_path) unless Dir.exists?(directory_path)
end

#dir_split_idObject



13
14
15
# File 'lib/paperbin/handler.rb', line 13

def dir_split_id
  split[0...-1]
end

#directory_pathObject



31
32
33
34
35
36
37
38
# File 'lib/paperbin/handler.rb', line 31

def directory_path
  dirs = []
  dirs << options[:path]
  dirs << item.send(options[:base_scope])
  dirs << type
  dirs += dir_split_id
  File.join(dirs.map(&:to_s))
end

#files_exist?(*args) ⇒ Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/paperbin/handler.rb', line 101

def files_exist?(*args)
  Array(args).map{|file| File.exist?(file)}.all?
end

#formatted_idObject



5
6
7
# File 'lib/paperbin/handler.rb', line 5

def formatted_id
  "%012d" % id
end

#generate_filesObject



125
126
127
128
129
130
# File 'lib/paperbin/handler.rb', line 125

def generate_files
  versions.each do |version|
    write_json_file version
    write_md5_file version
  end
end

#handle_old_files(z) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/paperbin/handler.rb', line 57

def handle_old_files(z)
  if File.directory?(old_directory_path)
    Dir.new(old_directory_path).each do |file|
      if file =~ /\.gz/
        name = file.split(".").first
        z["#{name}.json"] = Zlib::GzipReader.open(File.join(old_directory_path, file)) {|gz| gz.read }
      end
    end
    FileUtils.remove_dir(old_directory_path)
  end
end

#itemObject



25
26
27
28
29
# File 'lib/paperbin/handler.rb', line 25

def item
  scope = type.constantize
  scope = scope.with_deleted if scope.respond_to?(:with_deleted)
  scope.where(id: id).first
end

#json_file(version) ⇒ Object



93
94
95
# File 'lib/paperbin/handler.rb', line 93

def json_file(version)
  "#{version.id}.json"
end

#json_file_path(version, checked = false) ⇒ Object



97
98
99
# File 'lib/paperbin/handler.rb', line 97

def json_file_path(version, checked = false)
  File.join(directory_path, "#{json_file(version)}#{checked ? '' : '.unchecked'}")
end

#md5_file(version) ⇒ Object



85
86
87
# File 'lib/paperbin/handler.rb', line 85

def md5_file(version)
  "#{version.id}.md5"
end

#md5_file_path(version) ⇒ Object



89
90
91
# File 'lib/paperbin/handler.rb', line 89

def md5_file_path(version)
  File.join(directory_path, "#{md5_file(version)}")
end

#md5_valid?(version) ⇒ Boolean

Returns:

  • (Boolean)


105
106
107
108
109
110
# File 'lib/paperbin/handler.rb', line 105

def md5_valid?(version)
  record_md5 = File.read(md5_file_path(version))
  data = File.open(json_file_path(version), "r") { |f| f.read }
  check_md5 = Digest::MD5.hexdigest(data)
  record_md5 == check_md5
end

#old_directory_pathObject



40
41
42
# File 'lib/paperbin/handler.rb', line 40

def old_directory_path
  File.join(directory_path, archive_split_id)
end

#optionsObject



21
22
23
# File 'lib/paperbin/handler.rb', line 21

def options
  Paperbin::Config.default_options
end

#process_valid_records(version, last_item) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/paperbin/handler.rb', line 112

def process_valid_records(version, last_item)
  # remove records from db expcet the lastest one
  version.delete unless version == last_item
  # rename file extension
  File.rename(json_file_path(version), json_file_path(version, true))
  Zippy.open(archive_path) do |z|
    z["#{json_file(version).to_s}"] = File.open(json_file_path(version, true)) { |f| f.read }
  end
  File.delete(md5_file_path(version))
  File.delete(json_file_path(version, true))
  options[:callback].call(json_file_path(version, true)) if options[:callback]
end

#save_versionsObject



73
74
75
76
77
78
79
# File 'lib/paperbin/handler.rb', line 73

def save_versions
  return true unless item
  create_directory
  create_archive_file
  generate_files
  Paperbin::CheckWorker.perform_async(id, type)
end

#splitObject



9
10
11
# File 'lib/paperbin/handler.rb', line 9

def split
  formatted_id.scan(/.{4}/)
end

#string_data(version) ⇒ Object



148
149
150
# File 'lib/paperbin/handler.rb', line 148

def string_data(version)
  version.to_json
end

#versionsObject



69
70
71
# File 'lib/paperbin/handler.rb', line 69

def versions
  Version.where(item_type: type, item_id: id)
end

#write_json_file(version) ⇒ Object



132
133
134
135
136
137
138
139
# File 'lib/paperbin/handler.rb', line 132

def write_json_file(version)
  path = json_file_path version
  unless files_exist?(path)
    File.open(path, "w") { |f| f.write(string_data(version)) }
    timestamp = version.created_at.to_time
    File.utime timestamp, timestamp, path
  end
end

#write_md5_file(version) ⇒ Object



141
142
143
144
145
146
# File 'lib/paperbin/handler.rb', line 141

def write_md5_file(version)
  File.open(md5_file_path(version), "w") do |file|
    md5 = Digest::MD5.hexdigest(string_data(version))
    file.write md5
  end
end