Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Active Model Serializer conflicts with Active storage while streaming active storage blobs #2388

Open
pavan-itd opened this issue Jul 20, 2020 · 0 comments

Comments

@pavan-itd
Copy link

Active Model Serializer conflicts with Active storage while streaming active storage blobs

I want to download all the attachments by streaming them using ActionController:: Live. It's working fine if I remove gem active_model_serializers', '0.9.2'. It halts the server and I have to restart it.

Steps to reproduce

  1. Take any model with an attachment.
    Here is my model
class Model < ApplicationRecord
  has_many_attached :image
end
  1. Take add action to the controller
    like below
class ZipStreamingController < ApplicationController
  include ActionController::Live

  def download
    zipname = 'test_archive.zip'
    send_file_headers!(
      type: "application/zip",
      disposition: "attachment",
      filename: zipname
    )
    response.headers['Last-Modified'] = Time.now.httpdate.to_s
    response.headers['X-Accel-Buffering'] = 'no'

    writer = ZipTricks::BlockWrite.new do |chunk|
      response.stream.write(chunk)
    end

    ZipTricks::Streamer.open(writer) do |zip|
      Model.all.each do |model|
        model.image.each do |i|
          zip.write_deflated_file("#{model.name}/#{i.filename.to_s}") do |file_writer|
            i.blob.download do |chunk|
              file_writer << chunk
            end
          end
        end
      end
    end
  ensure
    response.stream.close
  end
end

Environment

active_model_serializers', '0.9.2'

OS Type & Version:

  • OS: Ubuntu
  • Version: 19.10

Application: Rails

Backtrace

Started POST "/download" for ::1 at 2020-07-20 11:46:07 +0530
Processing by ZipStreamingController#download as HTML
  Parameters: {"authenticity_token"=>"q93Vle1buS1EPTmqqsBlm8izATFmKa+9TscfWO6HNflOqzSou1+hReG67zpFBe/5uPa8CwahZJOzQeK+9tkVXA=="}
  Model Load (0.9ms)  SELECT "models".* FROM "models"
  ↳ app/controllers/zip_streaming_controller.rb:19
  ActiveStorage::Attachment Load (1.1ms)  SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = $1 AND "active_storage_attachments"."record_type" = $2 AND "active_storage_attachments"."name" = $3  [["record_id", 5], ["record_type", "Model"], ["name", "image"]]
  ↳ app/controllers/zip_streaming_controller.rb:20
  ActiveStorage::Blob Load (0.6ms)  SELECT  "active_storage_blobs".* FROM "active_storage_blobs" WHERE "active_storage_blobs"."id" = $1 LIMIT $2  [["id", 14], ["LIMIT", 1]]
  ↳ app/controllers/zip_streaming_controller.rb:21

Gemfile.lock

