myexperiment-hackers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[myexperiment-hackers] [2303] branches/bulk: bulk upload files


From: noreply
Subject: [myexperiment-hackers] [2303] branches/bulk: bulk upload files
Date: Tue, 24 Nov 2009 12:08:32 -0500 (EST)

Revision
2303
Author
dgc
Date
2009-11-24 12:08:32 -0500 (Tue, 24 Nov 2009)

Log Message

bulk upload files

Modified Paths

Added Paths

Diff

Added: branches/bulk/app/controllers/contributions_controller.rb (0 => 2303)


--- branches/bulk/app/controllers/contributions_controller.rb	                        (rev 0)
+++ branches/bulk/app/controllers/contributions_controller.rb	2009-11-24 17:08:32 UTC (rev 2303)
@@ -0,0 +1,225 @@
+# myExperiment: app/controllers/contributions.rb
+#
+# Copyright (c) 2007 University of Manchester and the University of Southampton.
+# See license.txt for details.
+
+require 'zip/zip'
+
+class ContributionsController < ApplicationController
+
+  before_filter :login_required
+  before_filter :find_pending_upload, : [:bulk_preview_image, :bulk_set_title, :bulk_set_description, :bulk_set_category, :bulk_set_content_type, :bulk_set_license, :bulk_set_preview_image]
+
+  def bulk_upload
+    @pending_uploads = PendingUpload.find_all_by_user_id(current_user.id)
+
+    @sharing_mode  = 0
+    @updating_mode = 6
+  end
+
+  def bulk_upload_zip
+
+    zip_data = params[:zip_file]
+
+    if zip_data.nil? or zip_data == ""
+      flash.now[:error] = "You must specify a Zip file to upload"
+      redirect_to(bulk_upload_contributions_path)
+      return
+    end
+
+    # Put the Zip file on disk for processing
+
+    Tempfile.open("bulk", "tmp") { |zip_file|
+
+      zip_file.write(zip_data.read)
+      zip_file.open
+
+      # Iterate through each Zip entry
+
+      Zip::ZipFile.foreach(zip_file.path) { |entry|
+        if entry.file?
+          stream = entry.get_input_stream
+          stream.extend FileUpload
+          stream.original_filename = entry.name
+
+          # Extract metadata if possible
+
+          stream.rewind
+          proc_class = WorkflowTypesHandler.processor_class_for_file(stream)
+
+          if proc_class != nil
+            
+            content_type = ContentType.find_by_title(proc_class.display_name)
+            category     = content_type.default_category
+
+            if proc_class.can_infer_metadata?
+
+              stream.rewind
+              proc_inst = proc_class.new(stream.read)
+
+              title = proc_inst.get_title
+              desc  = proc_inst.get_description
+
+              if proc_class.can_generate_preview_image?
+                image = proc_inst.get_preview_image.read
+              end
+
+              if proc_class.can_generate_preview_svg?
+                svg = proc_inst.get_preview_svg.read
+              end
+            end
+          end
+
+          stream.rewind
+
+          pu = PendingUpload.new(
+              :user         => current_user,
+              :content      => stream.read,
+              :content_type => content_type,
+              :category     => category,
+              :title        => title,
+              :description  => desc,
+              :image        => image,
+              :svg          => svg,
+              :file_name    => entry.name)
+
+          pu.save
+        end
+      }
+    }
+
+    redirect_to(bulk_upload_contributions_path)
+  end
+
+  def bulk_preview_image
+    if @pu.image
+      send_data(@pu.image, :filename => "preview.jpg", :type => 'image/jpeg',
+          :disposition => 'inline')
+    else 
+      redirect_to "/images/avatar.png"
+    end
+  end
+
+  def bulk_set_title
+    @pu.update_attributes(:title => params["title"])
+
+    render :partial => "contributions/new_upload", :locals => { :pending_upload => @pu }
+  end
+
+  def bulk_set_description
+    @pu.update_attributes(:description => params["description"])
+
+    render :partial => "contributions/new_upload", :locals => { :pending_upload => @pu }
+  end
+
+  def bulk_set_category
+
+    @pu.update_attributes(:category => params["category"])
+
+    render :partial => "contributions/new_upload", :locals => { :pending_upload => @pu }
+  end
+
+  def bulk_set_content_type
+
+    ct = ContentType.find_by_title(params["content type"])
+
+    if ct != nil
+      @pu.update_attributes(:content_type => ct)
+    end
+
+    render :partial => "contributions/new_upload", :locals => { :pending_upload => @pu }
+  end
+
+  def bulk_set_license
+
+    l = License.find_by_unique_name(params["license"])
+
+    if l != nil
+      @pu.update_attributes(:license => l)
+    end
+
+    render :partial => "contributions/new_upload", :locals => { :pending_upload => @pu }
+  end
+
+  def bulk_set_preview_image
+
+    @pu.update_attributes(:image => params["preview"].read)
+
+    render :partial => "contributions/new_upload", :locals => { :pending_upload => @pu }
+  end
+
+  def bulk_create
+
+    def do_workflow(pending_upload, query)
+      
+      @workflow = Workflow.new
+      @workflow.contributor = current_user
+      @workflow.last_edited_by = current_user.id
+      @workflow.content_type = pending_upload.content_type
+      @workflow.license = pending_upload.license
+      @workflow.content_blob = ContentBlob.new(:data ="" pending_upload.content)
+      @workflow.file_ext = pending_upload.file_name.split(".").last.downcase
+      
+      @workflow.title = pending_upload.title
+      @workflow.body = pending_upload.description
+#     @workflow.image = pending_upload.image
+#     @workflow.svg = pending_upload.svg
+
+      if @workflow.save == false
+
+
+        puts "workflow errors = address@hidden"
+        return false
+      end
+
+      if params[:workflow][:tag_list]
+        @workflow.refresh_tags(convert_tags_to_gem_format(params[:workflow][:tag_list]), current_user)
+      end
+      
+      update_policy(@workflow, params)
+      update_credits(@workflow, params)
+      update_attributions(@workflow, params)
+
+#     @titles.push(@workflow.title)
+      
+      return true
+    end
+
+    @pending_uploads = PendingUpload.find_all_by_user_id(current_user.id)
+
+    begin
+      Workflow.transaction do
+
+        @pending_uploads.each do |pending_upload|
+          if pending_upload.category == 'Workflow'
+            do_workflow(pending_upload, query)
+          end
+        end
+
+        # raise BulkUploadError unless @overall_success
+      end
+
+    rescue BulkUploadError
+      # Here so that the transaction will rollback upon failure
+    end
+
+    render(:text => 'Got here.')
+  end
+
+private
+
+  def find_pending_upload
+
+    pu = PendingUpload.find_by_id(params["id"])
+
+    if pu.nil? or current_user.id != pu.user_id
+      render :nothing => true, :status => "404 Not Found"
+      return false
+    end
+
+    @pu = pu
+    return true
+  end
+
+end
+

