Blame view
sources/apps/files/ajax/upload.php
6.96 KB
|
03e52840d
|
1 2 3 4 5 6 7 8 9 10 |
<?php
// 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\PERMISSION_ALL;
|
|
6d9380f96
|
11 |
$errorCode = null; |
|
03e52840d
|
12 13 14 15 16 17 18 19 20 21 22 |
$l = OC_L10N::get('files');
if (empty($_POST['dirToken'])) {
// The standard case, files are uploaded through logged in users :)
OCP\JSON::checkLoggedIn();
$dir = isset($_POST['dir']) ? $_POST['dir'] : "";
if (!$dir || empty($dir) || $dir === false) {
OCP\JSON::error(array('data' => array_merge(array('message' => $l->t('Unable to set upload directory.')))));
die();
}
} else {
|
|
6d9380f96
|
23 24 25 |
// TODO: ideally this code should be in files_sharing/ajax/upload.php // and the upload/file transfer code needs to be refactored into a utility method // that could be used there |
|
f7d878ff1
|
26 |
\OC_User::setIncognitoMode(true); |
|
03e52840d
|
27 28 |
// return only read permissions for public upload $allowedPermissions = OCP\PERMISSION_READ; |
|
6d9380f96
|
29 |
$publicDirectory = !empty($_POST['subdir']) ? $_POST['subdir'] : '/'; |
|
03e52840d
|
30 31 32 33 34 35 36 37 38 39 40 41 |
$linkItem = OCP\Share::getShareByToken($_POST['dirToken']);
if ($linkItem === false) {
OCP\JSON::error(array('data' => array_merge(array('message' => $l->t('Invalid Token')))));
die();
}
if (!($linkItem['permissions'] & OCP\PERMISSION_CREATE)) {
OCP\JSON::checkLoggedIn();
} else {
// resolve reshares
$rootLinkItem = OCP\Share::resolveReShare($linkItem);
|
|
a293d369c
|
42 |
OCP\JSON::checkUserExists($rootLinkItem['uid_owner']); |
|
03e52840d
|
43 |
// Setup FS with owner |
|
31b7f2792
|
44 |
OC_Util::tearDownFS(); |
|
03e52840d
|
45 46 47 48 49 50 51 |
OC_Util::setupFS($rootLinkItem['uid_owner']); // The token defines the target directory (security reasons) $path = \OC\Files\Filesystem::getPath($linkItem['file_source']); $dir = sprintf( "/%s/%s", $path, |
|
6d9380f96
|
52 |
$publicDirectory |
|
03e52840d
|
53 54 55 56 57 58 |
);
if (!$dir || empty($dir) || $dir === false) {
OCP\JSON::error(array('data' => array_merge(array('message' => $l->t('Unable to set upload directory.')))));
die();
}
|
|
6d9380f96
|
59 60 |
$dir = rtrim($dir, '/'); |
|
03e52840d
|
61 62 63 64 65 |
} } OCP\JSON::callCheck(); |
|
6d9380f96
|
66 67 68 69 |
if (!\OCP\App::isEnabled('files_encryption')) {
// encryption app need to create keys later, so can't close too early
\OC::$session->close();
}
|
|
03e52840d
|
70 71 72 |
// get array with current storage stats (e.g. max file size) |
|
31b7f2792
|
73 |
$storageStats = \OCA\Files\Helper::buildFileStorageStatistics($dir); |
|
03e52840d
|
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
if (!isset($_FILES['files'])) {
OCP\JSON::error(array('data' => array_merge(array('message' => $l->t('No file was uploaded. Unknown error')), $storageStats)));
exit();
}
foreach ($_FILES['files']['error'] as $error) {
if ($error != 0) {
$errors = array(
UPLOAD_ERR_OK => $l->t('There is no error, the file uploaded with success'),
UPLOAD_ERR_INI_SIZE => $l->t('The uploaded file exceeds the upload_max_filesize directive in php.ini: ')
. ini_get('upload_max_filesize'),
UPLOAD_ERR_FORM_SIZE => $l->t('The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form'),
UPLOAD_ERR_PARTIAL => $l->t('The uploaded file was only partially uploaded'),
UPLOAD_ERR_NO_FILE => $l->t('No file was uploaded'),
UPLOAD_ERR_NO_TMP_DIR => $l->t('Missing a temporary folder'),
UPLOAD_ERR_CANT_WRITE => $l->t('Failed to write to disk'),
);
|
|
6d9380f96
|
92 93 94 |
$errorMessage = $errors[$error];
\OC::$server->getLogger()->alert("Upload error: $error - $errorMessage", array('app' => 'files'));
OCP\JSON::error(array('data' => array_merge(array('message' => $errorMessage), $storageStats)));
|
|
03e52840d
|
95 96 97 98 |
exit(); } } $files = $_FILES['files']; |
|
31b7f2792
|
99 |
$error = false; |
|
03e52840d
|
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
$maxUploadFileSize = $storageStats['uploadMaxFilesize'];
$maxHumanFileSize = OCP\Util::humanFileSize($maxUploadFileSize);
$totalSize = 0;
foreach ($files['size'] as $size) {
$totalSize += $size;
}
if ($maxUploadFileSize >= 0 and $totalSize > $maxUploadFileSize) {
OCP\JSON::error(array('data' => array('message' => $l->t('Not enough storage available'),
'uploadMaxFilesize' => $maxUploadFileSize,
'maxHumanFilesize' => $maxHumanFileSize)));
exit();
}
$result = array();
if (strpos($dir, '..') === false) {
$fileCount = count($files['name']);
for ($i = 0; $i < $fileCount; $i++) {
|
|
6d9380f96
|
119 120 121 122 123 124 |
// target directory for when uploading folders
$relativePath = '';
if(!empty($_POST['file_directory'])) {
$relativePath = '/'.$_POST['file_directory'];
}
|
|
03e52840d
|
125 |
// $path needs to be normalized - this failed within drag'n'drop upload to a sub-folder |
|
31b7f2792
|
126 127 |
if (isset($_POST['resolution']) && $_POST['resolution']==='autorename') {
// append a number in brackets like 'filename (2).ext'
|
|
6d9380f96
|
128 |
$target = OCP\Files::buildNotExistingFileName(stripslashes($dir . $relativePath), $files['name'][$i]); |
|
31b7f2792
|
129 |
} else {
|
|
6d9380f96
|
130 |
$target = \OC\Files\Filesystem::normalizePath(stripslashes($dir . $relativePath).'/'.$files['name'][$i]); |
|
31b7f2792
|
131 |
} |
|
a293d369c
|
132 |
|
|
6d9380f96
|
133 134 135 136 137 138 139 |
// 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;
|
|
a293d369c
|
140 |
} |
|
6d9380f96
|
141 |
$returnedDir = \OC\Files\Filesystem::normalizePath($returnedDir); |
|
a293d369c
|
142 |
|
|
31b7f2792
|
143 144 145 146 147 148 149 150 151 152 153 154 155 |
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) {
|
|
6d9380f96
|
156 157 |
$error = $l->t('The target folder has been moved or deleted.');
$errorCode = 'targetnotfound';
|
|
31b7f2792
|
158 |
} else {
|
|
6d9380f96
|
159 160 161 162 163 164 165 166 |
$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; |
|
31b7f2792
|
167 168 169 170 171 172 173 174 |
}
} else {
$error = $l->t('Upload failed. Could not find uploaded file');
}
} catch(Exception $ex) {
$error = $ex->getMessage();
}
|
|
f7d878ff1
|
175 |
|
|
31b7f2792
|
176 177 |
} else {
// file already exists
|
|
03e52840d
|
178 |
$meta = \OC\Files\Filesystem::getFileInfo($target); |
|
03e52840d
|
179 |
if ($meta === false) {
|
|
31b7f2792
|
180 |
$error = $l->t('Upload failed. Could not get file info.');
|
|
03e52840d
|
181 |
} else {
|
|
6d9380f96
|
182 183 184 185 186 187 188 189 190 |
$data = \OCA\Files\Helper::formatFileInfo($meta); $data['permissions'] = $data['permissions'] & $allowedPermissions; $data['status'] = 'existserror'; $data['originalname'] = $files['tmp_name'][$i]; $data['uploadMaxFilesize'] = $maxUploadFileSize; $data['maxHumanFilesize'] = $maxHumanFileSize; $data['permissions'] = $meta['permissions'] & $allowedPermissions; $data['directory'] = $returnedDir; $result[] = $data; |
|
03e52840d
|
191 192 193 |
} } } |
|
03e52840d
|
194 195 196 |
} else {
$error = $l->t('Invalid directory.');
}
|
|
31b7f2792
|
197 198 |
if ($error === false) {
OCP\JSON::encodedPrint($result);
|
|
31b7f2792
|
199 |
} else {
|
|
6d9380f96
|
200 |
OCP\JSON::error(array(array('data' => array_merge(array('message' => $error, 'code' => $errorCode), $storageStats))));
|
|
31b7f2792
|
201 |
} |