commit f43b67479ed0ba007897fa2e1572022d6c2e8ad8
parent 424caee7b23462ed764b5b2367301312212ec2e0
Author: Jan Dankert <develop@jandankert.de>
Date: Thu, 7 Nov 2019 21:56:12 +0100
New: Uploading files wit a pure value (for API usage).
Diffstat:
3 files changed, 162 insertions(+), 119 deletions(-)
diff --git a/modules/cms-core/action/FileAction.class.php b/modules/cms-core/action/FileAction.class.php
@@ -65,32 +65,52 @@ class FileAction extends ObjectAction
/**
- * Ersetzt den Inhalt mit einer anderen Datei
+ * Ersetzt den Inhalt der Datei.
*/
public function editPost()
{
$upload = new Upload();
- try
+ if ( $upload->isAvailable() )
{
- $upload->processUpload();
+ // File received as attachement.
+ try
+ {
+ $upload->processUpload();
+ }
+ catch( \Exception $e )
+ {
+ throw $e;
+ }
+
+ $this->file->filename = $upload->filename;
+ $this->file->extension = $upload->extension;
+ $this->file->size = $upload->size;
+ $this->file->save();
+
+ $this->file->value = $upload->value;
+ $this->file->saveValue();
}
- catch( \Exception $e )
+ elseif( $this->hasRequestVar('value') )
{
- throw $e;
- }
+ // File value received
+ $this->file->value = $this->getRequestVar('value');
- $this->file->filename = $upload->filename;
- $this->file->extension = $upload->extension;
- $this->file->size = $upload->size;
+ if ( strtolower($this->getRequestVar('encoding')) == 'base64')
+ // file value is base64-encoded
+ $this->file->value = base64_decode($this->file->value);
- $this->file->save();
-
- $this->file->value = $upload->value;
- $this->file->saveValue();
- $this->file->setTimestamp();
+ $this->file->saveValue();
+ }
+ else
+ {
+ // No file received.
+ throw new \ValidationException('value');
+ }
+
+ $this->file->setTimestamp();
- $this->addNotice($this->file->getType(),$this->file->name,'VALUE_SAVED','ok');
+ $this->addNotice($this->file->getType(),$this->file->filename,'VALUE_SAVED','ok');
}
diff --git a/modules/cms-core/action/FolderAction.class.php b/modules/cms-core/action/FolderAction.class.php
@@ -122,9 +122,20 @@ class FolderAction extends ObjectAction
$file->size = strlen($http->body);
$file->value = $http->body;
$file->parentid = $this->folder->objectid;
+ $file->projectid = $this->folder->projectid;
}
+ elseif ( $this->hasRequestVar('value') )
+ {
+ // New file is inserted.
+ $file->filename = BaseObject::urlify( $name );
+ $file->value = $this->getRequestVar('value');
+ $file->size = strlen($file->value);
+ $file->parentid = $this->folder->objectid;
+ $file->projectid = $this->folder->projectid;
+ }
else
{
+ // File was uploaded.
$upload = new Upload('file');
try
@@ -153,7 +164,7 @@ class FolderAction extends ObjectAction
$file->add(); // Datei hinzufuegen
$file->setNameForAllLanguages( $name,$description );
- $this->addNotice('file',$file->name,'ADDED','ok');
+ $this->addNotice('file',$file->filename,'ADDED','ok');
$this->setTemplateVar('objectid',$file->objectid);
$this->folder->setTimestamp();
diff --git a/modules/util/Upload.class.php b/modules/util/Upload.class.php
@@ -1,104 +1,116 @@
-<?php
-// OpenRat Content Management System
-// Copyright (C) 2002-2012 Jan Dankert, cms@jandankert.de
-//
-// This program is free software; you can redistribute it and/or
-// modify it under the terms of the GNU General Public License
-// as published by the Free Software Foundation; either version 2
-// of the License, or (at your option) any later version.
-//
-// This program is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-// GNU General Public License for more details.
-//
-// You should have received a copy of the GNU General Public License
-// along with this program; if not, write to the Free Software
-// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-
-/**
- * Methoden fuer den Upload einer Datei
- *
- * @author $Author$
- * @version $Revision$
- * @package openrat.services
- */
-class Upload
-{
- public $filename;
- public $extension;
- public $value;
- public $size;
- public $mimeType;
-
- public $parameterName;
-
- public static $DEFAULT_PARAM_NAME = 'file';
-
-
- /**
- * Bearbeitet den Upload einer Datei.<br>
- *
- * @return Upload
- */
- function __construct( $name='file' ) // Konstruktor
- {
- $this->parameterName = $name;
- }
-
- /**
- * Provision of an uploaded file.
- */
- public function processUpload()
- {
- $name = $this->parameterName;
-
- if ( !isset($_FILES[$name]) || !is_array($_FILES[$name]) )
- throw new InvalidArgumentException('No file received under the key "'.$name.'"' );
-
- $uFile = $_FILES[$name];
-
- if ( !isset($uFile['tmp_name']) )
- throw new InvalidArgumentException('No temporary filename found for uploaded file key "'.$name.'"' );
-
- if ( !is_file($uFile['tmp_name']) )
- throw new InvalidArgumentException('Not a file: '.$uFile['tmp_name'] );
-
- switch( $uFile['error'] )
- {
- case UPLOAD_ERR_OK:
- break;
-
- case UPLOAD_ERR_INI_SIZE:
- throw new InvalidArgumentException('Uploaded file is bigger than allowed in server configuration');
-
- case UPLOAD_ERR_FORM_SIZE:
- throw new InvalidArgumentException('Uploaded file is bigger than allowed in form');
-
- default:
- throw new InvalidArgumentException('Error code while uploading file: '.$uFile['error'] );
- }
-
- $this->mimeType = $uFile['type'];
-
- $this->size = filesize($uFile['tmp_name']);
-
- $fh = fopen( $uFile['tmp_name'],'r' );
-
- $this->value = fread($fh,$this->size);
- fclose( $fh );
-
- $this->filename = $uFile['name'];
- $this->extension = '';
-
- $p = strrpos( $this->filename,'.' ); // Letzten Punkt suchen
-
- if ($p!==false) // Wennn letzten Punkt gefunden, dann dort aufteilen
- {
- $this->extension = substr( $this->filename,$p+1 );
- $this->filename = substr( $this->filename,0,$p );
- }
- }
-}
-
+<?php
+// OpenRat Content Management System
+// Copyright (C) 2002-2012 Jan Dankert, cms@jandankert.de
+//
+// This program is free software; you can redistribute it and/or
+// modify it under the terms of the GNU General Public License
+// as published by the Free Software Foundation; either version 2
+// of the License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+
+/**
+ * Methoden fuer den Upload einer Datei
+ *
+ * @author $Author$
+ * @version $Revision$
+ * @package openrat.services
+ */
+class Upload
+{
+ public $filename;
+ public $extension;
+ public $value;
+ public $size;
+ public $mimeType;
+
+ public $parameterName;
+
+ public static $DEFAULT_PARAM_NAME = 'file';
+
+
+ /**
+ * Bearbeitet den Upload einer Datei.<br>
+ *
+ * @return Upload
+ */
+ function __construct( $name='file' ) // Konstruktor
+ {
+ $this->parameterName = $name;
+ }
+
+ /**
+ * Provision of an uploaded file.
+ */
+ public function processUpload()
+ {
+ $name = $this->parameterName;
+
+ if ( ! $this->isAvailable() )
+ throw new InvalidArgumentException('No file received under the key "'.$name.'"' );
+
+ $uFile = $_FILES[$name];
+
+ if ( !isset($uFile['tmp_name']) )
+ throw new InvalidArgumentException('No temporary filename found for uploaded file key "'.$name.'"' );
+
+ if ( !is_file($uFile['tmp_name']) )
+ throw new InvalidArgumentException('Not a file: '.$uFile['tmp_name'] );
+
+ switch( $uFile['error'] )
+ {
+ case UPLOAD_ERR_OK:
+ break;
+
+ case UPLOAD_ERR_INI_SIZE:
+ throw new InvalidArgumentException('Uploaded file is bigger than allowed in server configuration');
+
+ case UPLOAD_ERR_FORM_SIZE:
+ throw new InvalidArgumentException('Uploaded file is bigger than allowed in form');
+
+ default:
+ throw new InvalidArgumentException('Error code while uploading file: '.$uFile['error'] );
+ }
+
+ $this->mimeType = $uFile['type'];
+
+ $this->size = filesize($uFile['tmp_name']);
+
+ $fh = fopen( $uFile['tmp_name'],'r' );
+
+ $this->value = fread($fh,$this->size);
+ fclose( $fh );
+
+ $this->filename = $uFile['name'];
+ $this->extension = '';
+
+ $p = strrpos( $this->filename,'.' ); // Letzten Punkt suchen
+
+ if ($p!==false) // Wennn letzten Punkt gefunden, dann dort aufteilen
+ {
+ $this->extension = substr( $this->filename,$p+1 );
+ $this->filename = substr( $this->filename,0,$p );
+ }
+ }
+
+ /**
+ * Is this upload available?
+ * @param $name Request variable name
+ * @return bool <code>true</code> if upload is available
+ */
+ public function isAvailable()
+ {
+ $name = $this->parameterName;
+
+ return isset($_FILES[$name]) && is_array($_FILES[$name]);
+ }
+}
+
?>
\ No newline at end of file