Commit 38966481

mo khan <mo@mokhan.ca>
2014-09-24 04:28:09
update admin section to re-process an image.
1 parent d513893
Changed files (3)
app
controllers
views
admin
config
app/controllers/admin/photos_controller.rb
@@ -1,7 +1,9 @@
 module Admin
   class PhotosController < AdminController
-    def initialize(photo_repository = Photo)
+    def initialize(photo_repository = Photo, message_bus = Spank::IOC.resolve(:message_bus), storage = Spank::IOC.resolve(:blob_storage))
       @photo_repository = photo_repository
+      @bus = message_bus
+      @storage = storage
       super()
     end
 
@@ -12,5 +14,33 @@ module Admin
     def show
       @photo = @photo_repository.find(params[:id])
     end
+
+    def update
+      photo = @photo_repository.find(params[:id])
+      original = OriginalVersion.new(photo)
+      key = original.create_key
+      @storage.download(key) do |file|
+        @bus.publish(:upload_photo, {
+          photo_id: photo.id,
+          file_path: move_to_temporary_storage(file.path, File.basename(key))
+        })
+      end
+
+      redirect_to admin_photos_path
+    end
+
+    private
+
+    def move_to_temporary_storage(temp_file_path, original_filename)
+      "#{create_tmp_dir}/#{original_filename}".tap do |new_path|
+        FileUtils.mv(temp_file_path, new_path)
+      end
+    end
+
+    def create_tmp_dir
+      Rails.root.join("tmp/uploads/#{SecureRandom.uuid}").tap do |directory|
+        system "mkdir -p #{directory}"
+      end
+    end
   end
 end
app/views/admin/photos/index.html.erb
@@ -12,16 +12,20 @@
           <th>geolocation</th>
           <th>created</th>
           <th>updated</th>
+          <th></th>
         </tr>
       </thead>
       <tbody>
         <% @photos.each do |photo| %>
           <tr>
-            <td><%= link_to photo.image, admin_photo_path(photo.id) %></td>
+            <td><%= link_to "#{photo.id} #{photo.image}", admin_photo_path(photo.id) %></td>
             <td><%= photo.content_type %></td>
             <td><%= photo.latitude %> <%= photo.longitude %></td>
             <td><%= time_ago_in_words(photo.created_at) %> ago</td>
             <td><%= time_ago_in_words(photo.updated_at) %> ago</td>
+            <td>
+              <%= link_to "Re-Process", admin_photo_path(photo.id), method: :put, class: 'btn', disable_with: 'Re-Processing..'  %>
+          </td>
           </tr>
         <% end %>
       </tbody>
config/routes.rb
@@ -75,7 +75,7 @@ Cake::Application.routes.draw do
     resources :jobs, only: [:index, :show, :update, :destroy]
     resources :activities, only: [:index]
     resources :subscriptions, only: [:index]
-    resources :photos, only: [:index, :show]
+    resources :photos, only: [:index, :show, :update]
     resources :blobs, only: [:index, :show]
     resources :errors, only: [:index, :create]
     resources :sessions, only: [:index, :destroy]