myexperiment-hackers
[Top][All Lists]
Advanced

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

[myexperiment-hackers] [2975] trunk: added a uniqueness constraint on mi


From: noreply
Subject: [myexperiment-hackers] [2975] trunk: added a uniqueness constraint on mime types with an exception list
Date: Sat, 24 Mar 2012 15:52:02 +0000 (UTC)

Revision
2975
Author
dgc
Date
2012-03-24 15:52:02 +0000 (Sat, 24 Mar 2012)

Log Message

added a uniqueness constraint on mime types with an exception list

Modified Paths

Diff

Modified: trunk/app/controllers/workflows_controller.rb (2974 => 2975)


--- trunk/app/controllers/workflows_controller.rb	2012-03-20 16:55:21 UTC (rev 2974)
+++ trunk/app/controllers/workflows_controller.rb	2012-03-24 15:52:02 UTC (rev 2975)
@@ -318,11 +318,11 @@
       
     # Custom metadata provided.
     elsif params[:metadata_choice] == 'custom'
-      worked = set_custom_metadata(@workflow, file)
+      worked, error_message = set_custom_metadata(@workflow, file)
       
       unless worked
         respond_to do |format|
-          flash.now[:error] = "The file provided isn't a workflow of the type specified. Please select a different file or set an appropriate content type."
+          flash.now[:error] = error_message
           format.html { render :action ="" "new" }
         end
         return
@@ -429,7 +429,7 @@
       
     # Custom metadata provided.
     elsif params[:metadata_choice] == 'custom'
-      worked = set_custom_metadata(@workflow, file)
+      worked, error_message = set_custom_metadata(@workflow, file)
       
       unless worked
         respond_to do |format|
@@ -970,7 +970,6 @@
   
   # Method used in the create and create_version methods.
   def set_custom_metadata(workflow_to_set, file)
-    worked = true
     
     workflow_to_set.title = params[:workflow][:title]
     workflow_to_set.body = params[:new_workflow][:body]
@@ -993,6 +992,17 @@
             :category => 'Workflow')
         end
 
+        if !ct.valid?
+
+          other_ct = ContentType.find_by_mime_type(file.content_type)
+
+          if other_ct
+            return [false, "Unable to create new type because the MIME type \"#{file.content_type}\" is already used by the \"#{other_ct.title}\" type."]
+          end
+
+          return [false, "Unable to create new type."]
+        end
+
         workflow_to_set.content_type = ct
       else
         workflow_to_set.content_type = ContentType.find_by_title(wf_type)
@@ -1001,7 +1011,9 @@
     
     # Check that the file uploaded is valid for the content type chosen (if supported by a workflow processor).
     # This is to ensure that the correct content type is being assigned to the workflow file uploaded.
-    return false unless workflow_file_matches_content_type_if_supported?(file, workflow_to_set)
+    if !workflow_file_matches_content_type_if_supported?(file, workflow_to_set)
+      return [false, "The file provided isn't a workflow of the type specified. Please select a different file or set an appropriate content type."]
+    end
     
     # Preview image
     # TODO: kept getting permission denied errors from the file_column and rmagick code, so disable for windows, for now.
@@ -1024,7 +1036,7 @@
     # Set the internal unique name for this particular workflow (or workflow_version).
     workflow_to_set.set_unique_name
     
-    return worked
+    return [true, nil]
   end
   
   # This method checks to to see if the file specified is a valid one for the existing workflow specified,

Modified: trunk/app/models/content_type.rb (2974 => 2975)


--- trunk/app/models/content_type.rb	2012-03-20 16:55:21 UTC (rev 2974)
+++ trunk/app/models/content_type.rb	2012-03-24 15:52:02 UTC (rev 2975)
@@ -11,6 +11,10 @@
   validates_presence_of :title
   validates_uniqueness_of :title
 
+  validates_uniqueness_of :mime_type, :unless => Proc.new { |ct|
+    Conf.duplicable_mime_types.include?(ct.mime_type)
+  }
+
   def label
     title
   end

Modified: trunk/config/default_settings.yml (2974 => 2975)


--- trunk/config/default_settings.yml	2012-03-20 16:55:21 UTC (rev 2974)
+++ trunk/config/default_settings.yml	2012-03-24 15:52:02 UTC (rev 2975)
@@ -453,6 +453,22 @@
 
 label_icons:
 
+# duplicable_mime_types - This is the list of mime types that can appear
+#                         multiple times in the content types.  These are for
+#                         situations where the mime type is generic and is not
+#                         specific enough to identify a particular content
+#                         type.
+
+duplicable_mime_types:
+
+  - application/x-zip-compressed
+  - application/zip
+  - application/x-gzip
+  - application/xml
+  - text/xml
+  - text/plain
+  - application/octet-stream
+
 # rdfgen_enable
 
 rdfgen_enable: false

Modified: trunk/lib/conf.rb (2974 => 2975)


--- trunk/lib/conf.rb	2012-03-20 16:55:21 UTC (rev 2974)
+++ trunk/lib/conf.rb	2012-03-24 15:52:02 UTC (rev 2975)
@@ -177,6 +177,10 @@
     self.fetch_entry('cookie_verifier_secret')
   end
 
+  def self.duplicable_mime_types
+    self.fetch_entry('duplicable_mime_types')
+  end
+
   def self.layouts
     layouts = self.fetch_entry('layouts', {})
     layouts.delete_if {|k,v| v["environment"] && (v["environment"] != ENV["RAILS_ENV"])}

reply via email to

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