import{a as p}from"./index-ec1be37d.js";import{c,g as E,d as D,e as f,f as L,h as w}from"./axios_helper-aabbd1f9.js";import{d as x}from"./string-5478061d.js";const A=(e,t)=>{const a=e instanceof Error?e.toString():e;let o="";return t&&(t instanceof Error||typeof t=="string")&&(o=` ${t.toString()}`),new Error(`${a}${o}`)},_=["image/jpg","image/jpeg","image/pjpeg","image/png","image/apng","image/x-png","image/gif","image/webp"],u=["video/mp4","video/quicktime","video/webm"],U=u,O=[..._,...u],d=(e,t=!0,a=!1,o=!1)=>({dimensions:{maxHeight:16e3,maxWidth:16e3,minHeight:o?50:300,minWidth:o?50:400},maxFileSize:10485760,maxVideoFileSize:a?104857600:20971520,mediaConstraints:{...t&&{animation:{exactDimensions:["400x300","800x600","1600x1200"]}},video:{aspectRatio:t}},permittedMimeTypes:[..._,...e?u:[]]}),b={maxFileSize:104857600},y=e=>e?8:1,S=e=>e?5:1,T=async(e=!1,t="default")=>{var a;try{const o=await p.get("/client_app/js_config/direct_upload",{params:{kind:t,...e&&{is_job:e}}}),i=(a=o.data)==null?void 0:a.data;if(!i)throw new Error(`Metadata not found: ${JSON.stringify(o)}`);return i}catch(o){throw A("Getting direct upload meta data failed",o)}},z=({uploadComplete:e,uploadFailed:t,bytesUploaded:a=0,bytesTotal:o=1})=>{let i=0;return!e&&!t&&(i=a/o*100),i},$=({error:e,fileName:t,context:a,isPro:o,isVideoIntroductions:i,isAvatar:r,maxFileSize:n})=>{let s="";switch(e instanceof Error?e.message:e){case"INVALID_MAX_FILES":o?(a==="media"&&(s=`Whoa, there! That's a lot of files. You can only upload ${y(o)} per shot.`),a==="downloads"&&(s=`Whoa, there! That's a lot of files. You can only upload ${S(o)} downloads per shot.`)):s='Upgrade to Pro to add more than one image to a shot! Go Pro';break;case"INVALID_MAX_VIDEO_FILE_SIZE":s=`Your file needs to be under ${c(d(o,!0,i).maxVideoFileSize)}`;break;case"INVALID_MAX_FILE_SIZE":a==="media"&&(s=`Your file needs to be under ${c(n||d(o).maxFileSize)}`),a==="downloads"&&(s=`Your file needs to be under ${c(n||b.maxFileSize)}`);break;case"INVALID_MAX_DIMENSIONS":s=`Your file's dimensions should be no more than ${d(o).dimensions.maxWidth}x${d(o).dimensions.maxHeight}.`;break;case"INVALID_MAX_FILE_DURATION":s="Your video duration can't be longer than 60 seconds.";break;case"INVALID_MIN_DIMENSIONS":s=`Go big or go home. Your file's dimensions should be at least ${d(o,!0,!1,r).dimensions.minWidth}x${d(o,!0,!1,r).dimensions.minHeight}.`;break;case"INVALID_EXACT_FILE_RATIO":s="Your file is not in a 4:3 aspect ratio.";break;case"INVALID_EXACT_FILE_DIMENSIONS":s="Your animated file must be 400x300, 800x600, or 1600x1200.";break;case"INVALID_FILE_TYPES":s="We don't support this file type";break;case"INVALID_VIDEO_FILE":s='Upgrade to Pro to upload videos. Go Pro';break;case"FILE_CORRUPT":s="Your file appears to be corrupted.";break;case"UNKNOWN":case"UPLOAD_FAILED":default:s="Uh oh! Something went wrong with the upload. Please try again."}Dribbble.EventBus.$emit("siteNotifications:add",{message:s,title:t||"Upload error"})},N=e=>{const t=e.split("/");return t.slice(t.length-2).join("/")},C=async({file:e,generatePreviews:t})=>{const a=E(e);let o;e.name?o=e.name:a.split("/")[0]==="image"?o=`${a.split("/")[0]}.${a.split("/")[1]}`:o="noname";const i=Number.isFinite(e.size)?e.size:null,r=f(o).extension||null;if(!i||!r)throw new Error("FILE_CORRUPT");let n={source:e.source||"",id:L(),name:o,extension:f(o).extension||"",type:a,data:e,progress:{bytesUploaded:0,bytesTotal:0,uploadComplete:!1,uploadStarted:!1},size:i,sizeReadable:c(i),remote:e.remote||""};return t&&(n={...n,keepOriginal:!1,croppedDetails:{},previewCropped:null,preview:null,isSelected:!1}),n},Y=(e,t)=>{const a=w(e.type);return new Promise((o,i)=>{t.permittedMimeTypes&&(t.permittedMimeTypes.some(n=>n===e.type)||i(a==="video"?new Error("INVALID_VIDEO_FILE"):new Error("INVALID_FILE_TYPES"))),a==="video"&&t.maxVideoFileSize!==void 0?e.size>t.maxVideoFileSize&&i(new Error("INVALID_MAX_VIDEO_FILE_SIZE")):t.maxFileSize!==void 0&&e.size>t.maxFileSize&&i(new Error("INVALID_MAX_FILE_SIZE")),o()})},P=e=>e.split(".").pop().split("?")[0],R=async({file:e,uploadMetaData:t=null,onInit:a=()=>{},onProgress:o=()=>{},onSuccess:i=()=>{},kind:r,maxFileSize:n})=>{const I=p.CancelToken.source();a({file:e,uploadCancelTokenSource:I}),t=t||await T(!1,r);const{s3Url:g,s3FormData:h}=t;if(!t)throw new Error("UPLOAD_FAILED");if(e.size>(n||d().maxFileSize))throw new Error("INVALID_MAX_FILE_SIZE");const F={headers:{"Content-Type":E(e)},timeout:3e5,cancelToken:I.token,onUploadProgress:({total:l=1,loaded:m=0})=>{o({file:e,progressPercent:m/l*100})}};try{const l=await p.post(g,D(e,h),F),m=x(l.data.match(/(.*?)<\/Key>/)[1]);if(!m)throw new Error("UPLOAD_FAILED");i({file:e,s3Url:g,s3FileDetailsId:N(m),s3FullUrl:`${g}/${m}`})}catch(l){if(!p.isCancel(l))throw new Error("UPLOAD_FAILED")}};export{T as a,z as b,Y as c,N as d,P as e,C as f,$ as g,d as h,O as i,U as p,R as u}; //# sourceMappingURL=file-upload-33be919a.js.map