Skip to content

Handling large files. #12

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
source 'http://rubygems.org'

gem 'rack', '~> 1.6.0'
gem 'grape', '~> 0.10.1'
# gem 'grape', '~> 0.10.1'
gem 'grape', path: '/Users/cdan/work/dan-corneanu/grape'
gem 'grape-entity', '~> 0.4.4'
gem 'json', '~> 1.7.7'
gem 'newrelic_rpm', '~> 3.5.4.34'
Expand All @@ -10,6 +11,8 @@ gem 'rack-cors', '~> 0.2.8'
gem 'grape-swagger', '~> 0.9.0'
gem 'mime-types'
gem 'nokogiri', '1.6.3.1'
gem 'pry'
gem 'pry-byebug'

group :development do
gem 'rake', '~> 10.0.3'
Expand Down
15 changes: 12 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ GEM
ice_nine (~> 0.11.0)
thread_safe (~> 0.3, >= 0.3.1)
builder (3.2.2)
byebug (4.0.5)
columnize (= 0.9.0)
capybara (2.4.4)
mime-types (>= 1.16)
nokogiri (>= 1.3.3)
Expand All @@ -28,10 +30,11 @@ GEM
coderay (1.1.0)
coercible (1.0.0)
descendants_tracker (~> 0.0.1)
columnize (0.9.0)
descendants_tracker (0.0.4)
thread_safe (~> 0.3, >= 0.3.1)
diff-lcs (1.2.5)
equalizer (0.0.9)
equalizer (0.0.11)
ffi (1.9.6)
formatador (0.2.5)
grape (0.10.1)
Expand Down Expand Up @@ -68,7 +71,7 @@ GEM
libnotify
rb-inotify
spoon
hashie (3.3.2)
hashie (3.4.1)
hitimes (1.2.2)
i18n (0.7.0)
ice_nine (0.11.1)
Expand Down Expand Up @@ -102,6 +105,10 @@ GEM
coderay (~> 1.1.0)
method_source (~> 0.8.1)
slop (~> 3.4)
pry-byebug (3.1.0)
byebug (~> 4.0)
pry (~> 0.10)
racc (1.4.12)
rack (1.6.0)
rack-accept (0.4.5)
rack (>= 0.4)
Expand Down Expand Up @@ -149,7 +156,7 @@ GEM
hitimes
tzinfo (1.2.2)
thread_safe (~> 0.1)
virtus (1.0.3)
virtus (1.0.5)
axiom-types (~> 0.1)
coercible (~> 1.0)
descendants_tracker (~> 0.0, >= 0.0.3)
Expand All @@ -175,6 +182,8 @@ DEPENDENCIES
newrelic-grape (~> 1.1.0)
newrelic_rpm (~> 3.5.4.34)
nokogiri (= 1.6.3.1)
pry
pry-byebug
racc
rack (~> 1.6.0)
rack-cors (~> 0.2.8)
Expand Down
30 changes: 30 additions & 0 deletions api/upload_big_file.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module Acme
class UploadBigFile < Grape::API
content_type :png, 'image/png'

desc 'Upload and download a big file of any format using IO.'
post 'big_download' do
filename = params[:file][:filename]
# content_type MIME::Types.type_for(filename)[0].to_s
content_type 'image/png'
env['api.format'] = :png
header 'Content-Disposition', "attachment; filename*=UTF-8''#{URI.escape(filename)}"

temp_file = params[:file][:tempfile]
body FileStreamer.new temp_file.path
nil
end
end

class FileStreamer
def initialize(file_path)
@file_path = file_path
end

def each(&blk)
File.open(@file_path, 'r') do |file|
file.each(10, &blk)
end
end
end
end
1 change: 1 addition & 0 deletions app/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class API < Grape::API
mount ::Acme::PostJson
mount ::Acme::ContentType
mount ::Acme::UploadFile
mount ::Acme::UploadBigFile
mount ::Acme::Entities::API
add_swagger_documentation api_version: 'v1'
end
Expand Down
20 changes: 20 additions & 0 deletions spec/api/upload_big_file_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require 'spec_helper'

describe Acme::API do
include Rack::Test::Methods

def app
Acme::API
end

it 'uploads and downloads a PNG file' do
image_filename = 'spec/fixtures/grape_logo.png'
post '/api/big_download.png', file: Rack::Test::UploadedFile.new(image_filename, 'image/png', true)
expect(last_response.status).to eq(201)
expect(last_response.headers['Content-Type']).to eq('image/png')
expect(last_response.headers['Content-Disposition']).to eq("attachment; filename*=UTF-8''grape_logo.png")
File.open(image_filename, 'rb') do |io|
expect(last_response.body).to eq io.read
end
end
end