From 6d0abf6db57106b4f4d4f501683816aca7b4339f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E6=98=B1=E8=A1=8C?= <laiyushing@unisharp.com>
Date: Thu, 8 Jun 2017 10:21:38 +0800
Subject: [PATCH 1/7] add exists and isDirectory function

---
 src/FileApi.php | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/src/FileApi.php b/src/FileApi.php
index f883113..e21f0ab 100644
--- a/src/FileApi.php
+++ b/src/FileApi.php
@@ -175,6 +175,19 @@ public function drop($filename)
         }
     }
 
+    public function exists($filename)
+    {
+        return Storage::exists($this->basepath . $filename);
+    }
+
+    public function isDirectory($name)
+    {
+        return is_array(
+            $this->basepath . DIRECTORY_SEPARATOR . $name,
+            Storage::Directories($this->basepath)
+        );
+    }
+
     /********************************************
      ***********  Private Functions *************
      ********************************************/

From 4a634eef18881a87d2bf02bd78ee944570299758 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E6=98=B1=E8=A1=8C?= <laiyushing@unisharp.com>
Date: Thu, 8 Jun 2017 12:11:53 +0800
Subject: [PATCH 2/7] add extension and move function

---
 src/FileApi.php | 55 +++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 51 insertions(+), 4 deletions(-)

diff --git a/src/FileApi.php b/src/FileApi.php
index e21f0ab..ed7291d 100644
--- a/src/FileApi.php
+++ b/src/FileApi.php
@@ -180,14 +180,39 @@ public function exists($filename)
         return Storage::exists($this->basepath . $filename);
     }
 
-    public function isDirectory($name)
+    public function isDirectory($filename)
     {
-        return is_array(
-            $this->basepath . DIRECTORY_SEPARATOR . $name,
-            Storage::Directories($this->basepath)
+        return in_array(
+            $this->basepath . DIRECTORY_SEPARATOR . $filename,
+            Storage::directories($this->basepath)
         );
     }
 
+    public function extension($filename)
+    {
+        if (!$this->exists($filename)) {
+            return null;
+        }
+
+        return pathinfo($filename)['extension'];
+    }
+
+    public function move($old_name, $new_name)
+    {
+        if ($this->isImage($old_name)) {
+            $old_thumb_name = $this->getPossibleThumbName($old_name);
+            $new_thumb_name = $this->getPossibleThumbName($new_name);
+
+            for ($i = 0; $i < count($old_thumb_name); $i++) {
+                if ($this->exists($old_thumb_name[$i])) {
+                    Storage::move($this->basepath . $old_thumb_name[$i], $this->basepath . $new_thumb_name[$i]);
+                }
+            }
+        }
+
+        Storage::move($this->basepath . $old_name, $this->basepath . $new_name);
+    }
+
     /********************************************
      ***********  Private Functions *************
      ********************************************/
@@ -444,4 +469,26 @@ private function saveCompress($img, $original_name, $suffix)
         // remove tmp image
         Storage::disk('local')->delete($tmp_filename);
     }
+
+    private function isImage($filename)
+    {
+        $image_types = array('image/png', 'image/gif', 'image/jpeg', 'image/jpg');
+        $file_type = Storage::mimeType($this->basepath . $filename);
+
+        return in_array($file_type, $image_types);
+    }
+
+    private function getPossibleThumbName($filename)
+    {
+        $file_filename = pathinfo($filename)['filename'];
+        $file_extension = pathinfo($filename)['extension'];
+        $default_thumbs = config('fileapi.default_thumbs');
+        $thumb_name = [];
+
+        foreach ($default_thumbs as $key => $value) {
+            $thumb_name[] = $file_filename . '_' . $key . '.' . $file_extension;
+        }
+
+        return $thumb_name;
+    }
 }

