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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
|
/**
* Tracks all images to be uploaded (local files and link-fetched images).
* Key is either the file name (for local) or URL (for link).
* @type {Map<string, { blob: Blob, rating: string, previewElement: HTMLElement, type: 'local' | 'link', nameOrUrl: string }>}
*/
const imageBlobMapping = new Map();
/**
* Shows or hides the Upload All button in the .upload-area container based on imageBlobMapping size.
* The button is created once and appended/removed as needed.
* @function
*/
let uploadAllBtn = null;
function updateUploadAllBtn() {
const uploadArea = document.querySelector('.upload-area');
if (!uploadArea) return;
if (!uploadAllBtn) {
uploadAllBtn = document.createElement('button');
uploadAllBtn.type = 'button';
uploadAllBtn.textContent = 'Upload All';
uploadAllBtn.className = 'upload-all-btn';
/**
* TODO: Implement actual upload logic here
*/
uploadAllBtn.onclick = function () {
alert('Upload All clicked!');
};
}
if (imageBlobMapping.size > 0) {
if (!uploadArea.contains(uploadAllBtn)) {
uploadArea.appendChild(uploadAllBtn);
}
} else {
if (uploadArea.contains(uploadAllBtn)) {
uploadArea.removeChild(uploadAllBtn);
}
}
}
/**
* Creates a preview element for an image (local or link).
* @param {string} key - The key for the image (filename or URL)
* @param {Blob} blob - The image blob
* @param {'local'|'link'} type - Type of image
* @param {string} nameOrUrl - Filename (local) or URL (link)
* @returns {HTMLDivElement}
*/
function createPreviewElement(key, blob, type, nameOrUrl) {
const previewElement = document.createElement('div');
previewElement.className = 'preview-area';
const previewImage = document.createElement('img');
previewImage.className = 'preview-image';
previewImage.src = URL.createObjectURL(blob);
previewElement.appendChild(previewImage);
const previewDetailsArea = document.createElement('div');
previewDetailsArea.className = 'preview-details';
previewElement.appendChild(previewDetailsArea);
if (type === 'link') {
const previewLink = document.createElement('a');
previewLink.className = 'preview-link';
previewLink.target = '_blank';
previewLink.href = nameOrUrl;
previewLink.textContent = nameOrUrl;
previewDetailsArea.appendChild(previewLink);
} else {
const previewFile = document.createElement('span');
previewFile.className = 'preview-link';
previewFile.textContent = nameOrUrl;
previewDetailsArea.appendChild(previewFile);
}
const previewRatingForm = document.createElement('form');
previewRatingForm.className = 'preview-rating-form';
['Safe', 'Questionable', 'Sensitive', 'Explicit'].forEach((rating, idx) => {
const inputId = `rating-${rating.toLowerCase()}-${key}`;
const input = document.createElement('input');
input.type = 'radio';
input.name = `rating-${key}`;
input.value = rating.toLowerCase();
input.checked = idx === 0;
input.id = inputId;
const label = document.createElement('label');
label.textContent = rating;
label.setAttribute('for', inputId);
previewRatingForm.appendChild(input);
previewRatingForm.appendChild(label);
});
previewDetailsArea.appendChild(previewRatingForm);
const removeBtn = document.createElement('button');
removeBtn.type = 'button';
removeBtn.textContent = 'Remove';
removeBtn.className = 'preview-remove-btn';
removeBtn.onclick = () => {
previewElement.remove();
imageBlobMapping.delete(key);
updateUploadAllBtn();
};
previewDetailsArea.appendChild(removeBtn);
return previewElement;
}
window.onbeforeunload = function (_event) {
if (imageBlobMapping.size > 0) {
return 'Are you sure you want to leave? Data you have entered may not be saved.';
}
};
/**
* Shows or hides a retro loading indicator in the upload area.
* @param {boolean} show
*/
function setUploadLoadingIndicator(show) {
let indicator = document.getElementById('_uploadLoadingIndicator');
const dragBox = document.querySelector('.upload-drag-box');
if (!dragBox) return;
dragBox.style.position = 'relative';
if (!indicator) {
indicator = document.createElement('div');
indicator.id = '_uploadLoadingIndicator';
indicator.className = 'upload-loading-indicator';
indicator.innerHTML = '<span class="upload-loading-icon">✦</span>';
dragBox.appendChild(indicator);
}
indicator.style.display = show ? 'flex' : 'none';
}
/**
* Handles uploading an image via link using the backend proxy.
* Optimized and uses async/await.
* @returns {Promise<void>}
*/
async function uploadViaLink() {
const uploadViaLinkInputBox = document.getElementById('_uploadViaLink_InputBox');
const uploadViaLinkUploadPreviewsArea = document.getElementById('_uploadViaLink_UploadPreviewsArea');
const link = uploadViaLinkInputBox.value.trim();
hideError();
if (!link) {
showError('Please enter a valid image URL.');
return;
}
if (imageBlobMapping.has(link)) {
showError('This image has already been added.');
return;
}
setUploadLoadingIndicator(true);
try {
const proxyUrl = `/posts/new/ilinkfetch?url=${encodeURIComponent(link)}`;
const response = await fetch(proxyUrl);
if (!response.ok) {
let errorMsg = 'Failed to fetch the image from the provided URL.';
try {
const text = await response.text();
if (text && text !== 'Failed to fetch image') errorMsg = text;
} catch {
errorMsg = 'An error occurred while fetching the image.';
}
showError(errorMsg || 'An error occurred while fetching the image.');
return;
}
const contentType = response.headers.get('content-type') || '';
if (!contentType.startsWith('image/')) {
showError('The URL does not point to a valid image.');
return;
}
const blob = await response.blob();
if (!blob) {
showError('No image data received from the URL.');
return;
}
const previewElement = createPreviewElement(link, blob, 'link', link);
uploadViaLinkUploadPreviewsArea.appendChild(previewElement);
imageBlobMapping.set(link, { blob, rating: 'safe', previewElement, type: 'link', nameOrUrl: link });
updateUploadAllBtn();
} catch (error) {
console.error('Error fetching image:', error);
showError('An error occurred while fetching the image.');
} finally {
setUploadLoadingIndicator(false);
uploadViaLinkInputBox.value = '';
}
}
/**
* Handles drag-and-drop and click-to-select for local image files.
*/
function setupLocalImageUpload() {
const dragBox = document.querySelector('.upload-drag-box');
const previewsArea = document.getElementById('_uploadViaLink_UploadPreviewsArea');
const dragHeading = dragBox ? dragBox.querySelector('h1') : null;
if (!dragBox || !previewsArea || !dragHeading) return;
dragBox.addEventListener('dragover', function (e) {
e.preventDefault();
dragBox.classList.add('dragover');
dragHeading.textContent = 'Release to upload!';
});
dragBox.addEventListener('dragleave', function (e) {
e.preventDefault();
dragBox.classList.remove('dragover');
dragHeading.textContent = 'Drop files here or just click this box!';
});
dragBox.addEventListener('drop', async function (e) {
e.preventDefault();
dragBox.classList.remove('dragover');
dragHeading.textContent = 'Drop files here or just click this box!';
const files = e.dataTransfer.files;
if (files && files.length > 0) {
handleFiles(files);
} else {
// Try to get a URL from the drop
let url = '';
if (e.dataTransfer.items) {
for (let i = 0; i < e.dataTransfer.items.length; i++) {
const item = e.dataTransfer.items[i];
if (item.kind === 'string' && (item.type === 'text/uri-list' || item.type === 'text/plain')) {
item.getAsString(function (s) {
if (s && s.match(/^https?:\/\/.+/)) {
handleDroppedUrl(s);
}
});
return;
}
}
}
// Fallback for some browsers
url = e.dataTransfer.getData('text/uri-list') || e.dataTransfer.getData('text/plain');
if (url && url.match(/^https?:\/\/.+/)) {
handleDroppedUrl(url);
}
}
});
dragBox.addEventListener('click', function () {
const input = document.createElement('input');
input.type = 'file';
input.accept = 'image/*';
input.multiple = true;
input.onchange = function () {
handleFiles(input.files);
};
input.click();
});
}
/**
* Handles dropped URLs, tries to upload if it's an image or fetch if not.
* @param {string} url
*/
function handleDroppedUrl(url) {
// Accept direct image URLs or blob/data URLs
const imageExt = /\.(jpg|jpeg|png|gif|webp|bmp|svg)$/i;
if (imageExt.test(url) || url.startsWith('blob:') || url.startsWith('data:image/')) {
uploadViaLinkDirect(url);
} else {
// Try to fetch and see if it's an image
fetch(url, { method: 'HEAD' })
.then(resp => {
const type = resp.headers.get('content-type') || '';
if (type.startsWith('image/')) {
uploadViaLinkDirect(url);
} else {
showError('Dropped URL is not a direct image. Try dragging the image itself, not the page.');
}
})
.catch(() => {
showError('Could not fetch dropped URL.');
});
}
}
/**
* Directly uploads an image via link (used for drag-drop URLs)
* @param {string} url
*/
async function uploadViaLinkDirect(url) {
setUploadLoadingIndicator(true);
const uploadViaLinkUploadPreviewsArea = document.getElementById('_uploadViaLink_UploadPreviewsArea');
if (imageBlobMapping.has(url)) {
showError('This image has already been added.');
return;
}
try {
const proxyUrl = `/posts/new/ilinkfetch?url=${encodeURIComponent(url)}`;
const response = await fetch(proxyUrl);
if (!response.ok) {
let errorMsg = 'Failed to fetch the image from the provided URL.';
try {
const text = await response.text();
if (text && text !== 'Failed to fetch image') errorMsg = text;
} catch {
errorMsg = 'An error occurred while fetching the image.';
}
showError(errorMsg || 'An error occurred while fetching the image.');
return;
}
const contentType = response.headers.get('content-type') || '';
if (!contentType.startsWith('image/')) {
showError('The URL does not point to a valid image.');
return;
}
const blob = await response.blob();
if (!blob) {
showError('No image data received from the URL.');
return;
}
const previewElement = createPreviewElement(url, blob, 'link', url);
uploadViaLinkUploadPreviewsArea.appendChild(previewElement);
imageBlobMapping.set(url, { blob, rating: 'safe', previewElement, type: 'link', nameOrUrl: url });
updateUploadAllBtn();
} catch (error) {
console.error('Error fetching image:', error);
showError('An error occurred while fetching the image.');
} finally {
setUploadLoadingIndicator(false);
}
}
/**
* Handles adding local files to the preview and mapping.
* @param {FileList} files
*/
function handleFiles(files) {
const previewsArea = document.getElementById('_uploadViaLink_UploadPreviewsArea');
for (const file of files) {
if (!file.type.startsWith('image/')) continue;
if (imageBlobMapping.has(file.name)) continue;
const previewElement = createPreviewElement(file.name, file, 'local', file.name);
previewsArea.appendChild(previewElement);
imageBlobMapping.set(file.name, { blob: file, rating: 'safe', previewElement, type: 'local', nameOrUrl: file.name });
}
updateUploadAllBtn();
}
setupLocalImageUpload();
|