Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
174 lines (154 loc) · 4.8 KB
/
Copy pathscript.js
File metadata and controls
174 lines (154 loc) · 4.8 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
// Handles drag & drop, file validation, preview, and progress
const dropArea = document.getElementById('drop-area');
const fileInput = document.getElementById('fileElem');
const progress = document.getElementById('progress');
const errorMsg = document.getElementById('error');
const preview = document.getElementById('preview');
const MAX_SIZE_MB = 5;
const ALLOWED_TYPES = [
'image/jpeg', 'image/png', 'image/gif',
'application/pdf', 'text/plain', 'application/msword',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
];
// Allow multiple files
fileInput.setAttribute('multiple', 'multiple');
fileInput.setAttribute('accept', [
'image/jpeg,image/png,image/gif,application/pdf,text/plain,.doc,.docx'
].join(','));
let uploadedFiles = [];
function showError(message) {
errorMsg.textContent = message;
progress.classList.add('hidden');
}
function clearError() {
errorMsg.textContent = '';
}
function getFileIcon(type, name) {
if (type.startsWith('image/')) {
return null; // Will use preview
}
if (type === 'application/pdf') {
return '📄';
}
if (type === 'text/plain') {
return '📄';
}
if (
type === 'application/msword' ||
type === 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' ||
name.endsWith('.doc') || name.endsWith('.docx')
) {
return '📝';
}
return '📁';
}
function renderPreviews() {
preview.innerHTML = '';
uploadedFiles.forEach((fileObj, idx) => {
const file = fileObj.file;
const wrapper = document.createElement('div');
wrapper.className = 'relative flex flex-col items-center m-2';
let content;
if (file.type.startsWith('image/')) {
content = document.createElement('img');
content.className = 'max-h-32 max-w-32 rounded shadow mb-2';
content.alt = file.name;
const reader = new FileReader();
reader.onload = function(e) {
content.src = e.target.result;
};
reader.readAsDataURL(file);
wrapper.appendChild(content);
} else {
const icon = document.createElement('div');
icon.className = 'text-5xl mb-2';
icon.textContent = getFileIcon(file.type, file.name);
wrapper.appendChild(icon);
}
// File name
const nameDiv = document.createElement('div');
nameDiv.className = 'text-xs text-gray-700 break-all text-center max-w-[8rem]';
nameDiv.textContent = file.name;
wrapper.appendChild(nameDiv);
// Remove button
const removeBtn = document.createElement('button');
removeBtn.className = 'absolute -top-2 -right-2 bg-red-500 text-white rounded-full w-6 h-6 flex items-center justify-center shadow hover:bg-red-700';
removeBtn.innerHTML = '×';
removeBtn.title = 'Remove';
removeBtn.onclick = () => {
uploadedFiles.splice(idx, 1);
renderPreviews();
};
wrapper.appendChild(removeBtn);
preview.appendChild(wrapper);
});
}
function validateFile(file) {
if (!ALLOWED_TYPES.includes(file.type) && !file.name.match(/\.(jpg|jpeg|png|gif|pdf|txt|doc|docx)$/i)) {
showError('Invalid file type. Only images, PDF, TXT, DOC, DOCX allowed.');
return false;
}
if (file.size > MAX_SIZE_MB * 1024 * 1024) {
showError('File is too large. Max size is 5 MB.');
return false;
}
return true;
}
function simulateUpload(files) {
progress.value = 0;
progress.classList.remove('hidden');
let percent = 0;
const interval = setInterval(() => {
percent += Math.random() * 30;
if (percent >= 100) {
percent = 100;
clearInterval(interval);
progress.classList.add('hidden');
renderPreviews();
}
progress.value = percent;
}, 150);
}
function handleFiles(files) {
clearError();
if (!files || files.length === 0) return;
let validFiles = [];
for (let i = 0; i < files.length; i++) {
const file = files[i];
if (validateFile(file)) {
validFiles.push({ file });
}
}
if (validFiles.length > 0) {
uploadedFiles = uploadedFiles.concat(validFiles);
simulateUpload(validFiles);
}
}
// Drag & Drop Events
['dragenter', 'dragover'].forEach(eventName => {
dropArea.addEventListener(eventName, e => {
e.preventDefault();
e.stopPropagation();
dropArea.classList.add('border-blue-600', 'text-blue-700');
});
});
['dragleave', 'drop'].forEach(eventName => {
dropArea.addEventListener(eventName, e => {
e.preventDefault();
e.stopPropagation();
dropArea.classList.remove('border-blue-600', 'text-blue-700');
});
});
dropArea.addEventListener('drop', e => {
const dt = e.dataTransfer;
const files = dt.files;
handleFiles(files);
});
// Click to open file dialog
dropArea.addEventListener('click', () => {
fileInput.value = ''; // Reset so same file can be selected again
fileInput.click();
});
fileInput.addEventListener('change', e => {
handleFiles(e.target.files);
});