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

View file

@ -281,6 +281,24 @@ export default function PackPage() {
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 (
<div>
{packDataLoading && (
@ -342,6 +360,7 @@ export default function PackPage() {
<ModTable
mods={packData.mods}
updatePack={_getPacksData}
downloadMods={downloadMods}
packID={id}
/>
</div>
@ -386,7 +405,15 @@ export default function PackPage() {
<Button onClick={() => addMod()}>Save</Button>
</ModalFooter>
</Modal>
<Modal show={downloadModalOpen}>
<Modal
show={downloadModalOpen}
dismissible={downloadProgressFile.current == downloadProgressFile.total}
onClose={() => {
if (downloadProgressFile.current == downloadProgressFile.total) {
setdownloadModalOpen(false);
}
}}
>
<ModalHeader>Download progress</ModalHeader>
<ModalBody>
<div className="mb-4">

View file

@ -108,3 +108,60 @@ def downloadPack():
"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",
},
)