comments, move functions, scrapbook progress

This commit is contained in:
zefie
2025-07-21 14:35:56 -04:00
parent e2b4aca277
commit 90522cc796
16 changed files with 756 additions and 532 deletions

View File

@@ -1,21 +1,140 @@
minisrv_service_file = true;
request_is_async = true;
if (!request_headers.query.mediaData) {
var errpage = wtvshared.doErrorPage(400, "Bad Request", "Missing mediaData parameter.");
function addToScrapbook(id, contentType, data) {
var result = session_data.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",
})
var files = session_data.listScrapbook();
var pageNum = Math.ceil(files.length / 6);
if (pageNum > 1) {
successScrapbook.buttonaction2 += '?pageNum=' + pageNum;
}
sendToClient(socket, {'Status': 302, 'wtv-expire-all': 'wtv-author:/scrapbook', 'Location': successScrapbook.getURL()}, '');
} else {
handleError('Failed to add image to scrapbook');
}
}
function handleError(reason) {
var errpage = wtvshared.doErrorPage(400, reason);
sendToClient(socket, errpage[0], errpage[1]);
}
if (!request_headers.query.mediaData && !request_headers.query.mediaPath) {
var errpage = wtvshared.doErrorPage(400, "Bad Request", "Missing mediaData or mediaPath parameter.");
headers = errpage[0];
data = errpage[1];
} else {
var id = session_data.pagestore.getFreeScrapbookID();
var result = session_data.pagestore.addToScrapbook(id, "image/jpg", request_headers.query.mediaData);
if (result) {
headers = `300 OK
const id = session_data.getFreeScrapbookID();
if (request_headers.query.mediaPath) {
if (!request_headers.query.confirm) {
var confirmScrapbook = new clientShowAlert({
'image': minisrv_config.config.service_logo,
'message': "You are about to add an image to your scrapbook.<br><br>Do you wish to continue?",
'buttonlabel1': "Continue",
'buttonaction1': "wtv-author:/scrapbook-add?confirm=true&mediaPath=" + encodeURIComponent(request_headers.query.mediaPath || ''),
'buttonlabel2': "Cancel",
'buttonaction2': "client:donothing"
}).getURL();
sendToClient(socket, {'Status': 302, 'Location': confirmScrapbook}, '');
} else {
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(request_headers.query.mediaPath);
const protocol = parsedUrl.protocol === 'https:' ? https : http;
protocol.get(request_headers.query.mediaPath, (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, request_headers.query.mediaPath)) {
handleError('URL is not a JPEG or GIF image');
res.resume();
return;
}
if (contentLength && contentLength > 1024 * 1024 * 4) {
handleError('Image is larger than 4MB');
res.resume();
return;
}
let data = [];
let totalLength = 0;
res.on('data', (chunk) => {
totalLength += chunk.length;
if (totalLength > 1024 * 1024 * 4) {
handleError('Image is larger than 4MB');
res.destroy();
return;
}
data.push(chunk);
});
res.on('end', () => {
if (totalLength > 1024 * 1024 * 4) return;
if (totalLength > 1024 * 1024) {
sharp(Buffer.concat(data))
.resize(640, 480, {
fit: 'inside',
withoutEnlargement: true
})
.jpeg({ quality: 75 })
.toBuffer()
.then(resizedBuffer => {
data = resizedBuffer;
addToScrapbook(id, "image/jpg", data);
})
.catch(err => {
handleError('Failed to resize image');
return;
});
} else {
data = Buffer.concat(data);
addToScrapbook(id, contentType, data);
}
});
res.on('error', (err) => {
handleError('Error downloading image');
});
}).on('error', (err) => {
handleError('Failed to fetch URL');
});
} catch (e) {
handleError(e.message);
}
}
} else {
var result = session_data.addToScrapbook(id, "image/jpg", request_headers.query.mediaData);
if (result) {
headers = `300 OK
Content-Type: text/html
wtv-expire-all: wtv-author:/scrapbook
Location: wtv-author:/scrapbook
wtv-visit: wtv-author:/scrapbook`;
} else {
var errpage = wtvshared.doErrorPage(500, "Internal Server Error", "Failed to add scrapbook item.");
headers = errpage[0];
data = errpage[1];
} else {
handleError('Failed to add image to scrapbook');
}
}
}

View File

@@ -1,12 +1,14 @@
var minisrv_service_file = true;
var files = session_data.pagestore.listScrapbook();
var dir = session_data.pagestore.scrapbookDir()
var start = 0;
var files = session_data.listScrapbook();
var dir = session_data.scrapbookDir()
var pageNum = parseInt(request_headers.query.pageNum || 1);
var start = (pageNum - 1) * 6;
headers = `200 OK
Connection: Keep-Alive
Content-Type: text/html`
Content-Type: text/html
wtv-expire-all: wtv-author:/scrapbook`
data = `<HTML>
<HEAD>
@@ -124,7 +126,38 @@ function StorageWarning() { }
</FORM>
<tr>
<td height=44 valign=middle>
<font size=+1 color=D1D1D1><blackface> Your scrapbook </blackface></font>
<font size=+1 color=D1D1D1><blackface> Your scrapbook </blackface></font>`
if (files.length > 6) {
data += `<td align=right valign=middle>
<table valign=middle>
<tr>
<td>`;
if (pageNum > 1) {
data += `<table cellspacing=0 cellpadding=0
href="wtv-author:scrapbook?addMediaURL=&mediaCategoryID=0&pageNum=${pageNum - 1}#minus" id=minus><tr><td><img src="wtv-author:/ROMCache/minus_button.gif">`
} else {
data += `<table cellspacing=0 cellpadding=0
><tr><td><img src="wtv-author:/ROMCache/minus_button_dim.gif">`
}
data += `
</table>
</td>
<td align=center><font color=D1D1D1><B>${pageNum} of ${Math.ceil(files.length / 6)}</B></font></td>
<td>`;
if (files.length > start + 6) {
data += `<table cellspacing=0 cellpadding=0
href="wtv-author:scrapbook?addMediaURL=&mediaCategoryID=0&pageNum=${pageNum + 1}#plus" id=plus><tr><td><img src="wtv-author:/ROMCache/plus_button.gif">`
} else {
data += `<table cellspacing=0 cellpadding=0><tr><td><img src="wtv-author:/ROMCache/plus_button_dim.gif">`
}
data += `
</table>
</td>
</tr>
</table>`
}
data += `
<tr>
<td colspan=2>
<table><tr><td width=20><td width=380>
@@ -144,7 +177,7 @@ Choose <b>Help</b> for instructions.
if (request_headers.query.addMediaURL) {
data += "Choose an image to add to your web page.";
} else {
data += "Choose one of your saved images to view it full size.";
data += `You are currently using ${session_data.getScrapbookUsagePercent()}% of your scrapbook storage space. Choose one of your saved images to view it full size.`;
}
}
data += `
@@ -176,8 +209,9 @@ if (files.length > 0) {
<IMG src=wtv-author:/ROMCache/up_arrow.gif> </TD>
<TD colspan=3 rowspan=3 align=center>
<table cellspacing=24 cellpadding=1 width=372 background="/ROMCache/light_blue_tile.gif">
<tr>`
for (let i = start; i < Math.min(files.length, start + 12); i++) {
<tr>
`
for (let i = start; i < Math.min(files.length, start + 6); i++) {
url = "wtv-tricks:/view-scrapbook-image?id=" + files[i];
if (request_headers.query.addMediaURL) {
url = unescape(request_headers.query.addMediaURL) + "&scrapbookID=" + files[i];
@@ -188,8 +222,9 @@ data += `
<img src="wtv-tricks:/view-scrapbook-image?id=${files[i]}&width=90" width=90>
</a>
</td>
${i % 4 === 1 ? '</tr><tr>' : ''}`
data += `</table>
${(i - start + 1) % 3 === 0 ? '<tr>' : ''}`
}
data += `</tr></table>
</TD>
</TR>
<TR>
@@ -207,7 +242,7 @@ data += `</table>
</TR>
</TABLE>
`
}
}
data += `
</CENTER>