/** * @param $mount * @param $selection * @param string $type * @return mixed */ public function downloadTo($url, $mount, $to) { xapp_import('xapp.Path.Utils'); xapp_import('xapp.File.Utils'); xapp_import('xapp.Commons.ErrorHandler'); xapp_import('xapp.Utils.Download'); xapp_import('xapp.Utils.SystemTextEncoding'); $success = array(); $error = array(); $mount = XApp_Path_Utils::getMount($mount); $vfs = $this->getFileSystem($mount); $realSrcFile = '' . $url; XApp_ErrorHandler::start(); if ($this->isLocal($mount, $this->getFSResources())) { $srcFile = '' . XApp_Path_Utils::decodeSecureMagic($to); $destFile = $vfs->toRealPath($mount . DIRECTORY_SEPARATOR . $srcFile); if (is_dir($destFile) && (!file_exists($destFile) || !is_file($destFile))) { $destDirectory = '' . $destFile; //default file name $fileName = 'remoteFile.download'; //try to get a file name from the url through parse_url(pathinfo()) $urlParts = parse_url($url); if ($urlParts['path']) { $pathInfo = pathinfo($urlParts['path']); if ($pathInfo['basename']) { $fileName = $pathInfo['basename']; } } $destFile = XApp_File_Utils::unique_filename($destFile, $fileName); $destFile = $destDirectory . DIRECTORY_SEPARATOR . $destFile; touch($destFile); } try { $srcPath = XApp_Download::download($realSrcFile); if (!file_exists($srcPath)) { return self::toRPCError(1, 'Error downloading ' . $srcPath . ' Error : ' . $srcPath); } $src = fopen($srcPath, "r"); $dest = fopen($destFile, "w"); if ($dest !== false) { while (!feof($src)) { stream_copy_to_stream($src, $dest, 4096); } fclose($dest); } fclose($src); @unlink($srcPath); } catch (Exception $e) { return self::toRPCError(1, $e->getMessage()); } } else { $mountManager = $vfs->getMountManager(); $to = XApp_Path_Utils::normalizePath($to, false, false); $to = $mount . '://' . $to; $to = $vfs->cleanSlashesRemote($to); try { $src = fopen($realSrcFile, "r"); while (!feof($src)) { $mountManager->updateStream($to, $src); } fclose($src); } catch (Exception $e) { return self::toRPCError(1, $e->getMessage()); } } /* $errors = XApp_ErrorHandler::stop(); if($errors){ xapp_clog($errors); $this->logger->log(json_encode($errors)); } */ /*xapp_clog($errors);*/ return true; }
/** * Returns a filename of a Temporary unique file. * Please note that the calling function must unlink() this itself. * * The filename is based off the passed parameter or defaults to the current unix timestamp, * while the directory can either be passed as well, or by leaving it blank, default to a writable temporary directory. * * @param string $filename (optional) Filename to base the Unique file off * @param string $dir (optional) Directory to store the file in * @return string a writable filename */ public static function tempname($filename = '', $dir = '') { xapp_import('xapp.File.Utils'); if (empty($dir)) { $dir = self::get_temp_dir(); } $filename = basename($filename); if (empty($filename)) { $filename = time(); } $filename = preg_replace('|\\..*$|', '.tmp', $filename); $filename = $dir . XApp_File_Utils::unique_filename($dir, $filename); touch($filename); return $filename; }