Added: branches/bulk/app/models/pending_upload.rb (0 => 2303)


--- branches/bulk/app/models/pending_upload.rb	                        (rev 0)
+++ branches/bulk/app/models/pending_upload.rb	2009-11-24 17:08:32 UTC (rev 2303)
@@ -0,0 +1,13 @@
+# myExperiment: app/models/pending_upload.rb
+#
+# Copyright (c) 2007 University of Manchester and the University of Southampton.
+# See license.txt for details.
+
+class PendingUpload < ActiveRecord::Base
+
+  belongs_to :user
+  belongs_to :license
+  belongs_to :content_type
+
+end
+

Added: branches/bulk/app/views/contributions/_new_upload.rhtml (0 => 2303)


--- branches/bulk/app/views/contributions/_new_upload.rhtml	                        (rev 0)
+++ branches/bulk/app/views/contributions/_new_upload.rhtml	2009-11-24 17:08:32 UTC (rev 2303)
@@ -0,0 +1,128 @@
+<% div_id = "pending-upload-#{pending_upload.id}" %>
+
+<h2><%=h pending_upload.file_name %></h2>
+
+<table>
+  <tr><td>Title:</td><td><%=h pending_upload.title %></td></tr>
+  <tr><td>Description:</td><td><%= white_list(pending_upload.description) %></td></tr>
+  <tr><td>Category:</td><td><%= pending_upload.category %></td></tr>
+  <tr><td>Type:</td><td>
+    <% if pending_upload.content_type.nil? %>
+      <span>Click to set type...</span>
+    <% else %>
+      <%= pending_upload.content_type.title %>
+    <% end %>
+  <tr><td>License:</td><td>
+    <% if pending_upload.license.nil? %>
+      <span>Click to set license...</span>
+    <% else %>
+      <%= pending_upload.license.title %>
+    <% end %>
+  </td></tr>
+  <tr><td>Image:</td><td><img src="" bulk_preview_image_contributions_path(:id => pending_upload.id) %>" style="width: 128px; height: 128px; border: 1px SOLID GRAY; padding: 2px" /></td></tr>
+</table>
+
+<% form_remote_tag(:url ="" bulk_set_title_contributions_path,
+    :update => div_id,
+    :loading => "Element.show('busy_indicator')",
+    :complete => "Element.hide('busy_indicator'); new Effect.Highlight('#{div_id}', { duration: 1.5 });") do %>
+				
+  <input name="title" />
+  <input type="submit" name="commit" value="Set title" />
+
+  <%= hidden_field_tag "id", pending_upload.id %>
+ 
+<% end %>
+
+<% form_remote_tag(:url ="" bulk_set_description_contributions_path,
+    :update => div_id,
+    :loading => "Element.show('busy_indicator')",
+    :complete => "Element.hide('busy_indicator'); new Effect.Highlight('#{div_id}', { duration: 1.5 });") do %>
+				
+  <input name="description" />
+  <input type="submit" name="commit" value="Set description" />
+
+  <%= hidden_field_tag "id", pending_upload.id %>
+ 
+<% end %>
+
+<% form_remote_tag(:url ="" bulk_set_category_contributions_path,
+    :update => div_id,
+    :loading => "Element.show('busy_indicator')",
+    :complete => "Element.hide('busy_indicator'); new Effect.Highlight('#{div_id}', { duration: 1.5 });") do %>
+				
+  <input name="category" />
+  <input type="submit" name="commit" value="Set category" />
+
+  <%= hidden_field_tag "id", pending_upload.id %>
+ 
+<% end %>
+
+<% form_remote_tag(:url ="" bulk_set_content_type_contributions_path,
+    :update => div_id,
+    :loading => "Element.show('busy_indicator')",
+    :complete => "Element.hide('busy_indicator'); new Effect.Highlight('#{div_id}', { duration: 1.5 });") do %>
+				
+  <input name="content type" />
+  <input type="submit" name="commit" value="Set content type" />
+
+  <%= hidden_field_tag "id", pending_upload.id %>
+ 
+<% end %>
+
+<% form_remote_tag(:url ="" bulk_set_license_contributions_path,
+    :update => div_id,
+    :loading => "Element.show('busy_indicator')",
+    :complete => "Element.hide('busy_indicator'); new Effect.Highlight('#{div_id}', { duration: 1.5 });") do %>
+				
+  <input name="license" />
+  <input type="submit" name="commit" value="Set license" />
+
+  <%= hidden_field_tag "id", pending_upload.id %>
+ 
+<% end %>
+
+
+<span><iframe name="upload-preview-1"></iframe></span>
+
+<form action="" enctype="multipart/form-data" method="post"  Ajax.Updater('pending-upload-1', '/contributions/bulk_set_preview_image', {asynchronous:true, evalScripts:true, onComplete:function(request){Element.hide('busy_indicator'); new Effect.Highlight('pending-upload-1', { duration: 1.5 });}, onLoading:function(request){Element.show('busy_indicator')}, parameters:Form.serialize(this)}); return false;">
+
+				
+  <input name="preview" type="file" />
+  <input type="submit" name="commit" value="Set preview image" />
+  <input id="id" name="id" type="hidden" value="1" />
+     
+</form>
+     
+<% form_tag({:action ="" :bulk_create}) do %>
+
+  <p style="text-align: center;">
+    <%= submit_tag "Upload and Save", :disable_with => "Uploading and saving..." %>
+  </p>
+
+  <br />
+	
+	<!-- Other metadata and settings -->
+	
+	<p class="step_text" style="text-align: center;">2. Other metadata and settings</p>
+	
+	<!-- Tags -->
+  <%= render :partial => "tags/tags_form", :locals => { :edit => false, :taggable => @workflow } -%>
+                
+  <!-- Credit and Attribution -->
+  <%= render :partial => "contributions/credit_attribution_form", :locals => { :edit => false, :contributable => @workflow } -%>
+                
+  <!-- Sharing -->              
+  <%= render :partial => "contributions/sharing_form", :locals => { :edit => false, :contributable => @workflow, :update_perms => true } -%>
+                
+  <!-- License/Rights -->
+  <%= render :partial => "workflows/license_form", :locals => { :edit => false } -%>
+  
+	
+
+  <p style="text-align: center;">
+    <%= submit_tag "Upload and Save", :disable_with => "Uploading and saving..." %>
+  </p>
+  
+<% end %>
+

