PageRenderTime 124ms CodeModel.GetById 50ms app.highlight 11ms RepoModel.GetById 60ms app.codeStats 0ms

/package/app/app/alpha/apps/kaltura/lib/myUploadUtils.class.php

https://bitbucket.org/pandaos/kaltura
PHP | 198 lines | 143 code | 31 blank | 24 comment | 21 complexity | ab7bd9dfb549ea69805a098da61efdd4 MD5 | raw file
  1<?php
  2class myUploadUtils
  3{
  4	public static function uploadFile ( $file_data , $id , $filename , $hash , $extra_id = null )
  5	{
  6		$realHash = myContentStorage::getTempUploadHash($filename, $id);
  7		
  8		// TODO - what if there is  an error while uploading ?
  9
 10		// filename is OK?
 11		if($realHash == $hash && $hash != "")
 12		{
 13			//create the directory if doesn't exists (should have write permissons)
 14			// if(!is_dir("./files")) mkdir("./files", 0755);
 15			//move the uploaded file
 16
 17			$origFilename = $file_data['name'];
 18			$parts = pathinfo($origFilename);
 19			$extension = strtolower($parts['extension']);
 20
 21			$filename = $id.'_'.$filename;
 22			// add the file extension after the "." character
 23			$fullPath = myContentStorage::getFSUploadsPath().$filename . ( $extra_id ? "_" . $extra_id : "" ) .".".$extension;
 24
 25			myContentStorage::fullMkdir($fullPath);
 26			if ( ! move_uploaded_file($file_data['tmp_name'], $fullPath) )
 27			{
 28				KalturaLog::log ( "Error while uploading [$id] [$filename] [$hash] [$extra_id] " . print_r ( $file_data ,true ) ."\n->[$fullPath]" );
 29				return false;
 30			}
 31			@chmod ( $fullPath , 0777 );
 32
 33			return true;
 34		}
 35		
 36		return false;
 37	}
 38
 39	public static function uploadFileByToken ( $file_data , $token , $filename , $extra_id = null , $create_thumb = false )
 40	{
 41		KalturaLog::log( "Trace while uploading1 [$filename] [$token] [$extra_id] " . print_r ( $file_data ,true ) );
 42		
 43		$origFilename = @$file_data['name'];
 44		if ( ! $origFilename )
 45		{
 46			KalturaLog::log ( "Error while uploading, file does not have a name. [$filename] [$token] [$extra_id] " . print_r ( $file_data ,true ) . 
 47				"\nerror: [" . @$file_data["error"] . "]" );			
 48			return;
 49		}
 50		$parts = pathinfo($origFilename);
 51		$extension = @strtolower($parts['extension']);
 52/*
 53		$filename = $token .'_'. $filename;
 54		// add the file extension after the "." character
 55		$fullPath = myContentStorage::getFSUploadsPath().$filename . ( $extra_id ? "_" . $extra_id : "" ) .".".$extension;
 56*/
 57		list ( $fullPath , $fullUrl )  = self::getUploadPathAndUrl ( $token , $filename , $extra_id , $extension );
 58		
 59		KalturaLog::log ( "Trace while uploading2 [$filename] [$token] [$extra_id] " . print_r ( $file_data ,true ) ."\n->[$fullPath]" );
 60
 61		// start tracking what will hopefully become an entry 
 62		$te = new TrackEntry();
 63		$te->setTrackEventTypeId( TrackEntry::TRACK_ENTRY_EVENT_TYPE_UPLOADED_FILE );
 64		$te->setParam1Str( $token );
 65		$te->setParam2Str( $filename );
 66		$te->setParam3Str( $fullPath );
 67		$te->setDescription(  __METHOD__ . ":" . __LINE__ );
 68		TrackEntry::addTrackEntry( $te );
 69		
 70		myContentStorage::fullMkdir($fullPath);
 71		if ( ! move_uploaded_file($file_data['tmp_name'], $fullPath) )
 72		{
 73			$err =  array ( 	"token" => $token , 
 74						"filename" => $filename , 
 75						"origFilename" => $origFilename ,
 76						"error" => @$file_data["error"] , );
 77
 78			KalturaLog::log ( "Error while uploading [$token] [$filename] [$extra_id] [$create_thumb] " . print_r ( $file_data ,true ) ."\n->[$fullPath]" . "\n" 
 79				. print_r ( $err , true ) );			
 80			return $err;
 81		}
 82		chmod ( $fullPath , 0777 );
 83		
 84		$upload_server_header = isset($_SERVER["HTTP_X_KALTURA_SERVER"]) ? $_SERVER["HTTP_X_KALTURA_SERVER"] : null;
 85		
 86		$thumb_created = false;
 87		
 88		// if the file originated from a kaltura upload server we dont need a thumbnail (kuploader)
 89		if ( $create_thumb && !$upload_server_header)
 90		{
 91			$thumbFullPath = self::getThumbnailPath ( $fullPath , ".jpg" );
 92			kFile::fullMkdir( $thumbFullPath );
 93			KalturaLog::log("Thumbnail full path [$thumbFullPath]");
 94			
 95			if(myContentStorage::fileExtAccepted ( $extension ))
 96			{
 97				KalturaLog::log("Generating image thumbnail");
 98				myFileConverter::createImageThumbnail($fullPath, $thumbFullPath, "image2" );
 99				$thumb_url = self::getThumbnailPath ( $fullUrl , ".jpg" ); 
100				$thumb_created = file_exists( $thumbFullPath );
101			}
102			elseif(myContentStorage::fileExtNeedConversion ( $extension ))
103			{
104				KalturaLog::log("Generating media thumbnail");
105				myFileConverter::captureFrame($fullPath, $thumbFullPath, 1, "image2", -1, -1, 3 );
106				if (!file_exists($thumbFullPath))
107					myFileConverter::captureFrame($fullPath, $thumbFullPath, 1, "image2", -1, -1, 0 );
108			}
109		}
110		
111		if(!$thumb_created)
112		{
113			KalturaLog::log("Thumbnail not generated");
114			// in this case no thumbnail was created - don't extract false data 
115			$thumb_url = ""; 
116		}
117		
118		return array ( 	"token" => $token , 
119						"filename" => $filename , 
120						"origFilename" => $origFilename ,
121						"thumb_url" => $thumb_url , 
122						"thumb_created" => $thumb_created ,
123//						"extra_data" => @$file_data, 
124						);
125	}
126	
127	
128	private static function getThumbnailPath ( $path , $new_extension = '' )
129	{
130		$fixed = str_replace ( "uploads/" , "uploads/thumbnail/thumb_" , $path ) ;
131		return kFile::getFileNameNoExtension( $fixed , true ). $new_extension;
132	}
133	
134	public static function uploadJpeg ( $data, $id , $filename , $hash , $extra_id = null , $return_extended_data=false)
135	{
136		//$realHash = myContentStorage::getTempUploadHash($filename, $id);
137
138		$origFilename = $filename; 
139		// filename is OK?
140		if( true /*$realHash == $hash && $hash != ""*/)
141		{
142			$filename = $id.'_'.$filename;
143			// add the file extension after the "." character
144			$fullPath = myContentStorage::getFSUploadsPath().$filename . ( $extra_id ? "_" . $extra_id : "" ) .".jpg";
145
146			kFile::setFileContent($fullPath, $data);
147			chmod ( $fullPath , 0777 );
148
149			if ( $return_extended_data )
150			{
151				return array ( 	"token" => $id , 
152					"filename" => $filename , 
153					"origFilename" => $origFilename ,
154					"thumb_url" => null , 
155					"thumb_created" => false);
156			}
157			return true;
158		}
159		
160		return false;
161	}
162	
163	// return the file path WITHOUT the extension
164	// if the extension is known - pass it as the 4rt parameter
165	public static function getUploadPath ($token , $file_alias , $extra_id = null , $extension = "" )
166	{
167//		$extension = ""; // strtolower($parts['extension']);
168		if (strpos($token, "token-") === 0) // starts with "token_", means that the file was uploaded with upload.getUploadTokenId
169		{
170			$files = glob(myContentStorage::getFSUploadsPath().$token."*");
171			if (count($files) > 0)
172			{
173				$token = strtolower(pathinfo($files[0], PATHINFO_BASENAME));
174				$extension = strtolower(pathinfo($token, PATHINFO_EXTENSION));
175				$token = str_replace("_.".$extension, "", $token);
176			}
177		}
178		$filename = $token .'_'. $file_alias;
179		// add the file extension after the "." character
180		$fullPath = myContentStorage::getFSUploadsPath().$filename . ( $extra_id ? "_" . $extra_id : "" ) .".".$extension;
181
182		return $fullPath;
183	}
184	
185	public static function getUploadPathAndUrl ($token , $file_alias , $extra_id = null , $extension = "" )
186	{
187//		$extension = ""; // strtolower($parts['extension']);
188		
189		$filename = $token .'_'. $file_alias;
190		// add the file extension after the "." character
191		$suffix = $filename . ( $extra_id ? "_" . $extra_id : "" ) .".".$extension;
192		$fullPath = myContentStorage::getFSUploadsPath().$suffix;
193		$fullUrl = requestUtils::getRequestHost()."/". myContentStorage::getFSUploadsPath( false ).$suffix;
194		return array ( $fullPath , $fullUrl );
195	}
196	
197}
198?>