Merge pull request #2 from luciochen233/fix-large-upload-413
Fix large image uploads (bogus CSRF 403) + client-side compress-to-fit
This commit is contained in:
4 files changed
+129
-50
No files matched your search
@@ -53,6 +53,7 @@ type pageData struct {
|
||||
Uploads any
|
||||
LoggedIn bool
|
||||
CSRFToken string
|
||||
UploadMaxMB int64
|
||||
}
|
||||
|
||||
type UploadInfo struct {
|
||||
@@ -344,9 +345,10 @@ func (s *Server) handleLogout(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
func (s *Server) adminPage(w http.ResponseWriter, r *http.Request) pageData {
|
||||
return pageData{
|
||||
BaseURL: s.baseURL(r),
|
||||
LoggedIn: true,
|
||||
CSRFToken: csrfToken(w, r),
|
||||
BaseURL: s.baseURL(r),
|
||||
LoggedIn: true,
|
||||
CSRFToken: csrfToken(w, r),
|
||||
UploadMaxMB: s.cfg.Upload.MaxSize,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,22 +1,39 @@
|
||||
// Shared upload helper for the admin uploads page and the paste editor.
|
||||
// Wires up an "Upload Image" and "Upload File" button pair to their hidden
|
||||
// file inputs and posts to /admin/upload (images, 50 MB) or
|
||||
// /admin/upload-file (any file, 5 MB).
|
||||
// file inputs and posts to /admin/upload (images) or /admin/upload-file
|
||||
// (any file, 5 MB).
|
||||
//
|
||||
// Images at or above the configured size limit cannot be uploaded at full
|
||||
// size; the helper offers to compress them in the browser (canvas downscale +
|
||||
// JPEG re-encode), iteratively halving resolution / lowering quality until the
|
||||
// result fits just under the limit. Only the compressed result is uploaded —
|
||||
// the oversized original is never sent.
|
||||
//
|
||||
// Usage: call window.GlimmerUpload.init({
|
||||
// imgBtn, fileBtn, imgInput, fileInput, statusEl,
|
||||
// onUploaded: function(data, isImage) { ... } // required
|
||||
// maxImageMB, // server image limit (MB)
|
||||
// onUploaded: function(data, isImage) { ... } // required
|
||||
// });
|
||||
|
||||
(function(window) {
|
||||
var IMG_MAX = 50 * 1024 * 1024;
|
||||
var FILE_MAX = 5 * 1024 * 1024;
|
||||
// Images larger than this offer in-browser compression before upload.
|
||||
// Images larger than this (but still under the limit) offer optional
|
||||
// compression for a faster upload.
|
||||
var COMPRESS_THRESHOLD = 10 * 1024 * 1024;
|
||||
// Downscale so the longest edge is at most this many pixels, then re-encode
|
||||
// as JPEG at this quality. Done entirely client-side via <canvas>.
|
||||
var COMPRESS_MAX_DIMENSION = 3000;
|
||||
var COMPRESS_QUALITY = 0.85;
|
||||
// Aim for the compressed result to land at this fraction of the hard limit,
|
||||
// leaving headroom for multipart overhead so it's reliably accepted.
|
||||
var COMPRESS_SAFETY = 0.95;
|
||||
// Initial longest-edge cap; resolution is halved from here on each retry.
|
||||
var COMPRESS_MAX_DIMENSION = 4000;
|
||||
// Stop halving once the longest edge drops to this — give up rather than
|
||||
// produce an unusably tiny image.
|
||||
var COMPRESS_MIN_DIMENSION = 320;
|
||||
// JPEG qualities tried at each resolution, best first.
|
||||
var COMPRESS_QUALITIES = [0.85, 0.7, 0.55, 0.4];
|
||||
|
||||
function mb(bytes) {
|
||||
return (bytes / (1024 * 1024)).toFixed(1);
|
||||
}
|
||||
|
||||
// renameToJpg replaces a filename's extension with .jpg (compression always
|
||||
// outputs JPEG). Falls back to "image.jpg" if there's no usable base name.
|
||||
@@ -27,31 +44,61 @@
|
||||
return base + '.jpg';
|
||||
}
|
||||
|
||||
// compressImage downscales + re-encodes an image File to a JPEG Blob using a
|
||||
// canvas. cb(blob, err): on any failure cb is called with a null blob and an
|
||||
// error string so the caller can fall back to uploading the original.
|
||||
function compressImage(file, cb) {
|
||||
// compressToTarget downscales + re-encodes an image File to a JPEG Blob no
|
||||
// larger than targetBytes. It starts at COMPRESS_MAX_DIMENSION and, for each
|
||||
// resolution, tries the quality ladder; if nothing fits it halves the
|
||||
// resolution and retries, down to COMPRESS_MIN_DIMENSION.
|
||||
// onAttempt(width, height, size) — progress callback (optional)
|
||||
// cb(blob, err, smallestBytes) — blob on success; on failure blob is
|
||||
// null, err is set, and smallestBytes is the smallest size achieved.
|
||||
function compressToTarget(file, targetBytes, onAttempt, cb) {
|
||||
if (!window.URL || !window.URL.createObjectURL) { cb(null, 'no object URL support'); return; }
|
||||
var url = window.URL.createObjectURL(file);
|
||||
var img = new Image();
|
||||
img.onload = function() {
|
||||
var w = img.naturalWidth || img.width;
|
||||
var h = img.naturalHeight || img.height;
|
||||
var ow = img.naturalWidth || img.width;
|
||||
var oh = img.naturalHeight || img.height;
|
||||
window.URL.revokeObjectURL(url);
|
||||
if (!w || !h) { cb(null, 'bad image dimensions'); return; }
|
||||
var scale = Math.min(1, COMPRESS_MAX_DIMENSION / Math.max(w, h));
|
||||
var cw = Math.max(1, Math.round(w * scale));
|
||||
var ch = Math.max(1, Math.round(h * scale));
|
||||
var canvas = document.createElement('canvas');
|
||||
canvas.width = cw;
|
||||
canvas.height = ch;
|
||||
var ctx = canvas.getContext('2d');
|
||||
if (!ctx) { cb(null, 'no canvas context'); return; }
|
||||
ctx.drawImage(img, 0, 0, cw, ch);
|
||||
if (!canvas.toBlob) { cb(null, 'no toBlob support'); return; }
|
||||
canvas.toBlob(function(blob) {
|
||||
cb(blob, blob ? null : 'compression produced no data');
|
||||
}, 'image/jpeg', COMPRESS_QUALITY);
|
||||
if (!ow || !oh) { cb(null, 'bad image dimensions'); return; }
|
||||
|
||||
var smallest = 0; // smallest blob size seen, for failure messaging
|
||||
|
||||
function attempt(scale) {
|
||||
var cw = Math.max(1, Math.round(ow * scale));
|
||||
var ch = Math.max(1, Math.round(oh * scale));
|
||||
var canvas = document.createElement('canvas');
|
||||
canvas.width = cw;
|
||||
canvas.height = ch;
|
||||
var ctx = canvas.getContext('2d');
|
||||
if (!ctx) { cb(null, 'no canvas context'); return; }
|
||||
ctx.drawImage(img, 0, 0, cw, ch);
|
||||
if (!canvas.toBlob) { cb(null, 'no toBlob support'); return; }
|
||||
|
||||
var qi = 0;
|
||||
function tryQuality() {
|
||||
if (qi >= COMPRESS_QUALITIES.length) {
|
||||
// Nothing fit at this resolution. Halve and retry, unless
|
||||
// we've already shrunk as far as we're willing to.
|
||||
if (Math.max(cw, ch) <= COMPRESS_MIN_DIMENSION) {
|
||||
cb(null, 'too large', smallest);
|
||||
return;
|
||||
}
|
||||
attempt(scale * 0.5);
|
||||
return;
|
||||
}
|
||||
var q = COMPRESS_QUALITIES[qi++];
|
||||
canvas.toBlob(function(blob) {
|
||||
if (!blob) { tryQuality(); return; }
|
||||
if (!smallest || blob.size < smallest) smallest = blob.size;
|
||||
if (onAttempt) onAttempt(cw, ch, blob.size);
|
||||
if (blob.size <= targetBytes) { cb(blob, null); return; }
|
||||
tryQuality();
|
||||
}, 'image/jpeg', q);
|
||||
}
|
||||
tryQuality();
|
||||
}
|
||||
|
||||
attempt(Math.min(1, COMPRESS_MAX_DIMENSION / Math.max(ow, oh)));
|
||||
};
|
||||
img.onerror = function() {
|
||||
window.URL.revokeObjectURL(url);
|
||||
@@ -72,6 +119,8 @@
|
||||
var fileInput = opts.fileInput;
|
||||
var statusEl = opts.statusEl;
|
||||
var onUploaded = opts.onUploaded;
|
||||
var imgMaxBytes = (opts.maxImageMB ? opts.maxImageMB : 50) * 1024 * 1024;
|
||||
var imgMaxMB = Math.round(imgMaxBytes / (1024 * 1024));
|
||||
|
||||
function setDisabled(v) {
|
||||
if (imgBtn) imgBtn.disabled = v;
|
||||
@@ -88,23 +137,24 @@
|
||||
setStatus('Not an image file');
|
||||
return;
|
||||
}
|
||||
// Offer in-browser compression for large images before the size
|
||||
// guard, so a >50 MB photo can still be uploaded once shrunk.
|
||||
var fileMB = mb(file.size);
|
||||
|
||||
if (file.size > imgMaxBytes) {
|
||||
// Over the limit — the original cannot be uploaded. Compress to fit.
|
||||
var ok = window.confirm('This image is ' + fileMB + ' MB, over the ' + imgMaxMB + ' MB limit.\n\nCompress it in your browser to fit under the limit? It cannot be uploaded at full size — press Cancel to abort.');
|
||||
if (!ok) {
|
||||
setStatus('Cancelled — image exceeds the ' + imgMaxMB + ' MB limit.');
|
||||
return;
|
||||
}
|
||||
runCompression(file, Math.floor(imgMaxBytes * COMPRESS_SAFETY), true);
|
||||
return;
|
||||
}
|
||||
|
||||
if (file.size > COMPRESS_THRESHOLD) {
|
||||
var mb = (file.size / (1024 * 1024)).toFixed(1);
|
||||
var ok = window.confirm('This image is ' + mb + ' MB. Compress it in your browser before uploading?\n\nRecommended — this resizes and re-encodes it as JPEG for a much faster upload. Cancel to upload the original.');
|
||||
if (ok) {
|
||||
setStatus('Compressing…');
|
||||
setDisabled(true);
|
||||
compressImage(file, function(blob, err) {
|
||||
if (err || !blob) {
|
||||
// Compression failed — fall back to the original.
|
||||
proceedUpload(file, true, null);
|
||||
return;
|
||||
}
|
||||
setStatus('Compressed ' + mb + ' MB → ' + (blob.size / (1024 * 1024)).toFixed(1) + ' MB');
|
||||
proceedUpload(blob, true, renameToJpg(file.name));
|
||||
});
|
||||
// Under the limit but large — offer optional compression.
|
||||
var ok2 = window.confirm('This image is ' + fileMB + ' MB. Compress it in your browser for a faster upload?\n\nPress Cancel to upload the original.');
|
||||
if (ok2) {
|
||||
runCompression(file, COMPRESS_THRESHOLD, false);
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -112,13 +162,38 @@
|
||||
proceedUpload(file, isImage, null);
|
||||
}
|
||||
|
||||
// runCompression drives compressToTarget and then uploads the result.
|
||||
// When mandatory (original is over the limit), a failure to reach the
|
||||
// target aborts; otherwise it falls back to uploading the original.
|
||||
function runCompression(file, targetBytes, mandatory) {
|
||||
setStatus('Compressing…');
|
||||
setDisabled(true);
|
||||
compressToTarget(file, targetBytes, function(cw, ch, size) {
|
||||
setStatus('Compressing… ' + cw + '×' + ch + ' → ' + mb(size) + ' MB');
|
||||
}, function(blob, err, smallest) {
|
||||
if (err || !blob) {
|
||||
if (mandatory) {
|
||||
setDisabled(false);
|
||||
var extra = smallest ? (' Smallest achievable was ' + mb(smallest) + ' MB.') : '';
|
||||
setStatus('Could not compress below the ' + imgMaxMB + ' MB limit.' + extra + ' Try a smaller image.');
|
||||
} else {
|
||||
// Optional path: just upload the original.
|
||||
proceedUpload(file, true, null);
|
||||
}
|
||||
return;
|
||||
}
|
||||
setStatus('Compressed to ' + mb(blob.size) + ' MB, uploading…');
|
||||
proceedUpload(blob, true, renameToJpg(file.name));
|
||||
});
|
||||
}
|
||||
|
||||
// proceedUpload performs the actual POST. filename, when set, is sent as
|
||||
// the multipart part filename (used for compressed blobs that have none).
|
||||
function proceedUpload(file, isImage, filename) {
|
||||
if (isImage) {
|
||||
if (file.size > IMG_MAX) {
|
||||
if (file.size > imgMaxBytes) {
|
||||
setDisabled(false);
|
||||
setStatus('File too large (max 50 MB)');
|
||||
setStatus('File too large (max ' + imgMaxMB + ' MB)');
|
||||
return;
|
||||
}
|
||||
} else if (file.size > FILE_MAX) {
|
||||
|
||||
@@ -169,6 +169,7 @@
|
||||
imgInput: document.getElementById('img-input'),
|
||||
fileInput: document.getElementById('file-input'),
|
||||
statusEl: status,
|
||||
maxImageMB: {{.UploadMaxMB}},
|
||||
onUploaded: function(data) {
|
||||
insertAtCursor(data.markdown);
|
||||
status.textContent = 'Uploaded';
|
||||
|
||||
@@ -183,6 +183,7 @@
|
||||
imgInput: document.getElementById('img-input'),
|
||||
fileInput: document.getElementById('file-input'),
|
||||
statusEl: document.getElementById('upload-status'),
|
||||
maxImageMB: {{.UploadMaxMB}},
|
||||
onUploaded: function() { location.reload(); }
|
||||
});
|
||||
</script>
|
||||
|
||||
Reference in new issue
Block a user