Last active
August 29, 2015 14:01
-
-
Save dchandekstark/5199359f9874a9ce3b58 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CHUNK = 1024**2 | |
def add_file(file, dsid, file_name) | |
return add_external_file(file, file_name) if dsid == 'content' | |
super | |
end | |
def add_external_file(file, original_filename) | |
external_file_path = generate_external_file_path | |
if file.respond_to? :read | |
File.open(external_file_path, "wb") do |f| | |
f.write(file.read(CHUNK)) until file.eof? | |
file.rewind | |
end | |
elsif File.exists? file | |
FileUtils.mv file, external_file_path | |
else | |
raise ArgumentError, "File not found: #{file.inspect}" | |
end | |
content.dsLocation = URI.escape("file://#{external_file_path}") | |
mime = MIME::Types.type_for(original_filename).first | |
content.mimeType = mime.content_type if mime # mime can't always be detected by filename | |
self.label = original_filename | |
save! | |
end | |
private | |
# Generates a random filename | |
def generate_external_file_path | |
filename = SecureRandom.uuid | |
dir = File.join CourseAssets.external_datastore_base, filename[0], filename[1] | |
FileUtils.mkdir_p dir unless Dir.exists? dir | |
File.join dir, filename | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment