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

Fixes streaming demo #42

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
14 changes: 6 additions & 8 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ GEM
pry (>= 0.13.0)
shellany (~> 0.0)
thor (>= 0.18.1)
guard-bundler (2.2.1)
bundler (>= 1.3.0, < 3)
guard-bundler (3.0.0)
bundler (>= 2.1, < 3)
guard (~> 2.2)
guard-compat (~> 1.1)
guard-compat (1.2.1)
Expand All @@ -90,7 +90,6 @@ GEM
mime-types-data (~> 3.2015)
mime-types-data (3.2022.0105)
mini_mime (1.1.2)
mini_portile2 (2.8.0)
minitest (5.16.0)
multi_json (1.15.0)
mustermann (1.1.1)
Expand All @@ -99,8 +98,7 @@ GEM
mustermann (>= 1.0.0)
nenv (0.3.0)
newrelic_rpm (8.8.0)
nokogiri (1.13.6)
mini_portile2 (~> 2.8.0)
nokogiri (1.13.6-x86_64-darwin)
racc (~> 1.4)
notiffany (0.1.3)
nenv (~> 0.1)
Expand Down Expand Up @@ -153,7 +151,7 @@ GEM
parser (>= 3.1.1.0)
rubocop-rake (0.6.0)
rubocop (~> 1.0)
rubocop-rspec (2.9.0)
rubocop-rspec (2.11.1)
rubocop (~> 1.19)
ruby-progressbar (1.11.0)
ruby2_keywords (0.0.5)
Expand All @@ -174,7 +172,7 @@ GEM
zeitwerk (2.6.0)

PLATFORMS
ruby
x86_64-darwin-19

DEPENDENCIES
capybara
Expand Down Expand Up @@ -202,4 +200,4 @@ RUBY VERSION
ruby 2.6.5p114

BUNDLED WITH
1.17.3
2.3.15
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]
stream 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 @@ -12,6 +12,7 @@ class API < Grape::API
mount ::Acme::GetJson
mount ::Acme::ContentType
mount ::Acme::UploadFile
mount ::Acme::UploadBigFile
mount ::Acme::Entities::API
mount ::Acme::Headers
add_swagger_documentation api_version: 'v1'
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")
data = File.read(image_filename, encoding: 'UTF-8')
expect(last_response.body.encoding).to eq data.encoding
expect(last_response.body).to eq data
end
end