Class: Backup
- Inherits:
-
Object
- Object
- Backup
- Defined in:
- app/models/backup.rb
Defined Under Namespace
Classes: Entry
Constant Summary collapse
- ALLOWED_EXTENSIONS =
[:tgz, :zip]
Class Method Summary collapse
Instance Method Summary collapse
- #content_disposition ⇒ Object
- #filename ⇒ Object
-
#initialize(extension) ⇒ Backup
constructor
A new instance of Backup.
- #stream_to(response) ⇒ Object
- #tgz(stream) ⇒ Object
-
#zip(stream) ⇒ Object
TODO: Maybe investigate zipline gem a little more and figure out how it streams zips, and use that here, so this can be properly streamed.
Constructor Details
#initialize(extension) ⇒ Backup
Returns a new instance of Backup.
4 5 6 7 |
# File 'app/models/backup.rb', line 4 def initialize(extension) raise "Invalid extension '#{extension}'" unless ALLOWED_EXTENSIONS.include? extension.to_sym @extension = extension.to_sym end |
Class Method Details
.each ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'app/models/backup.rb', line 54 def each Cartoonist::Backup.all.each do |key, proc| proc.call.find_each do |value| if value.respond_to? :to_backup_entries value.to_backup_entries.each do |entry| yield entry.with_key(key) end else yield Backup::Entry.from_record(value).with_key(key) end end end end |
Instance Method Details
#content_disposition ⇒ Object
9 10 11 |
# File 'app/models/backup.rb', line 9 def content_disposition %{attachment; filename="#{filename}"} end |
#filename ⇒ Object
13 14 15 16 |
# File 'app/models/backup.rb', line 13 def filename prefix = "dev-" unless Rails.env.production? "#{prefix}cartoonist-backup-#{Time.now.strftime("%Y-%m-%d_%H%M%S")}.#{@extension}" end |
#stream_to(response) ⇒ Object
18 19 20 21 22 23 |
# File 'app/models/backup.rb', line 18 def stream_to(response) response.headers["Content-Disposition"] = content_disposition send @extension, response.stream ensure response.stream.close end |
#tgz(stream) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'app/models/backup.rb', line 39 def tgz(stream) gzip = Zlib::GzipWriter.new stream Archive::Tar::Minitar::Writer.open gzip do |tar| Backup.each do |entry| tar.add_file_simple entry.path, :mode => 0644, :size => entry.content.length do |output| output.write entry.content end end end ensure gzip.close end |
#zip(stream) ⇒ Object
TODO: Maybe investigate zipline gem a little more and figure out how it streams zips, and use that here, so this can be properly streamed.
28 29 30 31 32 33 34 35 36 37 |
# File 'app/models/backup.rb', line 28 def zip(stream) buffer = Zip::OutputStream.write_buffer do |zos| Backup.each do |entry| zos.put_next_entry entry.path zos.write entry.content end end stream.write buffer.string end |