Added: branches/bulk/app/views/contributions/bulk_upload.rhtml (0 => 2303)


--- branches/bulk/app/views/contributions/bulk_upload.rhtml	                        (rev 0)
+++ branches/bulk/app/views/contributions/bulk_upload.rhtml	2009-11-24 17:08:32 UTC (rev 2303)
@@ -0,0 +1,91 @@
+<% t "Bulk upload" -%>
+
+<%= _javascript__include_tag :fckeditor %>
+<%= _javascript__include_tag "bulk_upload.js" %>
+<%= _javascript__include_tag "osp.js" %>
+
+<h1>Bulk upload</h1>
+
+<center>
+	<%= error_messages_for :workflow %>
+</center>
+
+<% form_tag({:action ="" :bulk_upload_zip}, :multipart => true) do %>
+
+  <!-- Zip File -->
+	
+	<p class="step_text">1. Zip file containing Workflow files/scripts</p>
+
+  <div class="box_infotext">
+    <p>Select a Zip file containing multiple Workflow files.</p>
+  </div>
+
+  <br />
+  <br />
+
+  <center>
+    <div class="box_form" style="width: 600px; text-align: center;">
+      <p style="text-align: center;">
+        <input id="zip_file" name="zip_file" size="70" type="file"/>
+      </p>
+    </div>
+  </center>
+
+  <br />
+
+  <p style="text-align: center;">
+    <%= submit_tag "Add files from a Zip file", :disable_with => "Adding files from a Zip file..." %>
+  </p>
+
+<% end %>
+  
+<div><%= "Entry count: address@hidden" %></div>
+
+<% @pending_uploads.each do |pending_upload| %>
+  <% div_id = "pending-upload-#{pending_upload.id}" %>
+  <div id="<%= div_id %>">
+    <%= render :partial => "contributions/new_upload", :locals => { :pending_upload => pending_upload } %>
+  </div>
+<% end %>
+
+<%= image_tag "/images/spinner.gif", :id => "busy_indicator", :style => "margin-left: 1em; display: none;" %>
+
+<% if false %>
+<% form_tag({:action ="" :bulk_create}, :multipart => true) do %>
+	
+	<!-- Other metadata and settings -->
+	
+	<p class="step_text" style="text-align: center;">2. Other metadata and settings</p>
+	
+	<!-- Tags -->
+  <%= render :partial => "tags/tags_form", :locals => { :edit => false, :taggable => @contributable } -%>
+                
+  <!-- Credit and Attribution -->
+  <%= render :partial => "contributions/credit_attribution_form", :locals => { :edit => false, :contributable => @contributable } -%>
+                
+  <!-- Sharing -->              
+  <%= render :partial => "contributions/sharing_form", :locals => { :edit => false, :contributable => @contributable, :update_perms => true } -%>
+                
+  <!-- License/Rights -->
+  <%= render :partial => "workflows/license_form", :locals => { :edit => false } -%>
+  
+	
+	<!-- Terms and conditions -->
+	
+	<p class="step_text">3. Terms and conditions</p>
+	
+  <%= render :partial => 'contributions/terms_and_conditions' %>
+	
+	<br/>
+
+	<!-- Upload and save -->
+	
+	<p class="step_text">4. Upload and save</p>
+
+  <p style="text-align: center;">
+    <%= submit_tag "Upload and Save", :disable_with => "Uploading and saving..." %>
+  </p>
+  
+<% end %>
+<% end %>
+

