OSDN Git Service

add catch-exception and fixed ruby-1.9.1
authorelixirel <elixirel@users.sourceforge.jp>
Mon, 18 May 2009 11:12:59 +0000 (20:12 +0900)
committerelixirel <elixirel@users.sourceforge.jp>
Mon, 18 May 2009 11:12:59 +0000 (20:12 +0900)
filemanager.rb

index 3fd42b3..6624001 100644 (file)
@@ -18,7 +18,7 @@ require "fileutils"
 # インターフェースのテーブルの幅
 TABLEWIDTH = 800
 # 画像フォルダの場所を定義
-IMGPATH = "./../lunardial/xml/img"
+IMGPATH = "./../lunardial/xml/img/"
 
 # バージョン情報を示す文字列です
 APPVERSION = "- FileManager for Ruby version 2.0.0.0 -<br>Copyright(c) 2009 Kureha.H (<a href=\"http://lunardial.sakura.ne.jp/\" target=\"_blank\">http://lunardial.sakura.ne.jp/</a>) & Yui Naruse (<a href=\"http://airemix.com/\" target=\"_blank\">http://airemix.com/</a>)"
@@ -49,6 +49,15 @@ class Object
   end
 end
 
+# = Dirクラス
+#
+# Dirクラスのオーバーライドを行います
+class Dir
+  def Dir.exist?(path)
+    File.exist?(path) && File.ftype(path) == "directory"
+  end
+end
+
 class NilClass
   def blank?
     nil?
@@ -76,7 +85,7 @@ class HtmlWriter
   # _template_ :: テンプレートファイル(*.erb)のパス
   # _binding_ :: binding変数
   def initialize(template, binding)
-    @erb = ERB.new(myopen(template, "r") {|f| f.read}, nil, "-")
+    @erb = ERB.new(myopen(template, "r:utf-8") {|f| f.read}, nil, "-")
     @binding = binding
   end
   
@@ -95,10 +104,10 @@ class WebFiler
   # _basepath_ :: クラス内で扱う最上位のフォルダ(root)とする実パス
   def initialize(basepath)
     @basepath = basepath
-    @basepath << "/" unless @basepath[-1] == "/"
+    @basepath << "/" unless @basepath[-1..-1] == "/"
     @relpath_list = []
     
-    raise "Target dir not found." unless check_dir_exist?(pwd)
+    raise "Target dir not found." unless Dir.exist?(pwd)
   end
   
   attr_reader :basepath
@@ -142,7 +151,7 @@ class WebFiler
     if pathname == ".."
       @relpath_list.delete_at(-1) unless @relpath_list.length == 0
     elsif pathname.match(/([^\w]|\.\.|\/|\.)/) == nil
-      if check_dir_exist?(pwd + "/" + File.basename(pathname))
+      if Dir.exist?(pwd + "/" + File.basename(pathname))
         @relpath_list << File.basename(pathname)
       else
         raise "Target dir not found."
@@ -208,15 +217,6 @@ class WebFiler
     end
   end
   
-  # 対象ディレクトリが存在するかを確認するメソッド
-  def check_dir_exist?(target_dir)
-    unless (File.exist?(target_dir) || File.ftype(target_dir) != "directory")
-      false
-    else
-      true
-    end
-  end
-  
 end
 
 # = Controllerクラス
@@ -232,7 +232,11 @@ class Controller
         filer.relpath_list = db["relpath_list"]
         
         if (cgi["updata"].size <= UPLOADLIMIT)
-          filer.upload(cgi["updata"], cgi["updata"].original_filename.gsub(/( | )/, "_"))
+          begin
+            filer.upload(cgi["updata"], cgi["updata"].original_filename.gsub(/( | )/, "_"))
+          rescue => ever
+            db["error"] = "既に同名のファイルが存在します!"
+          end
         else
           db["error"] = "ファイルの容量が大きすぎます!"
         end
@@ -272,7 +276,11 @@ class Controller
         filer = WebFiler.new(IMGPATH)
         filer.relpath_list = db["relpath_list"]
         
-        filer.mkdir(params["dirname"])
+        begin
+          filer.mkdir(params["dirname"])
+        rescue => ever
+          db["error"] = "既に同名のディレクトリが存在します!"
+        end
         
         db["filelist"] = filer.ls.reverse
         db["fileinfo"] = filer.lsinfo
@@ -284,7 +292,11 @@ class Controller
         filer = WebFiler.new(IMGPATH)
         filer.relpath_list = db["relpath_list"]
         
-        filer.cd(params["arg"])
+        begin
+          filer.cd(params["arg"])
+        rescue => ever
+          db["error"] = "移動先のディレクトリが見つかりません!"
+        end
         
         db["filelist"] = filer.ls.reverse
         db["fileinfo"] = filer.lsinfo
@@ -347,7 +359,7 @@ def main
   rescue => exception
     # エラーが発生した場合、それを画面に表示します
     htmlwriter = HtmlWriter.new("./erbtemp/exception.html.erb", binding)
-    cgi.out{htmlwriter.to_code}
+    cgi.out{ htmlwriter.to_code }
   end
 end