feat: download of specific mods

This commit is contained in:
Kentai Radiquum 2025-05-07 00:05:42 +05:00
parent 56569917c1
commit e44a961faa
Signed by: Radiquum
GPG key ID: 858E8EE696525EED
3 changed files with 107 additions and 10 deletions

View file

@ -18,6 +18,7 @@ export const ModTable = (props: {
mods: Mod[]; mods: Mod[];
updatePack: () => void; updatePack: () => void;
packID: string; packID: string;
downloadMods: (mods: string[]) => void;
}) => { }) => {
const [selectedMods, setSelectedMods] = useState<string[]>([]); const [selectedMods, setSelectedMods] = useState<string[]>([]);
@ -130,7 +131,10 @@ export const ModTable = (props: {
<TableCell>{mod.url}</TableCell> <TableCell>{mod.url}</TableCell>
<TableCell> <TableCell>
<div className="flex gap-2"> <div className="flex gap-2">
<Button size="sm"> <Button
size="sm"
onClick={() => props.downloadMods([mod.slug])}
>
Download <HiDownload className="ml-2 h-4 w-4" /> Download <HiDownload className="ml-2 h-4 w-4" />
</Button> </Button>
<Button <Button
@ -154,14 +158,23 @@ export const ModTable = (props: {
<TableCell></TableCell> <TableCell></TableCell>
<TableCell></TableCell> <TableCell></TableCell>
<TableCell> <TableCell>
<Button <div className="flex gap-2">
color={"red"} <Button
size="sm" size="sm"
disabled={selectedMods.length == 0} disabled={selectedMods.length == 0}
onClick={() => deleteSelectedMods()} onClick={() => props.downloadMods(selectedMods)}
> >
Delete Selected <HiTrash className="ml-2 h-4 w-4" /> Download Selected <HiDownload className="ml-2 h-4 w-4" />
</Button> </Button>
<Button
color={"red"}
size="sm"
disabled={selectedMods.length == 0}
onClick={() => deleteSelectedMods()}
>
Delete Selected <HiTrash className="ml-2 h-4 w-4" />
</Button>
</div>
</TableCell> </TableCell>
</TableRow> </TableRow>
</TableBody> </TableBody>

View file

@ -281,6 +281,24 @@ export default function PackPage() {
setdownloadModalOpen(true); setdownloadModalOpen(true);
} }
async function downloadMods(mods: string[]) {
if (!packData) return;
fetch(`${DOWNLOAD_ENDPOINT["downloadMods"]}`, {
method: "POST",
body: JSON.stringify({
pack_id: packData._id,
mods: mods,
}),
headers: {
"content-type": "application/json",
accept: "application/json",
},
});
setdownloadModalOpen(true);
}
return ( return (
<div> <div>
{packDataLoading && ( {packDataLoading && (
@ -342,6 +360,7 @@ export default function PackPage() {
<ModTable <ModTable
mods={packData.mods} mods={packData.mods}
updatePack={_getPacksData} updatePack={_getPacksData}
downloadMods={downloadMods}
packID={id} packID={id}
/> />
</div> </div>
@ -386,7 +405,15 @@ export default function PackPage() {
<Button onClick={() => addMod()}>Save</Button> <Button onClick={() => addMod()}>Save</Button>
</ModalFooter> </ModalFooter>
</Modal> </Modal>
<Modal show={downloadModalOpen}> <Modal
show={downloadModalOpen}
dismissible={downloadProgressFile.current == downloadProgressFile.total}
onClose={() => {
if (downloadProgressFile.current == downloadProgressFile.total) {
setdownloadModalOpen(false);
}
}}
>
<ModalHeader>Download progress</ModalHeader> <ModalHeader>Download progress</ModalHeader>
<ModalBody> <ModalBody>
<div className="mb-4"> <div className="mb-4">

View file

@ -108,3 +108,60 @@ def downloadPack():
"message": f"download of {pack_id} with {total} mods finished", "message": f"download of {pack_id} with {total} mods finished",
}, },
) )
@apiDownload.route("/mods", methods=["POST"])
def downloadMods():
pack = {}
pack_id = request.json.get("pack_id")
mods_slugs = request.json.get("mods")
with open(f"{PACKS_FOLDER}/{pack_id}/packfile.json") as fp:
pack = json.load(fp)
fp.close()
mods = pack.get("mods", [])
total = len(mods_slugs)
os.makedirs(f"{PACKS_FOLDER}/{pack_id}/mods", exist_ok=True)
for i, slug in enumerate(mods_slugs):
for mod in mods:
if mod.get("slug") == slug:
emit(
"download_total",
{
"status": "ok",
"total": total,
"current": i,
"title": mod.get("title"),
"filename": mod.get("file").get("filename"),
},
namespace="/",
broadcast=True,
)
download(
f"{PACKS_FOLDER}/{pack_id}/mods",
mod.get("file").get("url"),
mod.get("file").get("filename"),
mod.get("file").get("size"),
)
emit(
"download_total",
{
"status": "ok",
"total": total,
"current": total,
"title": "",
"filename": "",
},
namespace="/",
broadcast=True,
)
return jsonify(
{
"status": "ok",
"message": f"download of {pack_id} with {total} mods finished",
},
)