summaryrefslogtreecommitdiff
path: root/client/assets/index.js
blob: 520c7f0823a4e702962173e83130570c47715dbe (plain)
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
document.addEventListener('DOMContentLoaded', async () => {
    const urlParams = new URLSearchParams(window.location.search);
    const fileId = urlParams.get('id');
    const key = urlParams.get('key');

    if (fileId && key) {
        displayFileDetails(fileId, key);
    } else {
        document.getElementById('upload__form').style.display = 'block';
        setupUploadForm();
    }
});

const baseUrl = window.location.origin;

async function displayFileDetails(fileId, key) {
    try {
        const response = await fetch(`${baseUrl}/get/${fileId}?key=${key}`, {
            method: 'GET',
        });

        const fileDetails = document.getElementById('file__details');
        const fileNameElement = document.getElementById('file__name');
        const fileSizeElement = document.getElementById('file__size');
        const downloadButton = document.getElementById('download__btn');
        const copyButton = document.getElementById('copy__btn');

        if (!response.ok) {
            fileDetails.textContent = `Error: ${response.statusText}`;
            fileDetails.style.display = 'flex';
            return;
        }

        const contentType = response.headers.get('Content-Type');
        
        if (contentType && contentType.includes('application/json')) {
            const result = await response.json();
            const downloadUrl = `${baseUrl}/download/${fileId}?key=${key}`;
            const pageUrl = `${baseUrl}/?id=${fileId}&key=${key}`;
            fileNameElement.textContent = `${result.fileName}`;
            fileSizeElement.textContent = `${result.fileSize}`;
            downloadButton.innerHTML = `<a href="${downloadUrl}">Download</a>`;
            copyButton.onclick = () => {
                navigator.clipboard.writeText(pageUrl);
                copyButton.textContent = 'Copied!';
            };
        } else {
            const result = await response.text();
            fileDetails.textContent = result;
        }

        fileDetails.style.display = 'flex';
    } catch (error) {
        console.error('Error:', error);
        document.getElementById('file__details').textContent = 'An error occurred. Please try again.';
        document.getElementById('file__details').style.display = 'flex';
    }
}

function setupUploadForm() {
    const fileInput = document.querySelector('.upload__input');
    const overlayText = document.querySelector('.upload__input__overlay__text');

    fileInput.addEventListener('change', () => {
        if (fileInput.files.length > 0) {
            overlayText.textContent = fileInput.files[0].name;
        } else {
            overlayText.textContent = 'Choose a file or drag it here';
        }
    });

    document.getElementById('upload__form').addEventListener('submit', async (event) => {
        event.preventDefault();

        const file = fileInput.files[0];

        if (!file) {
            console.log('No file selected.');
            return;
        }

        const formData = new FormData();
        formData.append('file', file);

        try {
            const response = await fetch(`${baseUrl}/upload`, {
                method: 'POST',
                body: formData
            });

            const uploadResult = document.getElementById('upload__result');

            if (!response.ok) {
                uploadResult.textContent = `Error: ${response.statusText}`;
                uploadResult.classList.add('upload__result__visible');
                return;
            }

            const contentType = response.headers.get('Content-Type');
            
            if (contentType && contentType.includes('application/json')) {
                const result = await response.json();
                const pageUrl = `${baseUrl}/?id=${result.id}&key=${result.key}`;
                window.location.href = pageUrl;
            } else {
                const result = await response.text();
                uploadResult.textContent = result;
            }

            uploadResult.classList.add('upload__result__visible');
        } catch (error) {
            console.error('Error:', error);
            document.getElementById('upload__result').textContent = 'An error occurred. Please try again.';
            document.getElementById('upload__result').classList.add('upload__result__visible');
        }
    });
}