-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathsource.js
205 lines (202 loc) · 5.47 KB
/
source.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/**
* Source form. Returns either ytdl JSON or local file.
* @module boram/source/source
*/
import {shell, remote} from "electron";
import React from "react";
import cx from "classnames";
import {useSheet} from "../jss";
import YouTubeDL from "../yt-dlp";
import {Icon, Tip} from "../theme";
import {showErr} from "../util";
const YTDL_SUPPORTED_URL =
"https://github.com/yt-dlp/yt-dlp/blob/master/supportedsites.md";
const COMMON_VIDEO_EXTENSIONS = [
"mkv", "webm",
"ogg", "ogv", "ogm",
"mp4", "mov", "m4v",
"flv", "f4v",
"ts", "tp", "m2ts", "vob", "mpg",
"avi", "wmv", "asf", "3gp",
"y4m",
];
@useSheet({
source2: {
position: "absolute",
left: 50,
right: 50,
top: 30,
bottom: 100,
},
border: {
height: "100%",
boxSizing: "border-box",
cursor: "pointer",
border: "2px dashed #ccc",
color: "#999",
display: "flex",
alignItems: "center",
fontSize: "20px",
padding: 10,
},
borderDisabled: {
cursor: "not-allowed",
},
form: {
margin: "0 auto",
},
input: {
width: 200,
fontSize: "20px",
},
text: {
lineHeight: "1.7em",
},
padding: {
minHeight: 104,
},
error: {
color: "red",
marginTop: 20,
wordBreak: "break-all",
cursor: "initial",
display: "-webkit-box",
overflow: "hidden",
textOverflow: "ellipsis",
WebkitLineClamp: 7,
WebkitBoxOrient: "vertical",
},
icon: {
fontSize: "200px",
display: "block",
},
})
export default class extends React.PureComponent {
state = {url: ""};
componentDidMount() {
this.props.events.addListener("abort", this.abort);
}
componentWillUnmount() {
this.props.events.removeListener("abort", this.abort);
}
abort = () => {
try { this.ytdl.kill("SIGKILL"); } catch (e) { /* skip */ }
};
handleFormClick = () => {
if (this.state.infoLoading) return;
if (this.state.infoError) {
this.handleClearClick();
} else if (this.state.url) {
this.handleInfoGet();
} else {
const selected = remote.dialog.showOpenDialog({
filters: [
{name: "Videos", extensions: COMMON_VIDEO_EXTENSIONS},
{name: "All files", extensions: ["*"]},
],
});
if (!selected) return;
this.props.onSource({path: selected[0]});
}
};
handleFileLoad = () => {
const file = this.refs.file.files[0];
this.props.onSource({path: file.path});
};
handleDrop = (e) => {
e.preventDefault();
const files = e.dataTransfer.files;
if (!files.length) return;
this.props.onSource({path: files[0].path});
};
handleURLClick = (e) => {
e.stopPropagation();
};
handleTextClick = (e) => {
e.stopPropagation();
};
handleURLChange = (e) => {
this.setState({url: e.target.value});
};
handleInfoGet = (e = null) => {
if (e) e.preventDefault();
if (!this.state.url) return;
if (this.state.infoLoading) return;
this.setState({infoLoading: true});
this.ytdl = YouTubeDL.getInfo(this.state.url);
this.ytdl.then(info => {
this.props.onInfo(info);
}, err => {
this.setState({infoLoading: false, infoError: err});
});
};
handleClearClick = () => {
this.setState({infoError: null, url: ""});
// We can't focus disabled input and state updates will be flushed
// only on a next tick. This is a bit hacky.
this.refs.url.disabled = false;
this.refs.url.focus();
};
handleSupportedClick = (e) => {
e.preventDefault();
shell.openExternal(YTDL_SUPPORTED_URL);
};
render() {
const {classes} = this.sheet;
return (
<div>
<div className={classes.source2}>
<div
className={cx(classes.border,
this.state.infoLoading && classes.borderDisabled)}
onDrop={this.handleDrop}
onClick={this.handleFormClick}
>
<form className={classes.form} onSubmit={this.handleInfoGet}>
<div className={classes.text}>
<div>Click/drag your source video here</div>
<span>or </span>
<input
autoFocus
ref="url"
type="text"
placeholder="enter URL"
value={this.state.url}
className={classes.input}
onClick={this.handleURLClick}
onChange={this.handleURLChange}
disabled={this.state.infoLoading || this.state.infoError}
/>
</div>
<div className={classes.padding}>
<div className={classes.error} onClick={this.handleTextClick}>
{showErr(this.state.infoError)}
</div>
</div>
<Icon
ref="icon"
className={classes.icon}
name={this.state.infoError
? "remove"
: this.state.url ? "external-link" : "folder-open-o"}
title={this.state.infoError
? "Clear error"
: this.state.url
? "Request formats for that URL"
: "Open file dialog"}
/>
</form>
</div>
</div>
<Tip icon="info-circle">
<span>Any site </span>
<a href="" onClick={this.handleSupportedClick}
title="Open list in browser">
supported by yt-dlp
</a>
<span> is accepted</span>
</Tip>
</div>
);
}
}