Add one or multiple files to the zip.
public add ( $pathToAdd, $fileName = null ) | ||
$pathToAdd | array|string An array or string of files and folders to add |
public function testListFiles() { // testing empty file $this->file->shouldReceive('isFile')->with('foo.file')->andReturn(true); $this->file->shouldReceive('isFile')->with('bar.file')->andReturn(true); $this->assertEquals(array(), $this->archive->listFiles()); // testing not empty file $this->archive->add('foo.file'); $this->archive->add('bar.file'); $this->assertEquals(array('foo.file', 'bar.file'), $this->archive->listFiles()); // testing with a empty sub dir $this->file->shouldReceive('isFile')->with('/path/to/subDirEmpty')->andReturn(false); $this->file->shouldReceive('files')->with('/path/to/subDirEmpty')->andReturn(array()); $this->file->shouldReceive('directories')->with('/path/to/subDirEmpty')->andReturn(array()); $this->archive->folder('subDirEmpty')->add('/path/to/subDirEmpty'); $this->assertEquals(array('foo.file', 'bar.file'), $this->archive->listFiles()); // testing with a not empty sub dir $this->file->shouldReceive('isFile')->with('/path/to/subDir')->andReturn(false); $this->file->shouldReceive('isFile')->with('sub.file')->andReturn(true); $this->file->shouldReceive('files')->with('/path/to/subDir')->andReturn(array('sub.file')); $this->file->shouldReceive('directories')->with('/path/to/subDir')->andReturn(array()); $this->archive->folder('subDir')->add('/path/to/subDir'); $this->assertEquals(array('foo.file', 'bar.file', 'subDir/sub.file'), $this->archive->listFiles()); }
/** * Add one or multiple files to the zip. * * @param $pathToAdd array|string An array or string of files and folders to add * @return $this Zipper instance * @static */ public static function add($pathToAdd) { return \Chumper\Zipper\Zipper::add($pathToAdd); }
public function getJust($idCarga) { $carga = Cargas::find($idCarga); if ($carga) { //obtener cargas $empresa = $carga->empresa()->orderBy('tipo_receptor')->get(); $sat = $carga->sat()->orderBy('tipo_receptor')->get(); $diff = new DiferenciasCFID($sat, $empresa, $carga->rfc); $just_emitidos = $diff->get_justificados_emitidos(); $just_recibidos = $diff->get_justificados_recibidos(); $array = ['rfc' => $carga->rfc, 'nombre' => $carga->contribuyente->nombre, 'justEm' => $just_emitidos, 'justRec' => $just_recibidos]; $namePdf = $carga->rfc . "_" . time() . ".pdf"; \PDF::loadView('descargas.reporte-justificados', $array)->save(storage_path('temp') . "/{$namePdf}"); $nameZip = "just_" . time() . ".zip"; $zipper = new Zipper(); $zipper->make(storage_path("temp/{$nameZip}"))->add(storage_path('temp') . "/{$namePdf}"); $anexos = $diff->getAnexos(); foreach ($anexos as $anexo) { $zipper->folder($anexo['uuid']); foreach ($anexo['anexos'] as $file) { $zipper->add($file->file); } } $zipper->close(); return response()->download(storage_path("temp/{$nameZip}"), $nameZip); } }
/** * 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); }
public function add(Zipper $zipper, PathBuilder $pathBuilder, Request $request, BlueprintCollection $blueprintCollection, Filesystem $file, Repository $repository, Collection $collection) { $input = $request->get('collection'); // Check if the blueprint already belongs to this collection $search = $blueprintCollection->where(['collection_id' => $input['id'], 'blueprint_id' => $input['blueprint_id']])->get()->toArray(); if (!empty($search)) { return redirect()->back()->with('blueprintAlreadyInCollection', true); } // Insert the blueprint in the collection $insert = $blueprintCollection->insert(['collection_id' => $input['id'], 'blueprint_id' => $input['blueprint_id']]); if ($insert) { // Create an archive to server to the user rather than creating it when it's downloaded // Get all the collection current archives first $collection = $collection->select('id', 'archive_name', 'name')->where(['id' => $input['id']])->with(['blueprints' => function ($query) { $query->select('archive_name'); }])->first(); $buildcraftArchiveStoragePath = $pathBuilder->create()->fromPublicPath($repository->get('blueprint.paths.storage.buildcraft.archives')); // Delete the previous archive(if any) if (null !== $collection->archive_name) { $file->delete($buildcraftArchiveStoragePath . $collection->archive_name); } // Create a random name for the collection archive $collectionArchiveName = str_replace(' ', '', $collection->name) . '_' . getRandomId() . '_collection' . '.zip'; // Start creating the archive $zipper->make($buildcraftArchiveStoragePath . $collectionArchiveName); foreach ($collection->blueprints as $key => $value) { // Add each archive to the the newly created archive $zipper->add($buildcraftArchiveStoragePath . $value->archive_name); } //Clean up $zipper->close(); // Update the archive name in the collections table $collection->archive_name = $collectionArchiveName; $collection->save(); // Welp, that's that return redirect()->back()->with('blueprintAddedToCollection', true); } // Or not return redirect()->back()->with('blueprintNotAddedToCollection', true); }
public function getJust(Request $request) { try { $contr = $request->cont; $desde = Carbon::createFromFormat('Y-m-d', $request->desde)->startOfDay(); $hasta = Carbon::createFromFormat('Y-m-d', $request->hasta)->endOfDay(); } catch (\Exception $e) { return view('vacio'); } $sat = ArchivoSat::whereBetween('fecha', array($desde, $hasta))->where(function ($q) use($contr) { $q->where('rfc_emisor', '=', $contr)->orWhere('rfc_receptor', '=', $contr); })->get(); $empresa = ArchivoEmpresa::whereBetween('fecha', array($desde, $hasta))->where(function ($q) use($contr) { $q->where('rfc_emisor', '=', $contr)->orWhere('rfc_receptor', '=', $contr); })->get(); $todos = $sat->count() + $empresa->count(); if ($todos == 0) { return view('vacio'); } $diff = new DiferenciasCFID($sat, $empresa, $contr); $just_emitidos = $diff->get_justificados_emitidos(); $just_recibidos = $diff->get_justificados_recibidos(); $contr = Contribuyente::where('rfc', '=', $contr)->first(); $array = ['nombre' => $contr->nombre, 'rfc' => $contr->rfc, 'justEm' => $just_emitidos, 'justRec' => $just_recibidos]; $namePdf = $contr->rfc . "_" . time() . ".pdf"; \PDF::loadView('descargas.reporte-justificados', $array)->save(storage_path('temp') . "/{$namePdf}"); $nameZip = "just_" . time() . ".zip"; $zipper = new Zipper(); $zipper->make(storage_path("temp/{$nameZip}"))->add(storage_path('temp') . "/{$namePdf}"); $anexos = $diff->getAnexos(); foreach ($anexos as $anexo) { $zipper->folder($anexo['uuid']); foreach ($anexo['anexos'] as $file) { $zipper->add($file->file); } } $zipper->close(); return response()->download(storage_path("temp/{$nameZip}"), $nameZip); }