From c52f6e9066482779d6abec17fa113f285820374c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E6=98=B1=E8=A1=8C?= <laiyushing@unisharp.com>
Date: Thu, 8 Jun 2017 15:24:40 +0800
Subject: [PATCH 3/7] fix isDirectory function and add makeDirectory,
 directoryIsEmpty, deleteDirectory function

---
 src/FileApi.php | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)

diff --git a/src/FileApi.php b/src/FileApi.php
index ed7291d..bb0f638 100644
--- a/src/FileApi.php
+++ b/src/FileApi.php
@@ -183,7 +183,7 @@ public function exists($filename)
     public function isDirectory($filename)
     {
         return in_array(
-            $this->basepath . DIRECTORY_SEPARATOR . $filename,
+            $this->basepath . $filename,
             Storage::directories($this->basepath)
         );
     }
@@ -213,6 +213,27 @@ public function move($old_name, $new_name)
         Storage::move($this->basepath . $old_name, $this->basepath . $new_name);
     }
 
+    public function makeDirectory($folder_name)
+    {
+        Storage::makeDirectory($this->basepath . $folder_name);
+    }
+
+    public function directoryIsEmpty($folder_name)
+    {
+        if (!$this->isDirectory($folder_name)) {
+            return 'This file is not a directory';
+        }
+
+        return count(Storage::files($folder_name)) == 0;
+    }
+
+    public function deleteDirectory($folder_name)
+    {
+        if ($this->isDirectory($folder_name)) {
+            Storage::deleteDirectory($this->basepath . $folder_name);
+        }
+    }
+
     /********************************************
      ***********  Private Functions *************
      ********************************************/

From 6297df89c00ca109dc036b306ecccb92a11acb60 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E6=98=B1=E8=A1=8C?= <laiyushing@unisharp.com>
Date: Thu, 8 Jun 2017 22:59:11 +0800
Subject: [PATCH 4/7] add isFile, files, fileIsImage function and remove '/'
 the forward of basepath

---
 src/FileApi.php | 37 +++++++++++++++++++++++++++++++++++--
 1 file changed, 35 insertions(+), 2 deletions(-)

