Skip to content

Commit

Permalink
Download button
Browse files Browse the repository at this point in the history
  • Loading branch information
jphager2 committed Jan 6, 2018
1 parent 85b99be commit 8dfd7c5
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 3 deletions.
51 changes: 49 additions & 2 deletions src/ChapterList.js
Expand Up @@ -4,12 +4,16 @@ import './ChapterList.css';
class Chapter extends Component {
constructor(props) {
super(props);
this.id = this.props.chapter.id;
this.toggleRead = this.toggleRead.bind(this);
this.download = this.download.bind(this);
this.downloadPoll = this.downloadPoll.bind(this);
this.downloadPollInterval = null;
}

toggleRead(e) {
const chapter = this.props.chapter;
const url = `http://localhost:8999/chapters/${chapter.id}/read`
const url = `http://localhost:8999/chapters/${this.id}/read`;
const button = e.target;

fetch(url, {method: chapter.read ? 'DELETE' : 'POST'})
Expand All @@ -21,6 +25,49 @@ class Chapter extends Component {
})
}

download(e) {
const url = `http://localhost:8999/chapters/${this.id}/download`;
const button = e.target;

if (button.classList.contains('downloading')) { return; }

button.classList.add('downloading');

fetch(url, {method: 'POST'})
.then((res) => {
if (res.status !== 202 && res.status !== 409) {
throw new Error('Failed to download manga');
}
this.downloadPollInterval = window.setInterval(() => this.downloadPoll(button), 1000);
})
.catch((e) => {
console.error(e);
button.classList.remove('downloading');
});
}

downloadPoll(button) {
if (!this.downloadPollInterval) { return; }

fetch(`http://localhost:8999/manga/${this.id}/download`)
.then((res) => {
if (res.status === 409) { return; }

if (res.status !== 200) {
throw new Error('Failed to download manga');
}

button.classList.remove('downloading');
window.location.reload();
})
.catch((e) => {
console.error(e);
button.classList.remove('downloading');
window.clearInterval(this.downloadPollInterval);
this.downloadPollInterval = null;
});
}

render() {
const chapter = this.props.chapter;

Expand All @@ -31,7 +78,7 @@ class Chapter extends Component {
<div className='Chapter-toggle-read button small' onClick={this.toggleRead} >
Mark as {chapter.read ? 'Un' : ''}read
</div>
<div className="Chapter-download button small">Download</div>
<div className="Chapter-download button small" onClick={this.download}>Download</div>
</div>
);
}
Expand Down
41 changes: 40 additions & 1 deletion src/index.server.js
Expand Up @@ -128,7 +128,6 @@ app.post('/manga/:id/update', (req, res) => {
cmd.stdout.on('data', data => out.concat(data));
cmd.stderr.on('data', data => err.concat(data));
cmd.on('close', code => {
fs.writeFile('/home/john/log', `code: ${code}, err: ${err}, out: ${out}, manga: ${JSON.stringify(manga)}, cmd: ${JSON.stringify(cmd)}`, () => {});
if (code !== 0) { console.error(err); }
delete updatingSingleManga[id];
});
Expand All @@ -142,6 +141,46 @@ app.post('/manga/:id/update', (req, res) => {
res.status(status).end();
});

const downloadingSingleChapter = {};

app.get('/manga/:id/download', (req, res) => {
res.status(downloadingSingleChapter[req.params.id] ? 409 : 200).end(JSON.stringify(downloadingSingleChapter));
});

app.post('/chapters/:id/download', (req, res) => {
const id = req.params.id;

if (downloadingSingleChapter[id]) {
status = 409
} else {
status = 202;
downloadingSingleChapter[id] = true;

db('chapters')
.innerJoin('manga', 'chapters.manga_id', 'manga.id')
.select('manga.name AS manga', 'chapters.number')
.where('chapters.id', id)
.then(([{manga, number}]) => {
const cmd = spawn(MY_MANGA_PATH, ['download', manga, `--list=${number}`]);
const out = '';
const err = '';

cmd.stdout.on('data', data => out.concat(data));
cmd.stderr.on('data', data => err.concat(data));
cmd.on('close', code => {
if (code !== 0) { console.error(err); }
delete downloadingSingleChapter[id];
});
})
.catch((e) => {
console.error(e);
delete downloadingSingleChapter[id];
});
}

res.status(status).end();
});

app.post('/chapters/:id/read', (req, res) => {
const id = req.params.id;

Expand Down

0 comments on commit 8dfd7c5

Please sign in to comment.