UPLOADING IMAGE ON DROPBOX

Paperclip Dropbox

WE CAN USE FOLLOWING LINKS :-
PLEASE READ STEPS CAREFULLY..

1. Create drop box account.

2. Go to drop box developer console, Create App on drop box.









  
Now move to the coding part..

1. Add the gem in gemfile and run bundle.
     gem "paperclip-dropbox", ">= 1.1.7"
    gem "paperclip", "~> 4.2"


2. Run the following command,
    rails g paperclip users avatar

   if you getting issue in rails 5 you have to put in the migration :-

   class AddAttachmentFileToProfiles < ActiveRecord::Migration[5.1]


    end


3. Write this in user model.
    class Profile < ApplicationRecord
       has_attached_file :file
       validates_attachment_content_type :file, :content_type => []
    end



4. Add the following line to environment/production.rb/development.rb depends on your working environment.

   config.paperclip_defaults = {
     :storage => :dropbox,
     :dropbox_credentials => Rails.root.join("config/dropbox.yml"),
     :dropbox_visibility => 'public'
   }


5. Now run the following command:-

   rake dropbox:authorize APP_KEY="zpnjxberbbn5vbg" APP_SECRET="gga2ecgc008g6q6" ACCESS_TYPE=app_folder
Visit this URL: show on the command line:-

   https://www.dropbox.com/1/oauth/authorize?oauth_token=JHMYsfMMYqz3bIyM



Then open the link and click on allow button :-  






And after

     you approved the authorization confirm it here (y/n):

    After pressing the y button, It will generate the following keys..

       Authorization was successful. Here you go:

        access_token: ekzejvn95hm9o49f

        access_token_secret: b6g3znodjheq0zz

        user_id: 607357689


6. Create a new file "config/dropbox.yml" and paste this.

      app_key: "zpnjxberbbn5vbg"
      app_secret: "gga2ecgc008g6q6"
      access_token: "ekzejvn95hm9o49f"
      access_token_secret: "b6g3znodjheq0zz"
      user_id: "607357689"
      access_type: "app_folder"

7. IMAGE CONFLICT ISSUE : While saving image,it goes inside Dropbox >> Apps >>  application-name folder, so there will be possibility to replace the images with same name and will create error.

So here is the solution for that, put the below line to the user model :

     before_post_process :rename_avatar
     def rename_avatar
         extension = File.extname(avatar_file_name).downcase
         self.avatar.instance_write :file_name, "#{Time.now.to_i.to_s}#{extension}"
      end

8. Do not forget to change the application status development to production by clicking on button "Apply for production" if you testing on heroku.

 

 

Post a Comment

Popular posts from this blog

CARRIERWAVE - UPLOAD ANY MEDIA (AUDIO , VIDEO AND IMAGE ) FILE IN RAILS

RAILS: UPLOADING PHOTOS VIA AMAZON S3 AND PAPERCLIP (UPLOADING FILES TO S3 IN RUBY WITH PAPERCLIP)

SELECT COUNTRY WITH STATE AND IT'S CITIES RAILS

UNINITIALIZED CONSTANT PAPERCLIP::STORAGE::S3::AWS

Building native extensions. This could take a while... ERROR: Error installing mysql: ERROR: Failed to build gem native extension.

HOW TO CALL RAILS API FROM SIMPLE HTML PAGE.

CREATE API FOR USER SIGN UP, LOGIN AND CREATE PUBLISHER WITH CURRENT USER