Giter VIP home page Giter VIP logo

zipline's Introduction

Zipline

Build Status Gem Version

A gem to stream dynamically generated zip files from a rails application. Unlike other solutions that generate zips for user download, zipline does not wait for the entire zip file to be created (or even for the entire input file in the cloud to be downloaded) before it begins sending the zip file to the user. It does this by never seeking backwards during zip creation, and streaming the zip file over http as it is constructed. The advantages of this are:

  • Removes need for large disk space or memory allocation to generate zips, even huge zips. So it works on Heroku.
  • The user begins downloading immediately, which decreaceses latency, download time, and timeouts on Heroku.

Zipline now depends on zip tricks, and you might want to just use that directly if you have more advanced use cases.

Installation

Add this line to your application's Gemfile:

gem 'zipline'

And then execute:

$ bundle

Usage

Set up some models with ActiveStorage carrierwave, paperclip, or shrine. Right now only plain file storage and S3 are supported in the case of carrierwave and only plain file storage and S3 are supported in the case of paperclip. Mutiple file storages are supported with shrine.

You'll need to be using puma or some other server that supports streaming output.

class MyController < ApplicationController
  # enable zipline
  include Zipline

  def index
    users = User.all
    # you can replace user.avatar with any stream or any object that
    # responds to :url, :path or :file.
    # :modification_time is an optional third argument you can use.
    files = users.map{ |user| [user.avatar, "#{user.username}.png", modification_time: 1.day.ago] }
    zipline(files, 'avatars.zip')
  end
end

ActiveStorage

users = User.all
files = users.map{ |user| [user.avatar, user.avatar.filename] }
zipline(files, 'avatars.zip')

Carrierwave

users = User.all
files = users.map{ |user| [user.avatar, user.avatar_identifier] }
zipline(files, 'avatars.zip')

Paperclip (deprecated)

users = User.all
files = users.map{ |user| [user.avatar, user.avatar_file_name] }
zipline(files, 'avatars.zip')

Url

If you know the URL of the remote file you want to include, you can just pass in the URL directly in place of the attachment object.

avatars = [
  ['http://www.example.com/user1.png', 'user1.png']
  ['http://www.example.com/user2.png', 'user2.png']
  ['http://www.example.com/user3.png', 'user3.png']
]
zipline(avatars, 'avatars.zip')

Directories

For directories, just give the files names like "directory/file".

avatars = [
  # remote_url                          zip_path             zip_tricks_options
  [ 'http://www.example.com/user1.png', 'avatars/user1.png', modification_time: Time.now.utc ]
  [ 'http://www.example.com/user2.png', 'avatars/user2.png', modification_time: 1.day.ago ]
  [ 'http://www.example.com/user3.png', 'avatars/user3.png' ]
]

zipline(avatars, 'avatars.zip')

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

TODO (possible contributions?)

  • Add support for your favorite attachment plugin.
  • Tests.

zipline's People

Contributors

abcang avatar ardell avatar aried3r avatar arrtchiu avatar cacquani avatar dorialan avatar dot avatar exige81 avatar fringd avatar julik avatar kevinjxx avatar leboshi avatar mapreal19 avatar mfrister avatar mrloop avatar nagyt234 avatar nicolas-brousse avatar realtwiho avatar ryanfb avatar sunny avatar swiknaba avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.