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

Upload a file from an IO object #82

Closed
moonglum opened this issue Jul 11, 2019 · 7 comments
Closed

Upload a file from an IO object #82

moonglum opened this issue Jul 11, 2019 · 7 comments

Comments

@moonglum
Copy link
Contributor

Is your feature request related to a problem? Please describe.
I want to be able to upload files to Box without first writing them to Disk. For example data uploaded to a Rails App.

Describe the solution you'd like
The current upload_file method always expects a filename, I would instead just provide the data.

Describe alternatives you've considered
I don't really know an alternative except writing the file to disk first.

Additional context
Thanks for the Ruby Gem 🙇

@moonglum moonglum changed the title Upload a file from a String Upload a file from an IO object Jul 11, 2019
@moonglum
Copy link
Contributor Author

My current solution is the following monkey patch:

module Boxr
  class Client
    def upload_file_from_io(io, parent, name:, content_created_at: nil, content_modified_at: nil, preflight_check: true, send_content_md5: true)
      parent_id = ensure_id(parent)

      # TODO: the preflight check reads files from disk, so we can't use it right now
      # preflight_check(path_to_file, name, parent_id) if preflight_check
                                    
      if send_content_md5               
        content_md5 = Digest::SHA1.hexdigest(io.read)
        io.rewind            
      end                                       
                                                   
      attributes = {name: name, parent: {id: parent_id}}
      attributes[:content_created_at] = content_created_at.to_datetime.rfc3339 unless content_created_at.nil?
      attributes[:content_modified_at] = content_modified_at.to_datetime.rfc3339 unless content_modified_at.nil?

      body = {attributes: JSON.dump(attributes), file: io}

      file_info, response = post(FILES_UPLOAD_URI, body, process_body: false, content_md5: content_md5)

      file_info.entries[0]                   
    end                      
  end                                                                 
end

I think this could also be merged to the project. If you are interested, I can create a PR.

@xhocquet
Copy link
Collaborator

Hey @moonglum , thanks for writing in! Sorry for the late reply.

This has been a huge pain point for myself as well in my company's implementation, so I would love to see this enhancement. Would you please open a PR? Thanks! 🙏

@moonglum
Copy link
Contributor Author

No problem 👍 I'll get a PR ready. I'm using it in a Rails 6 app with ActiveStorage. I can also open source the Box ActiveStorage adapter I wrote after testing it for a little while.

@xhocquet
Copy link
Collaborator

@moonglum That sounds amazing! I had checked and was sad to see no open-source Box adapter for ActiveStorage, which probably blocks adoption for some people.

I'd be happy to help you maintain that if you need any help!

@moonglum
Copy link
Contributor Author

moonglum commented Aug 2, 2019

@xhocquet Pull Request is ready now 👍 Great! I'll get back to you when the adapter is ready for someone else but me 😄

@xhocquet
Copy link
Collaborator

xhocquet commented Aug 6, 2019

Resolved with #83! 🎉

@xhocquet xhocquet closed this as completed Aug 6, 2019
@moonglum
Copy link
Contributor Author

moonglum commented Aug 7, 2019

Awesome, thanks 🎺

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

2 participants