Modified: branches/bulk/config/routes.rb (2302 => 2303)


--- branches/bulk/config/routes.rb	2009-11-24 17:03:51 UTC (rev 2302)
+++ branches/bulk/config/routes.rb	2009-11-24 17:08:32 UTC (rev 2303)
@@ -38,6 +38,19 @@
   # openid authentication
   map.resource :openid
   
+  # contributions routes for bulk uploads
+  map.resources :contributions,
+    :collection => { :bulk_upload => :get,
+                     :bulk_upload_zip => :post,
+                     :bulk_set_title => :post,
+                     :bulk_set_description => :post,
+                     :bulk_set_category => :post,
+                     :bulk_set_content_type => :post,
+                     :bulk_set_license => :post,
+                     :bulk_set_preview_image => :post,
+                     :bulk_preview_image => :get,
+                     :bulk_create => :post }
+
   # packs
   map.resources :packs, 
     :collection => { :all => :get, :search => :get }, 

Added: branches/bulk/db/migrate/082_create_pending_uploads.rb (0 => 2303)


--- branches/bulk/db/migrate/082_create_pending_uploads.rb	                        (rev 0)
+++ branches/bulk/db/migrate/082_create_pending_uploads.rb	2009-11-24 17:08:32 UTC (rev 2303)
@@ -0,0 +1,51 @@
+# myExperiment: db/migrate/082_create_pending_uploads.rb
+#
+# Copyright (c) 2007 University of Manchester and the University of Southampton.
+# See license.txt for details.
+
+class CreatePendingUploads < ActiveRecord::Migration
+  def self.up
+    create_table :pending_uploads do |t|
+      t.column :user_id,         :integer
+      t.column :created_at,      :datetime
+      t.column :updated_at,      :datetime
+      t.column :content,         :binary, :limit => 1073741824
+      t.column :content_type_id, :integer
+      t.column :category,        :string
+      t.column :title,           :text
+      t.column :description,     :text
+      t.column :tags,            :text
+      t.column :image,           :binary, :limit => 1073741824
+      t.column :svg,             :binary, :limit => 1073741824
+      t.column :license_id,      :integer
+      t.column :file_name,       :string
+      t.column :valid,           :boolean, :default => false
+    end
+      
+    add_column :content_types, :default_category, :string
+
+    workflow_types = Workflow.find(:all).map do |w|
+      w.content_type_id
+    end
+
+    file_types = Blob.find(:all).map do |b|
+      b.content_type_id
+    end
+
+    ContentType.find(:all).each do |ct|
+      if workflow_types.include?(ct.id)
+        ct.update_attribute(:default_category, "Workflow")
+      elsif file_types.include?(ct.id)
+        ct.update_attribute(:default_category, "Blob")
+      else
+        ct.destroy
+      end
+    end
+  end
+
+  def self.down
+    drop_table :pending_uploads
+
+    remove_column :content_types, :default_category
+  end
+end

Added: branches/bulk/public/_javascript_s/bulk_upload.js (0 => 2303)


--- branches/bulk/public/_javascript_s/bulk_upload.js	                        (rev 0)
+++ branches/bulk/public/_javascript_s/bulk_upload.js	2009-11-24 17:08:32 UTC (rev 2303)
@@ -0,0 +1 @@
+

reply via email to

[Prev in Thread] Current Thread [Next in Thread]