mirror of
https://github.com/discourse/discourse.git
synced 2024-11-26 20:33:38 +08:00
identifies all uploads with the SHA1 hash of the file content
This commit is contained in:
parent
6ea91b4416
commit
6c4554b941
|
@ -17,11 +17,18 @@ class Upload < ActiveRecord::Base
|
|||
image_info = FastImage.new(file.tempfile, raise_on_failure: true)
|
||||
# compute image aspect ratio
|
||||
width, height = ImageSizer.resize(*image_info.size)
|
||||
# compute the sha
|
||||
sha = Digest::SHA1.file(file.tempfile).hexdigest
|
||||
# check if the file has already been uploaded
|
||||
upload = Upload.where(sha: sha).first
|
||||
# otherwise, create it
|
||||
if upload.blank?
|
||||
|
||||
upload = Upload.create!({
|
||||
user_id: user_id,
|
||||
original_filename: file.original_filename,
|
||||
filesize: File.size(file.tempfile),
|
||||
sha: sha,
|
||||
width: width,
|
||||
height: height,
|
||||
url: ""
|
||||
|
@ -35,6 +42,8 @@ class Upload < ActiveRecord::Base
|
|||
|
||||
upload.save
|
||||
|
||||
end
|
||||
|
||||
upload
|
||||
end
|
||||
|
||||
|
|
6
db/migrate/20130615075557_add_sha_to_uploads.rb
Normal file
6
db/migrate/20130615075557_add_sha_to_uploads.rb
Normal file
|
@ -0,0 +1,6 @@
|
|||
class AddShaToUploads < ActiveRecord::Migration
|
||||
def change
|
||||
add_column :uploads, :sha, :string, null: true
|
||||
add_index :uploads, :sha, unique: true
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue
Block a user