Skip to content

gitflarePublicPartly agent-ready50

cf-r2-drop

Drag-and-drop file sharing on Cloudflare R2: multipart uploads, expiring share links, previews, download counts, lifecycle expiry.

Serve the share page from the extensionless asset path0e4544f
JavaScript80 lines4.6 KB
// Pure QR encoder: byte mode, ECC M, versions 1-10, mask 0. Returns boolean[][].const EC = [null,[10,[[1,16]]],[16,[[1,28]]],[26,[[1,44]]],[18,[[2,32]]],[24,[[2,43]]],[16,[[4,27]]],  [18,[[4,31]]],[22,[[2,38],[2,39]]],[22,[[3,36],[2,37]]],[26,[[4,43],[1,44]]]];const ALIGN = [null,[],[6,18],[6,22],[6,26],[6,30],[6,34],[6,22,38],[6,24,42],[6,26,46],[6,28,50]];const EXP = new Array(512), LOG = new Array(256);for (let i = 0, x = 1; i < 255; i++) { EXP[i] = x; LOG[x] = i; x <<= 1; if (x & 256) x ^= 0x11d; }for (let i = 255; i < 512; i++) EXP[i] = EXP[i - 255];const mul = (a, b) => (a && b ? EXP[LOG[a] + LOG[b]] : 0);function rs(data, n) {  let g = [1];  for (let i = 0; i < n; i++) { const m = new Array(g.length + 1).fill(0); g.forEach((c, j) => { m[j] ^= c; m[j + 1] ^= mul(c, EXP[i]); }); g = m; }  const r = new Array(n).fill(0);  for (const d of data) { const f = d ^ r.shift(); r.push(0); for (let i = 0; i < n; i++) r[i] ^= mul(g[i + 1], f); }  return r;}const dataCap = (v) => EC[v][1].reduce((s, [c, d]) => s + c * d, 0);function bch(v, poly, top) { let d = v << (top); const t = 31 - Math.clz32(poly); for (let i = 31 - Math.clz32(d); i >= t; i--) if (d >> i & 1) d ^= poly << (i - t); return d; }export function encode(text) {  const bytes = [...new TextEncoder().encode(text)];  let v = 1;  while (v <= 10 && dataCap(v) < bytes.length + 2 + (v > 9 ? 1 : 0)) v++;  if (v > 10) throw new Error("text too long for QR");  const bits = [], put = (n, l) => { for (let i = l - 1; i >= 0; i--) bits.push(n >> i & 1); };  put(4, 4); put(bytes.length, v > 9 ? 16 : 8); bytes.forEach((b) => put(b, 8));  const cap = dataCap(v) * 8;  put(0, Math.min(4, cap - bits.length)); while (bits.length % 8) bits.push(0);  for (let p = 0; bits.length < cap; p++) put(p % 2 ? 0x11 : 0xec, 8);  const cw = []; for (let i = 0; i < bits.length; i += 8) cw.push(parseInt(bits.slice(i, i + 8).join(""), 2));  const [ecn, groups] = EC[v], blocks = []; let o = 0;  for (const [c, d] of groups) for (let i = 0; i < c; i++) { const b = cw.slice(o, o += d); blocks.push([b, rs(b, ecn)]); }  const out = [], maxD = Math.max(...blocks.map((b) => b[0].length));  for (let i = 0; i < maxD; i++) blocks.forEach((b) => i < b[0].length && out.push(b[0][i]));  for (let i = 0; i < ecn; i++) blocks.forEach((b) => out.push(b[1][i]));  return matrix(v, out);}function matrix(v, cws) {  const n = 17 + 4 * v, m = Array.from({ length: n }, () => new Array(n).fill(false)), fn = m.map((r) => r.map(() => false));  const set = (x, y, val) => { if (x >= 0 && y >= 0 && x < n && y < n) { m[y][x] = val; fn[y][x] = true; } };  for (const [cx, cy] of [[0, 0], [n - 7, 0], [0, n - 7]])    for (let dy = -1; dy <= 7; dy++) for (let dx = -1; dx <= 7; dx++) {      const d = Math.max(Math.abs(dx - 3), Math.abs(dy - 3));      set(cx + dx, cy + dy, d !== 2 && d !== 4);    }  const a = ALIGN[v];  for (const ay of a) for (const ax of a) {    if (fn[ay][ax]) continue;    for (let dy = -2; dy <= 2; dy++) for (let dx = -2; dx <= 2; dx++) set(ax + dx, ay + dy, Math.max(Math.abs(dx), Math.abs(dy)) !== 1);  }  for (let i = 8; i < n - 8; i++) { set(i, 6, i % 2 === 0); set(6, i, i % 2 === 0); }  set(8, n - 8, true);  const fmt = (0b00 << 3 | 0) ; // ECC M = 00, mask 0  const f = ((fmt << 10) | bch(fmt, 0x537, 10)) ^ 0x5412;  for (let i = 0; i < 15; i++) {    const b = (f >> i & 1) === 1;    if (i < 6) set(8, i, b); else if (i < 8) set(8, i + 1, b); else if (i === 8) set(7, 8, b); else set(14 - i, 8, b);    set(i < 8 ? n - 1 - i : 8, i < 8 ? 8 : n - 15 + i, b);  }  set(8, n - 8, true);  if (v >= 7) {    const vi = (v << 12) | bch(v, 0x1f25, 12);    for (let i = 0; i < 18; i++) { const b = (vi >> i & 1) === 1, x = Math.floor(i / 3), y = i % 3 + n - 11; set(x, y, b); set(y, x, b); }  }  let i = 0;  for (let x = n - 1; x > 0; x -= 2) {    if (x === 6) x--;    for (let k = 0; k < n; k++) for (let c = 0; c < 2; c++) {      const xx = x - c, yy = ((x + 1) & 2) === 0 ? n - 1 - k : k;      if (fn[yy][xx]) continue;      const bit = i < cws.length * 8 && (cws[i >> 3] >> (7 - (i & 7)) & 1) === 1; i++;      m[yy][xx] = bit !== ((xx + yy) % 2 === 0);    }  }  return m;}export function toSvg(text, { dark = "#000", light = "#fff", quiet = 2 } = {}) {  const m = encode(text), s = m.length + quiet * 2; let d = "";  m.forEach((r, y) => r.forEach((b, x) => { if (b) d += `M${x + quiet} ${y + quiet}h1v1h-1z`; }));  return `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${s} ${s}" shape-rendering="crispEdges" role="img" aria-label="QR code"><rect width="${s}" height="${s}" fill="${light}"/><path d="${d}" fill="${dark}"/></svg>`;}