comments, move functions, scrapbook progress
This commit is contained in:
@@ -7,83 +7,10 @@ function handleError(reason) {
|
||||
sendToClient(socket, errpage[0], errpage[1]);
|
||||
}
|
||||
|
||||
if (!request_headers.query.url) {
|
||||
if (!request_headers.query.url && !request_headers.query.mediaPath) {
|
||||
handleError('No URL provided');
|
||||
} else {
|
||||
var url = request_headers.query.url;
|
||||
function isValidImageType(contentType, url) {
|
||||
// Check content-type header or file extension
|
||||
if (contentType) {
|
||||
return contentType === 'image/jpeg' || contentType == 'image/jpg' || contentType === 'image/gif';
|
||||
}
|
||||
return url.endsWith('.jpg') || url.endsWith('.jpeg') || url.endsWith('.gif');
|
||||
}
|
||||
|
||||
try {
|
||||
const parsedUrl = new URL(url);
|
||||
const protocol = parsedUrl.protocol === 'https:' ? https : http;
|
||||
|
||||
protocol.get(url, (res) => {
|
||||
if (res.statusCode !== 200) {
|
||||
handleError('URL does not exist or returned status ' + res.statusCode);
|
||||
res.resume();
|
||||
return;
|
||||
}
|
||||
|
||||
const contentType = res.headers['content-type'];
|
||||
const contentLength = parseInt(res.headers['content-length'], 10);
|
||||
|
||||
if (!isValidImageType(contentType, url)) {
|
||||
handleError('URL is not a JPEG or GIF image');
|
||||
res.resume();
|
||||
return;
|
||||
}
|
||||
|
||||
if (contentLength && contentLength > 1024 * 1024) {
|
||||
handleError('Image is larger than 1MB');
|
||||
res.resume();
|
||||
return;
|
||||
}
|
||||
|
||||
let data = [];
|
||||
let totalLength = 0;
|
||||
res.on('data', (chunk) => {
|
||||
totalLength += chunk.length;
|
||||
if (totalLength > 1024 * 1024) {
|
||||
handleError('Image is larger than 1MB');
|
||||
res.destroy();
|
||||
return;
|
||||
}
|
||||
data.push(chunk);
|
||||
});
|
||||
|
||||
res.on('end', () => {
|
||||
if (totalLength > 1024 * 1024) return;
|
||||
data = Buffer.concat(data);
|
||||
var id = session_data.pagestore.getFreeScrapbookID();
|
||||
var result = session_data.pagestore.addToScrapbook(id, contentType, data);
|
||||
if (result) {
|
||||
var successScrapbook = new clientShowAlert({
|
||||
'image': minisrv_config.config.service_logo,
|
||||
'message': "The image has been added to your scrapbook. Would you like to view your scrapbook now?",
|
||||
'buttonlabel1': "No",
|
||||
'buttonaction1': "client:donothing",
|
||||
'buttonlabel2': "Yes",
|
||||
'buttonaction2': "wtv-author:/scrapbook",
|
||||
}).getURL();
|
||||
sendToClient(socket, {'Status': 302, 'Location': successScrapbook, 'wtv-visit': successScrapbook}, '');
|
||||
} else {
|
||||
handleError('Failed to add image to scrapbook');
|
||||
}
|
||||
});
|
||||
|
||||
res.on('error', (err) => {
|
||||
handleError('Error downloading image');
|
||||
});
|
||||
}).on('error', (err) => {
|
||||
handleError('Failed to fetch URL');
|
||||
});
|
||||
} catch (e) {
|
||||
handleError(e.message);
|
||||
}
|
||||
var mediaURL = request_headers.query.url || request_headers.query.mediaPath;
|
||||
var targetURL = 'wtv-author:/scrapbook-add?mediaPath=' + encodeURIComponent(mediaURL);
|
||||
sendToClient(socket, {'Status': 302, 'Location': targetURL, 'wtv-visit': targetURL}, '');
|
||||
}
|
||||
@@ -10,7 +10,7 @@ async function handleImage() {
|
||||
if (!request_headers.query.id) {
|
||||
handleError('No image ID specified');
|
||||
} else {
|
||||
data = session_data.pagestore.getScrapbookImage(request_headers.query.id);
|
||||
data = session_data.getScrapbookImage(request_headers.query.id);
|
||||
if (!data) {
|
||||
handleError('Image not found');
|
||||
} else {
|
||||
@@ -21,7 +21,7 @@ async function handleImage() {
|
||||
data = await sharp(data).resize({ width, withoutEnlargement: true }).toBuffer();
|
||||
}
|
||||
headers = `200 OK
|
||||
Content-Type: ${session_data.pagestore.getScrapbookImageType(request_headers.query.id)}
|
||||
Content-Type: ${session_data.getScrapbookImageType(request_headers.query.id)}
|
||||
Content-Length: ${data.length}`
|
||||
sendToClient(socket, headers, data);
|
||||
} catch (error) {
|
||||
|
||||
Reference in New Issue
Block a user