} else { $target = \OC\Files\Filesystem::normalizePath(stripslashes($dir . $relativePath) . '/' . $files['name'][$i]); } // relative dir to return to the client if (isset($publicDirectory)) { // path relative to the public root $returnedDir = $publicDirectory . $relativePath; } else { // full path $returnedDir = $dir . $relativePath; } $returnedDir = \OC\Files\Filesystem::normalizePath($returnedDir); if (!\OC\Files\Filesystem::file_exists($target) || isset($_POST['resolution']) && $_POST['resolution'] === 'replace') { // upload and overwrite file try { if (is_uploaded_file($files['tmp_name'][$i]) and \OC\Files\Filesystem::fromTmpFile($files['tmp_name'][$i], $target)) { // updated max file size after upload $storageStats = \OCA\Files\Helper::buildFileStorageStatistics($dir); $meta = \OC\Files\Filesystem::getFileInfo($target); if ($meta === false) { $error = $l->t('The target folder has been moved or deleted.'); $errorCode = 'targetnotfound'; } else { $data = \OCA\Files\Helper::formatFileInfo($meta); $data['status'] = 'success'; $data['originalname'] = $files['tmp_name'][$i]; $data['uploadMaxFilesize'] = $maxUploadFileSize; $data['maxHumanFilesize'] = $maxHumanFileSize; $data['permissions'] = $meta['permissions'] & $allowedPermissions; $data['directory'] = $returnedDir; $result[] = $data;
/** * @deprecated OC_Filesystem is replaced by \OC\Files\Filesystem */ public static function fromTmpFile($tmpFile, $path) { return \OC\Files\Filesystem::fromTmpFile($tmpFile, $path); }
/** * @NoAdminRequired * @NoCSRFRequired */ public function UploadFiles() { \OCP\JSON::setContentTypeHeader('text/plain'); if (!$this->AllowProtocolBT && !\OC_User::isAdminUser($this->CurrentUID)) { return new JSONResponse(array('ERROR' => true, 'MESSAGE' => (string) $this->L10N->t('You are not allowed to use the BitTorrent protocol'))); } if (!isset($_FILES['files'])) { return new JSONResponse(array('ERROR' => true, 'MESSAGE' => (string) $this->L10N->t('Error while uploading torrent file'))); } else { if (!isset($_FILES['files']['name'][0])) { throw new \Exception('Unable to find the uploaded file'); } $Target = rtrim($this->TorrentsFolder, '/') . '/' . $_FILES['files']['name'][0]; try { if (is_uploaded_file($_FILES['files']['tmp_name'][0]) && \OC\Files\Filesystem::fromTmpFile($_FILES['files']['tmp_name'][0], $Target)) { $StorageStats = \OCA\Files\Helper::buildFileStorageStatistics($this->TorrentsFolder); if (\OC\Files\Filesystem::getFileInfo($Target) !== false) { return new JSONResponse(array('ERROR' => false, 'MESSAGE' => (string) $this->L10N->t('Upload OK'))); } } else { return new JSONResponse(array('ERROR' => true, 'MESSAGE' => (string) $this->L10N->t('Error while uploading torrent file'))); } } catch (Exception $E) { return new JSONResponse(array('ERROR' => true, 'MESSAGE' => $E->getMessage())); } } }
/** * @NoAdminRequired * @NoCSRFRequired * @SSOCORS */ public function upload($dir = '/') { \OC::$server->getSession()->close(); // Firefox and Konqueror tries to download application/json for me. --Arthur \OCP\JSON::setContentTypeHeader('text/plain'); // If a directory token is sent along check if public upload is permitted. // If not, check the login. // If no token is sent along, rely on login only $allowedPermissions = \OCP\Constants::PERMISSION_ALL; $errorCode = null; if (\OC\Files\Filesystem::file_exists($dir) === false) { return new DataResponse(array('data' => array_merge(array('message' => 'Invalid directory.')), 'status' => 'error')); } // get array with current storage stats (e.g. max file size) $storageStats = \OCA\Files\Helper::buildFileStorageStatistics($dir); $files = $this->request->getUploadedFile('files'); if (!isset($files)) { return new DataResponse(array('data' => array_merge(array('message' => 'No file was uploaded. Unknown error'), $storageStats), 'status' => 'error')); } foreach ($files['error'] as $error) { if ($error != 0) { $errors = array(UPLOAD_ERR_OK => 'There is no error, the file uploaded with success', UPLOAD_ERR_INI_SIZE => 'The uploaded file exceeds the upload_max_filesize directive in php.ini: ' . ini_get('upload_max_filesize'), UPLOAD_ERR_FORM_SIZE => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form', UPLOAD_ERR_PARTIAL => 'The uploaded file was only partially uploaded', UPLOAD_ERR_NO_FILE => 'No file was uploaded', UPLOAD_ERR_NO_TMP_DIR => 'Missing a temporary folder', UPLOAD_ERR_CANT_WRITE => 'Failed to write to disk'); $errorMessage = $errors[$error]; \OC::$server->getLogger()->alert("Upload error: {$error} - {$errorMessage}", array('app' => 'files')); return new DataResponse(array('data' => array_merge(array('message' => $errorMessage), $storageStats), 'status' => 'error')); } } $error = false; $maxUploadFileSize = $storageStats['uploadMaxFilesize']; $maxHumanFileSize = \OCP\Util::humanFileSize($maxUploadFileSize); $totalSize = 0; foreach ($files['size'] as $size) { $totalSize += $size; } if ($maxUploadFileSize >= 0 and $totalSize > $maxUploadFileSize) { return new DataResponse(array('data' => array('message' => 'Not enough storage available', 'uploadMaxFilesize' => $maxUploadFileSize, ' maxHumanFilesize' => $maxHumanFileSize), 'status' => 'error')); } $result = array(); $fileCount = count($files['name']); for ($i = 0; $i < $fileCount; $i++) { // target directory for when uploading folders $relativePath = ''; $target = \OC\Files\Filesystem::normalizePath($dir . $relativePath . '/' . $files['name'][$i]); // relative dir to return to the client if (isset($publicDirectory)) { // path relative to the public root $returnedDir = $publicDirectory . $relativePath; } else { // full path $returnedDir = $dir . $relativePath; } $returnedDir = \OC\Files\Filesystem::normalizePath($returnedDir); $exists = \OC\Files\Filesystem::file_exists($target); if ($exists) { $target = \OCP\Files::buildNotExistingFileName($dir . $relativePath, $files['name'][$i]); } try { if (is_uploaded_file($files['tmp_name'][$i]) and \OC\Files\Filesystem::fromTmpFile($files['tmp_name'][$i], $target)) { // updated max file size after upload $storageStats = \OCA\Files\Helper::buildFileStorageStatistics($dir); $meta = \OC\Files\Filesystem::getFileInfo($target); if ($meta === false) { $error = 'The target folder has been moved or deleted.'; $errorCode = 'targetnotfound'; } else { $data = \OCA\Files\Helper::formatFileInfo($meta); $data['originalname'] = $files['name'][$i]; $data['uploadMaxFilesize'] = $maxUploadFileSize; $data['maxHumanFilesize'] = $maxHumanFileSize; $data['permissions'] = $meta['permissions'] & $allowedPermissions; $data['directory'] = $returnedDir; $result[] = $data; } } else { $error = 'Upload failed. Could not find uploaded file'; } } catch (Exception $ex) { $error = $ex->getMessage(); } } if ($error === false) { $result = \OCP\JSON::encode($result); return new DataResponse(array('data' => $result, 'status' => 'success')); } else { return new DataResponse(array('data' => array_merge(array('message' => $error, 'code' => $errorCode), $storageStats), 'status' => 'error')); } }