|
34 | 34 | use Illuminate\Contracts\View\Factory; |
35 | 35 | use Illuminate\Http\RedirectResponse; |
36 | 36 | use Illuminate\Http\Request; |
| 37 | +use Illuminate\Http\UploadedFile; |
37 | 38 | use Illuminate\Routing\Redirector; |
38 | 39 | use Illuminate\Support\MessageBag; |
39 | 40 | use Illuminate\View\View; |
@@ -104,78 +105,15 @@ public function upload(Request $request) |
104 | 105 | $flow = $request->cookie(Constants::FLOW_COOKIE); |
105 | 106 | $errors = new MessageBag; |
106 | 107 |
|
107 | | - if (null === $csvFile && 'csv' === $flow) { |
108 | | - $errors->add('csv_file', 'No file was uploaded.'); |
109 | | - |
110 | | - return redirect(route('003-upload.index'))->withErrors($errors); |
111 | | - } |
112 | | - if ('csv' === $flow) { |
113 | | - $errorNumber = $csvFile->getError(); |
114 | | - if (0 !== $errorNumber) { |
115 | | - $errors->add('csv_file', $this->getError($errorNumber)); |
116 | | - } |
117 | | - |
118 | | - |
119 | | - // upload the file to a temp directory and use it from there. |
120 | | - if (0 === $errorNumber) { |
121 | | - $content = file_get_contents($csvFile->getPathname()); |
122 | | - |
123 | | - // https://stackoverflow.com/questions/11066857/detect-eol-type-using-php |
124 | | - // because apparantly there are banks that use "\r" as newline. Looking at the morons of KBC Bank, Belgium. |
125 | | - // This one is for you: 🤦♀️ |
126 | | - $eol = $this->detectEOL($content); |
127 | | - if ("\r" === $eol) { |
128 | | - Log::error('You bank is dumb. Tell them to fix their CSV files.'); |
129 | | - $content = str_replace("\r", "\n", $content); |
130 | | - } |
131 | | - |
132 | | - $csvFileName = StorageService::storeContent($content); |
133 | | - session()->put(Constants::UPLOAD_CSV_FILE, $csvFileName); |
134 | | - session()->put(Constants::HAS_UPLOAD, true); |
135 | | - } |
136 | | - } |
137 | | - |
138 | | - // if present, and no errors, upload the config file and store it in the session. |
139 | | - if (null !== $configFile) { |
140 | | - Log::debug('Config file is present.'); |
141 | | - $errorNumber = $configFile->getError(); |
142 | | - if (0 !== $errorNumber) { |
143 | | - $errors->add('config_file', $errorNumber); |
144 | | - } |
145 | | - // upload the file to a temp directory and use it from there. |
146 | | - if (0 === $errorNumber) { |
147 | | - Log::debug('Config file uploaded.'); |
148 | | - $configFileName = StorageService::storeContent(file_get_contents($configFile->getPathname())); |
| 108 | + // process CSV file (if present) |
| 109 | + $errors = $this->processCsvFile($flow, $errors, $csvFile); |
149 | 110 |
|
150 | | - session()->put(Constants::UPLOAD_CONFIG_FILE, $configFileName); |
| 111 | + // process config file (if present) |
| 112 | + $errors = $this->processConfigFile($errors, $configFile); |
151 | 113 |
|
152 | | - // process the config file |
153 | | - try { |
154 | | - $configuration = ConfigFileProcessor::convertConfigFile($configFileName); |
155 | | - session()->put(Constants::CONFIGURATION, $configuration->toSessionArray()); |
156 | | - } catch (ImporterErrorException $e) { |
157 | | - $errors->add('config_file', $e->getMessage()); |
158 | | - } |
159 | | - } |
160 | | - } |
161 | | - // if no uploaded config file, read and use the submitted existing file, if any. |
162 | | - $existingFile = (string) $request->get('existing_config'); |
| 114 | + // process pre-selected file (if present): |
| 115 | + $errors = $this->processSelection($errors, (string) $request->get('existing_config'), $configFile); |
163 | 116 |
|
164 | | - if (null === $configFile && '' !== $existingFile) { |
165 | | - Log::debug('User selected a config file from the store.'); |
166 | | - $disk = Storage::disk('configurations'); |
167 | | - $configFileName = StorageService::storeContent($disk->get($existingFile)); |
168 | | - |
169 | | - session()->put(Constants::UPLOAD_CONFIG_FILE, $configFileName); |
170 | | - |
171 | | - // process the config file |
172 | | - try { |
173 | | - $configuration = ConfigFileProcessor::convertConfigFile($configFileName); |
174 | | - session()->put(Constants::CONFIGURATION, $configuration->toSessionArray()); |
175 | | - } catch (ImporterErrorException $e) { |
176 | | - $errors->add('config_file', $e->getMessage()); |
177 | | - } |
178 | | - } |
179 | 117 |
|
180 | 118 | if ($errors->count() > 0) { |
181 | 119 | return redirect(route('003-upload.index'))->withErrors($errors); |
@@ -245,4 +183,103 @@ private function detectEOL(string $string): string |
245 | 183 | return $curEol; |
246 | 184 | } |
247 | 185 |
|
| 186 | + /** |
| 187 | + * @return MessageBag |
| 188 | + */ |
| 189 | + private function processCsvFile(string $flow, MessageBag $errors, UploadedFile|null $file): MessageBag |
| 190 | + { |
| 191 | + if (null === $file && 'csv' === $flow) { |
| 192 | + $errors->add('csv_file', 'No file was uploaded.'); |
| 193 | + return $errors; |
| 194 | + } |
| 195 | + if ('csv' === $flow) { |
| 196 | + $errorNumber = $file->getError(); |
| 197 | + if (0 !== $errorNumber) { |
| 198 | + $errors->add('csv_file', $this->getError($errorNumber)); |
| 199 | + } |
| 200 | + |
| 201 | + |
| 202 | + // upload the file to a temp directory and use it from there. |
| 203 | + if (0 === $errorNumber) { |
| 204 | + $content = file_get_contents($file->getPathname()); |
| 205 | + |
| 206 | + // https://stackoverflow.com/questions/11066857/detect-eol-type-using-php |
| 207 | + // because apparantly there are banks that use "\r" as newline. Looking at the morons of KBC Bank, Belgium. |
| 208 | + // This one is for you: 🤦♀️ |
| 209 | + $eol = $this->detectEOL($content); |
| 210 | + if ("\r" === $eol) { |
| 211 | + Log::error('You bank is dumb. Tell them to fix their CSV files.'); |
| 212 | + $content = str_replace("\r", "\n", $content); |
| 213 | + } |
| 214 | + |
| 215 | + $csvFileName = StorageService::storeContent($content); |
| 216 | + session()->put(Constants::UPLOAD_CSV_FILE, $csvFileName); |
| 217 | + session()->put(Constants::HAS_UPLOAD, true); |
| 218 | + } |
| 219 | + } |
| 220 | + return $errors; |
| 221 | + } |
| 222 | + |
| 223 | + /** |
| 224 | + * @param MessageBag $errors |
| 225 | + * @param UploadedFile|null $file |
| 226 | + * @return MessageBag |
| 227 | + * @throws ImporterErrorException |
| 228 | + */ |
| 229 | + private function processConfigFile(MessageBag $errors, UploadedFile|null $file): MessageBag |
| 230 | + { |
| 231 | + if (count($errors) > 0 || null === $file) { |
| 232 | + return $errors; |
| 233 | + } |
| 234 | + Log::debug('Config file is present.'); |
| 235 | + $errorNumber = $file->getError(); |
| 236 | + if (0 !== $errorNumber) { |
| 237 | + $errors->add('config_file', $errorNumber); |
| 238 | + } |
| 239 | + // upload the file to a temp directory and use it from there. |
| 240 | + if (0 === $errorNumber) { |
| 241 | + Log::debug('Config file uploaded.'); |
| 242 | + $configFileName = StorageService::storeContent(file_get_contents($file->getPathname())); |
| 243 | + |
| 244 | + session()->put(Constants::UPLOAD_CONFIG_FILE, $configFileName); |
| 245 | + |
| 246 | + // process the config file |
| 247 | + try { |
| 248 | + $configuration = ConfigFileProcessor::convertConfigFile($configFileName); |
| 249 | + session()->put(Constants::CONFIGURATION, $configuration->toSessionArray()); |
| 250 | + } catch (ImporterErrorException $e) { |
| 251 | + $errors->add('config_file', $e->getMessage()); |
| 252 | + } |
| 253 | + } |
| 254 | + return $errors; |
| 255 | + } |
| 256 | + |
| 257 | + /** |
| 258 | + * @param MessageBag $errors |
| 259 | + * @param string $selection |
| 260 | + * @param UploadedFile|null $file |
| 261 | + * @return MessageBag |
| 262 | + * @throws ImporterErrorException |
| 263 | + * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException |
| 264 | + */ |
| 265 | + private function processSelection(MessageBag $errors, string $selection, UploadedFile|null $file): MessageBag |
| 266 | + { |
| 267 | + if (null === $file && '' !== $selection) { |
| 268 | + Log::debug('User selected a config file from the store.'); |
| 269 | + $disk = Storage::disk('configurations'); |
| 270 | + $configFileName = StorageService::storeContent($disk->get($selection)); |
| 271 | + |
| 272 | + session()->put(Constants::UPLOAD_CONFIG_FILE, $configFileName); |
| 273 | + |
| 274 | + // process the config file |
| 275 | + try { |
| 276 | + $configuration = ConfigFileProcessor::convertConfigFile($configFileName); |
| 277 | + session()->put(Constants::CONFIGURATION, $configuration->toSessionArray()); |
| 278 | + } catch (ImporterErrorException $e) { |
| 279 | + $errors->add('config_file', $e->getMessage()); |
| 280 | + } |
| 281 | + } |
| 282 | + return $errors; |
| 283 | + } |
| 284 | + |
248 | 285 | } |
0 commit comments