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