changeset 316:61f7f258a6fb

Move from-gzip paste parsing to model.
author edogawaconan <me@myconan.net>
date Sat, 05 Apr 2014 23:23:25 +0900
parents 325fcf388442
children b33a7e73e592
files app/controllers/pastes_controller.rb app/models/paste.rb
diffstat 2 files changed, 9 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/app/controllers/pastes_controller.rb	Sat Apr 05 23:20:32 2014 +0900
+++ b/app/controllers/pastes_controller.rb	Sat Apr 05 23:23:25 2014 +0900
@@ -29,16 +29,6 @@
   # POST /pastes.json
   # POST /pastes.txt
   def create
-    if params[:paste].is_a? Hash
-      if params[:paste][:paste_gzip_base64]
-        # 1. decode from base64
-        # 2. create StringIO from decoded string
-        # 3. unzip and read the stream
-        params[:paste][:paste] = Zlib::GzipReader.new(StringIO.new(Base64.decode64(params[:paste].delete(:paste_gzip_base64)))).read
-      elsif params[:paste][:paste_gzip]
-        params[:paste][:paste] = Zlib::GzipReader.new(StringIO.new(params[:paste].delete(:paste_gzip))).read
-      end
-    end
     @paste = Paste.new(paste_params.merge(ip: request.remote_ip))
 
     begin
@@ -77,6 +67,6 @@
 
   private
   def paste_params
-    params.require(:paste).permit(:paste, :is_private, :key)
+    params.require(:paste).permit(:paste, :paste_gzip, :paste_gzip_base64, :is_private, :key)
   end
 end
--- a/app/models/paste.rb	Sat Apr 05 23:20:32 2014 +0900
+++ b/app/models/paste.rb	Sat Apr 05 23:23:25 2014 +0900
@@ -22,6 +22,14 @@
     end
   end
 
+  def paste_gzip=(paste)
+    self.paste = ActiveSupport::Gzip.decompress paste
+  end
+
+  def paste_gzip_base64=(paste)
+    self.paste_gzip = Base64.decode64(paste)
+  end
+
   def safe_destroy(param_key)
     if key == param_key
       destroy