diff --git a/src/FileApi.php b/src/FileApi.php
index bb0f638..e5d19cf 100644
--- a/src/FileApi.php
+++ b/src/FileApi.php
@@ -39,8 +39,12 @@ public function __construct($basepath = DIRECTORY_SEPARATOR, $visibility = 'publ
 
     public function setBasePath($basepath)
     {
-        if (mb_substr($basepath, -1, 1, 'utf8') != '/') {
-            $basepath .= '/';
+        if (mb_substr($basepath, -1, 1, 'utf8') != DIRECTORY_SEPARATOR) {
+            $basepath .= DIRECTORY_SEPARATOR;
+        }
+
+        if (mb_substr($basepath, 0, 1, 'utf8') == DIRECTORY_SEPARATOR) {
+            $basepath = mb_substr($basepath, 1, null, 'utf8');
         }
 
         $this->basepath = $basepath;
@@ -188,6 +192,14 @@ public function isDirectory($filename)
         );
     }
 
+    public function isFile($filename)
+    {
+        return in_array(
+            $filename,
+            Storage::files($this->basepath)
+        );
+    }
+
     public function extension($filename)
     {
         if (!$this->exists($filename)) {
@@ -234,6 +246,27 @@ public function deleteDirectory($folder_name)
         }
     }
 
+    public function files()
+    {
+        $basepath = $this->basepath;
+
+        return array_map(function ($file) use ($basepath) {
+            return str_replace($basepath, '', $file);
+        }, Storage::files($basepath));
+    }
+
+    public function fileIsImage($filename)
+    {
+        $mime_type = Storage::mimeType($this->basepath . $filename);
+
+        return starts_with($mime_type, 'image');
+    }
+
+    public function getFileType($filename)
+    {
+        return Storage::mimeType($this->basepath . $filename);
+    }
+
     /********************************************
      ***********  Private Functions *************
      ********************************************/

From 2fd3891f191fe27df19b0422936e7ad228cf1c9d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E6=98=B1=E8=A1=8C?= <laiyushing@unisharp.com>
Date: Fri, 9 Jun 2017 00:53:02 +0800
Subject: [PATCH 5/7] add fileHasThumb, lastModified, size function

---
 src/FileApi.php | 32 ++++++++++++++++++++++++++++----
 1 file changed, 28 insertions(+), 4 deletions(-)

diff --git a/src/FileApi.php b/src/FileApi.php
index e5d19cf..3e1f65a 100644
--- a/src/FileApi.php
+++ b/src/FileApi.php
@@ -60,10 +60,6 @@ public function get($filename, $size = self::SIZE_LARGE)
         $file = explode('.', $filename);
         $file_path = '';
 
-        if (empty($filename)) {
-            return '';
-        }
-
         $file_path = $this->basepath . $filename;
 
         if ($size != self::SIZE_ORIGINAL
@@ -267,6 +263,34 @@ public function getFileType($filename)
         return Storage::mimeType($this->basepath . $filename);
     }
 
+    public function fileHasThumb($filename, $size = null)
+    {
+        if (is_null($size)) {
+            $thumb_names = $this->getPossibleThumbName($filename);
+            foreach ($thumb_names as $thumb_name) {
+                if (Storage::exists($this->basepath . $thumb_name)) {
+                    return true;
+                }
+            }
+        }
+
+        $file_filename = pathinfo($filename)['filename'];
+        $file_extension = pathinfo($filename)['extension'];
+        $thumb_name = $file_filename . '_' . $size . '.' . $file_extension;
+
+        return Storage::exists($thumb_name);
+    }
+
+    public function lastModified($filename)
+    {
+        return Storage::lastModified($this->basepath . $filename);
+    }
+
+    public function size($filename)
+    {
+        return Storage::size($this->basepath . $filename);
+    }
+
     /********************************************
      ***********  Private Functions *************
      ********************************************/

From b4c78aa8094645f59fe91ed12dbd8f23c30f6bd6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E6=98=B1=E8=A1=8C?= <laiyushing@unisharp.com>
Date: Fri, 9 Jun 2017 12:16:06 +0800
Subject: [PATCH 6/7] fix isFile and add directories function

---
 src/FileApi.php | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/src/FileApi.php b/src/FileApi.php
index 3e1f65a..f371ff0 100644
--- a/src/FileApi.php
+++ b/src/FileApi.php
@@ -191,7 +191,7 @@ public function isDirectory($filename)
     public function isFile($filename)
     {
         return in_array(
-            $filename,
+            $this->basepath . $filename,
             Storage::files($this->basepath)
         );
     }
@@ -242,6 +242,15 @@ public function deleteDirectory($folder_name)
         }
     }
 
+    public function directories()
+    {
+        $basepath = $this->basepath;
+
+        return array_map(function ($directory) use ($basepath) {
+            return str_replace($basepath, '', $directory);
+        }, Storage::directories($basepath));
+    }
+
     public function files()
     {
         $basepath = $this->basepath;

From 09e912733d220448d24986f7c74ee931051721a4 Mon Sep 17 00:00:00 2001
From: Stream <stream@unisharp.com>
Date: Mon, 13 Nov 2017 18:44:23 +0800
Subject: [PATCH 7/7] allow uploading images without thumbnails

---
 src/FileApi.php | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/FileApi.php b/src/FileApi.php
index f371ff0..7a0433a 100644
--- a/src/FileApi.php
+++ b/src/FileApi.php
@@ -77,9 +77,9 @@ public function get($filename, $size = self::SIZE_LARGE)
         }
     }
 
-    public function thumbs($thumb_sizes = array())
+    public function thumbs($thumb_sizes = null)
     {
-        if (!empty($thumb_sizes)) {
+        if (!is_null($thumb_sizes)) {
             $this->thumb_sizes = $thumb_sizes;
         }
 
