Jesus / dropbox_api

Ruby client library for Dropbox API v2
MIT License
171 stars 113 forks source link

Generating a thumbnail output #7

Closed mocerinoj closed 7 years ago

mocerinoj commented 7 years ago

I assume I am missing something.. I upload a file and get a thumbnail (for now, assume its a small jpeg being uploaded):

dropbox = DropboxApi::Client.new(ENV['DROPBOX_ACCESS_TOKEN'])
uploaded_file = dropbox.upload(uploaded_path, uploaded_io.read, autorename: true)
thumb = dropbox.get_thumbnail(uploaded_file.path_lower)

inspecting thumb gives me the same object as uploaded_file.. how do I get to the thumbnail?

mocerinoj commented 7 years ago

Just to clarify, get_thumbnail seems to be returning DropboxApi::Metadata::File which wouldn't include the thumbnail

Jesus commented 7 years ago

This was undocumented. I've updated the docs now.

Here's an example of how you can save a thumbnail:

client = DropboxApi::Client.new

File.open("thumbnail.png", "w") do |file|
  client.get_thumbnail "/dropbox_image.png" do |thumbnail_content|
    file.write thumbnail_content
  end
end

Please re-open if anything is still unclear.

elduglas commented 6 years ago

Hi Jesús.

I've a similar problem for display a thumbnail.

ApplicationController

  before_action :authenticator, :dropbox_client

  CLIENT_ID = "XXXXXXX"
  CLIENT_SECRET = "YYYYYY"
  CODE = 'ZZZZZZZ'

private
  def authenticator
    DropboxApi::Authenticator.new(CLIENT_ID, CLIENT_SECRET)
  end

  def dropbox_client
    @dropbox_client ||= DropboxApi::Client.new(CODE)
  end

DropboxController.

 def list
    files = dropbox_client.list_folder "/#{params[:folder_code]}"
    @files = files.entries
  end

ApplicationHelper

 def show_image(image)
    File.open("thumbnail.png", "w") do |file|
      @dropbox_client.get_thumbnail image.path_lower do |thumbnail_content|
        file.write thumbnail_content
      end
    end
  end

List.html.erb

<%= show_image(file) %>

Error.

"\xFF" from ASCII-8BIT to UTF-8

I changed File.open("thumbnail.png", "w") do |file| to File.open("thumbnail.png", "wb") do |file| and as result I had #

How to preview the thumbnail? image_tag, img_src?

Thanks so much. :+1:

Gracias.

http://www.xuuso.com/dropbox_api/DropboxApi/Client.html

Jesus commented 6 years ago

Looks like you're trying to download the file to your server on every browser request.

It would make more sense to download the thumbnail to your server once, then serving it just like any other asset.

Anyway, the mistake in your example was that you're failing to generate the image tag, you're embedding the bytes of the image as part of the HTML document. Instead, your view you should have something like:

<%= image_tag("/path/to/downloaded/file/thumbnail.png") %>