mirror of
https://github.com/discourse/discourse.git
synced 2024-11-26 18:03:38 +08:00
identifies all uploads with the SHA1 hash of the file content
This commit is contained in:
parent
6ea91b4416
commit
6c4554b941
|
@ -17,23 +17,32 @@ 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),
|
||||
width: width,
|
||||
height: height,
|
||||
url: ""
|
||||
})
|
||||
upload = Upload.create!({
|
||||
user_id: user_id,
|
||||
original_filename: file.original_filename,
|
||||
filesize: File.size(file.tempfile),
|
||||
sha: sha,
|
||||
width: width,
|
||||
height: height,
|
||||
url: ""
|
||||
})
|
||||
|
||||
# make sure we're at the beginning of the file (FastImage is moving the pointer)
|
||||
file.rewind
|
||||
# make sure we're at the beginning of the file (FastImage is moving the pointer)
|
||||
file.rewind
|
||||
|
||||
# store the file and update its url
|
||||
upload.url = Upload.store_file(file, image_info, upload.id)
|
||||
# store the file and update its url
|
||||
upload.url = Upload.store_file(file, image_info, upload.id)
|
||||
|
||||
upload.save
|
||||
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