GEM
  remote: https://rubygems.org/
  specs:
    actioncable (5.2.4.3)
      actionpack (= 5.2.4.3)
      nio4r (~> 2.0)
      websocket-driver (>= 0.6.1)
    actionmailer (5.2.4.3)
      actionpack (= 5.2.4.3)
      actionview (= 5.2.4.3)
      activejob (= 5.2.4.3)
      mail (~> 2.5, >= 2.5.4)
      rails-dom-testing (~> 2.0)
    actionpack (5.2.4.3)
      actionview (= 5.2.4.3)
      activesupport (= 5.2.4.3)
      rack (~> 2.0, >= 2.0.8)
      rack-test (>= 0.6.3)
      rails-dom-testing (~> 2.0)
      rails-html-sanitizer (~> 1.0, >= 1.0.2)
    actionview (5.2.4.3)
      activesupport (= 5.2.4.3)
      builder (~> 3.1)
      erubi (~> 1.4)
      rails-dom-testing (~> 2.0)
      rails-html-sanitizer (~> 1.0, >= 1.0.3)
    active_model_serializers (0.9.2)
      activemodel (>= 3.2)
    activejob (5.2.4.3)
      activesupport (= 5.2.4.3)
      globalid (>= 0.3.6)
    activemodel (5.2.4.3)
      activesupport (= 5.2.4.3)
    activerecord (5.2.4.3)
      activemodel (= 5.2.4.3)
      activesupport (= 5.2.4.3)
      arel (>= 9.0)
    activestorage (5.2.4.3)
      actionpack (= 5.2.4.3)
      activerecord (= 5.2.4.3)
      marcel (~> 0.3.1)
    activesupport (5.2.4.3)
      concurrent-ruby (~> 1.0, >= 1.0.2)
      i18n (>= 0.7, < 2)
      minitest (~> 5.1)
      tzinfo (~> 1.1)
    arel (9.0.0)
    bindex (0.8.1)
    bootsnap (1.4.6)
      msgpack (~> 1.0)
    builder (3.2.4)
    byebug (11.1.3)
    coderay (1.1.3)
    coffee-rails (4.2.2)
      coffee-script (>= 2.2.0)
      railties (>= 4.0.0)
    coffee-script (2.4.1)
      coffee-script-source
      execjs
    coffee-script-source (1.12.2)
    concurrent-ruby (1.1.6)
    crass (1.0.6)
    erubi (1.9.0)
    execjs (2.7.0)
    ffi (1.13.1)
    globalid (0.4.2)
      activesupport (>= 4.2.0)
    i18n (1.8.3)
      concurrent-ruby (~> 1.0)
    jbuilder (2.10.0)
      activesupport (>= 5.0.0)
    listen (3.1.5)
      rb-fsevent (~> 0.9, >= 0.9.4)
      rb-inotify (~> 0.9, >= 0.9.7)
      ruby_dep (~> 1.2)
    loofah (2.6.0)
      crass (~> 1.0.2)
      nokogiri (>= 1.5.9)
    mail (2.7.1)
      mini_mime (>= 0.1.1)
    marcel (0.3.3)
      mimemagic (~> 0.3.2)
    method_source (1.0.0)
    mimemagic (0.3.5)
    mini_mime (1.0.2)
    mini_portile2 (2.4.0)
    minitest (5.14.1)
    msgpack (1.3.3)
    nio4r (2.5.2)
    nokogiri (1.10.10)
      mini_portile2 (~> 2.4.0)
    pg (1.2.3)
    pry (0.13.1)
      coderay (~> 1.1)
      method_source (~> 1.0)
    pry-rails (0.3.9)
      pry (>= 0.10.4)
    puma (3.12.6)
    rack (2.2.3)
    rack-test (1.1.0)
      rack (>= 1.0, < 3)
    rails (5.2.4.3)
      actioncable (= 5.2.4.3)
      actionmailer (= 5.2.4.3)
      actionpack (= 5.2.4.3)
      actionview (= 5.2.4.3)
      activejob (= 5.2.4.3)
      activemodel (= 5.2.4.3)
      activerecord (= 5.2.4.3)
      activestorage (= 5.2.4.3)
      activesupport (= 5.2.4.3)
      bundler (>= 1.3.0)
      railties (= 5.2.4.3)
      sprockets-rails (>= 2.0.0)
    rails-dom-testing (2.0.3)
      activesupport (>= 4.2.0)
      nokogiri (>= 1.6)
    rails-html-sanitizer (1.3.0)
      loofah (~> 2.3)
    railties (5.2.4.3)
      actionpack (= 5.2.4.3)
      activesupport (= 5.2.4.3)
      method_source
      rake (>= 0.8.7)
      thor (>= 0.19.0, < 2.0)
    rake (13.0.1)
    rb-fsevent (0.10.4)
    rb-inotify (0.10.1)
      ffi (~> 1.0)
    ruby_dep (1.5.0)
    rubyzip (2.2.0)
    sass (3.7.4)
      sass-listen (~> 4.0.0)
    sass-listen (4.0.0)
      rb-fsevent (~> 0.9, >= 0.9.4)
      rb-inotify (~> 0.9, >= 0.9.7)
    sass-rails (5.1.0)
      railties (>= 5.2.0)
      sass (~> 3.1)
      sprockets (>= 2.8, < 4.0)
      sprockets-rails (>= 2.0, < 4.0)
      tilt (>= 1.1, < 3)
    spring (2.1.0)
    spring-watcher-listen (2.0.1)
      listen (>= 2.7, < 4.0)
      spring (>= 1.2, < 3.0)
    sprockets (3.7.2)
      concurrent-ruby (~> 1.0)
      rack (> 1, < 3)
    sprockets-rails (3.2.1)
      actionpack (>= 4.0)
      activesupport (>= 4.0)
      sprockets (>= 3.0.0)
    thor (1.0.1)
    thread_safe (0.3.6)
    tilt (2.0.10)
    turbolinks (5.2.1)
      turbolinks-source (~> 5.2)
    turbolinks-source (5.2.0)
    tzinfo (1.2.7)
      thread_safe (~> 0.1)
    uglifier (4.2.0)
      execjs (>= 0.3.0, < 3)
    web-console (3.7.0)
      actionview (>= 5.0)
      activemodel (>= 5.0)
      bindex (>= 0.4.0)
      railties (>= 5.0)
    websocket-driver (0.7.3)
      websocket-extensions (>= 0.1.0)
    websocket-extensions (0.1.5)
    zip_tricks (4.8.2)

PLATFORMS
  ruby

DEPENDENCIES
  active_model_serializers (= 0.9.2)
  bootsnap (>= 1.1.0)
  byebug
  coffee-rails (~> 4.2)
  jbuilder (~> 2.5)
  listen (>= 3.0.5, < 3.2)
  pg (>= 0.18, < 2.0)
  pry-rails
  puma (~> 3.11)
  rails (~> 5.2.4, >= 5.2.4.3)
  rubyzip
  sass-rails (~> 5.0)
  spring
  spring-watcher-listen (~> 2.0.0)
  turbolinks (~> 5)
  tzinfo-data
  uglifier (>= 1.3.0)
  web-console (>= 3.3.0)
  zip_tricks (~> 4.2, >= 4.2.1)

RUBY VERSION
   ruby 2.4.0p0

BUNDLED WITH
   2.0.2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant