Skip to content

Commit

Permalink
v1.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
froquede committed Sep 13, 2022
1 parent dc5eb89 commit 6a1443e
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 28 deletions.
65 changes: 42 additions & 23 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ module.exports = (app_path) => {

last_maps = {};

function listMaps(filter = "mtimeMs", sorting = "desc") {
function listMaps(filter = "mtimeMs", sorting = "desc", custom_path = maps_path) {
return new Promise((resolve, reject) => {
glob(maps_path + "**/*", { dot: true }, async (err, files) => {
glob(custom_path + "**/*", { dot: true }, async (err, files) => {
if(!err) {
let maps = {};
for(file of files) {
if(path.extname(file) == "") {
const stat = await fs.promises.lstat(path.resolve(maps_path, file));
let name = file.toLowerCase().split(maps_path.toLowerCase()).join("");
const stat = await fs.promises.lstat(path.resolve(custom_path, file));
let name = file.toLowerCase().split(custom_path.toLowerCase()).join("");
if(stat.isFile()) maps[name] = { file, name, ...stat };
}
if(path.extname(file) == ".jpg" || path.extname(file) == ".png") {
let name = file.toLowerCase().split(".jpg").join("").split(".png").join("");
name = name.split(maps_path.toLowerCase()).join("");
name = name.split(custom_path.toLowerCase()).join("");
if(maps[name]) {
maps[name].image = file;
}
Expand Down Expand Up @@ -71,22 +71,23 @@ const DecompressZip = require('decompress-zip');
let download_queue = [];
let queue_running = false;

function addToDownloadQueue(id, token) {
function addToDownloadQueue(id, token, custom_path = maps_path) {
request(`https://api.mod.io/v1/games/629/mods/${id}`, {headers: {Authorization: "Bearer " + token}}, (err, res, body) => {
try {
body = JSON.parse(body);
} catch(err) {
console.log(err);
}

if(!err && res.statusCode == 200) {
download_queue.push(body.modfile);
if(!queue_running) runQueue();
}
else {
reject(body);
}
});
try {
body = JSON.parse(body);
} catch(err) {
console.log(err);
}

if(!err && res.statusCode == 200) {
body.modfile.custom_path = custom_path;
download_queue.push(body.modfile);
if(!queue_running) runQueue();
}
else {
reject(body);
}
});
}

function runQueue() {
Expand Down Expand Up @@ -146,7 +147,7 @@ function decompress(file) {
io.emit("extracting-download", { percentage: ((fileIndex + 1) / fileCount) * 100, id });
});

unzipper.extract({path: maps_path, restrict: false});
unzipper.extract({path: file.custom_path, restrict: false});
}

function deleteFile(path, cb) {
Expand Down Expand Up @@ -187,7 +188,7 @@ app.get('/modio/maps', (req, res) => {
});

app.get('/local/maps', (req, res) => {
listMaps(req.query.filter, req.query.sorting).then(maps => {
listMaps(req.query.filter, req.query.sorting, req.query.custom_path).then(maps => {
res.send(maps);
}).catch(err => {
res.status(500).send(err);
Expand All @@ -211,7 +212,7 @@ app.get('/internal/open', (req, res) => {

app.post('/modio/download', (req, res) => {
if(req.body.id) {
addToDownloadQueue(req.body.id, req.body.token);
addToDownloadQueue(req.body.id, req.body.token, req.body.custom_path);
res.status(200).send();
}
else {
Expand Down Expand Up @@ -263,6 +264,24 @@ function sendImg(req, res) {
}
}

const { dialog } = require('electron');
function askPath() {
return new Promise((resolve, reject) => {
dialog.showOpenDialog({properties: ['openDirectory']}).then(folder => {
if(!folder.canceled) {
resolve(folder.filePaths[0]);
}
else {
reject();
}
});
})
}

app.get('/internal/path', (req, res) => {
askPath().then(path => res.status(200).send({path: (path + '\\').split("\\").join("/")})).catch(() => {res.status(444).send({})});
});

console.log(path.resolve(app_path, './webapp'));
app.use(express.static(path.resolve(app_path, './webapp')));

Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "MAPhub",
"keywords": [],
"main": "./main.js",
"version": "1.0.0",
"version": "1.0.1",
"author": "froquede",
"scripts": {
"start": "electron-forge start",
Expand Down Expand Up @@ -38,7 +38,8 @@
{
"name": "@electron-forge/maker-squirrel",
"config": {
"name": "map_manager"
"name": "MAPhub",
"version": "1.0.1"
}
},
{
Expand Down
25 changes: 24 additions & 1 deletion webapp/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,18 @@
height: 14px;
cursor: pointer;
}

.options {
opacity: .4;
-webkit-app-region: no-drag;
transition: .2s ease all;
}

.options:hover {
opacity: 1;
cursor: pointer;
transition: .2s ease all;
}
</style>
<script src="/latinize.js"></script>
<script src="/listeners.js"></script>
Expand All @@ -329,7 +341,8 @@
</div>
</div>
<div class="left">
<p class="version">v1.0.0</p>
<p class="options js-change-folder">change maps folder</p>
<p class="version">v1.0.1</p>
<img class="close js-close" src="close.svg"/>
</div>
</div>
Expand Down Expand Up @@ -390,5 +403,15 @@ <h2>Local maps</h2>
close.addEventListener("click", () => {
fetch("/internal/minimize",() =>{});
});

let change = document.querySelector(".js-change-folder");
change.addEventListener("click", () => {
fetch("/internal/path").then(res => res.json()).then(path => {
if(path.path) {
localStorage.setItem("custom-path", path.path);
window.location.reload();
}
});
});
</script>
</html>
3 changes: 2 additions & 1 deletion webapp/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@ function getModio(page = 0, refresh) {

function getLocal(refresh = false) {
document.querySelector(".local-container").classList.add("loading");
window.fetch("/local/maps?filter=" + local_filter.filter + "&sorting=" + local_filter.sorting).then(res => res.json()).then(data => {
let custom_path = localStorage.getItem("custom-path");
window.fetch("/local/maps?filter=" + local_filter.filter + "&sorting=" + local_filter.sorting + (custom_path ? "&custom_path=" + custom_path : "")).then(res => res.json()).then(data => {
let result = [];
for(let key in data) {
let map = data[key];
Expand Down
2 changes: 1 addition & 1 deletion webapp/map-card.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ export class mapCard extends LitElement {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({id, token: localStorage.getItem("modio-token")})
body: JSON.stringify({id, token: localStorage.getItem("modio-token"), custom_path: localStorage.getItem("custom-path")})
}).then(res => {
console.dir(res);
if(res.status == 200) {
Expand Down

0 comments on commit 6a1443e

Please sign in to comment.