diesel-core
Version:
Web framework built on Web Standards
2 lines (1 loc) • 6.14 kB
JavaScript
function g(e){if(typeof e!=="string")throw new TypeError("Path must be a string. Received "+JSON.stringify(e))}function S(e,r){var n="",i=0,l=-1,t=0,f;for(var o=0;o<=e.length;++o){if(o<e.length)f=e.charCodeAt(o);else if(f===47)break;else f=47;if(f===47){if(l===o-1||t===1);else if(l!==o-1&&t===2){if(n.length<2||i!==2||n.charCodeAt(n.length-1)!==46||n.charCodeAt(n.length-2)!==46){if(n.length>2){var a=n.lastIndexOf("/");if(a!==n.length-1){if(a===-1)n="",i=0;else n=n.slice(0,a),i=n.length-1-n.lastIndexOf("/");l=o,t=0;continue}}else if(n.length===2||n.length===1){n="",i=0,l=o,t=0;continue}}if(r){if(n.length>0)n+="/..";else n="..";i=2}}else{if(n.length>0)n+="/"+e.slice(l+1,o);else n=e.slice(l+1,o);i=o-l-1}l=o,t=0}else if(f===46&&t!==-1)++t;else t=-1}return n}function T(e,r){var n=r.dir||r.root,i=r.base||(r.name||"")+(r.ext||"");if(!n)return i;if(n===r.root)return n+i;return n+e+i}function v(){var e="",r=!1,n;for(var i=arguments.length-1;i>=-1&&!r;i--){var l;if(i>=0)l=arguments[i];else{if(n===void 0)n=process.cwd();l=n}if(g(l),l.length===0)continue;e=l+"/"+e,r=l.charCodeAt(0)===47}if(e=S(e,!r),r)if(e.length>0)return"/"+e;else return"/";else if(e.length>0)return e;else return"."}function x(e){if(g(e),e.length===0)return".";var r=e.charCodeAt(0)===47,n=e.charCodeAt(e.length-1)===47;if(e=S(e,!r),e.length===0&&!r)e=".";if(e.length>0&&n)e+="/";if(r)return"/"+e;return e}function E(e){return g(e),e.length>0&&e.charCodeAt(0)===47}function V(){if(arguments.length===0)return".";var e;for(var r=0;r<arguments.length;++r){var n=arguments[r];if(g(n),n.length>0)if(e===void 0)e=n;else e+="/"+n}if(e===void 0)return".";return x(e)}function I(e,r){if(g(e),g(r),e===r)return"";if(e=v(e),r=v(r),e===r)return"";var n=1;for(;n<e.length;++n)if(e.charCodeAt(n)!==47)break;var i=e.length,l=i-n,t=1;for(;t<r.length;++t)if(r.charCodeAt(t)!==47)break;var f=r.length,o=f-t,a=l<o?l:o,d=-1,s=0;for(;s<=a;++s){if(s===a){if(o>a){if(r.charCodeAt(t+s)===47)return r.slice(t+s+1);else if(s===0)return r.slice(t+s)}else if(l>a){if(e.charCodeAt(n+s)===47)d=s;else if(s===0)d=0}break}var w=e.charCodeAt(n+s),R=r.charCodeAt(t+s);if(w!==R)break;else if(w===47)d=s}var c="";for(s=n+d+1;s<=i;++s)if(s===i||e.charCodeAt(s)===47)if(c.length===0)c+="..";else c+="/..";if(c.length>0)return c+r.slice(t+d);else{if(t+=d,r.charCodeAt(t)===47)++t;return r.slice(t)}}function L(e){return e}function P(e){if(g(e),e.length===0)return".";var r=e.charCodeAt(0),n=r===47,i=-1,l=!0;for(var t=e.length-1;t>=1;--t)if(r=e.charCodeAt(t),r===47){if(!l){i=t;break}}else l=!1;if(i===-1)return n?"/":".";if(n&&i===1)return"//";return e.slice(0,i)}function z(e,r){if(r!==void 0&&typeof r!=="string")throw new TypeError('"ext" argument must be a string');g(e);var n=0,i=-1,l=!0,t;if(r!==void 0&&r.length>0&&r.length<=e.length){if(r.length===e.length&&r===e)return"";var f=r.length-1,o=-1;for(t=e.length-1;t>=0;--t){var a=e.charCodeAt(t);if(a===47){if(!l){n=t+1;break}}else{if(o===-1)l=!1,o=t+1;if(f>=0)if(a===r.charCodeAt(f)){if(--f===-1)i=t}else f=-1,i=o}}if(n===i)i=o;else if(i===-1)i=e.length;return e.slice(n,i)}else{for(t=e.length-1;t>=0;--t)if(e.charCodeAt(t)===47){if(!l){n=t+1;break}}else if(i===-1)l=!1,i=t+1;if(i===-1)return"";return e.slice(n,i)}}function $(e){g(e);var r=-1,n=0,i=-1,l=!0,t=0;for(var f=e.length-1;f>=0;--f){var o=e.charCodeAt(f);if(o===47){if(!l){n=f+1;break}continue}if(i===-1)l=!1,i=f+1;if(o===46){if(r===-1)r=f;else if(t!==1)t=1}else if(r!==-1)t=-1}if(r===-1||i===-1||t===0||t===1&&r===i-1&&r===n+1)return"";return e.slice(r,i)}function H(e){if(e===null||typeof e!=="object")throw new TypeError('The "pathObject" argument must be of type Object. Received type '+typeof e);return T("/",e)}function J(e){g(e);var r={root:"",dir:"",base:"",ext:"",name:""};if(e.length===0)return r;var n=e.charCodeAt(0),i=n===47,l;if(i)r.root="/",l=1;else l=0;var t=-1,f=0,o=-1,a=!0,d=e.length-1,s=0;for(;d>=l;--d){if(n=e.charCodeAt(d),n===47){if(!a){f=d+1;break}continue}if(o===-1)a=!1,o=d+1;if(n===46){if(t===-1)t=d;else if(s!==1)s=1}else if(t!==-1)s=-1}if(t===-1||o===-1||s===0||s===1&&t===o-1&&t===f+1){if(o!==-1)if(f===0&&i)r.base=r.name=e.slice(1,o);else r.base=r.name=e.slice(f,o)}else{if(f===0&&i)r.name=e.slice(1,t),r.base=e.slice(1,o);else r.name=e.slice(f,t),r.base=e.slice(f,o);r.ext=e.slice(t,o)}if(f>0)r.dir=e.slice(0,f-1);else if(i)r.dir="/";return r}var M="/",X=":",q=((e)=>(e.posix=e,e))({resolve:v,normalize:x,isAbsolute:E,join:V,relative:I,_makeLong:L,dirname:P,basename:z,extname:$,format:H,parse:J,sep:M,delimiter:X,win32:null,posix:null}),m=q;var u=[];for(let e=0;e<256;++e)u.push((e+256).toString(16).slice(1));function k(e,r=0){return(u[e[r+0]]+u[e[r+1]]+u[e[r+2]]+u[e[r+3]]+"-"+u[e[r+4]]+u[e[r+5]]+"-"+u[e[r+6]]+u[e[r+7]]+"-"+u[e[r+8]]+u[e[r+9]]+"-"+u[e[r+10]]+u[e[r+11]]+u[e[r+12]]+u[e[r+13]]+u[e[r+14]]+u[e[r+15]]).toLowerCase()}var A,F=new Uint8Array(16);function y(){if(!A){if(typeof crypto==="undefined"||!crypto.getRandomValues)throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");A=crypto.getRandomValues.bind(crypto)}return A(F)}var G=typeof crypto!=="undefined"&&crypto.randomUUID&&crypto.randomUUID.bind(crypto),b={randomUUID:G};function K(e,r,n){if(b.randomUUID&&!r&&!e)return b.randomUUID();e=e||{};let i=e.random??e.rng?.()??y();if(i.length<16)throw new Error("Random bytes length must be >= 16");if(i[6]=i[6]&15|64,i[8]=i[8]&63|128,r){if(n=n||0,n<0||n+16>r.length)throw new RangeError(`UUID byte range ${n}:${n+15} is out of buffer bounds`);for(let l=0;l<16;++l)r[n+l]=i[l];return r}return k(i)}var C=K;var{default:U}=(()=>({}));var D=import.meta.dir,h=m.resolve(D,"../../public/uploaded");if(!U.existsSync(h))U.mkdirSync(h,{recursive:!0});var te=(e={})=>{let r=e.dest?m.resolve(D,e.dest):h;return async(n)=>{try{let i=await n.body;n.req.files??={};for(let l of e?.fields??[]){let t=i[l];if(!t.name)continue;let f=`${l}_${C()}${m.extname(t?.name)}`,o=m.join(r,f);await Bun.write(o,await t.arrayBuffer()),n.req.files[l]=o}}catch(i){return console.error("File upload error:",i),n.json({status:500,message:"Error uploading files"},500)}}};export{te as fileSaveMiddleware};