2013-07-09 15:04:03 +07:00
|
|
|
class Paste
|
2013-07-11 02:16:26 +07:00
|
|
|
attr_reader :contents, :syntax
|
2013-07-09 15:04:03 +07:00
|
|
|
|
|
|
|
class << self
|
|
|
|
def find(id)
|
|
|
|
record = DB[:pastes].where(id: id).first
|
2013-07-11 02:16:26 +07:00
|
|
|
record ? new(record[:contents], record[:syntax]) : nil
|
2013-07-09 15:04:03 +07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-11 02:16:26 +07:00
|
|
|
def initialize(contents, syntax = nil)
|
2013-07-09 15:04:03 +07:00
|
|
|
@contents = contents
|
2013-07-11 02:16:26 +07:00
|
|
|
@syntax = syntax if CONFIG.available_syntaxes.include?(syntax)
|
2013-07-09 14:53:49 +07:00
|
|
|
end
|
|
|
|
|
2013-07-09 15:04:03 +07:00
|
|
|
def save
|
|
|
|
encrypt!
|
2013-07-11 02:16:26 +07:00
|
|
|
DB[:pastes].insert(contents: contents, syntax: syntax)
|
2013-07-09 15:04:03 +07:00
|
|
|
end
|
2013-07-09 14:53:49 +07:00
|
|
|
|
2013-07-09 15:04:03 +07:00
|
|
|
def decrypt(key)
|
|
|
|
@key = key
|
|
|
|
decrypt!
|
|
|
|
self
|
2013-07-09 14:53:49 +07:00
|
|
|
end
|
|
|
|
|
2013-07-09 15:04:03 +07:00
|
|
|
def key
|
|
|
|
@key ||= SecureRandom.hex
|
|
|
|
end
|
2013-07-09 14:53:49 +07:00
|
|
|
|
2013-07-09 15:04:03 +07:00
|
|
|
def encrypt!
|
|
|
|
@contents = Base64.encode64(Encryptor.encrypt(value: contents, key: key))
|
2013-07-09 14:53:49 +07:00
|
|
|
end
|
|
|
|
|
2013-07-09 15:04:03 +07:00
|
|
|
def decrypt!
|
|
|
|
@contents = Encryptor.decrypt(value: Base64.decode64(contents), key: key)
|
|
|
|
end
|
2013-07-10 19:02:19 +07:00
|
|
|
|
|
|
|
def highlighted
|
2013-07-11 02:16:26 +07:00
|
|
|
Pygments.highlight(contents, lexer: syntax, options: { linenos: 'table' })
|
2013-07-10 19:02:19 +07:00
|
|
|
end
|
|
|
|
|
|
|
|
def paragraph
|
2013-07-11 01:20:10 +07:00
|
|
|
"<pre>#{contents}</pre>"
|
2013-07-10 19:02:19 +07:00
|
|
|
end
|
|
|
|
|
|
|
|
def html
|
2013-07-11 02:16:26 +07:00
|
|
|
syntax ? highlighted : paragraph
|
2013-07-10 19:02:19 +07:00
|
|
|
end
|
2013-07-09 14:53:49 +07:00
|
|
|
end
|