Skip to content

Instantly share code, notes, and snippets.

@Unkas82
Created August 2, 2018 22:12
Show Gist options
  • Save Unkas82/4a934d8f73c579e9e2b65ff377544df3 to your computer and use it in GitHub Desktop.
Save Unkas82/4a934d8f73c579e9e2b65ff377544df3 to your computer and use it in GitHub Desktop.
class Seafile < ActiveRecord::Base
belongs_to :user
has_one :message
has_and_belongs_to_many :allow_users, class_name: 'User'
before_save :check_constraints
def share(user)
allow_users << user
end
def save_and_upload(uploaded_file)
raise 'File size should be less 20 Mb' if uploaded_file.size > 20_000_000
self.original_filename = uploaded_file.original_filename
self.content_type = uploaded_file.content_type
save!
File.open(path, 'wb'){ |file| file.write(uploaded_file.read) }
end
def name_html
CGI.escapeHTML(original_filename)
end
def url
path = Pathname.new(original_filename)
name = path.basename(path.extname).to_s
"/files/#{ id }/#{ name.to_url }#{ path.extname }"
end
def path
@path ||= Rails.env != 'test' ? Rails.root.join("public/uploads/f_#{id}") : Rails.root.join("public/uploads/test/" + "#{self.original_filename}")
end
private
def check_constraints
raise if user.blank?
raise if content_type.blank?
raise if original_filename.blank?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment