Browse Source

[Uploadable] Small modifications to the listener, so it can work not only with uploaded files. Improved docs.

comfortablynumb 13 years ago
parent
commit
e4c3a48e83

+ 81 - 1
doc/uploadable.md

@@ -17,6 +17,7 @@ Content:
 - [Yaml](#yaml-mapping) mapping example
 - [Xml](#xml-mapping) mapping example
 - Usage [examples](#usage)
+- [Using](#additional-usages) the extension to handle not only uploaded files
 
 <a name="including-extension"></a>
 
@@ -306,4 +307,83 @@ if (isset($_FILES['images']) && is_array($_FILES['images']) {
 $em->flush();
 ```
 
-Easy like that, any suggestions on improvements are very welcome.
+Easy like that, any suggestions on improvements are very welcome.
+
+<a name="additional-usages"></a>
+
+### Using the extension to handle not only uploaded files
+
+Maybe you want to handle files obtained from an URL, or even files that are already located in the same server than your app.
+This can be handled in a very simple way. First, you need to create a class that implements the FileInfoInterface
+interface. As an example:
+
+``` php
+use Gedmo\Uploadable\FileInfo\FileInfoInterface;
+
+class CustomFileInfo implements FileInfoInterface
+{
+    protected $path;
+    protected $size;
+    protected $type;
+    protected $filename;
+    protected $error = 0;
+
+    public function __construct($path)
+    {
+        $this->path = $path;
+
+        // Now, process the file and fill the rest of the properties.
+    }
+
+    // This returns the actual path of the file
+    public function getTmpName()
+    {
+        return $path;
+    }
+
+    // This returns the filename
+    public function getName()
+    {
+        return $this->name;
+    }
+
+    // This returns the file size in bytes
+    public function getSize()
+    {
+        return $this->size;
+    }
+
+    // This returns the mime type
+    public function getType()
+    {
+        return $this->type;
+    }
+
+    public function getError()
+    {
+        // This should return 0, as it's only used to return the codes from PHP file upload errors.
+        return $this->error;
+    }
+
+    // This method, if returns true, will produce that the extension uses "move_uploaded_file" function to move
+    // the file. If it returns false, the extension will move the file with the "copy" function.
+    public function isUploadedFile()
+    {
+        return false;
+    }
+}
+```
+
+And then, instead of getting the file info from the $_FILES array, you would do:
+
+``` php
+// We set the default path in the listener again
+$listener->setDefaultPath('/my/path');
+
+$file = new File();
+
+$listener->addEntityFileInfo($file, new CustomFileInfo('/path/to/file.txt'));
+
+$em->persist($file);
+$em->flush();
+```

+ 5 - 0
lib/Gedmo/Uploadable/FileInfo/FileInfoArray.php

@@ -57,4 +57,9 @@ class FileInfoArray implements FileInfoInterface
     {
         return $this->fileInfo['error'];
     }
+
+    public function isUploadedFile()
+    {
+        return true;
+    }
 }

+ 7 - 0
lib/Gedmo/Uploadable/FileInfo/FileInfoInterface.php

@@ -20,4 +20,11 @@ interface FileInfoInterface
     public function getSize();
     public function getType();
     public function getError();
+
+    /**
+     * This method must return true if the file is coming from $_FILES, or false instead.
+     *
+     * @return bool
+     */
+    public function isUploadedFile();
 }

+ 7 - 4
lib/Gedmo/Uploadable/UploadableListener.php

@@ -396,7 +396,7 @@ class UploadableListener extends MappedEventSubscriber
             }
         }
 
-        if (!$this->moveUploadedFile($fileInfo->getTmpName(), $info['filePath'])) {
+        if (!$this->doMoveFile($fileInfo->getTmpName(), $info['filePath'], $fileInfo->isUploadedFile())) {
             throw new UploadableUploadException(sprintf('File "%s" was not uploaded, or there was a problem moving it to the location "%s".',
                 $fileInfo->getName(),
                 $path
@@ -407,16 +407,19 @@ class UploadableListener extends MappedEventSubscriber
     }
 
     /**
-     * Simple wrapper to "move_uploaded_file" function to ease testing
+     * Simple wrapper method used to move the file. If it's an uploaded file
+     * it will use the "move_uploaded_file method. If it's not, it will
+     * simple move it
      *
      * @param string - Source file
      * @param string - Destination file
+     * @param bool - Is an uploaded file?
      *
      * @return bool
      */
-    public function moveUploadedFile($source, $dest)
+    public function doMoveFile($source, $dest, $isUploadedFile = true)
     {
-        return move_uploaded_file($source, $dest);
+        return $isUploadedFile ? move_uploaded_file($source, $dest) : copy($source, $dest);
     }
 
     /**

+ 2 - 2
tests/Gedmo/Uploadable/Stub/UploadableListenerStub.php

@@ -9,8 +9,8 @@ class UploadableListenerStub extends UploadableListener
 {
     public $returnFalseOnMoveUploadedFile = false;
 
-    public function moveUploadedFile($source, $dest)
+    public function doMoveFile($source, $dest, $isUploadedFile = true)
     {
-        return $this->returnFalseOnMoveUploadedFile ? false : copy($source, $dest);
+        return $this->returnFalseOnMoveUploadedFile ? false : parent::doMoveFile($source, $dest, false);
     }
 }