Mercurial > zeropaste
comparison app/models/paste.rb @ 499:d2ef59c9a4b4
Use rails rubocop
author | nanaya <me@nanaya.net> |
---|---|
date | Sun, 15 Dec 2024 22:33:27 +0900 |
parents | 84ca55a0568e |
children |
comparison
equal
deleted
inserted
replaced
498:300bac944674 | 499:d2ef59c9a4b4 |
---|---|
1 class Paste < ApplicationRecord | 1 class Paste < ApplicationRecord |
2 attr_accessor :is_private | 2 attr_accessor :is_private |
3 after_initialize :set_privacy, :if => :new_record? | 3 after_initialize :set_privacy, if: :new_record? |
4 | 4 |
5 before_validation :paste_limit | 5 before_validation :paste_limit |
6 before_validation :convert_newlines | 6 before_validation :convert_newlines |
7 before_validation :set_paste_hash | 7 before_validation :set_paste_hash |
8 before_validation :set_paste_key | 8 before_validation :set_paste_key |
9 before_validation :set_paste_secret | 9 before_validation :set_paste_secret |
10 validates :paste, :paste_hash, :key, :ip, :presence => true | 10 validates :paste, :paste_hash, :key, :ip, presence: true |
11 validates :paste, :length => { :maximum => 1_000_000 } | 11 validates :paste, length: { maximum: 1_000_000 } |
12 | 12 |
13 def to_param | 13 def to_param |
14 path | 14 path |
15 end | 15 end |
16 | 16 |
17 def self.safe_find(raw_id) | 17 def self.safe_find(raw_id) |
18 /\A(?<id>[0-9]+)(?:-(?<secret>[0-9a-f]+))?\z/i =~ raw_id.to_s | 18 /\A(?<id>[0-9]+)(?:-(?<secret>[0-9a-f]+))?\z/i =~ raw_id.to_s |
19 | 19 |
20 find_by(:secret => secret.try(:downcase), :id => id) | 20 find_by(secret: secret.try(:downcase), id: id) |
21 end | 21 end |
22 | 22 |
23 def self.graceful_create(params) | 23 def self.graceful_create(params) |
24 paste = new(params) | 24 paste = new(params) |
25 fresh = true | 25 fresh = true |
26 created = true | 26 created = true |
27 | 27 |
28 begin | 28 begin |
29 created = paste.save | 29 created = paste.save |
30 rescue ActiveRecord::RecordNotUnique | 30 rescue ActiveRecord::RecordNotUnique |
31 paste = find_by(:ip => paste.ip, :paste_hash => paste.paste_hash) | 31 paste = find_by(ip: paste.ip, paste_hash: paste.paste_hash) |
32 fresh = false | 32 fresh = false |
33 end | 33 end |
34 | 34 |
35 [created, paste, fresh] | 35 [ created, paste, fresh ] |
36 end | 36 end |
37 | 37 |
38 def paste_gzip=(paste) | 38 def paste_gzip=(paste) |
39 paste = paste.read if paste.is_a? ActionDispatch::Http::UploadedFile | 39 paste = paste.read if paste.is_a? ActionDispatch::Http::UploadedFile |
40 | 40 |
53 false | 53 false |
54 end | 54 end |
55 end | 55 end |
56 | 56 |
57 def path | 57 def path |
58 [id, secret.presence].compact.join("-") | 58 [ id, secret.presence ].compact.join("-") |
59 end | 59 end |
60 | 60 |
61 def set_paste_hash | 61 def set_paste_hash |
62 self.paste_hash = Digest::SHA512.hexdigest("#{paste}\n") | 62 self.paste_hash = Digest::SHA512.hexdigest("#{paste}\n") |
63 end | 63 end |
77 def convert_newlines | 77 def convert_newlines |
78 self.paste = paste.to_s.gsub("\r\n", "\n").tr("\r", "\n") | 78 self.paste = paste.to_s.gsub("\r\n", "\n").tr("\r", "\n") |
79 end | 79 end |
80 | 80 |
81 def paste_limit | 81 def paste_limit |
82 ip_post_recent_count = self.class.where(:ip => ip).where("created_at > ?", Time.zone.now - 1.hour).count | 82 ip_post_recent_count = self.class.where(ip: ip).where("created_at > ?", Time.zone.now - 1.hour).count |
83 errors.add :base, :limit if ip_post_recent_count > 100 | 83 errors.add :base, :limit if ip_post_recent_count > 100 |
84 end | 84 end |
85 | 85 |
86 def set_privacy | 86 def set_privacy |
87 self.is_private ||= "0" | 87 self.is_private ||= "0" |