[pLog-svn] r908 - plog/trunk/class/gallery/dao

mark at devel.plogworld.net mark at devel.plogworld.net
Tue Feb 1 09:21:59 GMT 2005


Author: mark
Date: 2005-02-01 09:21:58 +0000 (Tue, 01 Feb 2005)
New Revision: 908

Modified:
   plog/trunk/class/gallery/dao/galleryresourcestorage.class.php
Log:
Fix chinese file name upload error in windows server.
http://bugs.plogworld.net/view.php?id=188

Modified: plog/trunk/class/gallery/dao/galleryresourcestorage.class.php
===================================================================
--- plog/trunk/class/gallery/dao/galleryresourcestorage.class.php	2005-02-01 09:21:32 UTC (rev 907)
+++ plog/trunk/class/gallery/dao/galleryresourcestorage.class.php	2005-02-01 09:21:58 UTC (rev 908)
@@ -1,384 +1,407 @@
-<?php
-
-	include_once( PLOG_CLASS_PATH."class/object/object.class.php" );
-    include_once( PLOG_CLASS_PATH."class/file/fileupload.class.php" );
-    include_once( PLOG_CLASS_PATH."class/file/file.class.php" );
-    include_once( PLOG_CLASS_PATH."class/file/fileuploads.class.php" );
-    include_once( PLOG_CLASS_PATH."class/config/config.class.php" );
-    include_once( PLOG_CLASS_PATH."class/gallery/galleryconstants.php" );
-
-	define( "RESOURCE_STORAGE_STORE_COPY", 1 );
-	define( "RESOURCE_STORAGE_STORE_MOVE", 2 );
-
-    /**
-     * Takes care of dealing with resource files on disk
-     */
-    class GalleryResourceStorage extends Object
-    {
-
-    	function GalleryResourceStorage()
-        {
-        	$this->Object();
-        }
-		
-		/**
-		 * Returns the path to the folder where resources are stored. This method can 
-		 * be used as static.
-		 *
-		 * @static
-		 * @return Returns a string containing the base path to the resource storage folder.
-		 */
-		function getResourcesStorageFolder()
-		{
-			$config =& Config::getConfig();
-			$resourcesStorageFolder = $config->getValue( "resources_folder" );
-			
-			// just in case...
-			if( $resourcesStorageFolder == "" )
-				$resourcesStorageFolder = DEFAULT_RESOURCES_STORAGE_FOLDER;
-				
-			// append a forward slash to the folder if we forgot about it...
-			if( $resourcesStorageFolder[strlen($resourcesStorageFolder)-1] != '/')
-				$resourcesStorageFolder .= "/";
-				
-			
-			return $resourcesStorageFolder;
-		}
-		
-        /**
-         * @private
-         */
-        function _checkBaseStorageFolder()
-        {
-			$baseFolder = GalleryResourceStorage::getResourcesStorageFolder();
-			if( $baseFolder[strlen($baseFolder)-1] == "/") {
-        	   $baseFolder = substr($baseFolder,0,strlen($baseFolder)-1);
-        	   $this->log->debug("base folder = $baseFolder");
-            }			
-						
-			$this->log->debug( "Checking if base gallery folder exists = $baseFolder" );
-        	if( !File::isDir( $baseFolder )) {
-            	// folder does not exist, so we should try to create it
-                if( !File::createDir( $baseFolder, 0755 )) {
-					$this->log->debug( "There was an error creating the folder!" );
-                	throw( new Exception( "Could not create storage folder for resources: ".$baseFolder));
-					return false;
-                    //die();
-                }
-				
-				$this->log->debug( "Folder created ok!" );
-            }
-
-            if( !File::isReadable( $baseFolder )) {
-				$this->log->debug( "Folder exists but it is not readable!" );
-            	throw( new Exception( $baseFolder." storage folder exists but it is not readable!" ));
-				return false;
-                //die();
-            }
-			
-			$this->log->debug( "Base folder exists and it is readable." );
-
-            return true;
-        }
-		
-		/**
-		 * a nicer function that the one above. And it is also meant to be used
-		 * by external classes
-		 *
-		 * @static
-		 * @return Returns true if the base storage folder has been created and
-		 * it is readable.
-		 */
-        function checkBaseStorageFolder()
-        {
-			return GalleryResourceStorage::_checkBaseStorageFolder();
-        }		
-				
-        /**
-         * @private
-         */
-        function getUserFolder( $ownerId )
-        {
-        	return GalleryResourceStorage::getResourcesStorageFolder().$ownerId."/";
-        }
-
-        /**
-         * @public
-         */
-        function getPreviewsFolder( $ownerId )
-        {
-        	return GalleryResourceStorage::getResourcesStorageFolder().$ownerId."/previews/";
-        }
-		
-        /**
-         * @public
-         */
-        function getMediumSizePreviewsFolder( $ownerId )
-        {
-        	return GalleryResourceStorage::getResourcesStorageFolder().$ownerId."/previews-med/";
-        }		
-
-        /**
-         * @private
-         */
-        function _checkUserStorageFolder( $ownerId )
-        {
-			$baseFolder = GalleryResourceStorage::getResourcesStorageFolder();
-			if( $baseFolder[strlen($baseFolder)-1] == "/") {
-        	   $baseFolder = substr($baseFolder,0,strlen($baseFolder)-1);
-        	   $this->log->debug("base folder = $baseFolder");
-            }			
-
-        	$userFolder = GalleryResourceStorage::getUserFolder( $ownerId );
-        	if( $userFolder[strlen($userFolder)-1] == "/") {
-        	   $userFolder = substr($userFolder,0,strlen($userFolder)-1);
-            }
-			
-			$this->log->debug("user folder = $userFolder");			
-        	if( !File::isDir( $userFolder )) {
-            	// folder does not exist, so we should try to create it
-                if( !File::createDir( $userFolder, 0755 )) {
-					$this->log->debug("could not create folder!");
-                	throw( new Exception( "Could not create user storage folder for resources: ".$userFolder ));
-					return false;
-                    //die();
-                }
-            }
-
-            if( !File::isReadable( $userFolder )) {
-				$this->log->debug( "User storage folder exists but it is not readable!" );
-            	//throw( new Exception( $userFolder." user storage folder exists but it is not readable!" ));
-				return false;
-                //die();
-            }
-			
-            return true;
-        }
-		
-		function checkUserStorageFolder( $ownerId )
-		{
-			return GalleryResourceStorage::_checkUserStorageFolder( $ownerId );
-		}
-
-        /**
-         * @public
-         */
-        function checkPreviewsStorageFolder( $ownerId )
-        {
-            $previewsFolder = GalleryResourceStorage::getPreviewsFolder( $ownerId );
-        	if( $previewsFolder[strlen($previewsFolder)-1] == "/") {
-        	   $previewsFolder = substr($previewsFolder,0,strlen($previewsFolder)-1);
-				$this->log->debug("previews folder = $previewsFolder");			   
-            }
-
-			$this->log->debug( "Checking if previews folder exists = $previewsFolder" );
-        	if( !File::isDir( $previewsFolder )) {
-            	// folder does not exist, so we should try to create it
-                if( !File::createDir( $previewsFolder, 0755 )) {
-					$this->log->debug("Error creating $previewsFolder!");
-                	throw( new Exception( "Could not create user storage folder for previews: ".$previewsFolder ));
-                    //die();
-					return false;
-                }
-				
-				$this->log->debug( "Previews folder created ok!" );
-            }
-
-            if( !File::isReadable( $previewsFolder )) {
-            	throw( new Exception( $previewsFolder." user previews storage folder exists but it is not readable!" ));
-                //die();
-				return false;
-            }
-
-			$this->log->debug( "Previews folder checked ok!" );
-            return true;
-        }
-		
-        /**
-         * @public
-         */
-        function checkMediumSizePreviewsStorageFolder( $ownerId )
-        {
-            $previewsFolder = GalleryResourceStorage::getMediumSizePreviewsFolder( $ownerId );
-        	if( $previewsFolder[strlen($previewsFolder)-1] == "/") {
-        	   $previewsFolder = substr($previewsFolder,0,strlen($previewsFolder)-1);
-				$this->log->debug("medium size previews folder = $previewsFolder");			   
-            }
-
-			$this->log->debug( "Checking if medium size previews folder exists = $previewsFolder" );
-        	if( !File::isDir( $previewsFolder )) {
-            	// folder does not exist, so we should try to create it
-                if( !File::createDir( $previewsFolder, 0755 )) {
-					$this->log->debug("Error creating medium size folder $previewsFolder!");
-                	throw( new Exception( "Could not create user storage folder for medium size previews: ".$previewsFolder ));
-                    //die();
-					return false;
-                }
-				
-				$this->log->debug( "Previews folder created ok!" );
-            }
-
-            if( !File::isReadable( $previewsFolder )) {
-            	throw( new Exception( $previewsFolder." user previews storage folder exists but it is not readable!" ));
-                //die();
-				return false;
-            }
-
-			$this->log->debug( "Previews folder checked ok!" );
-            return true;
-        }		
-
-        /**
-         * stores a new resource in disk
-         *
-         * @param ownerId The id of the owner of this file
-         * @param albumId The album id to which the
-         * @param upload a FileUpload object with information about the
-         * uploaded file
-         */
-        function storeUpload( $resourceId, $ownerId, $upload )
-        {
-        	// check that the folders exist
-            if( !$this->_checkBaseStorageFolder())
-				return false;
-            if( !$this->_checkUserStorageFolder( $ownerId ))
-				return false;
-
-            // new name for the file
-            $fileParts = explode( ".", $upload->getFileName());
-            $fileExt = $fileParts[count($fileParts)-1];
-            $fileName = "$ownerId-$resourceId.$fileExt";
-            $filePath = $this->getUserFolder( $ownerId );
-			
-			$this->log->debug( "storeUpload: fileExt = $fileExt - fileName = $fileName - filePath = $filePath" );
-
-            // move the file to the temporaray folder first
-            $config =& Config::getConfig();
-            $tmpFolder = $config->getValue( "temp_folder" );
-            /*$files = HttpVars::getFiles();*/
-			// we don't need the parameter in the constructor though it is necessary 
-			// according to the signature of the method
-            $uploads = new FileUploads( null );
-            $result = $uploads->processFile( $upload, $tmpFolder );
-
-            if( $result < 0 ) {
-            	return $result;
-            }
-
-            // rename it while it's there
-            $this->log->debug("moving ".$tmpFolder."/".$upload->getFileName()." to $destFile");
-			$origFile = $tmpFolder."/".$upload->getFileName();
-			$destFile = $this->storeFile( $resourceId, $ownerId, $origFile, RESOURCE_STORAGE_STORE_MOVE );
-
-            return $destFile;
-        }
-
-	/**
-	 * the method above works only with files that have been uploaded while
-	 * this one works with files that are anywhere. It will take care of copying
-	 * the file to the right destination folder and so on
-	 *
-         * @param ownerId The id of the owner of this file
-         * @param albumId The album id to which the
-         * @param fileName full path and name to the file that we're trying to store
-	 */
-	function storeFile( $resourceId, $ownerId, $fileName, $mode = RESOURCE_STORAGE_STORE_COPY )
-	{
-        	// check that the folders exist
-            if( !$this->_checkBaseStorageFolder())
-				return false;
-            if( !$this->_checkUserStorageFolder( $ownerId ))
-				return false;
-
-            // new name for the file
-            $fileParts = explode( ".", $fileName);
-            $fileExt = $fileParts[count($fileParts)-1];
-            $destFile = "$ownerId-$resourceId.$fileExt";
-            $destPath = $this->getUserFolder( $ownerId );
-
-			$this->log->debug( "storeFile: fileExt = $fileExt - fileName = $fileName - destFile = $destFile - destPath = $destPath" );
-
-			// first of all, check if the file is readable and if not, quit	
-			if( !File::isReadable($fileName)) {
-				$this->log->debug( "storeFile: file $fileName is not readable!" ); 
-				return false;
-			}
-
-			$destFile = $destPath.$destFile;
-            $this->log->debug(" copying $fileName to $destFile" );
-			if( $mode == RESOURCE_STORAGE_STORE_COPY )
-            		$res = File::copy( $fileName, $destFile );
-			else 
-				$res = File::rename( $fileName, $destFile );
-            
-			if( !$res ) {
-				$this->log->debug( "There was an error copying the file!" );
-            	return false;
-			}			
-			// check that the permissions are correct
-			$this->log->debug( "chmodding file to 0777" );
-			File::chMod( $destFile, 0755 );
-
-            return $destFile;
-		}
-
-        /**
-         * removes a file from disk
-         *
-         * @param resource A GalleryResource object, representing the resource
-         * we'd like to delete.
-         * @return Returns ok if file was successfully deleted ok or false otherwise.
-         */
-        function remove( $resource )
-        {
-        	if( $resource ) {
-				// first of all, remove the resource file itself
-        		$fileParts = explode( ".", $resource->getFileName());
-                $fileExt = $fileParts[count($fileParts)-1];
-                $fileName = $resource->getOwnerId()."-".$resource->getId().".".$fileExt;
-                $filePath = $this->getUserFolder( $resource->getOwnerId());
-				$fullName = $filePath.$fileName;
-
-                $this->log->debug( "removing resource ".$fullName );
-				if( File::isReadable( $fullName)) {
-					$result = File::delete( $fullName );
-				}
-				
-				// and now if preview images are available, remove them too!
-				if( $resource->hasPreview()) {
-					// delete the small thumbnail
-					$this->log->debug( "Removing preview!" );
-					$previewFile = $resource->getPreviewFileName();
-					if( File::isReadable( $previewFile ))
-						File::delete( $previewFile );
-						
-					// and the medium-sized thumbnail
-					$this->log->debug( "Removing medium-size preview!");
-					$medPreviewFile = $resource->getMediumSizePreviewFileName();
-					if( File::isReadable( $medPreviewFile ))
-						File::delete( $medPreviewFile );
-				}
-            }
-            else
-            	$result = false;
-
-            return $result;
-        }
-		
-		/**
-		 * returns the path of a GalleryResource object within the storage area
-		 *
-		 * @param $resource A GalleryResource object
-		 * @return A string containing the path to the file relative to the storage area.
-		 */
-		function getResourcePath( $resource )
-		{
-			$fileParts = explode( ".", $resource->getFileName());
-			$fileExt = $fileParts[count($fileParts)-1];		
-			$filePath = $this->getUserFolder( $resource->getOwnerId());
-			$fileName = $resource->getOwnerId()."-".$resource->getId().".".$fileExt;
-			
-			return $filePath.$fileName;
-		}
-    }
-?>
+<?php
+
+	include_once( PLOG_CLASS_PATH."class/object/object.class.php" );
+    include_once( PLOG_CLASS_PATH."class/file/fileupload.class.php" );
+    include_once( PLOG_CLASS_PATH."class/file/file.class.php" );
+    include_once( PLOG_CLASS_PATH."class/file/fileuploads.class.php" );
+    include_once( PLOG_CLASS_PATH."class/config/config.class.php" );
+    include_once( PLOG_CLASS_PATH."class/gallery/galleryconstants.php" );
+
+	define( "RESOURCE_STORAGE_STORE_COPY", 1 );
+	define( "RESOURCE_STORAGE_STORE_MOVE", 2 );
+
+    /**
+     * Takes care of dealing with resource files on disk
+     */
+    class GalleryResourceStorage extends Object
+    {
+
+    	function GalleryResourceStorage()
+        {
+        	$this->Object();
+        }
+		
+		/**
+		 * Returns the path to the folder where resources are stored. This method can 
+		 * be used as static.
+		 *
+		 * @static
+		 * @return Returns a string containing the base path to the resource storage folder.
+		 */
+		function getResourcesStorageFolder()
+		{
+			$config =& Config::getConfig();
+			$resourcesStorageFolder = $config->getValue( "resources_folder" );
+			
+			// just in case...
+			if( $resourcesStorageFolder == "" )
+				$resourcesStorageFolder = DEFAULT_RESOURCES_STORAGE_FOLDER;
+				
+			// append a forward slash to the folder if we forgot about it...
+			if( $resourcesStorageFolder[strlen($resourcesStorageFolder)-1] != '/')
+				$resourcesStorageFolder .= "/";
+				
+			
+			return $resourcesStorageFolder;
+		}
+		
+        /**
+         * @private
+         */
+        function _checkBaseStorageFolder()
+        {
+			$baseFolder = GalleryResourceStorage::getResourcesStorageFolder();
+			if( $baseFolder[strlen($baseFolder)-1] == "/") {
+        	   $baseFolder = substr($baseFolder,0,strlen($baseFolder)-1);
+        	   $this->log->debug("base folder = $baseFolder");
+            }			
+						
+			$this->log->debug( "Checking if base gallery folder exists = $baseFolder" );
+        	if( !File::isDir( $baseFolder )) {
+            	// folder does not exist, so we should try to create it
+                if( !File::createDir( $baseFolder, 0755 )) {
+					$this->log->debug( "There was an error creating the folder!" );
+                	throw( new Exception( "Could not create storage folder for resources: ".$baseFolder));
+					return false;
+                    //die();
+                }
+				
+				$this->log->debug( "Folder created ok!" );
+            }
+
+            if( !File::isReadable( $baseFolder )) {
+				$this->log->debug( "Folder exists but it is not readable!" );
+            	throw( new Exception( $baseFolder." storage folder exists but it is not readable!" ));
+				return false;
+                //die();
+            }
+			
+			$this->log->debug( "Base folder exists and it is readable." );
+
+            return true;
+        }
+		
+		/**
+		 * a nicer function that the one above. And it is also meant to be used
+		 * by external classes
+		 *
+		 * @static
+		 * @return Returns true if the base storage folder has been created and
+		 * it is readable.
+		 */
+        function checkBaseStorageFolder()
+        {
+			return GalleryResourceStorage::_checkBaseStorageFolder();
+        }		
+				
+        /**
+         * @private
+         */
+        function getUserFolder( $ownerId )
+        {
+        	return GalleryResourceStorage::getResourcesStorageFolder().$ownerId."/";
+        }
+
+        /**
+         * @public
+         */
+        function getPreviewsFolder( $ownerId )
+        {
+        	return GalleryResourceStorage::getResourcesStorageFolder().$ownerId."/previews/";
+        }
+		
+        /**
+         * @public
+         */
+        function getMediumSizePreviewsFolder( $ownerId )
+        {
+        	return GalleryResourceStorage::getResourcesStorageFolder().$ownerId."/previews-med/";
+        }		
+
+        /**
+         * @private
+         */
+        function _checkUserStorageFolder( $ownerId )
+        {
+			$baseFolder = GalleryResourceStorage::getResourcesStorageFolder();
+			if( $baseFolder[strlen($baseFolder)-1] == "/") {
+        	   $baseFolder = substr($baseFolder,0,strlen($baseFolder)-1);
+        	   $this->log->debug("base folder = $baseFolder");
+            }			
+
+        	$userFolder = GalleryResourceStorage::getUserFolder( $ownerId );
+        	if( $userFolder[strlen($userFolder)-1] == "/") {
+        	   $userFolder = substr($userFolder,0,strlen($userFolder)-1);
+            }
+			
+			$this->log->debug("user folder = $userFolder");			
+        	if( !File::isDir( $userFolder )) {
+            	// folder does not exist, so we should try to create it
+                if( !File::createDir( $userFolder, 0755 )) {
+					$this->log->debug("could not create folder!");
+                	throw( new Exception( "Could not create user storage folder for resources: ".$userFolder ));
+					return false;
+                    //die();
+                }
+            }
+
+            if( !File::isReadable( $userFolder )) {
+				$this->log->debug( "User storage folder exists but it is not readable!" );
+            	//throw( new Exception( $userFolder." user storage folder exists but it is not readable!" ));
+				return false;
+                //die();
+            }
+			
+            return true;
+        }
+		
+		function checkUserStorageFolder( $ownerId )
+		{
+			return GalleryResourceStorage::_checkUserStorageFolder( $ownerId );
+		}
+
+        /**
+         * @public
+         */
+        function checkPreviewsStorageFolder( $ownerId )
+        {
+            $previewsFolder = GalleryResourceStorage::getPreviewsFolder( $ownerId );
+        	if( $previewsFolder[strlen($previewsFolder)-1] == "/") {
+        	   $previewsFolder = substr($previewsFolder,0,strlen($previewsFolder)-1);
+				$this->log->debug("previews folder = $previewsFolder");			   
+            }
+
+			$this->log->debug( "Checking if previews folder exists = $previewsFolder" );
+        	if( !File::isDir( $previewsFolder )) {
+            	// folder does not exist, so we should try to create it
+                if( !File::createDir( $previewsFolder, 0755 )) {
+					$this->log->debug("Error creating $previewsFolder!");
+                	throw( new Exception( "Could not create user storage folder for previews: ".$previewsFolder ));
+                    //die();
+					return false;
+                }
+				
+				$this->log->debug( "Previews folder created ok!" );
+            }
+
+            if( !File::isReadable( $previewsFolder )) {
+            	throw( new Exception( $previewsFolder." user previews storage folder exists but it is not readable!" ));
+                //die();
+				return false;
+            }
+
+			$this->log->debug( "Previews folder checked ok!" );
+            return true;
+        }
+		
+        /**
+         * @public
+         */
+        function checkMediumSizePreviewsStorageFolder( $ownerId )
+        {
+            $previewsFolder = GalleryResourceStorage::getMediumSizePreviewsFolder( $ownerId );
+        	if( $previewsFolder[strlen($previewsFolder)-1] == "/") {
+        	   $previewsFolder = substr($previewsFolder,0,strlen($previewsFolder)-1);
+				$this->log->debug("medium size previews folder = $previewsFolder");			   
+            }
+
+			$this->log->debug( "Checking if medium size previews folder exists = $previewsFolder" );
+        	if( !File::isDir( $previewsFolder )) {
+            	// folder does not exist, so we should try to create it
+                if( !File::createDir( $previewsFolder, 0755 )) {
+					$this->log->debug("Error creating medium size folder $previewsFolder!");
+                	throw( new Exception( "Could not create user storage folder for medium size previews: ".$previewsFolder ));
+                    //die();
+					return false;
+                }
+				
+				$this->log->debug( "Previews folder created ok!" );
+            }
+
+            if( !File::isReadable( $previewsFolder )) {
+            	throw( new Exception( $previewsFolder." user previews storage folder exists but it is not readable!" ));
+                //die();
+				return false;
+            }
+
+			$this->log->debug( "Previews folder checked ok!" );
+            return true;
+        }		
+
+        /**
+         * stores a new resource in disk
+         *
+         * @param ownerId The id of the owner of this file
+         * @param albumId The album id to which the
+         * @param upload a FileUpload object with information about the
+         * uploaded file
+         */
+        function storeUpload( $resourceId, $ownerId, $upload )
+        {
+        	// check that the folders exist
+            if( !$this->_checkBaseStorageFolder())
+				return false;
+            if( !$this->_checkUserStorageFolder( $ownerId ))
+				return false;
+
+            // new name for the file
+            $fileParts = explode( ".", $upload->getFileName());
+            $fileExt = $fileParts[count($fileParts)-1];
+            $fileName = "$ownerId-$resourceId.$fileExt";
+            $filePath = $this->getUserFolder( $ownerId );
+			
+			$this->log->debug( "storeUpload: fileExt = $fileExt - fileName = $fileName - filePath = $filePath" );
+
+            // move the file to the temporaray folder first
+            $config =& Config::getConfig();
+            $tmpFolder = $config->getValue( "temp_folder" );
+            /*$files = HttpVars::getFiles();*/
+			// we don't need the parameter in the constructor though it is necessary 
+			// according to the signature of the method
+            $uploads = new FileUploads( null );
+            $result = $uploads->processFile( $upload, $tmpFolder );
+
+            if( $result < 0 ) {
+            	return $result;
+            }
+
+            // rename it while it's there
+            $this->log->debug("moving ".$tmpFolder."/".$upload->getTmpName()." to $destFile");
+			$origFile = $tmpFolder."/".basename($upload->getTmpName());
+			//do not use storeFile method because I have change filename in $tmpFolder.
+			//$destFile = $this->storeFile( $resourceId, $ownerId, $origFile, RESOURCE_STORAGE_STORE_MOVE );
+			//$destFile use $filePath and $fileName generated above.
+			$destFile = $filePath.$fileName;
+//=========================================			
+//codes below are copy and modify from method storeFile
+			// first of all, check if the file is readable and if not, quit	
+			if( !File::isReadable($origFile)) {
+				$this->log->debug( "storeFile: file $origFile is not readable!" ); 
+				return false;
+			}
+
+            $this->log->debug(" copying $origFile to $destFile" );
+			$res = File::rename( $origFile, $destFile );
+            
+			if( !$res ) {
+				$this->log->debug( "There was an error copying the file!" );
+            	return false;
+			}			
+			// check that the permissions are correct
+			$this->log->debug( "chmodding file to 0777" );
+			File::chMod( $destFile, 0755 );
+
+//=========================================			
+
+            return $destFile;
+        }
+
+	/**
+	 * the method above works only with files that have been uploaded while
+	 * this one works with files that are anywhere. It will take care of copying
+	 * the file to the right destination folder and so on
+	 *
+         * @param ownerId The id of the owner of this file
+         * @param albumId The album id to which the
+         * @param fileName full path and name to the file that we're trying to store
+	 */
+	function storeFile( $resourceId, $ownerId, $fileName, $mode = RESOURCE_STORAGE_STORE_COPY )
+	{
+        	// check that the folders exist
+            if( !$this->_checkBaseStorageFolder())
+				return false;
+            if( !$this->_checkUserStorageFolder( $ownerId ))
+				return false;
+
+            // new name for the file
+            $fileParts = explode( ".", $fileName);
+            $fileExt = $fileParts[count($fileParts)-1];
+            $destFile = "$ownerId-$resourceId.$fileExt";
+            $destPath = $this->getUserFolder( $ownerId );
+
+			$this->log->debug( "storeFile: fileExt = $fileExt - fileName = $fileName - destFile = $destFile - destPath = $destPath" );
+
+			// first of all, check if the file is readable and if not, quit	
+			if( !File::isReadable($fileName)) {
+				$this->log->debug( "storeFile: file $fileName is not readable!" ); 
+				return false;
+			}
+
+			$destFile = $destPath.$destFile;
+            $this->log->debug(" copying $fileName to $destFile" );
+			if( $mode == RESOURCE_STORAGE_STORE_COPY )
+            		$res = File::copy( $fileName, $destFile );
+			else 
+				$res = File::rename( $fileName, $destFile );
+            
+			if( !$res ) {
+				$this->log->debug( "There was an error copying the file!" );
+            	return false;
+			}			
+			// check that the permissions are correct
+			$this->log->debug( "chmodding file to 0777" );
+			File::chMod( $destFile, 0755 );
+
+            return $destFile;
+		}
+
+        /**
+         * removes a file from disk
+         *
+         * @param resource A GalleryResource object, representing the resource
+         * we'd like to delete.
+         * @return Returns ok if file was successfully deleted ok or false otherwise.
+         */
+        function remove( $resource )
+        {
+        	if( $resource ) {
+				// first of all, remove the resource file itself
+        		$fileParts = explode( ".", $resource->getFileName());
+                $fileExt = $fileParts[count($fileParts)-1];
+                $fileName = $resource->getOwnerId()."-".$resource->getId().".".$fileExt;
+                $filePath = $this->getUserFolder( $resource->getOwnerId());
+				$fullName = $filePath.$fileName;
+
+                $this->log->debug( "removing resource ".$fullName );
+				if( File::isReadable( $fullName)) {
+					$result = File::delete( $fullName );
+				}
+				
+				// and now if preview images are available, remove them too!
+				if( $resource->hasPreview()) {
+					// delete the small thumbnail
+					$this->log->debug( "Removing preview!" );
+					$previewFile = $resource->getPreviewFileName();
+					if( File::isReadable( $previewFile ))
+						File::delete( $previewFile );
+						
+					// and the medium-sized thumbnail
+					$this->log->debug( "Removing medium-size preview!");
+					$medPreviewFile = $resource->getMediumSizePreviewFileName();
+					if( File::isReadable( $medPreviewFile ))
+						File::delete( $medPreviewFile );
+				}
+            }
+            else
+            	$result = false;
+
+            return $result;
+        }
+		
+		/**
+		 * returns the path of a GalleryResource object within the storage area
+		 *
+		 * @param $resource A GalleryResource object
+		 * @return A string containing the path to the file relative to the storage area.
+		 */
+		function getResourcePath( $resource )
+		{
+			$fileParts = explode( ".", $resource->getFileName());
+			$fileExt = $fileParts[count($fileParts)-1];		
+			$filePath = $this->getUserFolder( $resource->getOwnerId());
+			$fileName = $resource->getOwnerId()."-".$resource->getId().".".$fileExt;
+			
+			return $filePath.$fileName;
+		}
+    }
+?>




More information about the pLog-svn mailing list