@m5kdev/backend 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.cursor/rules/backend.mdc +70 -0
- package/.turbo/turbo-build.log +5 -0
- package/.turbo/turbo-check-types.log +5 -0
- package/.turbo/turbo-lint$colon$fix.log +255 -0
- package/CHANGELOG.md +19 -0
- package/dist/src/lib/posthog.d.ts +3 -0
- package/dist/src/lib/posthog.d.ts.map +1 -0
- package/dist/src/lib/posthog.js +7 -0
- package/dist/src/lib/sentry.d.ts +2 -0
- package/dist/src/lib/sentry.d.ts.map +1 -0
- package/dist/src/lib/sentry.js +9 -0
- package/dist/src/modules/access/access.repository.d.ts +2348 -0
- package/dist/src/modules/access/access.repository.d.ts.map +1 -0
- package/dist/src/modules/access/access.repository.js +32 -0
- package/dist/src/modules/access/access.service.d.ts +22 -0
- package/dist/src/modules/access/access.service.d.ts.map +1 -0
- package/dist/src/modules/access/access.service.js +51 -0
- package/dist/src/modules/access/access.test.d.ts +2 -0
- package/dist/src/modules/access/access.test.d.ts.map +1 -0
- package/dist/src/modules/access/access.test.js +182 -0
- package/dist/src/modules/access/access.utils.d.ts +17 -0
- package/dist/src/modules/access/access.utils.d.ts.map +1 -0
- package/dist/src/modules/access/access.utils.js +20 -0
- package/dist/src/modules/ai/ai.db.d.ts +396 -0
- package/dist/src/modules/ai/ai.db.d.ts.map +1 -0
- package/dist/src/modules/ai/ai.db.js +39 -0
- package/dist/src/modules/ai/ai.prompt.d.ts +28 -0
- package/dist/src/modules/ai/ai.prompt.d.ts.map +1 -0
- package/dist/src/modules/ai/ai.prompt.js +30 -0
- package/dist/src/modules/ai/ai.repository.d.ts +424 -0
- package/dist/src/modules/ai/ai.repository.d.ts.map +1 -0
- package/dist/src/modules/ai/ai.repository.js +26 -0
- package/dist/src/modules/ai/ai.router.d.ts +2 -0
- package/dist/src/modules/ai/ai.router.d.ts.map +1 -0
- package/dist/src/modules/ai/ai.router.js +132 -0
- package/dist/src/modules/ai/ai.service.d.ts +115 -0
- package/dist/src/modules/ai/ai.service.d.ts.map +1 -0
- package/dist/src/modules/ai/ai.service.js +207 -0
- package/dist/src/modules/ai/ai.trpc.d.ts +59 -0
- package/dist/src/modules/ai/ai.trpc.d.ts.map +1 -0
- package/dist/src/modules/ai/ai.trpc.js +20 -0
- package/dist/src/modules/ai/ideogram/ideogram.constants.d.ts +8 -0
- package/dist/src/modules/ai/ideogram/ideogram.constants.d.ts.map +1 -0
- package/dist/src/modules/ai/ideogram/ideogram.constants.js +167 -0
- package/dist/src/modules/ai/ideogram/ideogram.dto.d.ts +230 -0
- package/dist/src/modules/ai/ideogram/ideogram.dto.d.ts.map +1 -0
- package/dist/src/modules/ai/ideogram/ideogram.dto.js +49 -0
- package/dist/src/modules/ai/ideogram/ideogram.prompt.d.ts +3 -0
- package/dist/src/modules/ai/ideogram/ideogram.prompt.d.ts.map +1 -0
- package/dist/src/modules/ai/ideogram/ideogram.prompt.js +860 -0
- package/dist/src/modules/ai/ideogram/ideogram.repository.d.ts +7 -0
- package/dist/src/modules/ai/ideogram/ideogram.repository.d.ts.map +1 -0
- package/dist/src/modules/ai/ideogram/ideogram.repository.js +46 -0
- package/dist/src/modules/ai/ideogram/ideogram.service.d.ts +10 -0
- package/dist/src/modules/ai/ideogram/ideogram.service.d.ts.map +1 -0
- package/dist/src/modules/ai/ideogram/ideogram.service.js +11 -0
- package/dist/src/modules/auth/auth.db.d.ts +2336 -0
- package/dist/src/modules/auth/auth.db.d.ts.map +1 -0
- package/dist/src/modules/auth/auth.db.js +215 -0
- package/dist/src/modules/auth/auth.dto.d.ts +66 -0
- package/dist/src/modules/auth/auth.dto.d.ts.map +1 -0
- package/dist/src/modules/auth/auth.dto.js +38 -0
- package/dist/src/modules/auth/auth.lib.d.ts +4874 -0
- package/dist/src/modules/auth/auth.lib.d.ts.map +1 -0
- package/dist/src/modules/auth/auth.lib.js +284 -0
- package/dist/src/modules/auth/auth.middleware.d.ts +615 -0
- package/dist/src/modules/auth/auth.middleware.d.ts.map +1 -0
- package/dist/src/modules/auth/auth.middleware.js +52 -0
- package/dist/src/modules/auth/auth.repository.d.ts +2417 -0
- package/dist/src/modules/auth/auth.repository.d.ts.map +1 -0
- package/dist/src/modules/auth/auth.repository.js +541 -0
- package/dist/src/modules/auth/auth.service.d.ts +104 -0
- package/dist/src/modules/auth/auth.service.d.ts.map +1 -0
- package/dist/src/modules/auth/auth.service.js +201 -0
- package/dist/src/modules/auth/auth.trpc.d.ts +309 -0
- package/dist/src/modules/auth/auth.trpc.d.ts.map +1 -0
- package/dist/src/modules/auth/auth.trpc.js +157 -0
- package/dist/src/modules/auth/auth.utils.d.ts +2352 -0
- package/dist/src/modules/auth/auth.utils.d.ts.map +1 -0
- package/dist/src/modules/auth/auth.utils.js +97 -0
- package/dist/src/modules/base/base.abstract.d.ts +19 -0
- package/dist/src/modules/base/base.abstract.d.ts.map +1 -0
- package/dist/src/modules/base/base.abstract.js +53 -0
- package/dist/src/modules/base/base.dto.d.ts +70 -0
- package/dist/src/modules/base/base.dto.d.ts.map +1 -0
- package/dist/src/modules/base/base.dto.js +112 -0
- package/dist/src/modules/base/base.grants.d.ts +29 -0
- package/dist/src/modules/base/base.grants.d.ts.map +1 -0
- package/dist/src/modules/base/base.grants.js +123 -0
- package/dist/src/modules/base/base.grants.test.d.ts +2 -0
- package/dist/src/modules/base/base.grants.test.d.ts.map +1 -0
- package/dist/src/modules/base/base.grants.test.js +668 -0
- package/dist/src/modules/base/base.repository.d.ts +97 -0
- package/dist/src/modules/base/base.repository.d.ts.map +1 -0
- package/dist/src/modules/base/base.repository.js +307 -0
- package/dist/src/modules/base/base.service.d.ts +42 -0
- package/dist/src/modules/base/base.service.d.ts.map +1 -0
- package/dist/src/modules/base/base.service.js +109 -0
- package/dist/src/modules/base/base.types.d.ts +2 -0
- package/dist/src/modules/base/base.types.d.ts.map +1 -0
- package/dist/src/modules/base/base.types.js +2 -0
- package/dist/src/modules/billing/billing.db.d.ts +366 -0
- package/dist/src/modules/billing/billing.db.d.ts.map +1 -0
- package/dist/src/modules/billing/billing.db.js +29 -0
- package/dist/src/modules/billing/billing.repository.d.ts +2764 -0
- package/dist/src/modules/billing/billing.repository.d.ts.map +1 -0
- package/dist/src/modules/billing/billing.repository.js +235 -0
- package/dist/src/modules/billing/billing.router.d.ts +5 -0
- package/dist/src/modules/billing/billing.router.d.ts.map +1 -0
- package/dist/src/modules/billing/billing.router.js +56 -0
- package/dist/src/modules/billing/billing.service.d.ts +60 -0
- package/dist/src/modules/billing/billing.service.d.ts.map +1 -0
- package/dist/src/modules/billing/billing.service.js +147 -0
- package/dist/src/modules/billing/billing.trpc.d.ts +75 -0
- package/dist/src/modules/billing/billing.trpc.d.ts.map +1 -0
- package/dist/src/modules/billing/billing.trpc.js +17 -0
- package/dist/src/modules/clay/clay.repository.d.ts +6 -0
- package/dist/src/modules/clay/clay.repository.d.ts.map +1 -0
- package/dist/src/modules/clay/clay.repository.js +26 -0
- package/dist/src/modules/clay/clay.service.d.ts +29 -0
- package/dist/src/modules/clay/clay.service.d.ts.map +1 -0
- package/dist/src/modules/clay/clay.service.js +24 -0
- package/dist/src/modules/connect/connect.db.d.ts +357 -0
- package/dist/src/modules/connect/connect.db.d.ts.map +1 -0
- package/dist/src/modules/connect/connect.db.js +30 -0
- package/dist/src/modules/connect/connect.dto.d.ts +75 -0
- package/dist/src/modules/connect/connect.dto.d.ts.map +1 -0
- package/dist/src/modules/connect/connect.dto.js +36 -0
- package/dist/src/modules/connect/connect.linkedin.d.ts +3 -0
- package/dist/src/modules/connect/connect.linkedin.d.ts.map +1 -0
- package/dist/src/modules/connect/connect.linkedin.js +53 -0
- package/dist/src/modules/connect/connect.oauth.d.ts +28 -0
- package/dist/src/modules/connect/connect.oauth.d.ts.map +1 -0
- package/dist/src/modules/connect/connect.oauth.js +198 -0
- package/dist/src/modules/connect/connect.repository.d.ts +414 -0
- package/dist/src/modules/connect/connect.repository.d.ts.map +1 -0
- package/dist/src/modules/connect/connect.repository.js +54 -0
- package/dist/src/modules/connect/connect.router.d.ts +5 -0
- package/dist/src/modules/connect/connect.router.d.ts.map +1 -0
- package/dist/src/modules/connect/connect.router.js +54 -0
- package/dist/src/modules/connect/connect.service.d.ts +89 -0
- package/dist/src/modules/connect/connect.service.d.ts.map +1 -0
- package/dist/src/modules/connect/connect.service.js +114 -0
- package/dist/src/modules/connect/connect.trpc.d.ts +81 -0
- package/dist/src/modules/connect/connect.trpc.d.ts.map +1 -0
- package/dist/src/modules/connect/connect.trpc.js +21 -0
- package/dist/src/modules/connect/connect.types.d.ts +26 -0
- package/dist/src/modules/connect/connect.types.d.ts.map +1 -0
- package/dist/src/modules/connect/connect.types.js +2 -0
- package/dist/src/modules/crypto/crypto.db.d.ts +152 -0
- package/dist/src/modules/crypto/crypto.db.d.ts.map +1 -0
- package/dist/src/modules/crypto/crypto.db.js +17 -0
- package/dist/src/modules/crypto/crypto.repository.d.ts +160 -0
- package/dist/src/modules/crypto/crypto.repository.d.ts.map +1 -0
- package/dist/src/modules/crypto/crypto.repository.js +10 -0
- package/dist/src/modules/crypto/crypto.service.d.ts +11 -0
- package/dist/src/modules/crypto/crypto.service.d.ts.map +1 -0
- package/dist/src/modules/crypto/crypto.service.js +52 -0
- package/dist/src/modules/email/email.service.d.ts +57 -0
- package/dist/src/modules/email/email.service.d.ts.map +1 -0
- package/dist/src/modules/email/email.service.js +107 -0
- package/dist/src/modules/file/file.repository.d.ts +13 -0
- package/dist/src/modules/file/file.repository.d.ts.map +1 -0
- package/dist/src/modules/file/file.repository.js +79 -0
- package/dist/src/modules/file/file.router.d.ts +4 -0
- package/dist/src/modules/file/file.router.d.ts.map +1 -0
- package/dist/src/modules/file/file.router.js +99 -0
- package/dist/src/modules/file/file.service.d.ts +25 -0
- package/dist/src/modules/file/file.service.d.ts.map +1 -0
- package/dist/src/modules/file/file.service.js +150 -0
- package/dist/src/modules/recurrence/recurrence.db.d.ts +563 -0
- package/dist/src/modules/recurrence/recurrence.db.d.ts.map +1 -0
- package/dist/src/modules/recurrence/recurrence.db.js +66 -0
- package/dist/src/modules/recurrence/recurrence.repository.d.ts +585 -0
- package/dist/src/modules/recurrence/recurrence.repository.d.ts.map +1 -0
- package/dist/src/modules/recurrence/recurrence.repository.js +39 -0
- package/dist/src/modules/recurrence/recurrence.service.d.ts +30 -0
- package/dist/src/modules/recurrence/recurrence.service.d.ts.map +1 -0
- package/dist/src/modules/recurrence/recurrence.service.js +70 -0
- package/dist/src/modules/recurrence/recurrence.trpc.d.ts +243 -0
- package/dist/src/modules/recurrence/recurrence.trpc.d.ts.map +1 -0
- package/dist/src/modules/recurrence/recurrence.trpc.js +65 -0
- package/dist/src/modules/social/social.dto.d.ts +35 -0
- package/dist/src/modules/social/social.dto.d.ts.map +1 -0
- package/dist/src/modules/social/social.dto.js +18 -0
- package/dist/src/modules/social/social.linkedin.d.ts +11 -0
- package/dist/src/modules/social/social.linkedin.d.ts.map +1 -0
- package/dist/src/modules/social/social.linkedin.js +427 -0
- package/dist/src/modules/social/social.linkedin.test.d.ts +2 -0
- package/dist/src/modules/social/social.linkedin.test.d.ts.map +1 -0
- package/dist/src/modules/social/social.linkedin.test.js +235 -0
- package/dist/src/modules/social/social.service.d.ts +29 -0
- package/dist/src/modules/social/social.service.d.ts.map +1 -0
- package/dist/src/modules/social/social.service.js +76 -0
- package/dist/src/modules/social/social.types.d.ts +36 -0
- package/dist/src/modules/social/social.types.d.ts.map +1 -0
- package/dist/src/modules/social/social.types.js +2 -0
- package/dist/src/modules/tag/tag.db.d.ts +347 -0
- package/dist/src/modules/tag/tag.db.d.ts.map +1 -0
- package/dist/src/modules/tag/tag.db.js +42 -0
- package/dist/src/modules/tag/tag.dto.d.ts +1019 -0
- package/dist/src/modules/tag/tag.dto.d.ts.map +1 -0
- package/dist/src/modules/tag/tag.dto.js +9 -0
- package/dist/src/modules/tag/tag.repository.d.ts +384 -0
- package/dist/src/modules/tag/tag.repository.d.ts.map +1 -0
- package/dist/src/modules/tag/tag.repository.js +154 -0
- package/dist/src/modules/tag/tag.service.d.ts +36 -0
- package/dist/src/modules/tag/tag.service.d.ts.map +1 -0
- package/dist/src/modules/tag/tag.service.js +31 -0
- package/dist/src/modules/tag/tag.trpc.d.ts +191 -0
- package/dist/src/modules/tag/tag.trpc.d.ts.map +1 -0
- package/dist/src/modules/tag/tag.trpc.js +47 -0
- package/dist/src/modules/utils/applyPagination.d.ts +7 -0
- package/dist/src/modules/utils/applyPagination.d.ts.map +1 -0
- package/dist/src/modules/utils/applyPagination.js +16 -0
- package/dist/src/modules/utils/applySorting.d.ts +9 -0
- package/dist/src/modules/utils/applySorting.d.ts.map +1 -0
- package/dist/src/modules/utils/applySorting.js +18 -0
- package/dist/src/modules/utils/getConditionsFromFilters.d.ts +5 -0
- package/dist/src/modules/utils/getConditionsFromFilters.d.ts.map +1 -0
- package/dist/src/modules/utils/getConditionsFromFilters.js +200 -0
- package/dist/src/modules/video/video.service.d.ts +8 -0
- package/dist/src/modules/video/video.service.d.ts.map +1 -0
- package/dist/src/modules/video/video.service.js +84 -0
- package/dist/src/modules/webhook/webhook.constants.d.ts +9 -0
- package/dist/src/modules/webhook/webhook.constants.d.ts.map +1 -0
- package/dist/src/modules/webhook/webhook.constants.js +10 -0
- package/dist/src/modules/webhook/webhook.db.d.ts +137 -0
- package/dist/src/modules/webhook/webhook.db.d.ts.map +1 -0
- package/dist/src/modules/webhook/webhook.db.js +17 -0
- package/dist/src/modules/webhook/webhook.dto.d.ts +395 -0
- package/dist/src/modules/webhook/webhook.dto.d.ts.map +1 -0
- package/dist/src/modules/webhook/webhook.dto.js +7 -0
- package/dist/src/modules/webhook/webhook.repository.d.ts +149 -0
- package/dist/src/modules/webhook/webhook.repository.d.ts.map +1 -0
- package/dist/src/modules/webhook/webhook.repository.js +56 -0
- package/dist/src/modules/webhook/webhook.router.d.ts +4 -0
- package/dist/src/modules/webhook/webhook.router.d.ts.map +1 -0
- package/dist/src/modules/webhook/webhook.router.js +30 -0
- package/dist/src/modules/webhook/webhook.service.d.ts +10 -0
- package/dist/src/modules/webhook/webhook.service.d.ts.map +1 -0
- package/dist/src/modules/webhook/webhook.service.js +68 -0
- package/dist/src/modules/workflow/workflow.db.d.ts +297 -0
- package/dist/src/modules/workflow/workflow.db.d.ts.map +1 -0
- package/dist/src/modules/workflow/workflow.db.js +30 -0
- package/dist/src/modules/workflow/workflow.repository.d.ts +344 -0
- package/dist/src/modules/workflow/workflow.repository.d.ts.map +1 -0
- package/dist/src/modules/workflow/workflow.repository.js +105 -0
- package/dist/src/modules/workflow/workflow.service.d.ts +22 -0
- package/dist/src/modules/workflow/workflow.service.d.ts.map +1 -0
- package/dist/src/modules/workflow/workflow.service.js +37 -0
- package/dist/src/modules/workflow/workflow.trpc.d.ts +93 -0
- package/dist/src/modules/workflow/workflow.trpc.d.ts.map +1 -0
- package/dist/src/modules/workflow/workflow.trpc.js +21 -0
- package/dist/src/modules/workflow/workflow.types.d.ts +21 -0
- package/dist/src/modules/workflow/workflow.types.d.ts.map +1 -0
- package/dist/src/modules/workflow/workflow.types.js +2 -0
- package/dist/src/modules/workflow/workflow.utils.d.ts +22 -0
- package/dist/src/modules/workflow/workflow.utils.d.ts.map +1 -0
- package/dist/src/modules/workflow/workflow.utils.js +173 -0
- package/dist/src/test/stubs/utils.d.ts +3 -0
- package/dist/src/test/stubs/utils.d.ts.map +1 -0
- package/dist/src/test/stubs/utils.js +5 -0
- package/dist/src/trpc/context.d.ts +42 -0
- package/dist/src/trpc/context.d.ts.map +1 -0
- package/dist/src/trpc/context.js +17 -0
- package/dist/src/trpc/index.d.ts +4 -0
- package/dist/src/trpc/index.d.ts.map +1 -0
- package/dist/src/trpc/index.js +6 -0
- package/dist/src/trpc/procedures.d.ts +234 -0
- package/dist/src/trpc/procedures.d.ts.map +1 -0
- package/dist/src/trpc/procedures.js +32 -0
- package/dist/src/trpc/utils.d.ts +5 -0
- package/dist/src/trpc/utils.d.ts.map +1 -0
- package/dist/src/trpc/utils.js +20 -0
- package/dist/src/types.d.ts +486 -0
- package/dist/src/types.d.ts.map +1 -0
- package/dist/src/types.js +13 -0
- package/dist/src/utils/errors.d.ts +50 -0
- package/dist/src/utils/errors.d.ts.map +1 -0
- package/dist/src/utils/errors.js +104 -0
- package/dist/src/utils/logger.d.ts +2 -0
- package/dist/src/utils/logger.d.ts.map +1 -0
- package/dist/src/utils/logger.js +11 -0
- package/dist/src/utils/posthog.d.ts +14 -0
- package/dist/src/utils/posthog.d.ts.map +1 -0
- package/dist/src/utils/posthog.js +31 -0
- package/dist/src/utils/types.d.ts +5 -0
- package/dist/src/utils/types.d.ts.map +1 -0
- package/dist/src/utils/types.js +2 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/jest.config.ts +19 -0
- package/package.json +3 -6
- package/tsconfig.json +21 -0
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.FileService = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const node_fs_1 = require("node:fs");
|
|
6
|
+
const promises_1 = require("node:fs/promises");
|
|
7
|
+
const node_os_1 = require("node:os");
|
|
8
|
+
const node_path_1 = tslib_1.__importStar(require("node:path"));
|
|
9
|
+
const node_stream_1 = require("node:stream");
|
|
10
|
+
const promises_2 = require("node:stream/promises");
|
|
11
|
+
const file_constants_1 = require("@m5kdev/commons/modules/file/file.constants");
|
|
12
|
+
const neverthrow_1 = require("neverthrow");
|
|
13
|
+
const uuid_1 = require("uuid");
|
|
14
|
+
const base_service_1 = require("#modules/base/base.service");
|
|
15
|
+
class FileService extends base_service_1.BaseService {
|
|
16
|
+
isS3Path(path) {
|
|
17
|
+
return path.startsWith("s3::");
|
|
18
|
+
}
|
|
19
|
+
parseS3Path(S3Path) {
|
|
20
|
+
if (!this.isS3Path(S3Path)) {
|
|
21
|
+
return this.error("BAD_REQUEST", "Invalid S3 path");
|
|
22
|
+
}
|
|
23
|
+
const [bucket, path] = S3Path.split("s3::")[1].split("//");
|
|
24
|
+
return (0, neverthrow_1.ok)({ bucket, path });
|
|
25
|
+
}
|
|
26
|
+
wrapS3Path(path, bucket) {
|
|
27
|
+
return `s3::${bucket}//${path}`;
|
|
28
|
+
}
|
|
29
|
+
getS3UploadUrl(filename, filetype, expiresIn = 60 * 5) {
|
|
30
|
+
return this.repository.file.getS3UploadUrl(filename, filetype, expiresIn);
|
|
31
|
+
}
|
|
32
|
+
getS3DownloadUrl(path, expiresIn = 60 * 5) {
|
|
33
|
+
return this.repository.file.getS3DownloadUrl(path, expiresIn);
|
|
34
|
+
}
|
|
35
|
+
getS3Object(path) {
|
|
36
|
+
return this.repository.file.getS3Object(path);
|
|
37
|
+
}
|
|
38
|
+
deleteS3Object(path) {
|
|
39
|
+
return this.repository.file.deleteS3Object(path);
|
|
40
|
+
}
|
|
41
|
+
async uploadFileToS3(localPath, returnDownloadUrl = false) {
|
|
42
|
+
return this.throwableAsync(async () => {
|
|
43
|
+
const extension = localPath.split(".").pop()?.toLowerCase();
|
|
44
|
+
const filename = `${(0, uuid_1.v4)()}${extension ? `.${extension}` : ""}`;
|
|
45
|
+
const mimeByExt = {
|
|
46
|
+
jpg: "image/jpeg",
|
|
47
|
+
jpeg: "image/jpeg",
|
|
48
|
+
png: "image/png",
|
|
49
|
+
webp: "image/webp",
|
|
50
|
+
mp4: "video/mp4",
|
|
51
|
+
mov: "video/mov",
|
|
52
|
+
avi: "video/avi",
|
|
53
|
+
mkv: "video/mkv",
|
|
54
|
+
webm: "video/webm",
|
|
55
|
+
mp3: "audio/mp3",
|
|
56
|
+
wav: "audio/wav",
|
|
57
|
+
m4a: "audio/m4a",
|
|
58
|
+
};
|
|
59
|
+
const filetype = (extension && mimeByExt[extension]) || "application/octet-stream";
|
|
60
|
+
const presigned = await this.getS3UploadUrl(filename, filetype);
|
|
61
|
+
if (presigned.isErr())
|
|
62
|
+
return (0, neverthrow_1.err)(presigned.error);
|
|
63
|
+
const file = await (0, promises_1.readFile)(localPath);
|
|
64
|
+
const res = await fetch(presigned.value, {
|
|
65
|
+
method: "PUT",
|
|
66
|
+
body: file,
|
|
67
|
+
headers: { "Content-Type": filetype },
|
|
68
|
+
});
|
|
69
|
+
if (!res.ok) {
|
|
70
|
+
return this.error("INTERNAL_SERVER_ERROR", `Failed to upload to S3: ${res.status}`);
|
|
71
|
+
}
|
|
72
|
+
if (returnDownloadUrl) {
|
|
73
|
+
const downloadUrl = await this.getS3DownloadUrl(filename);
|
|
74
|
+
if (downloadUrl.isErr())
|
|
75
|
+
return (0, neverthrow_1.err)(downloadUrl.error);
|
|
76
|
+
return (0, neverthrow_1.ok)(downloadUrl.value);
|
|
77
|
+
}
|
|
78
|
+
return (0, neverthrow_1.ok)(filename);
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
async downloadS3ToFile(s3Path) {
|
|
82
|
+
return this.throwableAsync(async () => {
|
|
83
|
+
const extension = s3Path.split(".").pop();
|
|
84
|
+
const destinationPath = node_path_1.default.join((0, node_os_1.tmpdir)(), "s3-downloads", `${(0, uuid_1.v4)()}${extension ? `.${extension}` : ""}`);
|
|
85
|
+
const result = await this.repository.file.getS3Object(s3Path);
|
|
86
|
+
if (result.isErr())
|
|
87
|
+
return (0, neverthrow_1.err)(result.error);
|
|
88
|
+
const body = result.value.Body;
|
|
89
|
+
if (!body)
|
|
90
|
+
return this.error("NOT_FOUND", "S3 object body is empty");
|
|
91
|
+
await (0, promises_1.mkdir)((0, node_path_1.dirname)(destinationPath), { recursive: true });
|
|
92
|
+
// AWS SDK v3 SdkStream has transformToByteArray method - use it for reliable handling
|
|
93
|
+
if (typeof body === "object" &&
|
|
94
|
+
"transformToByteArray" in body &&
|
|
95
|
+
typeof body.transformToByteArray === "function") {
|
|
96
|
+
const bytes = await body.transformToByteArray();
|
|
97
|
+
await (0, promises_1.writeFile)(destinationPath, bytes);
|
|
98
|
+
return (0, neverthrow_1.ok)(destinationPath);
|
|
99
|
+
}
|
|
100
|
+
// Fallback: try streaming approaches
|
|
101
|
+
const writeStream = (0, node_fs_1.createWriteStream)(destinationPath);
|
|
102
|
+
let input = null;
|
|
103
|
+
const unknownBody = body;
|
|
104
|
+
if (typeof unknownBody === "object" &&
|
|
105
|
+
unknownBody !== null &&
|
|
106
|
+
"pipe" in unknownBody &&
|
|
107
|
+
typeof unknownBody.pipe === "function") {
|
|
108
|
+
input = unknownBody;
|
|
109
|
+
}
|
|
110
|
+
else if (typeof unknownBody === "object" &&
|
|
111
|
+
unknownBody !== null &&
|
|
112
|
+
"getReader" in unknownBody &&
|
|
113
|
+
typeof unknownBody.getReader === "function") {
|
|
114
|
+
input = node_stream_1.Readable.fromWeb(unknownBody);
|
|
115
|
+
}
|
|
116
|
+
else if (typeof unknownBody === "object" &&
|
|
117
|
+
unknownBody !== null &&
|
|
118
|
+
"stream" in unknownBody &&
|
|
119
|
+
typeof unknownBody.stream === "function") {
|
|
120
|
+
input = node_stream_1.Readable.fromWeb(unknownBody.stream());
|
|
121
|
+
}
|
|
122
|
+
if (input) {
|
|
123
|
+
await (0, promises_2.pipeline)(input, writeStream);
|
|
124
|
+
return (0, neverthrow_1.ok)(destinationPath);
|
|
125
|
+
}
|
|
126
|
+
if (typeof unknownBody === "object" &&
|
|
127
|
+
unknownBody !== null &&
|
|
128
|
+
"arrayBuffer" in unknownBody &&
|
|
129
|
+
typeof unknownBody.arrayBuffer === "function") {
|
|
130
|
+
const buffer = Buffer.from(await unknownBody.arrayBuffer());
|
|
131
|
+
await (0, promises_2.pipeline)(node_stream_1.Readable.from(buffer), writeStream);
|
|
132
|
+
return (0, neverthrow_1.ok)(destinationPath);
|
|
133
|
+
}
|
|
134
|
+
return this.error("INTERNAL_SERVER_ERROR", "Unsupported S3 body type");
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
getFileType(path) {
|
|
138
|
+
// determine the type of the file
|
|
139
|
+
const extension = path.split(".").pop();
|
|
140
|
+
if (!extension)
|
|
141
|
+
return undefined;
|
|
142
|
+
for (const [key, value] of Object.entries(file_constants_1.fileTypes)) {
|
|
143
|
+
if (value.extensions.includes(extension)) {
|
|
144
|
+
return { fileType: key, extension };
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
return undefined;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
exports.FileService = FileService;
|