gallery 0.0.11

Sign up to get free protection for your applications and to get access to all the features.
Files changed (102) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/Rakefile +34 -0
  3. data/app/assets/javascripts/gallery/admin/admin.js +2 -0
  4. data/app/assets/javascripts/gallery/admin/albums.js +2 -0
  5. data/app/assets/javascripts/gallery/albums.js +2 -0
  6. data/app/assets/javascripts/gallery/application.js +13 -0
  7. data/app/assets/javascripts/gallery/gallery.js.coffee +229 -0
  8. data/app/assets/javascripts/gallery/images_loaded.js +6 -0
  9. data/app/assets/javascripts/gallery/omniauth_callbacks.js +2 -0
  10. data/app/assets/javascripts/gallery/photos.js +2 -0
  11. data/app/assets/javascripts/gallery/spinner.js +1 -0
  12. data/app/assets/stylesheets/gallery/admin/admin.css +4 -0
  13. data/app/assets/stylesheets/gallery/admin/albums.css +4 -0
  14. data/app/assets/stylesheets/gallery/albums.css +4 -0
  15. data/app/assets/stylesheets/gallery/application.css +13 -0
  16. data/app/assets/stylesheets/gallery/omniauth_callbacks.css +4 -0
  17. data/app/assets/stylesheets/gallery/photos.css +4 -0
  18. data/app/assets/stylesheets/gallery/style.css.scss +193 -0
  19. data/app/controllers/gallery/admin/admin_controller.rb +14 -0
  20. data/app/controllers/gallery/admin/albums_controller.rb +36 -0
  21. data/app/controllers/gallery/albums_controller.rb +9 -0
  22. data/app/controllers/gallery/application_controller.rb +4 -0
  23. data/app/controllers/gallery/omniauth_callbacks_controller.rb +27 -0
  24. data/app/controllers/gallery/photos_controller.rb +23 -0
  25. data/app/helpers/gallery/admin/admin_helper.rb +4 -0
  26. data/app/helpers/gallery/admin/albums_helper.rb +4 -0
  27. data/app/helpers/gallery/albums_helper.rb +4 -0
  28. data/app/helpers/gallery/application_helper.rb +4 -0
  29. data/app/helpers/gallery/omniauth_callbacks_helper.rb +4 -0
  30. data/app/helpers/gallery/photos_helper.rb +4 -0
  31. data/app/models/gallery/album.rb +66 -0
  32. data/app/models/gallery/identity.rb +43 -0
  33. data/app/models/gallery/photo.rb +14 -0
  34. data/app/views/gallery/admin/admin/index.html.erb +46 -0
  35. data/app/views/gallery/admin/albums/show.html.erb +14 -0
  36. data/app/views/gallery/albums/_admin_album_element.html.erb +18 -0
  37. data/app/views/gallery/albums/_album_element.html.erb +15 -0
  38. data/app/views/gallery/albums/index.html.erb +9 -0
  39. data/config/initializers/omniauth.rb +18 -0
  40. data/config/routes.rb +22 -0
  41. data/db/migrate/20130806170912_create_gallery_identities.rb +14 -0
  42. data/db/migrate/20130806212451_add_token_to_identity.rb +5 -0
  43. data/db/migrate/20130806213308_create_gallery_albums.rb +19 -0
  44. data/db/migrate/20130806214102_create_gallery_photos.rb +18 -0
  45. data/db/migrate/20130806221405_add_public_to_album.rb +5 -0
  46. data/db/migrate/20130806221438_add_public_to_photo.rb +5 -0
  47. data/db/migrate/20130807113616_add_thumbnail_to_photo.rb +6 -0
  48. data/lib/gallery.rb +4 -0
  49. data/lib/gallery/engine.rb +9 -0
  50. data/lib/gallery/version.rb +3 -0
  51. data/lib/tasks/gallery_tasks.rake +4 -0
  52. data/test/controllers/gallery/admin/admin_controller_test.rb +9 -0
  53. data/test/controllers/gallery/admin/albums_controller_test.rb +9 -0
  54. data/test/controllers/gallery/albums_controller_test.rb +9 -0
  55. data/test/controllers/gallery/omniauth_callbacks_controller_test.rb +9 -0
  56. data/test/controllers/gallery/photos_controller_test.rb +9 -0
  57. data/test/dummy/README.rdoc +28 -0
  58. data/test/dummy/Rakefile +6 -0
  59. data/test/dummy/app/assets/javascripts/application.js +13 -0
  60. data/test/dummy/app/assets/stylesheets/application.css +13 -0
  61. data/test/dummy/app/controllers/application_controller.rb +5 -0
  62. data/test/dummy/app/helpers/application_helper.rb +2 -0
  63. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  64. data/test/dummy/bin/bundle +3 -0
  65. data/test/dummy/bin/rails +4 -0
  66. data/test/dummy/bin/rake +4 -0
  67. data/test/dummy/config.ru +4 -0
  68. data/test/dummy/config/application.rb +23 -0
  69. data/test/dummy/config/boot.rb +5 -0
  70. data/test/dummy/config/database.yml +25 -0
  71. data/test/dummy/config/environment.rb +5 -0
  72. data/test/dummy/config/environments/development.rb +29 -0
  73. data/test/dummy/config/environments/production.rb +80 -0
  74. data/test/dummy/config/environments/test.rb +36 -0
  75. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  76. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  77. data/test/dummy/config/initializers/inflections.rb +16 -0
  78. data/test/dummy/config/initializers/mime_types.rb +5 -0
  79. data/test/dummy/config/initializers/secret_token.rb +12 -0
  80. data/test/dummy/config/initializers/session_store.rb +3 -0
  81. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  82. data/test/dummy/config/locales/en.yml +23 -0
  83. data/test/dummy/config/routes.rb +4 -0
  84. data/test/dummy/public/404.html +58 -0
  85. data/test/dummy/public/422.html +58 -0
  86. data/test/dummy/public/500.html +57 -0
  87. data/test/dummy/public/favicon.ico +0 -0
  88. data/test/fixtures/gallery/albums.yml +23 -0
  89. data/test/fixtures/gallery/identities.yml +13 -0
  90. data/test/fixtures/gallery/photos.yml +21 -0
  91. data/test/gallery_test.rb +7 -0
  92. data/test/helpers/gallery/admin/admin_helper_test.rb +6 -0
  93. data/test/helpers/gallery/admin/albums_helper_test.rb +6 -0
  94. data/test/helpers/gallery/albums_helper_test.rb +6 -0
  95. data/test/helpers/gallery/omniauth_callbacks_helper_test.rb +6 -0
  96. data/test/helpers/gallery/photos_helper_test.rb +6 -0
  97. data/test/integration/navigation_test.rb +10 -0
  98. data/test/models/gallery/album_test.rb +9 -0
  99. data/test/models/gallery/identity_test.rb +9 -0
  100. data/test/models/gallery/photo_test.rb +9 -0
  101. data/test/test_helper.rb +15 -0
  102. metadata +324 -0
