diff --git a/packages/api-file-manager/package.json b/packages/api-file-manager/package.json index 9067feca2fc..6f87b5e406a 100644 --- a/packages/api-file-manager/package.json +++ b/packages/api-file-manager/package.json @@ -22,6 +22,7 @@ "@webiny/api": "0.0.0", "@webiny/api-core": "0.0.0", "@webiny/api-headless-cms": "0.0.0", + "@webiny/api-websockets": "0.0.0", "@webiny/aws-sdk": "0.0.0", "@webiny/build-tools": "0.0.0", "@webiny/error": "0.0.0", diff --git a/packages/api-file-manager/src/index.ts b/packages/api-file-manager/src/index.ts index 425349e3a46..8e029d8a348 100644 --- a/packages/api-file-manager/src/index.ts +++ b/packages/api-file-manager/src/index.ts @@ -7,6 +7,7 @@ import { createGraphQLSchemaPlugin } from "./graphql/index.js"; import { applyThreatScanning } from "./enterprise/applyThreatScanning.js"; import type { FileManagerConfig } from "./createFileManager/types.js"; import { FileManagerFeature } from "~/features/FileManagerFeature.js"; +import { createFileTaggingTask } from "./tasks/createFileTaggingTask.js"; export * from "./modelModifier/CmsModelModifier.js"; export * from "./plugins/index.js"; @@ -15,7 +16,7 @@ export * from "./delivery/index.js"; export const createFileManagerContext = ({ storageOperations }: Pick) => { - const plugin = new ContextPlugin(async context => { + const fmContextPlugin = new ContextPlugin(async context => { const fmContext = new FileManagerContextSetup(context); context.fileManager = await fmContext.setupContext(storageOperations); @@ -26,9 +27,28 @@ export const createFileManagerContext = ({ FileManagerFeature.register(context.container); }); - plugin.name = "file-manager.createContext"; - - return plugin; + fmContextPlugin.name = "file-manager.createContext"; + + return [ + // Main context plugin + fmContextPlugin, + + // Background task + createFileTaggingTask(), + + // Trigger background task + new ContextPlugin(context => { + context.fileManager.onFileAfterCreate.subscribe(({ file }) => { + context.tasks.trigger({ + definition: "fmAiImageTagging", + input: { + file: file + }, + name: "AI Image Tagging Task" + }); + }); + }) + ]; }; export const createFileManagerGraphQL = () => { diff --git a/packages/api-file-manager/src/tasks/createFileTaggingTask.ts b/packages/api-file-manager/src/tasks/createFileTaggingTask.ts new file mode 100644 index 00000000000..52579bdb49a --- /dev/null +++ b/packages/api-file-manager/src/tasks/createFileTaggingTask.ts @@ -0,0 +1,28 @@ +import { createTaskDefinition } from "@webiny/tasks"; +import type { FileManagerContext } from "~/types.js"; +import { Context as WebsocketsContext } from "@webiny/api-websockets"; + +export const createFileTaggingTask = () => { + return createTaskDefinition({ + id: "fmAiImageTagging", + title: "File Upload Background task", + isPrivate: true, + async run(params) { + const { input, response, context } = params; + + // TODO: Get tag information from AI + + // TODO: Update File Tags + await context.fileManager.updateFile(input.fileId, { + // tags: ["tag1", "tag2", "tag3"] + }); + + // TODO: send message to WebSocket + + return response.done( + "successfully ran the fmAiImageTagging background task", + input.fileId + ); + } + }); +}; diff --git a/packages/api-file-manager/tsconfig.build.json b/packages/api-file-manager/tsconfig.build.json index 354b188a5dd..b348e05ff43 100644 --- a/packages/api-file-manager/tsconfig.build.json +++ b/packages/api-file-manager/tsconfig.build.json @@ -5,6 +5,7 @@ { "path": "../api/tsconfig.build.json" }, { "path": "../api-core/tsconfig.build.json" }, { "path": "../api-headless-cms/tsconfig.build.json" }, + { "path": "../api-websockets/tsconfig.build.json" }, { "path": "../aws-sdk/tsconfig.build.json" }, { "path": "../error/tsconfig.build.json" }, { "path": "../feature/tsconfig.build.json" }, @@ -154,6 +155,8 @@ "@webiny/api-core": ["../api-core/src"], "@webiny/api-headless-cms/*": ["../api-headless-cms/src/*"], "@webiny/api-headless-cms": ["../api-headless-cms/src"], + "@webiny/api-websockets/*": ["../api-websockets/src/*"], + "@webiny/api-websockets": ["../api-websockets/src"], "@webiny/aws-sdk/*": ["../aws-sdk/src/*"], "@webiny/aws-sdk": ["../aws-sdk/src"], "@webiny/error/*": ["../error/src/*"], diff --git a/packages/api-file-manager/tsconfig.json b/packages/api-file-manager/tsconfig.json index ebea88e95a9..f869a6b5e8e 100644 --- a/packages/api-file-manager/tsconfig.json +++ b/packages/api-file-manager/tsconfig.json @@ -5,6 +5,7 @@ { "path": "../api" }, { "path": "../api-core" }, { "path": "../api-headless-cms" }, + { "path": "../api-websockets" }, { "path": "../aws-sdk" }, { "path": "../error" }, { "path": "../feature" }, @@ -154,6 +155,8 @@ "@webiny/api-core": ["../api-core/src"], "@webiny/api-headless-cms/*": ["../api-headless-cms/src/*"], "@webiny/api-headless-cms": ["../api-headless-cms/src"], + "@webiny/api-websockets/*": ["../api-websockets/src/*"], + "@webiny/api-websockets": ["../api-websockets/src"], "@webiny/aws-sdk/*": ["../aws-sdk/src/*"], "@webiny/aws-sdk": ["../aws-sdk/src"], "@webiny/error/*": ["../error/src/*"], diff --git a/yarn.lock b/yarn.lock index 585bbfbe1e6..2915c6c7643 100644 --- a/yarn.lock +++ b/yarn.lock @@ -14109,6 +14109,7 @@ __metadata: "@webiny/api": "npm:0.0.0" "@webiny/api-core": "npm:0.0.0" "@webiny/api-headless-cms": "npm:0.0.0" + "@webiny/api-websockets": "npm:0.0.0" "@webiny/aws-sdk": "npm:0.0.0" "@webiny/build-tools": "npm:0.0.0" "@webiny/error": "npm:0.0.0"