Skip to content

Commit fabbf0d

Browse files
committed
Allow to download audio without size specified
1 parent d6e4158 commit fabbf0d

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

src/source/download.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ export default React.createClass({
5555
download() {
5656
this.setState({vdata: 0, adata: 0, downloadingError: null});
5757
const format = this.props.format;
58+
5859
// vp8.0 format lacks file size info.
5960
const videoHasSize = !!format.video.filesize;
60-
6161
// TODO(Kagami): How to handle file write errors?
6262
let vpromise = new Promise((resolve, reject) => {
6363
this.vreq = request({
@@ -96,6 +96,7 @@ export default React.createClass({
9696

9797
let apromise;
9898
if (this.apath) {
99+
const audioHasSize = !!format.audio.filesize;
99100
apromise = new Promise((resolve, reject) => {
100101
this.areq = request({
101102
url: format.audio.url,
@@ -106,13 +107,17 @@ export default React.createClass({
106107
`Got ${res.statusCode} error while downloading audio`
107108
));
108109
}
110+
if (!audioHasSize) {
111+
const reslen = +res.headers["content-length"];
112+
if (reslen) format.audio.filesize = reslen;
113+
}
109114
res.on("error", err => {
110115
reject(err);
111116
}).on("data", chunk => {
112117
this.setState({adata: this.state.adata + chunk.length});
113118
}).on("end", () => {
114119
if (!this.areq) return;
115-
if (this.state.adata !== format.audio.filesize) {
120+
if (audioHasSize && this.state.adata !== format.audio.filesize) {
116121
return reject(new Error("Got wrong audio data"));
117122
}
118123
resolve();
@@ -163,6 +168,10 @@ export default React.createClass({
163168
const format = this.props.format;
164169
return format.video.filesize ? showSize(format.video.filesize) : "unknown";
165170
},
171+
getAudioSize() {
172+
const format = this.props.format;
173+
return format.audio.filesize ? showSize(format.audio.filesize) : "unknown";
174+
},
166175
handleCancelClick() {
167176
this.abort();
168177
this.props.onCancel();
@@ -175,7 +184,7 @@ export default React.createClass({
175184
<div style={this.styles.text}>
176185
<span>Saving audio (</span>
177186
<span style={this.styles.size}>{showSize(this.state.adata)}</span>
178-
<span> of {showSize(format.audio.filesize)}):</span>
187+
<span> of {this.getAudioSize()}):</span>
179188
</div>
180189
<progress
181190
value={this.state.adata}

src/source/format.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,17 @@ export default React.createClass({
9494
// much) so that doesn't matter.
9595
return b.abr - a.abr;
9696
},
97+
getAudioSize(f) {
98+
return f.filesize ? `(${showSize(f.filesize)})` : "";
99+
},
97100
getAudioFormats() {
98101
return this.props.info.formats
99102
.filter(f => f.acodec === "vorbis" || f.acodec === "opus")
100103
.sort(this.compareAudio)
101104
.map(f => ({
102105
key: f.format_id,
103106
text: `${toCapitalCase(f.acodec)} ${f.abr}kbits
104-
(${showSize(f.filesize)})`,
107+
${this.getAudioSize(f)}`,
105108
}))
106109
.concat({key: null, text: "none"});
107110
},

0 commit comments

Comments
 (0)