| Conditions | 16 |
| Paths | 49 |
| Total Lines | 121 |
| Code Lines | 79 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 26 | public static function doUpload() |
||
| 27 | { |
||
| 28 | // parse account id. |
||
| 29 | if (isset($_POST["parentID"])) { // will be set if a standard upload is used. |
||
| 30 | $dstID = $_POST["parentID"]; |
||
| 31 | } else { |
||
| 32 | if (isset($_SERVER['HTTP_X_FILE_DESTINATION'])) { // will be set if the upload is a ajax upload. |
||
| 33 | $dstID = $_SERVER['HTTP_X_FILE_DESTINATION']; |
||
| 34 | } else { |
||
| 35 | Logger::error(self::LOG_CONTEXT, "upload failed: No destination given"); |
||
| 36 | echo json_encode(array('success' => false, 'response' => 'No destination given', 'message' => 'No destination given')); |
||
| 37 | die(); |
||
|
|
|||
| 38 | } |
||
| 39 | } |
||
| 40 | |||
| 41 | $accountID = substr($dstID, 3, (strpos($dstID, '/') - 3)); |
||
| 42 | |||
| 43 | // relative node ID. We need to trim off the #R# and account ID |
||
| 44 | $relNodeId = substr($dstID, strpos($dstID, '/')); |
||
| 45 | |||
| 46 | // Initialize the account and backendstore |
||
| 47 | $accountStore = new \Files\Core\AccountStore(); |
||
| 48 | $backendStore = \Files\Backend\BackendStore::getInstance(); |
||
| 49 | |||
| 50 | $account = $accountStore->getAccount($accountID); |
||
| 51 | |||
| 52 | // initialize the backend |
||
| 53 | $initializedBackend = $backendStore->getInstanceOfBackend($account->getBackend()); |
||
| 54 | $initializedBackend->init_backend($account->getBackendConfig()); |
||
| 55 | |||
| 56 | try { |
||
| 57 | $initializedBackend->open(); |
||
| 58 | } catch (\Files\Backend\Exception $e) { |
||
| 59 | Logger::error(self::LOG_CONTEXT, "backend initialization failed: " . $e->getMessage()); |
||
| 60 | echo json_encode(array('success' => false, 'response' => $e->getCode(), 'message' => $e->getMessage())); |
||
| 61 | die(); |
||
| 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']) && isset($_SERVER['HTTP_X_FILE_SIZE'])) { // use the ajax method |
||
| 66 | |||
| 67 | $targetPath = stringToUTF8Encode($relNodeId . $_SERVER['HTTP_X_FILE_NAME']); |
||
| 68 | // check if backend supports streaming - this is the preferred way to upload files! |
||
| 69 | if ($initializedBackend->supports(\Files\Backend\BackendStore::FEATURE_STREAMING)) { |
||
| 70 | $fileReader = fopen('php://input', "r"); |
||
| 71 | $targetPath = UploadHandler::checkFilesNameConflict($targetPath, $initializedBackend, $relNodeId); |
||
| 72 | $fileWriter = $initializedBackend->getStreamwriter($targetPath); |
||
| 73 | |||
| 74 | while (true) { |
||
| 75 | set_time_limit(0); |
||
| 76 | $buffer = fgets($fileReader, 4096); |
||
| 77 | if (strlen($buffer) == 0) { |
||
| 78 | fclose($fileReader); |
||
| 79 | fclose($fileWriter); |
||
| 80 | break; |
||
| 81 | } |
||
| 82 | |||
| 83 | fwrite($fileWriter, $buffer); |
||
| 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(array('success' => true, 'parent' => $dstID, 'item' => $targetPath)); |
||
| 111 | die(); |
||
| 112 | } else { // upload the standard way with $_FILES |
||
| 113 | $items = array(); |
||
| 114 | try { |
||
| 115 | for ($i = 0; $i < count($_FILES['attachments']['name']); $i++) { |
||
| 116 | $targetPath = stringToUTF8Encode($relNodeId . $_FILES['attachments']['name'][$i]); |
||
| 117 | |||
| 118 | // upload the file |
||
| 119 | // check if backend supports streaming - this is the preferred way to upload files! |
||
| 120 | if ($initializedBackend->supports(\Files\Backend\BackendStore::FEATURE_STREAMING)) { |
||
| 121 | $fileReader = fopen($_FILES['attachments']['tmp_name'][$i], "r"); |
||
| 122 | $fileWriter = $initializedBackend->getStreamwriter($targetPath); |
||
| 123 | |||
| 124 | while (true) { |
||
| 125 | set_time_limit(0); |
||
| 126 | $buffer = fgets($fileReader, 4096); |
||
| 127 | if (strlen($buffer) == 0) { |
||
| 128 | fclose($fileReader); |
||
| 129 | fclose($fileWriter); |
||
| 130 | break; |
||
| 131 | } |
||
| 132 | |||
| 133 | fwrite($fileWriter, $buffer); |
||
| 134 | } |
||
| 135 | } else { // use the normal way - might have a high memory footprint |
||
| 136 | $initializedBackend->put_file($targetPath, $_FILES['attachments']['tmp_name'][$i]); |
||
| 137 | } |
||
| 138 | |||
| 139 | $items[] = array('tmp_name' => $_FILES['attachments']['tmp_name'][$i], 'name' => $_FILES['attachments']['name'][$i]); |
||
| 140 | } |
||
| 141 | echo json_encode(array('success' => true, 'parent' => $dstID, 'items' => $items)); |
||
| 142 | die(); |
||
| 143 | } catch (\Files\Backend\Exception $e) { |
||
| 144 | Logger::error(self::LOG_CONTEXT, "upload failed: " . $e->getMessage()); |
||
| 145 | echo json_encode(array('success' => false, 'response' => $e->getCode(), 'message' => $e->getMessage())); |
||
| 146 | die(); |
||
| 147 | } |
||
| 181 | } |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.