-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
289 lines (236 loc) · 9.29 KB
/
script.js
File metadata and controls
289 lines (236 loc) · 9.29 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// FileCord Frontend - Connects to FastAPI backend
class FileCordApp {
constructor() {
this.apiUrl = 'http://localhost:8000';
this.currentJobId = null;
this.eventSource = null;
this.initializeElements();
this.bindEvents();
}
initializeElements() {
// Areas
this.uploadArea = document.getElementById('upload-area');
this.loadingArea = document.getElementById('loading-area');
this.successArea = document.getElementById('success-area');
this.errorArea = document.getElementById('error-area');
// Upload elements
this.fileInput = document.getElementById('file-input');
// Loading elements
this.loadingText = document.getElementById('loading-text');
this.progressFill = document.getElementById('progress-fill');
this.progressText = document.getElementById('progress-text');
// Success elements
this.fileInfo = document.getElementById('file-info');
this.downloadBtn = document.getElementById('download-btn');
this.resetBtn = document.getElementById('reset-btn');
// Error elements
this.errorMessage = document.getElementById('error-message');
this.retryBtn = document.getElementById('retry-btn');
}
bindEvents() {
// Upload area click
this.uploadArea.addEventListener('click', () => {
this.fileInput.click();
});
// File selection
this.fileInput.addEventListener('change', (e) => {
if (e.target.files.length > 0) {
this.handleFileUpload(e.target.files[0]);
}
});
// Drag and drop
this.uploadArea.addEventListener('dragover', (e) => {
e.preventDefault();
this.uploadArea.classList.add('drag-over');
});
this.uploadArea.addEventListener('dragleave', () => {
this.uploadArea.classList.remove('drag-over');
});
this.uploadArea.addEventListener('drop', (e) => {
e.preventDefault();
this.uploadArea.classList.remove('drag-over');
const files = e.dataTransfer.files;
if (files.length > 0 && files[0].type.startsWith('video/')) {
this.handleFileUpload(files[0]);
} else {
this.showError('Please upload a valid video file');
}
});
// Button events
this.downloadBtn.addEventListener('click', () => this.downloadVideo());
this.resetBtn.addEventListener('click', () => this.reset());
this.retryBtn.addEventListener('click', () => this.reset());
}
async handleFileUpload(file) {
console.log('Uploading file:', file.name);
// Validate file
if (!file.type.startsWith('video/')) {
this.showError('Please select a video file');
return;
}
// Check file size (limit to 100MB for upload)
if (file.size > 10000 * 1024 * 1024) {
this.showError('File too large. Please select a file smaller than 10000MB');
return;
}
this.showLoading('Uploading video...');
try {
// Upload file to backend
const formData = new FormData();
formData.append('file', file);
const response = await fetch(`${this.apiUrl}/upload`, {
method: 'POST',
body: formData
});
if (!response.ok) {
const error = await response.json();
throw new Error(error.detail || 'Upload failed');
}
const result = await response.json();
this.currentJobId = result.job_id;
console.log('Upload successful, job ID:', this.currentJobId);
// Start listening for progress updates
this.startProgressUpdates();
} catch (error) {
console.error('Upload error:', error);
this.showError(`Upload failed: ${error.message}`);
}
}
startProgressUpdates() {
if (!this.currentJobId) return;
// Close existing EventSource if any
if (this.eventSource) {
this.eventSource.close();
}
// Create new EventSource for progress updates
this.eventSource = new EventSource(`${this.apiUrl}/progress/${this.currentJobId}`);
this.eventSource.onmessage = (event) => {
try {
const data = JSON.parse(event.data);
this.handleProgressUpdate(data);
} catch (error) {
console.error('Failed to parse progress data:', error);
}
};
this.eventSource.onerror = (error) => {
console.error('SSE error:', error);
this.eventSource.close();
// Try to get final status
setTimeout(() => this.checkFinalStatus(), 1000);
};
}
handleProgressUpdate(data) {
console.log('Progress update:', data);
switch (data.status) {
case 'queued':
let queueMessage = 'Video queued for processing...';
if (data.queue_position && data.queue_size) {
queueMessage = `Position ${data.queue_position} of ${data.queue_size} in queue`;
}
this.updateProgress(0, queueMessage);
break;
case 'processing':
this.updateProgress(data.progress, data.message);
break;
case 'completed':
this.eventSource?.close();
this.showSuccess(data.message);
break;
case 'failed':
this.eventSource?.close();
this.showError(data.error || 'Conversion failed');
break;
}
}
async checkFinalStatus() {
if (!this.currentJobId) return;
try {
const response = await fetch(`${this.apiUrl}/status/${this.currentJobId}`);
if (response.ok) {
const status = await response.json();
this.handleProgressUpdate(status);
}
} catch (error) {
console.error('Failed to check final status:', error);
this.showError('Connection lost. Please try again.');
}
}
updateProgress(progress, message) {
this.loadingText.textContent = message;
this.progressFill.style.width = `${progress}%`;
this.progressText.textContent = `${Math.round(progress)}%`;
}
showLoading(message) {
this.hideAllAreas();
this.loadingArea.classList.remove('hidden');
this.updateProgress(0, message);
}
showSuccess(message) {
this.hideAllAreas();
this.successArea.classList.remove('hidden');
this.fileInfo.textContent = message;
}
showError(message) {
this.hideAllAreas();
this.errorArea.classList.remove('hidden');
this.errorMessage.textContent = message;
}
hideAllAreas() {
this.uploadArea.classList.add('hidden');
this.loadingArea.classList.add('hidden');
this.successArea.classList.add('hidden');
this.errorArea.classList.add('hidden');
}
async downloadVideo() {
if (!this.currentJobId) return;
try {
// Create download link
const downloadUrl = `${this.apiUrl}/download/${this.currentJobId}`;
// Create temporary link element
const link = document.createElement('a');
link.href = downloadUrl;
link.download = 'converted_video.mp4';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
console.log('Download started');
} catch (error) {
console.error('Download error:', error);
this.showError('Download failed. Please try again.');
}
}
async reset() {
// Clean up EventSource
if (this.eventSource) {
this.eventSource.close();
this.eventSource = null;
}
// Clean up backend files if we have a job ID
if (this.currentJobId) {
try {
await fetch(`${this.apiUrl}/cleanup/${this.currentJobId}`, {
method: 'DELETE'
});
} catch (error) {
console.warn('Cleanup failed:', error);
}
}
// Reset state
this.currentJobId = null;
this.fileInput.value = '';
// Show upload area
this.hideAllAreas();
this.uploadArea.classList.remove('hidden');
}
}
// Initialize app when DOM is loaded
document.addEventListener('DOMContentLoaded', () => {
window.fileCordApp = new FileCordApp();
console.log('FileCord app initialized');
});
// Clean up on page unload
window.addEventListener('beforeunload', () => {
if (window.fileCordApp) {
window.fileCordApp.reset();
}
});