Shrine-url Storage

146 views
Skip to first unread message

aureli...@gmail.com

unread,
Oct 4, 2017, 7:48:44 AM10/4/17
to Shrine
Hi Everyone, 

I'm using a Tus server to upload files and i want to store files with url. But I have an error ; undefined url method.

My code : 

-> shrine.rb : 
  require "shrine"
 
require "shrine/storage/file_system"
 
require "shrine/storage/url"


 
Shrine.storages = {
    cache
: Shrine::Storage::Url.new(downloader: :wget),
    store
: Shrine::Storage::FileSystem.new("public", prefix: "uploads/store"), # permanent
 
}


-> application.js : 

 
var upload = null
 
var input = document.querySelector('input[type=file]')
 
var createFile = document.querySelector('#createFile')


 
if (createFile != null) {
    createFile
.addEventListener('click', startUpload)


     
function startUpload(){
       
var file = input.files[0]
        console
.log(file);
       
var options = {
          endpoint
: "http://localhost:3000/files/",
          chunkSize
: 5 * 1024 * 1024,
       
}
       
var upload = new tus.Upload(file, options)


        upload
.options.onProgress = function(bytesUploaded, bytesTotal) {
         
var percentage = (bytesUploaded / bytesTotal * 100).toFixed(2)
          console
.log(bytesUploaded, bytesTotal, percentage + '%')
       
}


        upload
.options.onSuccess = function() {
         
var fileData = {
            id
: upload.url,
            storage
: "cache",
            metadata
: {
              filename
: upload.file.name.match(/[^\/\\]+$/)[0], // IE returns full path
              size
: upload.file.size,
              mime_type
: upload.file.type,
           
}
         
};
         
var hiddenInput = input.parentNode.querySelector("input[type=hidden]");
          hiddenInput
.value = JSON.stringify(fileData);
          console
.log(hiddenInput);
       
}
        upload
.start()
     
}
 
}




-> video_controller : 

   
 def create
   
@video = Video.new(video_params)
 
end
 
def video_params
     
params.require(:video).permit(:file, :file_data, :video_url)
 
end



I try to understand how I can use upload.url to id. 


Thanks for help !

Aurélie

Janko Marohnić

unread,
Oct 4, 2017, 8:01:52 AM10/4/17
to aureli...@gmail.com, Shrine
Can you post here the full error and backtrace, and show me the lines of code where it occurs?
--
You received this message because you are subscribed to the Google Groups "Shrine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ruby-shrine...@googlegroups.com.
To post to this group, send email to ruby-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ruby-shrine/74f62a72-d804-4219-ad88-ac9a5fa02142%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

aureli...@gmail.com

unread,
Oct 4, 2017, 8:15:16 AM10/4/17
to Shrine

undefined method `url' for #<ActionDispatch::Http::UploadedFile:0x007f8d967e6e88>


def create
@video = Video.new(video_params)

In my application.js, the id in fileData is defined by : id:upload.url, but I use the hiddenInput with cached_attachment_data to store files and it need the id, not the url, it's why I wanted to use shrine_url.

aureli...@gmail.com

unread,
Oct 4, 2017, 10:39:13 AM10/4/17
to Shrine
And console : 

Started POST "/videos" for 127.0.0.1 at 2017-10-04 16:37:45 +0200
Processing by VideosController#create as HTML
 
Parameters: {"utf8"=>"✓", "authenticity_token"=>"ogc1/5+6sCYH6Xc9+GFkU+Vf8eSgbimWcRB1/7AVe32Xe62ff04e6FQohplB/11m8EIqNVRj0pu5SjY+eclMbQ==", "video"=>{"file"=>#<ActionDispatch::Http::UploadedFile:0x000000024adc40 @tempfile=#<Tempfile:/tmp/RackMultipart20171004-22706-1af97q.mp4>, @original_filename="TestVideo.mp4", @content_type="video/mp4", @headers="Content-Disposition: form-data; name=\"video[file]\"; filename=\"TestVideo.mp4\"\r\nContent-Type: video/mp4\r\n">}, "commit"=>"Submit"}
Completed 500 Internal Server Error in 3ms (ActiveRecord: 0.0ms)




 
NoMethodError (undefined method `url' for #<ActionDispatch::Http::UploadedFile:0x000000024adc40>):
 
app/controllers/videos_controller.rb:28:in `
create'



Janko Marohnić

unread,
Oct 4, 2017, 1:23:49 PM10/4/17
to aureli...@gmail.com, Shrine
As you can see from the logs, your form is sending the raw file instead of the JSON data. The shrine-url storage is then failing to "upload" that file, because it expects it to respond to #url (and a raw ActionDispatch::Http::UploadedFile doesn't respond to #url).

You are forgetting to clear the value of the file field after you've started uploading the selected file, so the raw file is still being sent and the hidden field value is being ignored. You need this line from the shrine-tus-demo.

Kind regards,
Janko

--
You received this message because you are subscribed to the Google Groups "Shrine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ruby-shrine+unsubscribe@googlegroups.com.

To post to this group, send email to ruby-...@googlegroups.com.

aureli...@gmail.com

unread,
Oct 5, 2017, 3:16:02 AM10/5/17
to Shrine
Thanks a lot for your help ! 
I can't believe it was a stupid error ! 

Aurélie
Reply all
Reply to author
Forward
0 new messages