@@ -0,0 +1,14 @@
1
+ require_dependency "gallery/application_controller"
2
+
3
+ module Gallery
4
+ class Admin::AdminController < ApplicationController
5
+ before_filter :login_required
6
+ def index
7
+ @identities = Identity.all
8
+ @public_albums = Album.published
9
+ @private_albums = Album.private
10
+ configpath = Rails.root.join('config', 'gallery_api_keys.yml')
11
+ @providers = YAML.load(ERB.new(File.new(configpath).read).result).keys
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,36 @@
1
+ require_dependency "gallery/application_controller"
2
+
3
+ module Gallery
4
+ class Admin::AlbumsController < ApplicationController
5
+
6
+ before_filter :login_required
7
+
8
+ def fetch_albums
9
+ Identity.all.each(&:fetch_albums)
10
+ redirect_to :back, :notice => "Albums fetched"
11
+ end
12
+
13
+ def set_public
14
+ album = Album.find(params[:album_id])
15
+ album.update_attributes(:public => true)
16
+ redirect_to :back, :notice => "Album #{album.name} set public"
17
+ end
18
+
19
+ def set_private
20
+ album = Album.find(params[:album_id])
21
+ album.update_attributes(:public => false)
22
+ redirect_to :back, :notice => "Album #{album.name} set private"
23
+ end
24
+
25
+ def fetch_photos
26
+ album = Album.find(params[:album_id])
27
+ album.fetch_photos
28
+ redirect_to :back, :notice => "Photos fetched"
29
+ end
30
+
31
+ def show
32
+ @album = Album.find(params[:id])
33
+ end
34
+
35
+ end
36
+ end
@@ -0,0 +1,9 @@
1
+ require_dependency "gallery/application_controller"
2
+
3
+ module Gallery
4
+ class AlbumsController < ApplicationController
5
+ def index
6
+ @albums = Album.published
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,4 @@
1
+ module Gallery
2
+ class ApplicationController < ::ApplicationController
3
+ end
4
+ end
@@ -0,0 +1,27 @@
1
+ require_dependency "gallery/application_controller"
2
+
3
+ module Gallery
4
+ class OmniauthCallbacksController < ApplicationController
5
+ def callback
6
+ @omniauth_hash = request.env["omniauth.auth"]
7
+ @omniauth_hash.provider = 'google' if @omniauth_hash.provider == "google_oauth2"
8
+
9
+ identity = Identity.find_by_uid_and_provider(@omniauth_hash.uid, @omniauth_hash.provider)
10
+ identity.update_attributes({:token => @omniauth_hash.credentials.token}) if identity
11
+ identity = Identity.create parse_omniauth_hash_to_identity(@omniauth_hash) unless identity
12
+
13
+ redirect_to admin_path
14
+ end
15
+
16
+ def parse_omniauth_hash_to_identity(omniauth_hash)
17
+ case omniauth_hash.provider
18
+ when 'facebook'
19
+ return { :uid => @omniauth_hash.uid, :provider => @omniauth_hash.provider, :token => @omniauth_hash.credentials.token, :name => @omniauth_hash.info.name, :link => @omniauth_hash.extra.raw_info.link }
20
+ when 'eyeem'
21
+ return { :uid => @omniauth_hash.uid, :provider => @omniauth_hash.provider, :token => @omniauth_hash.credentials.token, :name => @omniauth_hash.info.name, :link => @omniauth_hash.extra.raw_info.user.webUrl }
22
+ when 'google'
23
+ return { :uid => @omniauth_hash.uid, :provider => @omniauth_hash.provider, :token => @omniauth_hash.credentials.token, :name => @omniauth_hash.info.name, :link => @omniauth_hash.extra.raw_info.link }
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,23 @@
1
+ require_dependency "gallery/application_controller"
2
+
3
+ module Gallery
4
+ class PhotosController < ApplicationController
5
+ respond_to :json
6
+
7
+ if respond_to?('caches_action')
8
+ caches_action :index
9
+ end
10
+
11
+ def index
12
+ album = Album.find_by_id(params[:album_id])
13
+ album = Album.find_by_uid_and_provider(params[:album_id].split('-').first, params[:album_id].split('-').last) unless album
14
+ photos = album.photos
15
+ if album.public
16
+ respond_with photos, :only => [:id, :album_id, :source, :name, :aspect_ratio, :thumbnail]
17
+ else
18
+ respond_with []
19
+ end
20
+ end
21
+
22
+ end
23
+ end
@@ -0,0 +1,4 @@
1
+ module Gallery
2
+ module Admin::AdminHelper
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module Gallery
2
+ module Admin::AlbumsHelper
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module Gallery
2
+ module AlbumsHelper
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module Gallery
2
+ module ApplicationHelper
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module Gallery
2
+ module OmniauthCallbacksHelper
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module Gallery
2
+ module PhotosHelper
3
+ end
4
+ end
@@ -0,0 +1,66 @@
1
+ module Gallery
2
+ class Album < ActiveRecord::Base
3
+ validates :uid, :uniqueness => { :scope => :provider }
4
+
5
+ has_many :photos, :dependent => :destroy
6
+ belongs_to :identity, :touch => true
7
+
8
+ scope :published, -> { where(public: true) }
9
+ scope :private, -> {where(public: false)}
10
+ default_scope order('updated_at DESC')
11
+
12
+ def fetch_photos
13
+ case provider.to_s
14
+ when 'eyeem'
15
+
16
+ client = OAuth2::Client.new('5kkCFgioQ2UgtZwTnpMFPH4FYCYWpkkq', 'RfdwC60OGDQOCaPssQIqiAbG5Y5HWzwG', :site => 'https://www.eyeem.com', :parse_json => true)
17
+ access_token = OAuth2::AccessToken.new(client, identity.token, :site => 'https://eyeem.com/')
18
+ response = JSON.parse(access_token.get('/api/v2/users/me/photos?detailed=1').body)
19
+
20
+ raw_photos = response['photos']['items']
21
+
22
+ processed_photos = []
23
+
24
+ Album.transaction do
25
+ processed_photos = raw_photos.map { |photo| photos.create :uid => photo['id'], :provider => :eyeem, :thumbs => {}, :source => photo['photoUrl'], :name => photo['id'], :url => photo['webUrl'], :aspect_ratio => photo['width']/photo['height'].to_f, :lat => (photo['latitude'] == '' ? nil : photo['latitude']), :long => (photo['longitude'] == '' ? nil : photo['longitude']), :taken_at => DateTime.parse(photo['updated']), :like_count => photo['totalLikes'], :comment_count => photo['totalComments'], :filter_name => (photo.has_key?('filter') ? photo['filter'] : nil) }
26
+ end
27
+
28
+ # fetch more if we have created them all and not empty
29
+ while !processed_photos.map(&:persisted?).any?(&:blank?) and processed_photos.length > 0 do
30
+ limit = response['photos']['limit']
31
+ offset = response['photos']['offset'] + limit
32
+ response = JSON.parse(access_token.get("/api/v2/users/me/photos?detailed=1&limit=#{limit}&offset=#{offset}").body)
33
+ raw_photos = response['photos']['items']
34
+
35
+ Album.transaction do
36
+ processed_photos = raw_photos.map { |photo| photos.create :uid => photo['id'], :provider => :eyeem, :thumbs => {}, :source => photo['photoUrl'], :name => photo['id'], :url => photo['webUrl'], :aspect_ratio => photo['width']/photo['height'].to_f, :lat => (photo['latitude'] == '' ? nil : photo['latitude']), :long => (photo['longitude'] == '' ? nil : photo['longitude']), :taken_at => DateTime.parse(photo['updated']), :like_count => photo['totalLikes'], :comment_count => photo['totalComments'], :filter_name => (photo.has_key?('filter') ? photo['filter'] : nil) }
37
+ end
38
+
39
+ end
40
+
41
+
42
+ when 'google'
43
+ client = OAuth2::Client.new('1040704984149.apps.googleusercontent.com', 'RHObwIrckTzGtwLA6aRY-vLB', :site => 'https://picasaweb.google.com/data/')
44
+ access_token = OAuth2::AccessToken.new(client, self.identity.token, :site => 'https://picasaweb.google.com/')
45
+ album_response = JSON.parse(access_token.get("/data/feed/api/user/default/albumid/#{uid}?alt=json").body)
46
+
47
+ return unless album_response['feed'].has_key? 'entry'
48
+
49
+ raw_photos = album_response['feed']['entry']
50
+
51
+ Album.transaction do
52
+ processed_photos = raw_photos.map { |photo| photos.create :uid => photo['gphoto$id']['$t'], :provider => :google, :thumbnail => photo['content']['src'].gsub(/\/[^\/]*\z/, '/w512-h512-no/photo'), :url => photo['link'][1]['href'], :name => photo['title']['$t'], :source => photo['content']['src'].gsub(/\/[^\/]*\z/, '/w2048-h2048-no/photo'), :aspect_ratio => photo['gphoto$width']['$t'].to_f/photo['gphoto$height']['$t'].to_f}
53
+
54
+ # only fetch more if all photos were successfully created and we have equal total to per page
55
+ # TODO: actually do this, google right now returns 1000, plenty
56
+ end
57
+
58
+ end
59
+ end
60
+
61
+ def cover_photo
62
+ photos.first
63
+ end
64
+
65
+ end
66
+ end
@@ -0,0 +1,43 @@
1
+ module Gallery
2
+ class Identity < ActiveRecord::Base
3
+ has_many :albums
4
+
5
+ def fetch_albums(force_fetch = false)
6
+ case provider
7
+ when 'eyeem'
8
+
9
+ album = albums.find_by_provider_and_uid( :eyeem, 'eyeem_identifier')
10
+ album = albums.create :provider => :eyeem, :uid => 'eyeem_identifier', :name => "My Eyeem Photos" unless album
11
+
12
+ album.fetch_photos
13
+
14
+ when 'google'
15
+
16
+ # Load api keys
17
+
18
+ configpath = Rails.root.join('config', 'gallery_api_keys.yml')
19
+ providers = {}
20
+ if File.exists?(configpath)
21
+
22
+ providers = YAML.load(ERB.new(File.new(configpath).read).result)
23
+ end
24
+
25
+ return unless providers["google_oauth2"]
26
+
27
+ client = OAuth2::Client.new(providers["google_oauth2"][0], providers["google_oauth2"][1], :site => 'https://picasaweb.google.com/data/')
28
+ access_token = OAuth2::AccessToken.new(client, self.token, :site => 'https://picasaweb.google.com/')
29
+ album_response = JSON.parse(access_token.get('/data/feed/api/user/default?alt=json').body)
30
+ # albums
31
+ raw_albums = album_response['feed']['entry']
32
+
33
+ raw_albums.each do |album|
34
+ processed_album = albums.where({ :uid => album['gphoto$id']['$t'], :provider => 'google', :name => album['title']['$t'], :description => album['summary']['$t'], :url => album['link'][1]['href']}).first_or_create
35
+
36
+ # only fetch if we are forced, or if updated_at is old enough
37
+ processed_album.fetch_photos if force_fetch or processed_album.updated_at < album['updated']['$t']
38
+ end
39
+ end
40
+ end
41
+
42
+ end
43
+ end
@@ -0,0 +1,14 @@
1
+ module Gallery
2
+ class Photo < ActiveRecord::Base
3
+
4
+ validates :uid, :uniqueness => { :scope => :provider }
5
+ validates :source, :presence => true
6
+ validates :url, :presence => true
7
+ validates :uid, :presence => true
8
+ validates :provider, :presence => true
9
+ validates :thumbnail, :presence => true
10
+ validates :aspect_ratio, :presence => true
11
+
12
+ belongs_to :album, :counter_cache => true, :touch => true
13
+ end
14
+ end
@@ -0,0 +1,46 @@
1
+
2
+
3
+ <% if !@identities.blank? %>
4
+ <h3>All your accounts</h3>
5
+ <% for identity in @identities %>
6
+ <li>
7
+ <%= link_to identity.name, identity.link %>
8
+ </li>
9
+ <% end %>
10
+ <%= link_to "Fetch Albums", admin_fetch_albums_path, :method => :post %>
11
+ <% else %>
12
+ <h3>No Accounts connected</h3>
13
+ <% end %>
14
+
15
+ <% if !@providers.blank? %>
16
+ <h3>Connect other accounts</h3>
17
+ <p>
18
+ I haven't really figured out how to get omniauth routes mounted inside an engine, so for now, you got to initiate the oauth process manually. Below you will see a list of providers currently initialized from your config/gallery_api_keys.yml. Visit "/#{mount_point}/auth/#{provider_name}". It should then show up here.
19
+ </p>
20
+ <% for provider in @providers %>
21
+ <li><%= provider %></li>
22
+ <% end %>
23
+ <% else %>
24
+
25
+ <% end %>
26
+
27
+
28
+ <h4>Published Albums</h4>
29
+
30
+ <% if !@public_albums.blank? %>
31
+ <% for album in @public_albums %>
32
+ <%= render "gallery/albums/admin_album_element", album: album %>
33
+ <% end %>
34
+ <% else %>
35
+ <p>You have no published albums</p>
36
+ <% end %>
37
+
38
+ <h4>Published Albums</h4>
39
+
40
+ <% if !@private_albums.blank? %>
41
+ <% for album in @private_albums %>
42
+ <%= render "gallery/albums/admin_album_element", album: album %>
43
+ <% end %>
44
+ <% else %>
45
+ <p>You have no private albums</p>
46
+ <% end %>
@@ -0,0 +1,14 @@
1
+ <h3><%= @album.name %></h3>
2
+ <p>
3
+ ID: <%= @album.id %> <br>
4
+ UID: <%= @album.uid %> <br>
5
+ Provider: <%= @album.provider %>
6
+ </p>
7
+
8
+ <% if !@album.photos.blank? %>
9
+ <% for photo in @album.photos %>
10
+ <img src="<%= photo.thumbnail %>" alt=" ">
11
+ <% end %>
12
+ <% else %>
13
+
14
+ <% end %>
@@ -0,0 +1,18 @@
1
+ <div class="gallery-album-admin">
2
+ <img src="<%= album.cover_photo.try(:thumbnail) %>" alt="">
3
+ <h3><%= album.name %></h3>
4
+ <p>
5
+ ID: <%= album.id %> <br>
6
+ UID: <%= album.uid %> <br>
7
+ Provider: <%= album.provider %>
8
+ </p>
9
+ <p>
10
+ <%= link_to (album.public ? "Make Album private" : "Make Album public"), (album.public ? admin_album_private_path(album) : admin_album_public_path(album)), :method => :post %>
11
+
12
+ <br>
13
+ <%= link_to "Fetch Photos", admin_album_fetch_photos_path(album), :method => :post %>
14
+ <br>
15
+ <%= link_to "Go to Album", admin_album_path(album) %>
16
+ </p>
17
+
18
+ </div>
@@ -0,0 +1,15 @@
1
+ <div class="gallery-album">
2
+ <h3><%= album.name %></h3>
3
+ <div class="gallery-album-photos">
4
+ <div class="gallery-album-photos-canvas" tabindex="0">
5
+ <% if !album.photos.blank? %>
6
+ <% for photo in album.photos %>
7
+ <img data-src="<%= photo.thumbnail %>" data-big-src="<%= photo.source %>" loaded=false class='' alt="">
8
+ <% end %>
9
+ <% else %>
10
+
11
+ <% end %>
12
+ </div>
13
+
14
+ </div>
15
+ </div>
@@ -0,0 +1,9 @@
1
+ <% cache @albums do %>
2
+ <% if !@albums.blank? %>
3
+ <% for album in @albums %>
4
+ <%= render "album_element", album: album %>
5
+ <% end %>
6
+ <% else %>
7
+
8
+ <% end %>
9
+ <% end %>
@@ -0,0 +1,18 @@
1
+ configpath = Rails.root.join('config', 'gallery_api_keys.yml')
2
+ providers = {}
3
+ if File.exists?(configpath)
4
+
5
+ providers = YAML.load(ERB.new(File.new(configpath).read).result)
6
+ end
7
+
8
+
9
+ Rails.application.config.middleware.use OmniAuth::Builder do
10
+
11
+ configure do |config|
12
+ config.path_prefix = '/gallery/auth'
13
+ end
14
+
15
+ providers.each do |k,v|
16
+ provider *(v.unshift(k))
17
+ end
18
+ end
data/config/routes.rb ADDED
@@ -0,0 +1,22 @@
1
+ Gallery::Engine.routes.draw do
2
+ namespace :admin do
3
+ post 'fetch_albums' => "albums#fetch_albums"
4
+ resources :albums do
5
+ post 'public' => "albums#set_public"
6
+ post 'private' => "albums#set_private"
7
+ post 'fetch_photos' => "albums#fetch_photos"
8
+ resources :photos do
9
+
10
+ end
11
+ end
12
+ get '' => "admin#index"
13
+ end
14
+
15
+ resources :albums do
16
+ resources :photos
17
+ end
18
+
19
+ get "auth/:provider/callback" => "omniauth_callbacks#callback"
20
+
21
+ get '' => "albums#index", :as => :gallery_root
22
+ end