Kaynağa Gözat

增加"File:;retainable"方法

jqh 5 yıl önce
ebeveyn
işleme
6a828eb04d
1 değiştirilmiş dosya ile 26 ekleme ve 4 silme
  1. 26 4
      src/Form/Field/UploadField.php

+ 26 - 4
src/Form/Field/UploadField.php

@@ -59,6 +59,13 @@ trait UploadField
      */
     protected $tempFilePath;
 
+    /**
+     * Retain file when delete record from DB.
+     *
+     * @var bool
+     */
+    protected $retainable = false;
+
     /**
      * Initialize the storage instance.
      *
@@ -133,6 +140,18 @@ trait UploadField
         return $this->directory ?: $this->defaultDirectory();
     }
 
+    /**
+     * Indicates if the underlying field is retainable.
+     *
+     * @return $this
+     */
+    public function retainable($retainable = true)
+    {
+        $this->retainable = $retainable;
+
+        return $this;
+    }
+
     /**
      * Upload File.
      *
@@ -213,7 +232,7 @@ trait UploadField
             return $file;
         }
 
-        $tmpDir = $this->getTmpDir($id);
+        $tmpDir = $this->getTempDir($id);
         $newFilename = md5($file->getClientOriginalName());
 
         $file->move($tmpDir, "{$newFilename}.{$chunk}.part");
@@ -229,7 +248,7 @@ trait UploadField
         if (!$done) return;
 
         $this->tempFilePath = $tmpDir.'/'.$newFilename.'.tmp';
-        $this->putTmpFileContent($chunks, $tmpDir, $newFilename);
+        $this->putTempFileContent($chunks, $tmpDir, $newFilename);
 
         return new UploadedFile(
             $this->tempFilePath,
@@ -265,7 +284,7 @@ trait UploadField
      * @param string $tmpDir
      * @param string $newFilename
      */
-    protected function putTmpFileContent($chunks, $tmpDir, $newFileame)
+    protected function putTempFileContent($chunks, $tmpDir, $newFileame)
     {
         $out = fopen($this->tempFilePath, 'wb');
 
@@ -293,7 +312,7 @@ trait UploadField
      * @param $id
      * @return string
      */
-    protected function getTmpDir($id)
+    protected function getTempDir($id)
     {
         $tmpDir = storage_path('tmp/' . $id);
 
@@ -467,6 +486,9 @@ trait UploadField
      */
     public function destroy()
     {
+        if ($this->retainable) {
+            return;
+        }
         $this->deleteFile($this->original);
     }