File size: 3,639 Bytes
f47cd32
 
 
 
 
 
 
 
 
 
 
 
67b3880
f47cd32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67b3880
5b85603
f47cd32
b2fe4e6
67b3880
f47cd32
 
 
 
67b3880
f47cd32
 
 
 
 
67b3880
f47cd32
 
 
 
 
 
2b7844f
 
cdfc2a0
2b7844f
 
 
f47cd32
cdfc2a0
 
 
f47cd32
 
b2fe4e6
 
5b85603
b2fe4e6
 
 
5b85603
b2fe4e6
f47cd32
5b85603
cdfc2a0
f47cd32
 
 
2b7844f
f47cd32
 
 
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
document.addEventListener('DOMContentLoaded', () => {
    const dropArea = document.getElementById('drop-area');
    const fileInput = document.getElementById('fileInput');
    const options = document.getElementById('options');
    const multiplicationFactor = document.getElementById('multiplicationFactor');
    const factorValue = document.getElementById('factorValue');
    const processButton = document.getElementById('processButton');
    const result = document.getElementById('result');
    const audioPlayer = document.getElementById('audioPlayer');
    const downloadLink = document.getElementById('downloadLink');

    let isDecrypting = false;
    let currentFile = null;

    ['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
        dropArea.addEventListener(eventName, preventDefaults, false);
    });

    function preventDefaults(e) {
        e.preventDefault();
        e.stopPropagation();
    }

    ['dragenter', 'dragover'].forEach(eventName => {
        dropArea.addEventListener(eventName, highlight, false);
    });

    ['dragleave', 'drop'].forEach(eventName => {
        dropArea.addEventListener(eventName, unhighlight, false);
    });

    function highlight() {
        dropArea.classList.add('highlight');
    }

    function unhighlight() {
        dropArea.classList.remove('highlight');
    }

    dropArea.addEventListener('drop', handleDrop, false);

    function handleDrop(e) {
        const dt = e.dataTransfer;
        const files = dt.files;
        handleFiles(files);
    }

    fileInput.addEventListener('change', function() {
        handleFiles(this.files);
    });

    function handleFiles(files) {
        if (files.length > 0) {
            currentFile = files[0];
            isDecrypting = currentFile.type === 'audio/mpeg';
            options.style.display = 'block';
            processButton.textContent = isDecrypting ? 'Decrypt' : 'Encrypt';
            dropArea.textContent = `File selected: ${currentFile.name}`;
        }
    }

    processButton.addEventListener('click', function() {
        if (!currentFile) {
            alert('Please select a file first.');
            return;
        }

        const formData = new FormData();
        formData.append('file', currentFile);
        formData.append('factor', multiplicationFactor.value);

        fetch('/process', {
            method: 'POST',
            body: formData
        })
        .then(response => {
            if (!response.ok) {
                return response.json().then(err => { throw new Error(err.error || 'Unknown error occurred') });
            }
            return response.blob();
        })
        .then(blob => {
            if (blob.size === 0) {
                throw new Error('Received empty file from server');
            }
            const url = URL.createObjectURL(blob);
            result.style.display = 'block';
            if (isDecrypting) {
                audioPlayer.style.display = 'none';
                downloadLink.textContent = 'Download Decrypted File';
            } else {
                audioPlayer.style.display = 'block';
                audioPlayer.src = url;
                downloadLink.textContent = 'Download Encrypted MP3';
            }
            downloadLink.href = url;
            downloadLink.download = isDecrypting ? 'decrypted_file' : 'encrypted.mp3';
            console.log('File processed successfully. Size:', blob.size, 'bytes');
        })
        .catch(error => {
            console.error('Error:', error);
            alert('An error occurred while processing the file: ' + error.message);
        });
    });
});