paperclip-google-drive 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 06f945a112a7fc9c7edc1c8b925bb957011f26ba
4
- data.tar.gz: 7ff404b4f10d203c306efebe144ca86e5f449a06
3
+ metadata.gz: c7c2bb6eae67f447365e5f5e0a11a6a751582ba7
4
+ data.tar.gz: 151052f23e6e53c2b785ec0aa65897ea90903bbe
5
5
  SHA512:
6
- metadata.gz: c228c2ff00f14992a98974fb94d4da90e230d7b357feb93ab4805f49923ceec9a2aeb09922b0b7fd53107c788673ff46fc261539ce3db6683c2223f3716ed64f
7
- data.tar.gz: 68d6878eea81fb2b9fec1607ad32a77f932c1e0199ba0630ac506833b9f88b10aafe7b42b1ad9f1ea1c3380b217c4451bd1af5a8026ee74b32e1f0ca29570f6f
6
+ metadata.gz: 02a3a5e97b90105dc8ac32789da4edb9e0074df2b863bcba7b6661e5bc469353f723c886a94bf18118bbffa0670e043e6b200492425eab44c9989b7d2e023b24
7
+ data.tar.gz: 79ccd25892080cb5b41894086524ec6f43575d983b93b6bb9a3ab4586c4447ab522624d3243fa470e0f4b5787f24ea9bdefa85830a77f370ba1fc03dd700c6ba
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
  [![GitHub version](https://badge.fury.io/gh/degzcs%2Fpaperclip-googledrive.svg)](https://badge.fury.io/gh/degzcs%2Fpaperclip-googledrive)
3
3
  [![Code Climate](https://codeclimate.com/github/degzcs/paperclip-googledrive/badges/gpa.svg)](https://codeclimate.com/github/degzcs/paperclip-googledrive)
4
4
 
5
- PaperclipGoogledrive is a gem that extends paperclip storage for Google Drive (V3). Works with Rails 3.x. and later.
5
+ PaperclipGoogleDrive is a gem that extends paperclip storage for Google Drive (V3). Works with Rails 3.x. and later.
6
6
  IMPORTANT NOTE: This repo was forked and upgraded to use Google Drive V3.
7
7
 
8
8
  ## Installation
@@ -35,7 +35,7 @@ Google Drive is a free service for file storage files. In order to use this stor
35
35
  ```sh
36
36
  $ rake google_drive:authorize"[path/to/client_secret.json, 'application_name']"
37
37
  ```
38
- NOTE:
38
+ NOTE:
39
39
  - the `path/to/client_secret.json` path is the file downloaded from Google console (it will be overrided with the fresh token).
40
40
  - the `application_name` param is the name that you set for the application credentials on Google console.
41
41
 
@@ -86,7 +86,7 @@ class Product < ActiveRecord::Base
86
86
  :google_drive_client_secret_path => "#{Rails.root}/config/client_secret.json"
87
87
  :styles => { :medium => "300x300" },
88
88
  :google_drive_options => {
89
- :path => proc { |style| "#{style}_#{id}_#{photo.original_filename}" },
89
+ :path => proc { |style| "#{id}_#{photo.original_filename}_#{style}" },
90
90
  :public_folder_id => 'AAAARRRRGGGBBBFFFFadsasdX'
91
91
  }
92
92
  end
@@ -98,9 +98,16 @@ Public/14_some_photo_medium.jpg
98
98
 
99
99
  The another file is called some_photo_medium.jpg because style names (other than original) will always be appended to the filenames, for better management.
100
100
 
101
+ Also, you can use the resize feature provided by GDrive API, you only have to pass as parameter in the url params the option `:custom` and `width`, as follows:
102
+
103
+ ```ruby
104
+ some_product.photo.url(:custom, width: 500)
105
+ ```
106
+
101
107
  ## Misc
102
108
 
103
- Useful links
109
+ ## Useful links
110
+
104
111
  [Google APIs console](https://code.google.com/apis/console/)
105
112
 
106
113
  [Google Drive scopes](https://developers.google.com/drive/scopes)
@@ -111,7 +118,7 @@ Useful links
111
118
 
112
119
  ## License
113
120
 
114
- [MIT License](https://github.com/evinsou/paperclip-googledrive/blob/master/LICENSE)
121
+ [MIT License](https://github.com/degzcs/paperclip-googledrive/blob/master/LICENSE)
115
122
 
116
123
  ## Contributing
117
124
 
@@ -52,7 +52,7 @@ module Paperclip
52
52
  @queued_for_write.each do |style, file|
53
53
  raise FileExists, "file \"#{path(style)}\" already exists in your Google Drive" if exists?(path(style))
54
54
 
55
- name, mime_type = name_for_file_from(style), "#{ file.content_type }"
55
+ name, mime_type = filename_from(style), "#{ file.content_type }"
56
56
 
57
57
  file_metadata = {
58
58
  name: name,
@@ -95,28 +95,28 @@ module Paperclip
95
95
 
96
96
  alias_method :google_drive, :google_api_client
97
97
 
98
- # This could be used to scale image as Google does. e.i. `<url>=s220`,
99
- # where 220 is the width in pixeles OR as Paperclip does.
98
+ # Retrives the origina file or this also could be used to scale images
99
+ # as Google does. e.i. `<url>=s220`, where 220 is the width in pixeles
100
+ # OR as Paperclip does.
100
101
  # @params args [ Array ]
101
102
  # @return [ String ]
102
103
  # ex.
103
104
  # 1. If you want the medium version of your image (Paperclip way)
104
105
  # some_model.avatar.url(:medium)
105
- # 2. If you want a custom version of your image/pdf (Google way)
106
- # some_model.avatar.url(:custom, width: 500)
106
+ # 2. If you want a custom thumbanil of your image/pdf (Google way)
107
+ # some_model.avatar.url(:custom_thumb, width: 500)
107
108
  def url(*args)
108
109
  if present?
109
110
  style = args.first.is_a?(Symbol) ? args.first : default_style
110
111
  options = args.last.is_a?(Hash) ? args.last : {}
111
- if style == :custom
112
+ if style == :custom_thumb
112
113
  custom_width = options[:width] || 220
113
- file_name = name_for_file_from(default_style)
114
+ file_name = filename_from(default_style)
115
+ public_url_custom_thumbnail_from(file_name, custom_width)
114
116
  else
115
- custom_width = nil
116
- file_name = name_for_file_from(style)
117
+ file_name = filename_from(style)
118
+ public_url_for(file_name)
117
119
  end
118
-
119
- public_url_for(file_name, custom_width)
120
120
  else
121
121
  default_image
122
122
  end
@@ -125,7 +125,7 @@ module Paperclip
125
125
  # Gets full title/name
126
126
  # @param style [ String ]
127
127
  # @return [ String ]
128
- def name_for_file_from(style)
128
+ def filename_from(style)
129
129
  file_name = instance.instance_exec(style, &file_title)
130
130
  style_suffix = (style != default_style ? "_#{style}" : "")
131
131
  if original_extension.present? && file_name =~ /#{original_extension}$/
@@ -135,35 +135,51 @@ module Paperclip
135
135
  end
136
136
  end
137
137
 
138
- alias_method :path, :name_for_file_from
138
+ alias_method :path, :filename_from
139
+
140
+ # Gets the public url for a passed filename
141
+ # @param title [ String ]
142
+ # @return [ String ] with url
143
+ def public_url_for(title)
144
+ metadata_or_default_img_from(title) do |metadata|
145
+ # TODO: fix permission issues on the view with this command
146
+ # effective_url_from(metadata.web_content_link)
147
+ custom_thumbnail_image_for(metadata.thumbnail_link, 1000)
148
+ end
149
+ end
139
150
 
140
151
  # Gets the public url for a passed filename
141
152
  # @param title [ String ]
142
153
  # @param custom_width [ Integer ]
143
154
  # @return [ String ] with url
144
- def public_url_for(title, custom_width)
145
- searched_id = search_for_title(title) #return id if any or style
146
- if searched_id.nil? # it finds some file
147
- default_image
148
- else
149
- metadata = metadata_by_id(searched_id)
150
- custom_image_for(metadata.thumbnail_link, custom_width)
155
+ def public_url_custom_thumbnail_from(title, custom_width)
156
+ metadata_or_default_img_from(title) do |metadata|
157
+ custom_thumbnail_image_for(metadata.thumbnail_link, custom_width)
151
158
  end
152
159
  end
153
160
 
154
161
  # Retrieves the specific image with a custom size. It is resized by GDrive API if you
155
- # pass the :custom as style option. In other cases it removes the last parameter `=s220`
156
- # which is inchaged to do the scaling process.
157
- # @param drive_thumbnail_link [ String ]
158
- # @param custom_width [ Integer ]
162
+ # pass the :custom_thumb as style option. In other cases, it removes the last parameter
163
+ # `=s220` which is inchaged to do the scaling process.
164
+ # @param drive_thumbnail_link [ String ] with the form: https://<url value>=s220
165
+ # @param custom_width [ Integer ] ex. 512
159
166
  # @return [ String ]
160
- def custom_image_for(drive_thumbnail_link, custom_width=nil)
167
+ def custom_thumbnail_image_for(drive_thumbnail_link, custom_width)
161
168
  file_url, current_width = drive_thumbnail_link.split(/=s/)
162
- new_file_url = if custom_width.nil?
163
- file_url
164
- else
165
- "#{ file_url }=s#{ custom_width }"
166
- end
169
+ "#{ file_url }=s#{ custom_width }"
170
+ end
171
+
172
+ # Gets the effective url from the web content link
173
+ # These are a series of steps to hack the way that GDrive API
174
+ # handle its urls. It consists in catch a Google::Apis::RedirectError error
175
+ # and take the correct url where is located the file.
176
+ # @param driver_web_content_link [ String ]
177
+ # @return [ String ]
178
+ def effective_url_from(drive_web_content_link)
179
+ redirect_url = drive_web_content_link.split(/&export=/)[0]
180
+ google_drive.http(:get, redirect_url) do |result, err|
181
+ err.header[:location].split('&continue=')[1]
182
+ end
167
183
  end
168
184
 
169
185
  # Takes the file title/name and search it in a given folder
@@ -192,7 +208,7 @@ module Paperclip
192
208
  client = google_api_client
193
209
  metadata = client.get_file(
194
210
  file_id,
195
- fields: 'id, name, thumbnailLink'
211
+ fields: 'id, name, thumbnailLink, webContentLink, webViewLink, trashed'
196
212
  )
197
213
  validate_metadata(metadata)
198
214
  metadata
@@ -206,6 +222,8 @@ module Paperclip
206
222
  raise 'the file id was not retrieved' if metadata.id.nil?
207
223
  raise 'the file name was not retrieved' if metadata.name.nil?
208
224
  raise 'the file thumbnail_link was not retrieved' if metadata.thumbnail_link.nil?
225
+ raise 'the file web_content_link was not retrieved' if metadata.web_content_link.nil?
226
+ raise 'the file trashed was not retrieved' if metadata.trashed.nil?
209
227
  end
210
228
 
211
229
  # Checks if the image already exits
@@ -222,12 +240,30 @@ module Paperclip
222
240
  end
223
241
  end
224
242
 
243
+ # Gets the file metadata if it exists
244
+ # in other case returns the defaul image
245
+ # @param title [ String ]
246
+ # @param block [ Proc ]
247
+ def metadata_or_default_img_from(title, &block)
248
+ searched_id = search_for_title(title) #return id if any or style
249
+ if searched_id.nil? # it finds some file
250
+ default_image
251
+ else
252
+ metadata = metadata_by_id(searched_id)
253
+ yield metadata
254
+ end
255
+ end
256
+
225
257
  def default_image
226
258
  if @google_drive_options[:default_url] #if default image is set
227
259
  title = @google_drive_options[:default_url]
228
260
  searched_id = search_for_title(title) # id
229
- metadata = metadata_by_id(searched_id) unless searched_id.nil?
230
- custom_image_for(metadata.thumbnail_link)
261
+ if searched_id.nil?
262
+ raise 'Default image not found, please double check its name'
263
+ else
264
+ metadata = metadata_by_id(searched_id)
265
+ effective_url_from(metadata.web_content_link)
266
+ end
231
267
  else
232
268
  'No picture' # ---- ?
233
269
  end
@@ -1,3 +1,3 @@
1
1
  module PaperclipGoogleDrive
2
- VERSION = "0.3.0"
2
+ VERSION = "0.3.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: paperclip-google-drive
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Diego Gomez
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-12-25 00:00:00.000000000 Z
11
+ date: 2016-12-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: paperclip