/**
  * Sweet baby jesus, I know I know!
  *
  * Used to upload a blueprint and do the necessary
  * checks to ensure that the upload is valid
  *
  * @param NbtParser         $parser
  * @param ImageManipulate   $manipulator
  * @param Zipper            $zipper
  * @param PathBuilder       $pathBuilder
  * @param PostUploadRequest $request
  * @param Blueprint         $blueprint
  * @param BlueprintModsUsed $blueprintModsUsed
  * @param Filesystem        $file
  * @param Guard             $guard
  * @param Repository        $repository
  *
  * @return \Illuminate\Http\RedirectResponse
  */
 public function postUpload(NbtParser $parser, ImageManipulate $manipulator, Zipper $zipper, PathBuilder $pathBuilder, PostUploadRequest $request, Blueprint $blueprint, BlueprintModsUsed $blueprintModsUsed, Filesystem $file, Guard $guard, Repository $repository)
 {
     $input = $request->get('upload');
     // Make sure that the blueprint is valid
     // ...well sort of, it can't be checked for sure
     $parser->loadFile($request->file('upload.blueprint'));
     if (!array_key_exists(0, $parser->root) || empty($parser->root) || !array_key_exists('type', $parser->root[0])) {
         return redirect()->back()->with('blueprintNotValid', true)->withInput();
     }
     // Arrays to be inserted into the database
     // Blueprint things
     $blueprintInsert = [];
     // Process the blueprint
     foreach ($parser->root[0]['value'] as $key => $value) {
         $value['name'] != 'version' ?: ($blueprintInsert['bc_version'] = $value['value']);
         $value['name'] != 'sizeY' ?: ($blueprintInsert['sizeY'] = $value['value']);
         $value['name'] != 'sizeZ' ?: ($blueprintInsert['sizeZ'] = $value['value']);
         $value['name'] != 'sizeX' ?: ($blueprintInsert['sizeX'] = $value['value']);
     }
     // Set some fields that are easy to set, just to get it out of the way
     $blueprintInsert['name'] = trim($input['name']);
     $blueprintInsert['version'] = trim($input['version']);
     $blueprintInsert['author_name'] = trim($input['author_name']);
     $blueprintInsert['survival_creative'] = $input['play_mode'];
     $blueprintInsert['description'] = trim($input['description']);
     $blueprintInsert['user_id'] = $guard->user()->getAuthIdentifier();
     $blueprintInsert['uploaded_at'] = Carbon::now()->toDateTimeString();
     $blueprintInsert['archive_name'] = str_replace(' ', '', $input['name']) . '_' . getRandomId() . '.zip';
     // Get the buildcraft image storage path
     $buildcraftImagesStoragePath = $pathBuilder->create()->fromPublicPath($repository->get('blueprint.paths.storage.buildcraft.images'));
     // Get the buildcraft archives storage path
     $buildcraftArchivesStoragePath = $pathBuilder->create()->fromPublicPath($repository->get('blueprint.paths.storage.buildcraft.archives'));
     // Get the buildcraft temp storage path
     $buildcraftTempStoragePath = $pathBuilder->create()->fromPublicPath($repository->get('blueprint.paths.storage.buildcraft.temp'));
     // Compress and move the required screenshot
     $manipulator->setFileInfo($request->file('upload.required_screenshot'))->compress()->move($buildcraftImagesStoragePath)->createThumb(500)->exitClean();
     //Might as well add it to the insert array
     $blueprintInsert['screenshot'] = $manipulator->getFileName();
     // Get a temporary name for the blueprint
     $temporary_name = getRandomId() . '.' . $request->file('upload.blueprint')->getClientOriginalExtension();
     // Move the blueprint to a temporary folder in order to archive it.
     $request->file('upload.blueprint')->move($buildcraftTempStoragePath, $temporary_name);
     // Add blueprint to the archive
     $zipper->make($buildcraftArchivesStoragePath . $blueprintInsert['archive_name'])->add($buildcraftTempStoragePath . $temporary_name)->add($buildcraftImagesStoragePath . $blueprintInsert['screenshot']);
     // Compress and move the first optional screenshot
     // Get the file name, create a thumb
     // Also add the file to the archive
     if (null !== $request->file('upload.optional_screenshot_0')) {
         $manipulator->setFileInfo($request->file('upload.optional_screenshot_0'))->compress()->move($buildcraftImagesStoragePath)->createThumb(500)->exitClean();
         $blueprintInsert['screenshot_optional_0'] = $manipulator->getFileName();
         $zipper->add($buildcraftImagesStoragePath . $blueprintInsert['screenshot_optional_0']);
     } else {
         $blueprintInsert['screenshot_optional_0'] = null;
     }
     // Compress and move the second optional screenshot
     // Get the file name, create a thumb
     // Also add the file to the archive
     if (null !== $request->file('upload.optional_screenshot_1')) {
         $manipulator->setFileInfo($request->file('upload.optional_screenshot_1'))->compress()->move($buildcraftImagesStoragePath)->createThumb(500)->exitClean();
         $blueprintInsert['screenshot_optional_1'] = $manipulator->getFileName();
         $zipper->add($buildcraftImagesStoragePath . $blueprintInsert['screenshot_optional_1']);
     } else {
         $blueprintInsert['screenshot_optional_1'] = null;
     }
     //Clean up
     $zipper->close();
     if ($file->exists($buildcraftTempStoragePath . $temporary_name)) {
         $file->delete($buildcraftTempStoragePath . $temporary_name);
     }
     // Insert the blueprint data
     $blueprint->insert($blueprintInsert);
     // Get the last inserted id
     $blueprint_id = $blueprint->id;
     $modsUsedInsert = [];
     // Get a list of the mods used
     $mods_used = $this->getModsUsed($parser->root[0]);
     if (null !== $mods_used) {
         // Make a valid array to insert
         foreach ($mods_used as $key => $value) {
             $modsUsedInsert[] = ['blueprint_id' => $blueprint_id, 'mod_name' => $value];
         }
         // Insert into mods used too
         $blueprintModsUsed->insert($modsUsedInsert);
     }
     // Welp, that's that
     return redirect()->route('buildcraft::user::getUpload', ['user_id' => Auth::user()->id])->with('blueprintUploadSuccess', true);
 }