UNPKG

@squarecloud/blob

Version:
1 lines 2.76 kB
{"version":3,"sources":["../../src/structures/error.ts","../../src/utils/object-url.ts"],"sourcesContent":["export class SquareCloudBlobError extends Error {\n\tconstructor(code: string, message?: string, cause?: unknown) {\n\t\tsuper(message, { cause });\n\n\t\tthis.name = SquareCloudBlobError.name;\n\t\tthis.message = this.getMessage(code);\n\t}\n\n\tprivate getMessage(rawCode: string) {\n\t\tconst code = rawCode\n\t\t\t.replaceAll(\"_\", \" \")\n\t\t\t.toLowerCase()\n\t\t\t.replace(/(^|\\s)\\S/g, (L) => L.toUpperCase());\n\t\tconst message = this.message ? `: ${this.message}` : \"\";\n\n\t\treturn `${code}${message}`;\n\t}\n}\n\nexport class SquareCloudValidationError extends SquareCloudBlobError {\n\tconstructor(...args: ConstructorParameters<typeof SquareCloudBlobError>) {\n\t\tsuper(...args);\n\t\tthis.name = SquareCloudValidationError.name;\n\t}\n}\n","import { SquareCloudBlobError } from \"../structures/error\";\n\nconst objectUrlRegex =\n\t/^(?<url>https:\\/\\/public-blob\\.squarecloud\\.dev)?\\/?(?<userId>\\d+\\/)(?<prefix>[\\w\\d-_]+\\/)?(?<name>[\\w\\d_]+)-(?<hash>[\\w\\d]+)\\.(?<extension>\\w+)$/;\n\n/**\n * Parses the object URL to extract id, userId, prefix, name, hash and extension.\n *\n * @param url - The object URL to parse.\n */\nexport function parseObjectUrl(url: string) {\n\tconst match = url.match(objectUrlRegex);\n\n\tif (!match?.groups) {\n\t\tthrow new SquareCloudBlobError(\"Invalid object URL\");\n\t}\n\n\tconst payload = {\n\t\tuserId: match.groups.userId.replace(\"/\", \"\"),\n\t\tprefix: match.groups.prefix?.replace(\"/\", \"\"),\n\t\tname: match.groups.name,\n\t\thash: match.groups.hash,\n\t\textension: match.groups.extension,\n\t};\n\n\treturn {\n\t\tid: `${payload.userId}/${payload.prefix ? `${payload.prefix}/` : \"\"}${\n\t\t\tpayload.name\n\t\t}-${payload.hash}.${payload.extension}`,\n\t\t...payload,\n\t};\n}\n"],"mappings":";AAAO,IAAM,uBAAN,MAAM,8BAA6B,MAAM;AAAA,EAC/C,YAAY,MAAc,SAAkB,OAAiB;AAC5D,UAAM,SAAS,EAAE,MAAM,CAAC;AAExB,SAAK,OAAO,sBAAqB;AACjC,SAAK,UAAU,KAAK,WAAW,IAAI;AAAA,EACpC;AAAA,EAEQ,WAAW,SAAiB;AACnC,UAAM,OAAO,QACX,WAAW,KAAK,GAAG,EACnB,YAAY,EACZ,QAAQ,aAAa,CAAC,MAAM,EAAE,YAAY,CAAC;AAC7C,UAAM,UAAU,KAAK,UAAU,KAAK,KAAK,OAAO,KAAK;AAErD,WAAO,GAAG,IAAI,GAAG,OAAO;AAAA,EACzB;AACD;;;ACfA,IAAM,iBACL;AAOM,SAAS,eAAe,KAAa;AAC3C,QAAM,QAAQ,IAAI,MAAM,cAAc;AAEtC,MAAI,CAAC,OAAO,QAAQ;AACnB,UAAM,IAAI,qBAAqB,oBAAoB;AAAA,EACpD;AAEA,QAAM,UAAU;AAAA,IACf,QAAQ,MAAM,OAAO,OAAO,QAAQ,KAAK,EAAE;AAAA,IAC3C,QAAQ,MAAM,OAAO,QAAQ,QAAQ,KAAK,EAAE;AAAA,IAC5C,MAAM,MAAM,OAAO;AAAA,IACnB,MAAM,MAAM,OAAO;AAAA,IACnB,WAAW,MAAM,OAAO;AAAA,EACzB;AAEA,SAAO;AAAA,IACN,IAAI,GAAG,QAAQ,MAAM,IAAI,QAAQ,SAAS,GAAG,QAAQ,MAAM,MAAM,EAAE,GAClE,QAAQ,IACT,IAAI,QAAQ,IAAI,IAAI,QAAQ,SAAS;AAAA,IACrC,GAAG;AAAA,EACJ;AACD;","names":[]}