Laravel下载并解压档案

Laravel download and extract archives

提问人:LaravelLover 提问时间:6/25/2023 更新时间:6/25/2023 访问量:59

问:

有没有办法缩短或更轻松地保存和解压缩 laravel 中的存档,如下所示?

    public function upload(Request $request, string $slug)
    {
        $request->validate([
            "zipfile" => 'required'
        ]);

        $game = Game::where('slug', $slug)->first();

        if ($request->user()->id != $game->user_id) {
            return response([
                "status" => "invalid",
                "message" => "User is not author of the game"
            ], 400);
        }

        $version = Version::create([
            "game_id" => $game->id,
            "version" => now(),
            "path" => "/$game->slug/temp/index.html",
        ]);

        $dir_path = $game->slug . '/';
        $file = $request->file('zipfile');
        $zip = new ZipArchive();
        $file_new_path = $file->storeAs($dir_path . $version->id, 'zipname', 'public');
        $zipFile = $zip->open(Storage::disk('public')->path($file_new_path));
        if ($zipFile === TRUE) {
            $zip->extractTo(Storage::disk('public')->path($dir_path . $version->id));
            $zip->close();
        } else {
            return response([
                "status" => "invalid",
                "message" => "ZIP file extraction fails"
            ], 400);
        }
        $url = Storage::url($dir_path . $version->id);

        if (file_exists(Storage::disk('public')->path($dir_path . $version->id . "/thumbnail.png"))) {
            $game->thumbnail = url($url . "/thumbnail.png");
            $game->save();
        }
        $version->path = url($url . "/index.html");
        $version->save();
        return response($version);
    }

这段代码很有效,但它看起来很大,我想让它更小

php laravel zip laravel-8 ziparchive

评论


答:

0赞 didene tahi 6/25/2023 #1
public function upload(Request $request, string $slug)
{
    $request->validate([
        "zipfile" => 'required'
    ]);

    $game = Game::where('slug', $slug)->first();

    if ($request->user()->id != $game->user_id) {
        return response([
            "status" => "invalid",
            "message" => "User is not the author of the game"
        ], 400);
    }

    $version = Version::create([
        "game_id" => $game->id,
        "version" => now(),
        "path" => "/$game->slug/temp/index.html",
    ]);

    $dir_path = $game->slug . '/';
    $file = $request->file('zipfile');
    $file_new_path = $file->storeAs($dir_path . $version->id, 'zipname', 'public');
    $zipFile = new \ZipArchive();
    $zipFile->open(Storage::disk('public')->path($file_new_path));

    if ($zipFile->extractTo(Storage::disk('public')->path($dir_path . $version->id))) {
        $zipFile->close();

        $url = Storage::url($dir_path . $version->id);

        $thumbnailPath = $dir_path . $version->id . "/thumbnail.png";
        if (Storage::disk('public')->exists($thumbnailPath)) {
            $game->thumbnail = url($url . "/thumbnail.png");
            $game->save();
        }

        $version->path = url($url . "/index.html");
        $version->save();

        return response($version);
    } else {
        return response([
            "status" => "invalid",
            "message" => "ZIP file extraction fails"
        ], 400);
    }
}

所以我做了这个改变:

  • 删除了 $zipFile->close() 之后的 else 条件。由于 提取是在 if 语句中完成的,您可以假设它是 如果代码执行达到该点,则成功。
  • 利用存储::d isk('public')->exists($thumbnailPath) 检查是否 缩略图文件代替 file_exists() 存在。
  • 重新组织了代码以提高可读性。

通过应用这些更改,可以使代码更简洁、更易于理解。