Shrine upload_endpoint on Sinatra 400 Bad Request
dcalixto opened this issue · comments
Report
Following the Shrine docs, seems simple to use upload_endpoint with sinatra!
just place the on config.ru
map "/upload" do
run ImageUploader.upload_endpoint(:cache)
end
but after that and test with
curl -i -X POST http://localhost:9393/upload
the log spits out
Upload Not Found[daniel@daniel-82mf boilerplate]$ curl -i -X POST http://localhost:9393/upload
HTTP/1.1 400 Bad Request
Content-Type: text/plain
Content-Length: 16
my config.ru
# frozen_string_literal: true
# $LOAD_PATH.unshift '.'
require './config/environment'
# require_relative "../uploaders/image_uploader"
if ActiveRecord::Base.connection.migration_context.needs_migration?
raise 'Migrations are pending. Run `rake db:migrate` to resolve the issue.'
end
#map '/upload/store' do
# run Shrine.upload_enpdoint(:cache)
# run ImageUploader.upload_endpoint(:store) => '/upload/store'
#end
map "/upload" do
run ImageUploader.upload_endpoint(:cache)
end
use Rack::MethodOverride
use SessionController
use LocationsController
run ApplicationController
my shrine.rb initializer
# frozen_string_literal: true
require 'shrine'
require 'shrine/storage/file_system'
Shrine.storages = {
cache: Shrine::Storage::FileSystem.new('public', prefix: 'uploads/cache'), # temporary
store: Shrine::Storage::FileSystem.new('public', prefix: 'uploads') # permanent
}
Shrine.plugin :upload_endpoint#, url: true
Shrine.plugin :activerecord
Shrine.plugin :cached_attachment_data
Shrine.plugin :restore_cached_data
Shrine.plugin :rack_file # for non-Rails apps
Shrine.plugin :determine_mime_type
So I've tried the routes
post "/upload" do
local_file = params[‘image’][:filename]
temp_file = params[‘image’][:tempfile]
@location = image_upload(local_file, temp_file)
if @location.save
erb :"/locations/new"
else
erb :"/locations/new"
end
end
or
post "/upload2" do
uploader = ImageUploader.new(:cache)
file = Shrine.rack_file(params["file"]) # only `params[:file]` in Rails
uploaded_file = uploader.upload(file)
json uploaded_file.data
end
i even tried to place the after the controllers on config.ru
map "/upload" do
run ImageUploader.upload_endpoint(:cache)
end
an that result in
Upload Not Found[daniel@daniel-82mf boilerplate]$ curl -i -X POST http://localhost:9393/upload
HTTP/1.1 500 Internal Server Error
Content-Type: text/html;charset=utf-8
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
Set-Cookie: rack.session=d225ELUcsplnIXnqdN8Xc4umLhzCleNLmbzQlVyHquENWpaGZhX26Nc7Rjcd2Zg%2Buqvs2fiHqGyAPKANQApUrTewpdG7I9MUGsg877ABMgTe
So someone can help configure shrine with sinatra we appreciate
Expected Behavior
curl -i -X POST http://localhost:9393/upload
HTTP/1.1 200
Actual Behavior
Upload Not Found[daniel@daniel-82mf boilerplate]$ curl -i -X POST http://localhost:9393/upload
HTTP/1.1 400 Bad Request
Content-Type: text/plain
Content-Length: 16
Steps to Reproduce the Problem
frozen_string_literal: true
require 'shrine'
require 'shrine/storage/file_system'
Shrine.storages = {
cache: Shrine::Storage::FileSystem.new('public', prefix: 'uploads/cache'), # temporary
store: Shrine::Storage::FileSystem.new('public', prefix: 'uploads') # permanent
}
Shrine.plugin :upload_endpoint#, url: true
Shrine.plugin :activerecord
Shrine.plugin :cached_attachment_data
Shrine.plugin :restore_cached_data
Shrine.plugin :rack_file # for non-Rails apps
Shrine.plugin :determine_mime_type
frozen_string_literal: true
$LOAD_PATH.unshift '.'
require './config/environment'
require_relative "../uploaders/image_uploader"
if ActiveRecord::Base.connection.migration_context.needs_migration?
raise 'Migrations are pending. Run rake db:migrate
to resolve the issue.'
end
#map '/upload/store' do
run Shrine.upload_enpdoint(:cache)
run ImageUploader.upload_endpoint(:store) => '/upload/store'
#end
#use Rack::Logger
use Rack::MethodOverride
use SessionController
use LocationsController
run ApplicationController
map "/upload" do
run ImageUploader.upload_endpoint(:cache)
end
Ruby Version
No response
Shrine Version
No response
Anything else?
No response
You need to pass an actual file to the POST request, concretely a file
multipart POST parameter.
$ curl -i -X POST -F file=@/path/to/file http://localhost:9393/upload
ahh great, thank you!