function testGetMimeType() { $dir = OC::$SERVERROOT . '/tests/data'; $result = OC_Helper::getMimeType($dir . "/"); $expected = 'httpd/unix-directory'; $this->assertEquals($result, $expected); $result = OC_Helper::getMimeType($dir . "/data.tar.gz"); $expected = 'application/x-gzip'; $this->assertEquals($result, $expected); $result = OC_Helper::getMimeType($dir . "/data.zip"); $expected = 'application/zip'; $this->assertEquals($result, $expected); $result = OC_Helper::getMimeType($dir . "/desktopapp.svg"); $expected = 'image/svg+xml'; $this->assertEquals($result, $expected); $result = OC_Helper::getMimeType($dir . "/desktopapp.png"); $expected = 'image/png'; $this->assertEquals($result, $expected); }
public static function getPackage($url, $version) { $path = \OC_Helper::tmpFile(); if (!copy($url, $path)) { \OC_Log::write(App::APP_ID, "Failed to download {$url} package to {$path}", \OC_Log::ERROR); return false; } //Mimetype bug workaround $mime = rtrim(\OC_Helper::getMimeType($path), ';'); if ($mime == 'application/zip') { rename($path, $path . '.zip'); $path .= '.zip'; } elseif ($mime == 'application/x-gzip') { rename($path, $path . '.tgz'); $path .= '.tgz'; } elseif ($mime == 'application/x-bzip2') { rename($path, $path . '.tar.bz2'); $path .= '.tar.bz2'; } else { \OC_Log::write(App::APP_ID, 'Archives of type ' . $mime . ' are not supported', \OC_Log::ERROR); return false; } $extractDir = self::getPackageDir($version); if (!mkdir($extractDir, 0777, true)) { \OC_Log::write(App::APP_ID, 'Unable to create temporary directory', \OC_Log::ERROR); return false; } $archive = \OC_Archive::open($path); if ($archive) { $archive->extract($extractDir); } else { \OC_Log::write(App::APP_ID, "Failed to open package {$path}", \OC_Log::ERROR); \OC_Helper::rmdirr($extractDir); @unlink($path); return false; } return $extractDir . DIRECTORY_SEPARATOR . self::PACKAGE_ROOT; }
/** * @param array $data * @return array * @throws Exception */ public static function downloadApp($data = array()) { $l = \OC::$server->getL10N('lib'); if (!isset($data['source'])) { throw new \Exception($l->t("No source specified when installing app")); } //download the file if necessary if ($data['source'] == 'http') { $pathInfo = pathinfo($data['href']); $path = OC_Helper::tmpFile('.' . $pathInfo['extension']); if (!isset($data['href'])) { throw new \Exception($l->t("No href specified when installing app from http")); } copy($data['href'], $path); } else { if (!isset($data['path'])) { throw new \Exception($l->t("No path specified when installing app from local file")); } $path = $data['path']; } //detect the archive type $mime = OC_Helper::getMimeType($path); if ($mime !== 'application/zip' && $mime !== 'application/x-gzip') { throw new \Exception($l->t("Archives of type %s are not supported", array($mime))); } //extract the archive in a temporary folder $extractDir = OC_Helper::tmpFolder(); OC_Helper::rmdirr($extractDir); mkdir($extractDir); if ($archive = OC_Archive::open($path)) { $archive->extract($extractDir); } else { OC_Helper::rmdirr($extractDir); if ($data['source'] == 'http') { unlink($path); } throw new \Exception($l->t("Failed to open archive when installing app")); } return array($extractDir, $path); }
public function writeBack($tmpFile) { if (!isset(self::$tmpFiles[$tmpFile])) { return false; } $path = self::$tmpFiles[$tmpFile]; $stat = $this->stat($path); if (empty($stat)) { // create new file $stat = array('permissions' => \OCP\PERMISSION_ALL); } // update stat with new data $mTime = time(); $stat['size'] = filesize($tmpFile); $stat['mtime'] = $mTime; $stat['storage_mtime'] = $mTime; $stat['mimetype'] = \OC_Helper::getMimeType($tmpFile); $stat['etag'] = $this->getETag($path); $fileId = $this->getCache()->put($path, $stat); try { //upload to object storage $this->objectStore->writeObject($this->getURN($fileId), fopen($tmpFile, 'r')); } catch (\Exception $ex) { $this->getCache()->remove($path); \OCP\Util::writeLog('objectstore', 'Could not create object: ' . $ex->getMessage(), \OCP\Util::ERROR); return false; } return true; }
public function writeBack($tmpFile) { if (isset(self::$tempFiles[$tmpFile])) { $path = self::$tempFiles[$tmpFile]; $parentFolder = $this->getDriveFile(dirname($path)); if ($parentFolder) { // TODO Research resumable upload $mimetype = \OC_Helper::getMimeType($tmpFile); $data = file_get_contents($tmpFile); $params = array('data' => $data, 'mimeType' => $mimetype); $result = false; if ($this->file_exists($path)) { $file = $this->getDriveFile($path); $result = $this->service->files->update($file->getId(), $file, $params); } else { $file = new \Google_DriveFile(); $file->setTitle(basename($path)); $file->setMimeType($mimetype); $parent = new \Google_ParentReference(); $parent->setId($parentFolder->getId()); $file->setParents(array($parent)); $result = $this->service->files->insert($file, $params); } if ($result) { $this->setDriveFile($path, $result); } } unlink($tmpFile); } }
private function uploadFile($path, $target) { $entry = $this->getResource($target); if (!$entry) { if (dirname($target) == '.' || dirname($target) == '/') { $uploadUri = 'https://docs.google.com/feeds/upload/create-session/default/private/full/folder%3Aroot/contents'; } else { $entry = $this->getResource(dirname($target)); } } if (!isset($uploadUri) && $entry) { $links = $entry->getElementsByTagName('link'); foreach ($links as $link) { if ($link->getAttribute('rel') == 'http://schemas.google.com/g/2005#resumable-create-media') { $uploadUri = $link->getAttribute('href'); break; } } } if (isset($uploadUri) && ($handle = fopen($path, 'r'))) { $uploadUri .= '?convert=false'; $mimetype = OC_Helper::getMimeType($path); $size = filesize($path); $headers = array('X-Upload-Content-Type: ' => $mimetype, 'X-Upload-Content-Length: ' => $size); $postData = '<?xml version="1.0" encoding="UTF-8"?>'; $postData .= '<entry xmlns="http://www.w3.org/2005/Atom" xmlns:docs="http://schemas.google.com/docs/2007">'; $postData .= '<title>' . basename($target) . '</title>'; $postData .= '</entry>'; $result = $this->sendRequest($uploadUri, 'POST', $postData, $headers, false, true); if ($result) { // Get location to upload file if (preg_match('@^Location: (.*)$@m', $result, $matches)) { $uploadUri = trim($matches[1]); } } else { return false; } // 512 kB chunks $chunkSize = 524288; $i = 0; while (!feof($handle)) { if ($i + $chunkSize > $size) { if ($i == 0) { $chunkSize = $size; } else { $chunkSize = $size % $i; } } $end = $i + $chunkSize - 1; $headers = array('Content-Length: ' . $chunkSize, 'Content-Type: ' . $mimetype, 'Content-Range: bytes ' . $i . '-' . $end . '/' . $size); $postData = fread($handle, $chunkSize); $result = $this->sendRequest($uploadUri, 'PUT', $postData, $headers, false, true, false, true); if ($result['code'] == '308') { if (preg_match('@^Location: (.*)$@m', $result['result'], $matches)) { // Get next location to upload file chunk $uploadUri = trim($matches[1]); } $i += $chunkSize; } else { return false; } } // TODO Wait for resource entry } }
public function writeBack($tmpFile) { if (!isset(self::$tmpFiles[$tmpFile])) { return false; } try { $result = $this->connection->putObject(array('Bucket' => $this->bucket, 'Key' => $this->cleanKey(self::$tmpFiles[$tmpFile]), 'SourceFile' => $tmpFile, 'ContentType' => \OC_Helper::getMimeType($tmpFile), 'ContentLength' => filesize($tmpFile))); $this->testTimeout(); unlink($tmpFile); } catch (S3Exception $e) { \OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR); return false; } }
public function getMimeType($path) { if ($this->isReadable($path)) { if ($path == '' || $path == '/') { return 'httpd/unix-directory'; } else { return \OC_Helper::getMimeType($this->groupoffice_data . $this->get_real_path($path)); } } else { return false; } }
public function getMimeType($path) { if (!$this->file_exists($path)) { return false; } if ($this->is_dir($path)) { return 'httpd/unix-directory'; } $source = $this->fopen($path, 'r'); if (!$source) { return false; } $head = fread($source, 8192); //8kb should suffice to determine a mimetype if ($pos = strrpos($path, '.')) { $extension = substr($path, $pos); } else { $extension = ''; } $tmpFile = OC_Helper::tmpFile($extension); file_put_contents($tmpFile, $head); $mime = OC_Helper::getMimeType($tmpFile); unlink($tmpFile); return $mime; }
/** * get the mimetype form a local file * @param string path * @return string * does NOT work for ownClouds filesystem, use OC_FileSystem::getMimeType instead */ static function getMimeType($path) { return \OC_Helper::getMimeType($path); }
/** * @brief Installs an app * @param $data array with all information * @returns integer * * This function installs an app. All information needed are passed in the * associative array $data. * The following keys are required: * - source: string, can be "path" or "http" * * One of the following keys is required: * - path: path to the file containing the app * - href: link to the downloadable file containing the app * * The following keys are optional: * - pretend: boolean, if set true the system won't do anything * - noinstall: boolean, if true appinfo/install.php won't be loaded * - inactive: boolean, if set true the appconfig/app.sample.php won't be * renamed * * This function works as follows * -# fetching the file * -# unzipping it * -# check the code * -# installing the database at appinfo/database.xml * -# including appinfo/install.php * -# setting the installed version * * It is the task of oc_app_install to create the tables and do whatever is * needed to get the app working. */ public static function installApp($data = array()) { if (!isset($data['source'])) { OC_Log::write('core', 'No source specified when installing app', OC_Log::ERROR); return false; } //download the file if necesary if ($data['source'] == 'http') { $path = OC_Helper::tmpFile(); if (!isset($data['href'])) { OC_Log::write('core', 'No href specified when installing app from http', OC_Log::ERROR); return false; } copy($data['href'], $path); } else { if (!isset($data['path'])) { OC_Log::write('core', 'No path specified when installing app from local file', OC_Log::ERROR); return false; } $path = $data['path']; } //detect the archive type $mime = OC_Helper::getMimeType($path); if ($mime == 'application/zip') { rename($path, $path . '.zip'); $path .= '.zip'; } elseif ($mime == 'application/x-gzip') { rename($path, $path . '.tgz'); $path .= '.tgz'; } else { OC_Log::write('core', 'Archives of type ' . $mime . ' are not supported', OC_Log::ERROR); return false; } //extract the archive in a temporary folder $extractDir = OC_Helper::tmpFolder(); OC_Helper::rmdirr($extractDir); mkdir($extractDir); if ($archive = OC_Archive::open($path)) { $archive->extract($extractDir); } else { OC_Log::write('core', 'Failed to open archive when installing app', OC_Log::ERROR); OC_Helper::rmdirr($extractDir); if ($data['source'] == 'http') { unlink($path); } return false; } //load the info.xml file of the app if (!is_file($extractDir . '/appinfo/info.xml')) { //try to find it in a subdir $dh = opendir($extractDir); while ($folder = readdir($dh)) { if (substr($folder, 0, 1) != '.' and is_dir($extractDir . '/' . $folder)) { if (is_file($extractDir . '/' . $folder . '/appinfo/info.xml')) { $extractDir .= '/' . $folder; } } } } if (!is_file($extractDir . '/appinfo/info.xml')) { OC_Log::write('core', 'App does not provide an info.xml file', OC_Log::ERROR); OC_Helper::rmdirr($extractDir); if ($data['source'] == 'http') { unlink($path); } return false; } $info = OC_App::getAppInfo($extractDir . '/appinfo/info.xml', true); $basedir = OC::$APPSROOT . '/apps/' . $info['id']; // check the code for not allowed calls if (!OC_Installer::checkCode($info['id'], $extractDir)) { OC_Log::write('core', 'App can\'t be installed because of not allowed code in the App', OC_Log::ERROR); OC_Helper::rmdirr($extractDir); return false; } // check if the app is compatible with this version of ownCloud $version = OC_Util::getVersion(); if (!isset($info['require']) or $version[0] > $info['require']) { OC_Log::write('core', 'App can\'t be installed because it is not compatible with this version of ownCloud', OC_Log::ERROR); OC_Helper::rmdirr($extractDir); return false; } //check if an app with the same id is already installed if (self::isInstalled($info['id'])) { OC_Log::write('core', 'App already installed', OC_Log::WARN); OC_Helper::rmdirr($extractDir); if ($data['source'] == 'http') { unlink($path); } return false; } //check if the destination directory already exists if (is_dir($basedir)) { OC_Log::write('core', 'App directory already exists', OC_Log::WARN); OC_Helper::rmdirr($extractDir); if ($data['source'] == 'http') { unlink($path); } return false; } if (isset($data['pretent']) and $data['pretent'] == true) { return false; } //copy the app to the correct place if (@(!mkdir($basedir))) { OC_Log::write('core', 'Can\'t create app folder. Please fix permissions. (' . $basedir . ')', OC_Log::ERROR); OC_Helper::rmdirr($extractDir); if ($data['source'] == 'http') { unlink($path); } return false; } OC_Helper::copyr($extractDir, $basedir); //remove temporary files OC_Helper::rmdirr($extractDir); //install the database if (is_file($basedir . '/appinfo/database.xml')) { OC_DB::createDbFromStructure($basedir . '/appinfo/database.xml'); } //run appinfo/install.php if ((!isset($data['noinstall']) or $data['noinstall'] == false) and file_exists($basedir . '/appinfo/install.php')) { include $basedir . '/appinfo/install.php'; } //set the installed version OC_Appconfig::setValue($info['id'], 'installed_version', OC_App::getAppVersion($info['id'])); OC_Appconfig::setValue($info['id'], 'enabled', 'no'); //set remote/public handelers foreach ($info['remote'] as $name => $path) { OCP\CONFIG::setAppValue('core', 'remote_' . $name, '/apps/' . $info['id'] . '/' . $path); } foreach ($info['public'] as $name => $path) { OCP\CONFIG::setAppValue('core', 'public_' . $name, '/apps/' . $info['id'] . '/' . $path); } OC_App::setAppTypes($info['id']); return $info['id']; }
public function writeBack($tmpFile) { if (!isset(self::$tmpFiles[$tmpFile])) { return false; } $object = $this->container->DataObject(); $object->Create(array('name' => self::$tmpFiles[$tmpFile], 'content_type' => \OC_Helper::getMimeType($tmpFile)), $tmpFile); unlink($tmpFile); }
/** * @brief Installs an app * @param $data array with all information * @throws \Exception * @returns integer * * This function installs an app. All information needed are passed in the * associative array $data. * The following keys are required: * - source: string, can be "path" or "http" * * One of the following keys is required: * - path: path to the file containing the app * - href: link to the downloadable file containing the app * * The following keys are optional: * - pretend: boolean, if set true the system won't do anything * - noinstall: boolean, if true appinfo/install.php won't be loaded * - inactive: boolean, if set true the appconfig/app.sample.php won't be * renamed * * This function works as follows * -# fetching the file * -# unzipping it * -# check the code * -# installing the database at appinfo/database.xml * -# including appinfo/install.php * -# setting the installed version * * It is the task of oc_app_install to create the tables and do whatever is * needed to get the app working. */ public static function installApp($data = array()) { $l = \OC_L10N::get('lib'); if (!isset($data['source'])) { throw new \Exception($l->t("No source specified when installing app")); } //download the file if necessary if ($data['source'] == 'http') { $pathInfo = pathinfo($data['href']); $path = OC_Helper::tmpFile('.' . $pathInfo['extension']); if (!isset($data['href'])) { throw new \Exception($l->t("No href specified when installing app from http")); } copy($data['href'], $path); } else { if (!isset($data['path'])) { throw new \Exception($l->t("No path specified when installing app from local file")); } $path = $data['path']; } //detect the archive type $mime = OC_Helper::getMimeType($path); if ($mime !== 'application/zip' && $mime !== 'application/x-gzip') { throw new \Exception($l->t("Archives of type %s are not supported", array($mime))); } //extract the archive in a temporary folder $extractDir = OC_Helper::tmpFolder(); OC_Helper::rmdirr($extractDir); mkdir($extractDir); if ($archive = OC_Archive::open($path)) { $archive->extract($extractDir); } else { OC_Helper::rmdirr($extractDir); if ($data['source'] == 'http') { unlink($path); } throw new \Exception($l->t("Failed to open archive when installing app")); } //load the info.xml file of the app if (!is_file($extractDir . '/appinfo/info.xml')) { //try to find it in a subdir $dh = opendir($extractDir); if (is_resource($dh)) { while (($folder = readdir($dh)) !== false) { if ($folder[0] != '.' and is_dir($extractDir . '/' . $folder)) { if (is_file($extractDir . '/' . $folder . '/appinfo/info.xml')) { $extractDir .= '/' . $folder; } } } } } if (!is_file($extractDir . '/appinfo/info.xml')) { OC_Helper::rmdirr($extractDir); if ($data['source'] == 'http') { unlink($path); } throw new \Exception($l->t("App does not provide an info.xml file")); } $info = OC_App::getAppInfo($extractDir . '/appinfo/info.xml', true); // check the code for not allowed calls if (!OC_Installer::checkCode($info['id'], $extractDir)) { OC_Helper::rmdirr($extractDir); throw new \Exception($l->t("App can't be installed because of not allowed code in the App")); } // check if the app is compatible with this version of ownCloud if (!isset($info['require']) or !OC_App::isAppVersionCompatible(OC_Util::getVersion(), $info['require'])) { OC_Helper::rmdirr($extractDir); throw new \Exception($l->t("App can't be installed because it is not compatible with this version of ownCloud")); } // check if shipped tag is set which is only allowed for apps that are shipped with ownCloud if (isset($info['shipped']) and $info['shipped'] == 'true') { OC_Helper::rmdirr($extractDir); throw new \Exception($l->t("App can't be installed because it contains the <shipped>true</shipped> tag which is not allowed for non shipped apps")); } // check if the ocs version is the same as the version in info.xml/version $versionFile = $extractDir . '/appinfo/version'; if (is_file($versionFile)) { $version = trim(file_get_contents($versionFile)); } else { $version = trim($info['version']); } if ($version != trim($data['appdata']['version'])) { OC_Helper::rmdirr($extractDir); throw new \Exception($l->t("App can't be installed because the version in info.xml/version is not the same as the version reported from the app store")); } $basedir = OC_App::getInstallPath() . '/' . $info['id']; //check if the destination directory already exists if (is_dir($basedir)) { OC_Helper::rmdirr($extractDir); if ($data['source'] == 'http') { unlink($path); } throw new \Exception($l->t("App directory already exists")); } if (isset($data['pretent']) and $data['pretent'] == true) { return false; } //copy the app to the correct place if (@(!mkdir($basedir))) { OC_Helper::rmdirr($extractDir); if ($data['source'] == 'http') { unlink($path); } throw new \Exception($l->t("Can't create app folder. Please fix permissions. %s", array($basedir))); } OC_Helper::copyr($extractDir, $basedir); //remove temporary files OC_Helper::rmdirr($extractDir); //install the database if (is_file($basedir . '/appinfo/database.xml')) { if (OC_Appconfig::getValue($info['id'], 'installed_version') === null) { OC_DB::createDbFromStructure($basedir . '/appinfo/database.xml'); } else { OC_DB::updateDbFromStructure($basedir . '/appinfo/database.xml'); } } //run appinfo/install.php if ((!isset($data['noinstall']) or $data['noinstall'] == false) and file_exists($basedir . '/appinfo/install.php')) { include $basedir . '/appinfo/install.php'; } //set the installed version OC_Appconfig::setValue($info['id'], 'installed_version', OC_App::getAppVersion($info['id'])); OC_Appconfig::setValue($info['id'], 'enabled', 'no'); //set remote/public handelers foreach ($info['remote'] as $name => $path) { OCP\CONFIG::setAppValue('core', 'remote_' . $name, $info['id'] . '/' . $path); } foreach ($info['public'] as $name => $path) { OCP\CONFIG::setAppValue('core', 'public_' . $name, $info['id'] . '/' . $path); } OC_App::setAppTypes($info['id']); return $info['id']; }
public function getMimeType($path) { if ($this->isReadable($path)) { return OC_Helper::getMimeType($this->datadir . $path); } else { return false; } }
public function getMimeType($path) { if ($this->isReadable($path)) { return \OC_Helper::getMimeType($this->buildPath($path)); } else { return false; } }
/** * Internal check to get the proper mimetype. * * This function would go over the available PHP methods to get * the MIME type. * * By default it will try to use the PHP fileinfo library which is * available from PHP 5.3 or as an PECL extension * (http://pecl.php.net/package/Fileinfo). * * It will get the magic file by default from the system wide file * which is usually available in /usr/share/magic on Unix or try * to use the file specified in the source directory of the API * (share directory). * * if fileinfo is not available it will try to use the internal * mime_content_type function. * * @param string $handle name of file or buffer to guess the type from * @return boolean <kbd>True</kbd> if successful * @throws BadContentTypeException */ function _guess_content_type($handle) { if ($this->content_type) { return; } // if (function_exists("finfo_open")) { // $local_magic = dirname(__FILE__) . "/share/magic"; // $finfo = @finfo_open(FILEINFO_MIME, $local_magic); // // if (!$finfo) // $finfo = @finfo_open(FILEINFO_MIME); // // if ($finfo) { // // if (is_file((string)$handle)) // $ct = @finfo_file($finfo, $handle); // else // $ct = @finfo_buffer($finfo, $handle); // // /* PHP 5.3 fileinfo display extra information like // charset so we remove everything after the ; since // we are not into that stuff */ // if ($ct) { // $extra_content_type_info = strpos($ct, "; "); // if ($extra_content_type_info) // $ct = substr($ct, 0, $extra_content_type_info); // } // // if ($ct && $ct != 'application/octet-stream') // $this->content_type = $ct; // // @finfo_close($finfo); // } // } // // if (!$this->content_type && (string)is_file($handle) && function_exists("mime_content_type")) { // $this->content_type = @mime_content_type($handle); // } //use OC's mimetype detection for files if (is_file($handle)) { $this->content_type = OC_Helper::getMimeType($handle); } else { $this->content_type = OC_Helper::getStringMimeType($handle); } if (!$this->content_type) { throw new BadContentTypeException("Required Content-Type not set"); } return True; }