Jesus/dropbox_api

Generating a thumbnail output

Closed this issue · 4 comments

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?

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

Jesus commented

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.

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 #DropboxApi::Metadata::File:0x00007f834900f770

How to preview the thumbnail? image_tag, img_src?

Thanks so much. 👍

Gracias.

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

Jesus commented

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") %>