OSDN Git Service

t#29318:create auto_publish
authoryasushiito <yas@pen-chan.jp>
Wed, 20 Mar 2013 04:57:42 +0000 (13:57 +0900)
committeryasushiito <yas@pen-chan.jp>
Wed, 20 Mar 2013 04:57:42 +0000 (13:57 +0900)
app/models/original_picture.rb
script/uploader.rb [new file with mode: 0644]

index 17eed3a..2415578 100644 (file)
@@ -203,4 +203,56 @@ class OriginalPicture < ActiveRecord::Base
     res
   end
   
+  def self.publish oid, lsname, attr
+    l = License.find_by_name lsname
+    op = OriginalPicture.find oid
+    lg = l.license_group
+    attr[:license_id] = l.id
+    
+    ctl = lg.classname.pluralize + '::Attribute'
+    le = ctl.constantize.new attr
+    
+    rp = ResourcePicture.new
+    rp.attributes = le.resource_picture_attributes op
+    rp.overwrite op
+    
+    imager = PettanImager.load op.restore
+    rp.store imager
+  end
+  
+  def self.upload fn, auth
+    b = Base64.encode64(File.open(fn, 'rb').read)
+    u = 'http://localhost:3000/original_pictures'
+    r = RestClient.post(u, 
+      {:original_picture => {:file  => b}, :auth_token => auth}.to_json, 
+      :content_type => :json, :accept => :json
+    )
+    o = JSON.parse r
+    oid = o['id']
+    oid
+  end
+  
+  def self.auto_publish dirname, auth
+    Dir.glob File.expand_path(dirname) + '/*' do |filename|
+      if File.directory?(filename)
+        img = nil
+        lsname = nil
+        attr  = nil
+        Dir.glob(filename + '/*') do |fn|
+          ext = File.extname(fn).downcase
+          case ext
+          when '.json'
+            json = JSON.parse(File.open(fn).read)
+            lsname = json["license_name"]
+            attr = json["attributes"]
+          when '.png', '.gif', '.jpeg'
+            img = fn
+          end
+        end
+        oid = OriginalPicture.upload img, auth
+        OriginalPicture.publish oid, lsname, attr
+      end
+    end
+  end
+  
 end
diff --git a/script/uploader.rb b/script/uploader.rb
new file mode 100644 (file)
index 0000000..fb66a5a
--- /dev/null
@@ -0,0 +1,11 @@
+#サポートツール\r
+require 'rest_client'\r
+require 'yaml'\r
+require 'json'\r
+require 'base64'\r
+\r
+if ARGV.size < 1\r
+  puts "uploader.rb dir"\r
+  puts "upload on child dirs"\r
+  exit\r
+end\r