/** * Imports board attachments. * * @return void */ public function importInfinityBoardAssets() { $this->info("\tImporting board assets ..."); BoardAsset::whereDoesntHave('flagPosts')->forceDelete(); Board::orderBy('board_uri', 'asc')->chunk(1, function ($boards) { foreach ($boards as $board) { $this->line("\t\tImporting assets from /{$board->board_uri}/"); $flagsMade = 0; $bannersMade = 0; # FLAGS $flagsPath = "{$this->targetLocation}/static/custom-flags/{$board->board_uri}/"; $flagSerPath = "{$this->targetLocation}/{$board->board_uri}/flags.ser"; $flags = []; if (file_exists($flagSerPath)) { try { $flags = @unserialize(@file_get_contents("{$this->targetLocation}/{$board->board_uri}/flags.ser")); } catch (\Exception $e) { $this->warn("\t\t\tFailed to unserialize flags.ser"); } if (is_array($flags) && count($flags)) { foreach ($flags as $flagFile => $flagName) { $flag = new File("{$flagsPath}{$flagFile}.png", false); if ($flag->isReadable()) { $storage = FileStorage::storeUpload($flag); $asset = $board->assets()->create(['file_id' => $storage->file_id, 'asset_type' => "board_flags", 'asset_name' => $flagName]); ++$flagsMade; } } } } # BANNERS $bannersPath = "{$this->targetLocation}/static/banners/{$board->board_uri}/"; if (is_readable($bannersPath)) { $banners = array_filter(scandir($bannersPath), function ($item) use($bannersPath) { return !is_dir("{$bannersPath}{$item}"); }); foreach ($banners as $bannerName) { $banner = new File("{$bannersPath}{$bannerName}", false); if ($banner->isReadable() && !!FileFacade::get($banner)) { $storage = FileStorage::storeUpload($banner); $asset = $board->assets()->create(['file_id' => $storage->file_id, 'asset_type' => "board_banner", 'asset_name' => null]); ++$bannersMade; } } } $this->line("\t\tImported {$flagsMade} flags and {$bannersMade} banners."); } }); }
/** * Add new assets. * * @return Response */ public function putAssets(Request $request, Board $board) { if (!$board->canEditConfig($this->user)) { return abort(403); } if (!!Input::get('delete', false)) { return $this->deleteAssets($request, $board); } $input = Input::all(); $assetType = Input::get('asset_type', false); $validator = Validator::make($input, ['asset_type' => ["required", "in:board_banner,board_banned,board_icon,file_deleted,file_spoiler"], 'new_board_banned' => ["required_if:asset_type,board_banned", "image", "image_size:100-500", "max:250"], 'new_board_banner' => ["required_if:asset_type,board_banner", "image", "image_size:<=300,<=100", "max:1024"], 'new_board_icon' => ["required_if:asset_type,board_icon", "image", "image_aspect:1", "image_size:64,64", "max:50"], 'new_file_deleted' => ["required_if:asset_type,file_deleted", "image", "image_size:100-500", "max:250"], 'new_file_spoiler' => ["required_if:asset_type,file_spoiler", "image", "image_size:100-500", "max:250"]]); if (!$validator->passes()) { return redirect()->back()->withErrors($validator->errors()); } // Fetch the asset. $upload = Input::file("new_{$input['asset_type']}"); $multiples = $assetType == "board_banner" || $assetType == "board_banned"; if (file_exists($upload->getPathname())) { $storage = FileStorage::storeUpload($upload); if ($storage->exists) { if (!$multiples) { $assets = $board->assets()->with('storage')->where('asset_type', $input['asset_type'])->get(); foreach ($assets as $asset) { $asset->delete(); $asset->storage->challengeExistence(); } } $asset = new BoardAsset(); $asset->asset_type = $input['asset_type']; $asset->board_uri = $board->board_uri; $asset->file_id = $storage->file_id; $asset->save(); } else { return redirect()->back()->withErrors(["validation.custom.file_generic"]); } } Event::fire(new BoardWasModified($board)); return $this->getAssets($board); }
/** * Display existing assets. * * @return Response */ public function putAssets(Request $request, Board $board) { if (!$board->canEditConfig($this->user)) { return abort(403); } $input = Input::all(); $validator = Validator::make($input, ['asset_type' => ["required", "in:board_banner,file_deleted,file_none,file_spoiler"], 'new_board_banner' => ["required_if:asset_type,board_banner", "image", "image_size:<=300,<=100"]]); if (!$validator->passes()) { return redirect()->back()->withErrors($validator->errors()); } // Fetch the asset. $upload = Input::file("new_{$input['asset_type']}"); if (file_exists($upload->getPathname())) { $storage = FileStorage::storeUpload($upload); $asset = new BoardAsset(); $asset->asset_type = "board_banner"; $asset->board_uri = $board->board_uri; $asset->file_id = $storage->file_id; $asset->save(); } return $this->view(static::VIEW_CONFIG, ['board' => $board, 'banners' => $board->getBanners(), 'tab' => "assets"]); }
/** * Add new assets. * * @return Response */ public function putAssets(Request $request, Board $board) { if (!$board->canEditConfig($this->user)) { return abort(403); } if (!!Input::get('delete', false)) { return $this->deleteAssets($request, $board); } $input = Input::all(); $assetType = Input::get('asset_type', false); $validator = Validator::make($input, ['asset_type' => ["required", "in:board_banner,board_banned,board_icon,board_flags,file_deleted,file_spoiler"], 'new_board_banned' => ["required_if:asset_type,board_banned", "image", "image_size:100-500", "max:250"], 'new_board_banner' => ["required_if:asset_type,board_banner", "image", "image_size:<=300,<=100", "max:1024"], 'new_board_flags' => ["required_if:asset_type,board_flags", "array", "min:1", "max:500"], 'new_board_icon' => ["required_if:asset_type,board_icon", "image", "image_aspect:1", "image_size:64,64", "max:50"], 'new_file_deleted' => ["required_if:asset_type,file_deleted", "image", "image_size:100-500", "max:250"], 'new_file_spoiler' => ["required_if:asset_type,file_spoiler", "image", "image_size:100-500", "max:250"]]); if (!$validator->passes()) { return redirect()->back()->withErrors($validator->errors()); } // Fetch the asset. $multiples = $assetType == "board_banner" || $assetType == "board_banned" || $assetType == "board_flags"; if ($assetType == "board_flags") { $new = $input["new_{$input['asset_type']}"]; $names = isset($new['name']) ? $new['name'] : []; $uploads = isset($new['file']) ? $new['file'] : []; $rules = []; $nameRules = ["required", "string", "between:1,128"]; $imageRules = array_merge(["required"], BoardAsset::getRulesForFlags($board)); foreach (range(0, count($uploads) - 1) as $index) { $rules["name.{$index}"] = $nameRules; $rules["file.{$index}"] = $imageRules; } $validator = Validator::make($new, $rules); if (!$validator->passes()) { return redirect()->back()->withErrors($validator->errors()); } } else { $uploads = [Input::file("new_{$input['asset_type']}")]; } foreach ((array) $uploads as $index => $upload) { if (file_exists($upload->getPathname())) { $storage = FileStorage::storeUpload($upload); if ($storage->exists) { if (!$multiples) { $assets = $board->assets()->with('storage')->where('asset_type', $input['asset_type'])->get(); foreach ($assets as $asset) { $asset->delete(); $asset->storage->challengeExistence(); } } $asset = new BoardAsset(); $asset->asset_type = $input['asset_type']; $asset->asset_name = isset($names[$index]) ? $names[$index] : null; $asset->board_uri = $board->board_uri; $asset->file_id = $storage->file_id; $asset->save(); } else { return redirect()->back()->withErrors(["validation.custom.file_generic"]); } } } Event::fire(new BoardWasModified($board)); return $this->getAssets($board); }