forked from Gitlink/forgeplus
fixed 附件uuid生成暂不使用
This commit is contained in:
parent
4d9e36a683
commit
70e0754315
|
@ -2,38 +2,47 @@
|
|||
#
|
||||
# Table name: attachments
|
||||
#
|
||||
# id :integer not null, primary key
|
||||
# container_id :integer
|
||||
# container_type :string(30)
|
||||
# filename :string(255) default(""), not null
|
||||
# disk_filename :string(255) default(""), not null
|
||||
# filesize :integer default("0"), not null
|
||||
# content_type :string(255) default("")
|
||||
# digest :string(60) default(""), not null
|
||||
# downloads :integer default("0"), not null
|
||||
# author_id :integer default("0"), not null
|
||||
# created_on :datetime
|
||||
# description :text(65535)
|
||||
# disk_directory :string(255)
|
||||
# attachtype :integer default("1")
|
||||
# is_public :integer default("1")
|
||||
# copy_from :integer
|
||||
# quotes :integer
|
||||
# is_publish :integer default("1")
|
||||
# publish_time :datetime
|
||||
# memo_image :boolean default("0")
|
||||
# extra_type :integer default("0")
|
||||
# cloud_url :string(255)
|
||||
# id :integer not null, primary key
|
||||
# container_id :integer
|
||||
# container_type :string(30)
|
||||
# filename :string(255) default(""), not null
|
||||
# disk_filename :string(255) default(""), not null
|
||||
# filesize :integer default("0"), not null
|
||||
# content_type :string(255) default("")
|
||||
# digest :string(60) default(""), not null
|
||||
# downloads :integer default("0"), not null
|
||||
# author_id :integer default("0"), not null
|
||||
# created_on :datetime
|
||||
# description :text(65535)
|
||||
# disk_directory :string(255)
|
||||
# attachtype :integer default("1")
|
||||
# is_public :integer default("1")
|
||||
# copy_from :integer
|
||||
# quotes :integer default("0")
|
||||
# is_publish :integer default("1")
|
||||
# publish_time :datetime
|
||||
# resource_bank_id :integer
|
||||
# unified_setting :boolean default("1")
|
||||
# cloud_url :string(255) default("")
|
||||
# course_second_category_id :integer default("0")
|
||||
# delay_publish :boolean default("0")
|
||||
# uuid :string(255)
|
||||
#
|
||||
# Indexes
|
||||
#
|
||||
# index_attachments_on_author_id (author_id)
|
||||
# index_attachments_on_container_id_and_container_type (container_id,container_type)
|
||||
# index_attachments_on_course_second_category_id (course_second_category_id)
|
||||
# index_attachments_on_created_on (created_on)
|
||||
# index_attachments_on_is_public (is_public)
|
||||
# index_attachments_on_quotes (quotes)
|
||||
# index_attachments_on_uuid (uuid)
|
||||
#
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
class Attachment < ApplicationRecord
|
||||
include BaseModel
|
||||
include Publicable
|
||||
|
@ -60,11 +69,48 @@ class Attachment < ApplicationRecord
|
|||
scope :simple_columns, -> { select(:id, :filename, :filesize, :created_on, :cloud_url, :author_id, :content_type, :container_type, :container_id) }
|
||||
scope :search_by_container, -> (ids) {where(container_id: ids)}
|
||||
scope :unified_setting, -> {where("unified_setting = ? ", 1)}
|
||||
scope :where_id_or_uuid, -> (id) { (Float(id) rescue nil).present? ? where(id: id) : id.present? ? where(uuid: id) : [] }
|
||||
|
||||
validates_length_of :description, maximum: 100, message: "不能超过100个字符"
|
||||
validates_length_of :description, maximum: 255, message: "不能超过255个字符"
|
||||
|
||||
DCODES = %W(2 3 4 5 6 7 8 9 a b c f e f g h i j k l m n o p q r s t u v w x y z)
|
||||
|
||||
def self.build_from_remote_url(user, name, url, container=nil)
|
||||
ext = name.split('.')[-1]
|
||||
tmp_path = "#{Rails.root}/#{name}"
|
||||
uri = URI(url)
|
||||
size = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
|
||||
response = http.get(uri.path)
|
||||
File.open(tmp_path, 'wb') do |file|
|
||||
file.write(response.body)
|
||||
end
|
||||
end
|
||||
digest = "#{Digest::MD5.file(tmp_path).hexdigest}_#{(Time.now.to_f * 1000).to_i}.#{ext}"
|
||||
month_folder = "#{Time.now.year}/#{Time.now.month.to_s.rjust(2, '0')}"
|
||||
save_path = "#{Rails.root}#{EduSetting.get("attachment_folder")}#{month_folder}"
|
||||
unless Dir.exists?(save_path)
|
||||
FileUtils.mkdir_p(save_path) ##不成功这里会抛异常
|
||||
end
|
||||
path = File.join(save_path, digest)
|
||||
FileUtils.mv(tmp_path, path)
|
||||
attachment = Attachment.new
|
||||
attachment.filename = name
|
||||
attachment.disk_filename = path[save_path.size+1, path.size]
|
||||
attachment.filesize = size
|
||||
attachment.content_type = 'application/octet-stream'
|
||||
attachment.digest = digest.split('.')[0]
|
||||
attachment.author_id = user.id
|
||||
attachment.disk_directory = month_folder
|
||||
attachment.cloud_url = url
|
||||
attachment.uuid = SecureRandom.uuid
|
||||
attachment.container = container
|
||||
attachment.save!
|
||||
|
||||
return attachment
|
||||
rescue
|
||||
return nil
|
||||
end
|
||||
|
||||
def diskfile
|
||||
File.join(File.join(Rails.root, "files"), disk_directory.to_s, disk_filename.to_s)
|
||||
end
|
||||
|
@ -89,6 +135,11 @@ class Attachment < ApplicationRecord
|
|||
downloads
|
||||
end
|
||||
|
||||
def generate_uuid
|
||||
self.uuid = uuid || SecureRandom.uuid
|
||||
save!
|
||||
end
|
||||
|
||||
def quotes_count
|
||||
quotes.nil? ? 0 : quotes
|
||||
end
|
||||
|
@ -179,4 +230,14 @@ class Attachment < ApplicationRecord
|
|||
is_pdf
|
||||
end
|
||||
|
||||
end
|
||||
def to_builder
|
||||
Jbuilder.new do |attachment|
|
||||
attachment.id self.id
|
||||
attachment.title self.title
|
||||
attachment.filesize self.filesize
|
||||
attachment.is_pdf self.is_pdf?
|
||||
attachment.created_on self.created_on.strftime("%Y-%m-%d %H:%M")
|
||||
attachment.content_type self.content_type
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,5 +1,5 @@
|
|||
json.id attachment.id
|
||||
json.uuid attachment.uuid
|
||||
json.uuid attachment.uuid.present? ? attachment.uuid : attachment.id
|
||||
json.title attachment.title
|
||||
json.filesize number_to_human_size attachment.filesize
|
||||
json.description attachment.description
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
class AddUuidToAttachments < ActiveRecord::Migration[5.2]
|
||||
def change
|
||||
add_column :attachments, :uuid, :string, index: true
|
||||
end
|
||||
end
|
|
@ -0,0 +1,5 @@
|
|||
class AddIndexUuidToAttachments < ActiveRecord::Migration[5.2]
|
||||
def change
|
||||
add_index :attachments, :uuid
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue