| Conditions | 15 |
| Paths | 49 |
| Total Lines | 131 |
| Code Lines | 78 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 22 | public static function doUpload() { |
||
| 23 | // parse account id. |
||
| 24 | if (isset($_POST["parentID"])) { // will be set if a standard upload is used. |
||
| 25 | $dstID = $_POST["parentID"]; |
||
| 26 | } |
||
| 27 | else { |
||
| 28 | if (isset($_SERVER['HTTP_X_FILE_DESTINATION'])) { // will be set if the upload is a ajax upload. |
||
| 29 | $dstID = $_SERVER['HTTP_X_FILE_DESTINATION']; |
||
| 30 | } |
||
| 31 | else { |
||
| 32 | Logger::error(self::LOG_CONTEXT, "upload failed: No destination given"); |
||
| 33 | echo json_encode(['success' => false, 'response' => 'No destination given', 'message' => 'No destination given']); |
||
| 34 | |||
| 35 | exit(); |
||
|
|
|||
| 36 | } |
||
| 37 | } |
||
| 38 | |||
| 39 | $accountID = substr($dstID, 3, (strpos($dstID, '/') - 3)); |
||
| 40 | |||
| 41 | // relative node ID. We need to trim off the #R# and account ID |
||
| 42 | $relNodeId = substr($dstID, strpos($dstID, '/')); |
||
| 43 | |||
| 44 | // Initialize the account and backendstore |
||
| 45 | $accountStore = new \Files\Core\AccountStore(); |
||
| 46 | $backendStore = \Files\Backend\BackendStore::getInstance(); |
||
| 47 | |||
| 48 | $account = $accountStore->getAccount($accountID); |
||
| 49 | |||
| 50 | // initialize the backend |
||
| 51 | $initializedBackend = $backendStore->getInstanceOfBackend($account->getBackend()); |
||
| 52 | $initializedBackend->init_backend($account->getBackendConfig()); |
||
| 53 | |||
| 54 | try { |
||
| 55 | $initializedBackend->open(); |
||
| 56 | } |
||
| 57 | catch (\Files\Backend\Exception $e) { |
||
| 58 | Logger::error(self::LOG_CONTEXT, "backend initialization failed: " . $e->getMessage()); |
||
| 59 | echo json_encode(['success' => false, 'response' => $e->getCode(), 'message' => $e->getMessage()]); |
||
| 60 | |||
| 61 | exit(); |
||
| 62 | } |
||
| 63 | |||
| 64 | // check if we are getting the file via the "new" method (ajax - XMLHttpRequest) or the standard way |
||
| 65 | if (isset($_SERVER['HTTP_X_FILE_NAME'], $_SERVER['HTTP_X_FILE_SIZE'])) { // use the ajax method |
||
| 66 | $targetPath = stringToUTF8Encode($relNodeId . $_SERVER['HTTP_X_FILE_NAME']); |
||
| 67 | // check if backend supports streaming - this is the preferred way to upload files! |
||
| 68 | if ($initializedBackend->supports(\Files\Backend\BackendStore::FEATURE_STREAMING)) { |
||
| 69 | $fileReader = fopen('php://input', "r"); |
||
| 70 | $targetPath = UploadHandler::checkFilesNameConflict($targetPath, $initializedBackend, $relNodeId); |
||
| 71 | $fileWriter = $initializedBackend->getStreamwriter($targetPath); |
||
| 72 | |||
| 73 | while (true) { |
||
| 74 | set_time_limit(0); |
||
| 75 | $buffer = fgets($fileReader, 4096); |
||
| 76 | if (strlen($buffer) == 0) { |
||
| 77 | fclose($fileReader); |
||
| 78 | fclose($fileWriter); |
||
| 79 | break; |
||
| 80 | } |
||
| 81 | |||
| 82 | fwrite($fileWriter, $buffer); |
||
| 83 | } |
||
| 84 | } |
||
| 85 | else { // fallback to tmp files |
||
| 86 | $targetPath = UploadHandler::checkFilesNameConflict($targetPath, $initializedBackend, $relNodeId); |
||
| 87 | $targetPath = rawurldecode($targetPath); |
||
| 88 | $temp_file = tempnam(TMP_PATH, "{$targetPath}"); |
||
| 89 | $fileReader = fopen('php://input', "r"); |
||
| 90 | $fileWriter = fopen($temp_file, "w"); |
||
| 91 | |||
| 92 | // store post data to tmp file |
||
| 93 | while (true) { |
||
| 94 | set_time_limit(0); |
||
| 95 | $buffer = fgets($fileReader, 4096); |
||
| 96 | if (strlen($buffer) == 0) { |
||
| 97 | fclose($fileReader); |
||
| 98 | fclose($fileWriter); |
||
| 99 | break; |
||
| 100 | } |
||
| 101 | |||
| 102 | fwrite($fileWriter, $buffer); |
||
| 103 | } |
||
| 104 | |||
| 105 | // upload tmp file to backend |
||
| 106 | $initializedBackend->put_file($targetPath, $temp_file); |
||
| 107 | // clean up tmp file |
||
| 108 | unlink($temp_file); |
||
| 109 | } |
||
| 110 | echo json_encode(['success' => true, 'parent' => $dstID, 'item' => $targetPath]); |
||
| 111 | |||
| 112 | exit(); |
||
| 113 | } // upload the standard way with $_FILES |
||
| 114 | $items = []; |
||
| 115 | |||
| 116 | try { |
||
| 117 | for ($i = 0; $i < count($_FILES['attachments']['name']); ++$i) { |
||
| 118 | $targetPath = stringToUTF8Encode($relNodeId . $_FILES['attachments']['name'][$i]); |
||
| 119 | |||
| 120 | // upload the file |
||
| 121 | // check if backend supports streaming - this is the preferred way to upload files! |
||
| 122 | if ($initializedBackend->supports(\Files\Backend\BackendStore::FEATURE_STREAMING)) { |
||
| 123 | $fileReader = fopen($_FILES['attachments']['tmp_name'][$i], "r"); |
||
| 124 | $fileWriter = $initializedBackend->getStreamwriter($targetPath); |
||
| 125 | |||
| 126 | while (true) { |
||
| 127 | set_time_limit(0); |
||
| 128 | $buffer = fgets($fileReader, 4096); |
||
| 129 | if (strlen($buffer) == 0) { |
||
| 130 | fclose($fileReader); |
||
| 131 | fclose($fileWriter); |
||
| 132 | break; |
||
| 133 | } |
||
| 134 | |||
| 135 | fwrite($fileWriter, $buffer); |
||
| 136 | } |
||
| 137 | } |
||
| 138 | else { // use the normal way - might have a high memory footprint |
||
| 139 | $initializedBackend->put_file($targetPath, $_FILES['attachments']['tmp_name'][$i]); |
||
| 140 | } |
||
| 141 | |||
| 142 | $items[] = ['tmp_name' => $_FILES['attachments']['tmp_name'][$i], 'name' => $_FILES['attachments']['name'][$i]]; |
||
| 143 | } |
||
| 144 | echo json_encode(['success' => true, 'parent' => $dstID, 'items' => $items]); |
||
| 145 | |||
| 146 | exit(); |
||
| 147 | } |
||
| 148 | catch (\Files\Backend\Exception $e) { |
||
| 149 | Logger::error(self::LOG_CONTEXT, "upload failed: " . $e->getMessage()); |
||
| 150 | echo json_encode(['success' => false, 'response' => $e->getCode(), 'message' => $e->getMessage()]); |
||
| 151 | |||
| 152 | exit(); |
||
| 153 | } |
||
| 188 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.