Created
June 21, 2017 00:04
-
-
Save lkfken/149b107ba448f3a4b905bd89f196523a to your computer and use it in GitHub Desktop.
Ruby Sequel Blob datatype
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
Sequel.migration do | |
up do | |
create_table(:office_files) do | |
primary_key :id | |
column :uuid, 'uniqueidentifier' | |
String :name | |
Date :created_at | |
File :file # datatype File = blob | |
end | |
end | |
down do | |
drop_table(:office_files) | |
end | |
end |
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
class OfficeFile < Sequel::Model(DB) | |
plugin :uuid, :field => :uuid | |
plugin :timestamps | |
end |
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
desc 'upload a file' | |
task :upload do | |
record = OfficeFile.new | |
record.name = 'some_file.docx' | |
record.file = File.read('some_file.docx', :binmode => true) # important: binmode must be true | |
record.save | |
end | |
desc 'download a file' | |
task :download do | |
record = OfficeFile.where(:name => 'some_file.docx').first | |
bin_stream = record.file | |
File.open('./tmp/download.docx', 'w') { |f| f.write(bin_stream) } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment