{"version":3,"file":"index.mjs","sources":["../src/index.ts"],"sourcesContent":["import type { ReadStream } from 'node:fs';\nimport { UTApi, UTFile } from \"uploadthing/server\"\nimport { Time } from '@uploadthing/shared';\nimport { UploadFileResult, UploadFilesOptions, FileEsque } from 'uploadthing/types';\n\nexport interface File {\n  name: string;\n  alternativeText?: string;\n  caption?: string;\n  width?: number;\n  height?: number;\n  formats?: Record<string, unknown>;\n  hash: string;\n  ext?: string;\n  mime: string;\n  size: number;\n  sizeInBytes: number;\n  url: string;\n  previewUrl?: string;\n  path?: string;\n  provider?: string;\n  provider_metadata?: Record<string, unknown>;\n  stream?: ReadStream;\n  buffer?: Buffer;\n}\n\n\nexport interface UTApiOptions {\n  apiKey?: string;\n  logLevel?: LogLevel;\n  defaultKeyType?: \"fileKey\" | \"customId\";\n  signedURLExpires?: Time;\n  ACL: string;\n}\n\nexport type LogLevel = \"error\" | \"warn\" | \"info\" | \"debug\" | \"trace\";\n\nexport type InitOptions = ({ uTOptions: UTApiOptions }) & {\n  [k: string]: any;\n}\n\n\nconst getConfig = ({ uTOptions, }: InitOptions) => {\n  const config = {\n    ...uTOptions,\n  };\n  return config;\n};\n\nexport default {\n  init({ uTOptions }: InitOptions) {\n    const config = getConfig({ uTOptions });\n    const utapi = new UTApi(config);\n\n    const upload = async (file: File) => {\n\n      try {\n\n        const utFile = new UTFile(\n          [Buffer.from(file.buffer as any, 'binary')],\n          file.name,\n        )\n        const utResponse: UploadFileResult = await utapi.uploadFiles(utFile);\n\n        if (!utResponse || utResponse.error) {\n          throw new Error(`Failed to upload image: ${utResponse?.error?.message || 'Unknown error'}`);\n        }\n\n        if (!utResponse.data) {\n          throw new Error(\"Failed to upload image: No data returned\");\n        }\n\n        const imageUrl = utResponse.data.url;\n        if (!imageUrl) {\n          throw new Error(\"Failed to upload image: url not returned\");\n        }\n\n        const key = utResponse.data.key;\n        if (!key) {\n          throw new Error(\"Failed to upload image: key not returned\");\n        }\n\n        file.url = utResponse.data?.url\n        file.previewUrl = utResponse.data?.url\n        file.provider_metadata = {\n          \"fileKey\": utResponse.data?.key,\n          \"customId\": utResponse.data?.customId\n        }\n\n      } catch (error) {\n        console.error(\"Error in uploading file:\", error);\n        if (error instanceof Error) {\n          console.error(\"Error message:\", error.message);\n          console.error(\"Error stack:\", error.stack);\n        }\n        throw error;\n      }\n    }\n\n    return {\n\n      async upload(file: File) {\n        return upload(file);\n      },\n\n      async delete(file: File) {\n        try {\n          const fileKey = file.provider_metadata?.fileKey as string;\n\n          if (!fileKey) {\n            throw new Error(\"Failed to upload image: key not returned\");\n          }\n\n          return await utapi.deleteFiles(fileKey);\n\n        } catch (error) {\n          console.error(\"Error in deleting file:\", error);\n          if (error instanceof Error) {\n            console.error(\"Error message:\", error.message);\n            console.error(\"Error stack:\", error.stack);\n          }\n          throw error;\n        }\n\n      },\n\n      async isPrivate() {\n        return false;\n      },\n\n      async getSignedUrl() {\n       \n      },\n\n    };\n  },\n};"],"names":[],"mappings":";AA0CA,MAAM,YAAY,CAAC,EAAE,gBAA8B;AACjD,QAAM,SAAS;AAAA,IACb,GAAG;AAAA,EAAA;AAEE,SAAA;AACT;AAEA,MAAe,QAAA;AAAA,EACb,KAAK,EAAE,aAA0B;AAC/B,UAAM,SAAS,UAAU,EAAE,UAAW,CAAA;AAChC,UAAA,QAAQ,IAAI,MAAM,MAAM;AAExB,UAAA,SAAS,OAAO,SAAe;AAE/B,UAAA;AAEF,cAAM,SAAS,IAAI;AAAA,UACjB,CAAC,OAAO,KAAK,KAAK,QAAe,QAAQ,CAAC;AAAA,UAC1C,KAAK;AAAA,QAAA;AAEP,cAAM,aAA+B,MAAM,MAAM,YAAY,MAAM;AAE/D,YAAA,CAAC,cAAc,WAAW,OAAO;AACnC,gBAAM,IAAI,MAAM,2BAA2B,YAAY,OAAO,WAAW,eAAe,EAAE;AAAA,QAC5F;AAEI,YAAA,CAAC,WAAW,MAAM;AACd,gBAAA,IAAI,MAAM,0CAA0C;AAAA,QAC5D;AAEM,cAAA,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