private function moveFile(KalturaBatchJob $job, $fromPath, $toPath)
 {
     KalturaLog::debug("moveFile from[{$fromPath}] to[{$toPath}]");
     // move file/dir to the new location
     $res = @rename($fromPath, $toPath);
     // chmod + chown + check file seen by apache - for each moved file/directory
     if ($res) {
         if (is_dir($toPath)) {
             $contents = kFile::listDir($toPath);
             sort($contents, SORT_STRING);
             foreach ($contents as $current) {
                 $res = $res && $this->setAndCheck($toPath . '/' . $current);
             }
         } else {
             $res = $this->setAndCheck($toPath);
         }
     }
     if ($res) {
         $job->status = KalturaBatchJobStatus::FINISHED;
         $job->message = "File moved to final destination";
     } else {
         $job->status = KalturaBatchJobStatus::FAILED;
         $job->message = "File not moved correctly";
     }
     return $this->closeJob($job, null, null, $job->message, $job->status, null, $job->data);
 }
 /**
  * Returns directory $path contents as an array of :
  *  array[0] = name
  *  array[1] = type (dir/file)
  *  array[2] = filesize
  * @param string $path
  * @param string $pathPrefix
  */
 public static function listDir($path, $pathPrefix = '')
 {
     $fileList = array();
     $path = str_ireplace(DIRECTORY_SEPARATOR, '/', $path);
     $handle = opendir($path);
     if ($handle) {
         while (false !== ($file = readdir($handle))) {
             if ($file != '.' && $file != '..') {
                 $fullPath = $path . '/' . $file;
                 $tmpPrefix = $pathPrefix . $file;
                 if (is_dir($fullPath)) {
                     $tmpPrefix = $tmpPrefix . '/';
                     $fileList[] = array($tmpPrefix, 'dir', kFile::fileSize($fullPath));
                     $fileList = array_merge($fileList, kFile::listDir($fullPath, $tmpPrefix));
                 } else {
                     $fileList[] = array($tmpPrefix, 'file', kFile::fileSize($fullPath));
                 }
             }
         }
         closedir($handle);
     }
     return $fileList;
 }
예제 #3
0
 public static function serveFileToRemoteDataCenter($file_sync, $file_hash, $file_name)
 {
     $file_sync_id = $file_sync->getId();
     KalturaLog::log("File sync id [{$file_sync_id}], file_hash [{$file_hash}], file_name [{$file_name}]");
     // TODO - verify security
     $current_dc = self::getCurrentDc();
     $current_dc_id = $current_dc["id"];
     if ($file_sync->getDc() != $current_dc_id) {
         $error = "DC[{$current_dc_id}]: FileSync with id [{$file_sync_id}] does not belong to this DC";
         KalturaLog::err($error);
         KExternalErrors::dieError(KExternalErrors::BAD_QUERY);
     }
     // resolve if file_sync is link
     $file_sync_resolved = $file_sync;
     $file_sync_resolved = kFileSyncUtils::resolve($file_sync);
     // check if file sync path leads to a file or a directory
     $resolvedPath = $file_sync_resolved->getFullPath();
     $fileSyncIsDir = is_dir($resolvedPath);
     if ($fileSyncIsDir && $file_name) {
         $resolvedPath .= '/' . $file_name;
     }
     if (!file_exists($resolvedPath)) {
         $file_name_msg = $file_name ? "file name [{$file_name}] " : '';
         $error = "DC[{$current_dc_id}]: Path for fileSync id [{$file_sync_id}] " . $file_name_msg . "does not exist, resolved path [{$resolvedPath}]";
         KalturaLog::err($error);
         KExternalErrors::dieError(KExternalErrors::FILE_NOT_FOUND);
     }
     // validate the hash
     $expected_file_hash = md5($current_dc["secret"] . $file_sync_id);
     // will be verified on the other side to make sure not some attack or external invalid request
     if ($file_hash != $expected_file_hash) {
         $error = "DC[{$current_dc_id}]: FileSync with id [{$file_sync_id}] - invalid hash";
         KalturaLog::err($error);
         KExternalErrors::dieError(KExternalErrors::INVALID_TOKEN);
     }
     if ($fileSyncIsDir && is_dir($resolvedPath)) {
         KalturaLog::log("Serving directory content from [" . $resolvedPath . "]");
         $contents = kFile::listDir($resolvedPath);
         sort($contents, SORT_STRING);
         $contents = serialize($contents);
         header("file-sync-type: dir");
         echo $contents;
         KExternalErrors::dieGracefully();
     } else {
         KalturaLog::log("Serving file from [" . $resolvedPath . "]");
         kFileUtils::dumpFile($resolvedPath);
     }
 }
 public static function serveFileToRemoteDataCenter($file_sync_id, $file_hash, $file_name)
 {
     KalturaLog::log("File sync id [{$file_sync_id}], file_hash [{$file_hash}], file_name [{$file_name}]");
     // TODO - verify security
     $current_dc = self::getCurrentDc();
     $current_dc_id = $current_dc["id"];
     // retrieve the object
     $file_sync = FileSyncPeer::retrieveByPk($file_sync_id);
     if (!$file_sync) {
         $error = "DC[{$current_dc_id}]: Cannot find FileSync with id [{$file_sync_id}]";
         KalturaLog::err($error);
         throw new Exception($error);
     }
     if ($file_sync->getDc() != $current_dc_id) {
         $error = "DC[{$current_dc_id}]: FileSync with id [{$file_sync_id}] does not belong to this DC";
         KalturaLog::err($error);
         throw new Exception($error);
     }
     // resolve if file_sync is link
     $file_sync_resolved = $file_sync;
     if ($file_sync->getFileType() == FileSync::FILE_SYNC_FILE_TYPE_LINK) {
         $file_sync_resolved = kFileSyncUtils::resolve($file_sync);
     }
     // check if file sync path leads to a file or a directory
     $resolvedPath = $file_sync_resolved->getFullPath();
     $fileSyncIsDir = is_dir($resolvedPath);
     if ($fileSyncIsDir && $file_name) {
         $resolvedPath .= '/' . $file_name;
     }
     if (!file_exists($resolvedPath)) {
         $file_name_msg = $file_name ? "file name [{$file_name}] " : '';
         $error = "DC[{$current_dc_id}]: Path for fileSync id [{$file_sync_id}] " . $file_name_msg . "does not exist";
         KalturaLog::err($error);
         throw new Exception($error);
     }
     // validate the hash
     $expected_file_hash = md5($current_dc["secret"] . $file_sync_id);
     // will be verified on the other side to make sure not some attack or external invalid request
     if ($file_hash != $expected_file_hash) {
         $error = "DC[{$current_dc_id}]: FileSync with id [{$file_sync_id}] - invalid hash";
         KalturaLog::err($error);
         throw new Exception($error);
     }
     if ($fileSyncIsDir && is_dir($resolvedPath)) {
         KalturaLog::log("Serving directory content from [" . $resolvedPath . "]");
         $contents = kFile::listDir($resolvedPath);
         sort($contents, SORT_STRING);
         $contents = serialize($contents);
         header("file-sync-type: dir");
         echo $contents;
         die;
     } else {
         KalturaLog::log("Serving file from [" . $resolvedPath . "]");
         kFile::dumpFile($resolvedPath);
     }
 }