added album zipping

This commit is contained in:
Jill 2021-10-20 20:17:28 +03:00
parent bef741fce2
commit ab297b48fc
3 changed files with 45 additions and 7 deletions

View File

@ -20,7 +20,9 @@ it's intended use is for small groups of people to self-host, and as such there'
3. `npm install`
4. (optionally) put the service on pm2 like such: `pm2 start src/index.js --name deemix-web-frontend` (or just run it with `node src/index.js`)
4. install the `zip` linux tool into your path (there are currently no plans for windows support, however feel free to contribute)
5. (optionally) put the service on pm2 like such: `pm2 start src/index.js --name deemix-web-frontend` (or just run it with `node src/index.js`)
### nginx addenum

View File

@ -79,7 +79,21 @@ function startDownload(id, isAlbum) {
log = addlog(log, 'Download finished');
}
document.getElementById('progress-album').innerHTML = `<div class="album album-downloading" id="album-${id}"><span class="album-image-wrapper"><img class="album-image" width="128" height="128" src="${coverArt}"></span><span class="big">${title || ''}</span><br><span class="small">by ${artist || ''}</span><br><div id="progress-state">${log || ''}</div></div>`;
document.getElementById('progress-album').innerHTML = `
<div class="album album-downloading" id="album-${id}">
<div class="album-metadata">
<span class="metadata">
<span class="big">${title || ''}</span>
<br>
<span class="small">by ${artist || ''}</span>
</span>
<div id="progress-state">${log || ''}</div>
</div>
<div class="album-image-wrapper">
<img class="album-image" width="128" height="128" src="${coverArt}">
</div>
</div>
`;
}
ws.onerror = (e) => {
console.log('error: ' + e);
@ -176,9 +190,9 @@ window.onload = () => {
if (d.data.length === 0) return document.getElementById('albums').innerHTML = '<span class="small">Not found!</span>';
for (c of document.getElementById('albums').children) {
if (c.children[5]) {
if (c.children[0] && c.children[0].children[1]) {
let id = c.id.split('-')[1];
c.children[5].onclick = () => {
c.children[0].children[1].onclick = () => {
clearError();
startDownload(id, true);
}

View File

@ -2,9 +2,10 @@ const express = require('express');
const deezer = require('deezer-js');
const deemix = require('deemix');
const path = require('path');
const { inspect } = require('util');
const { inspect, promisify } = require('util');
const ws = require('ws');
const fs = require('fs');
const { exec } = require('child_process');
const port = process.env.PORT || 4500;
@ -83,11 +84,14 @@ app.get('/api/album', async (req, res) => {
app.ws('/api/album', async (ws, req) => {
if (!req.query.id) return ws.close(1008, 'Supply a track ID in the query!');
let trackpaths = [];
const listener = {
send(key, data) {
if (data.downloaded) {
ws.send(JSON.stringify({key: 'download', data: data.downloadPath.replace(process.cwd(), '')}));
console.log('downloaded ' + data.downloadPath + ', deleting in 1hr')
// ws.send(JSON.stringify({key: 'download', data: data.downloadPath.replace(process.cwd(), '')}));
trackpaths.push(data.downloadPath);
console.log('downloaded ' + data.downloadPath + ', deleting in 1hr');
setTimeout(() => {
try {
fs.unlinkSync(data.downloadPath);
@ -117,6 +121,24 @@ app.ws('/api/album', async (ws, req) => {
await deemixDownloader.start();
const folderName = trackpaths[0].split('/').slice(-2)[0];
try {
await promisify(exec)(`zip -0rD "data/${folderName}.zip" "data/${folderName}"`);
} catch(err) {
return ws.close(1006, 'Zipping album failed');
}
await ws.send(JSON.stringify({key: 'download', data: `data/${folderName}.zip`}));
console.log('zipped up data/' + folderName + '.zip, deleting in 1hr');
setTimeout(() => {
try {
fs.unlinkSync('./data/' + folderName + '.zip');
} catch(err) {
console.log('tried to delete ' + folderName + '.zip, but failed? its likely already gone');
}
}, 1000 * 60 * 60 /* 1 hour */);
ws.close(1000);
});