ソースを参照

fix: inline step2 assets for template generation

ChenYu 3 ヶ月 前
親
コミット
287fa65ea2

ファイルの差分が大きいため隠しています
+ 97 - 0
backend/src/modules/templates/step2-assets.ts


+ 59 - 0
backend/src/modules/templates/template-task-files.spec.ts

@@ -0,0 +1,59 @@
+import { copyFile, mkdir, mkdtemp, readFile, writeFile } from 'fs/promises';
+import { TemplateTaskFiles } from './template-task-files';
+
+jest.mock('fs/promises', () => ({
+  mkdir: jest.fn(),
+  mkdtemp: jest.fn(),
+  readFile: jest.fn(),
+  writeFile: jest.fn(),
+  copyFile: jest.fn(),
+  rm: jest.fn(),
+}));
+
+describe('TemplateTaskFiles', () => {
+  const storageService = {
+    uploadBuffer: jest.fn(),
+  };
+
+  beforeEach(() => {
+    jest.resetAllMocks();
+
+    (mkdir as jest.Mock).mockResolvedValue(undefined);
+    (mkdtemp as jest.Mock).mockResolvedValue('/tmp/template-make/tmpl_123');
+    (readFile as jest.Mock).mockResolvedValue(Buffer.from('zip-binary'));
+    (writeFile as jest.Mock).mockResolvedValue(undefined);
+    (copyFile as jest.Mock).mockResolvedValue(undefined);
+    storageService.uploadBuffer.mockResolvedValue({
+      url: 'https://cdn.example.com/template.zip',
+    });
+
+    global.fetch = jest.fn().mockResolvedValue({
+      ok: true,
+      arrayBuffer: async () => Buffer.from('image-binary'),
+    }) as unknown as typeof fetch;
+  });
+
+  it('writes step2 config and content from in-code assets instead of copying source json files', async () => {
+    const service = new TemplateTaskFiles(storageService as never);
+    jest
+      .spyOn(service as never, 'zipFiles')
+      .mockResolvedValue(undefined as never);
+
+    await service.prepareStep2Bundle({
+      totalTaskId: 'tmpl_123',
+      step1ResultImg: 'https://cdn.example.com/step1.jpg',
+      clothesUrl: 'https://cdn.example.com/clothes.jpg',
+      backgroundUrl: 'https://cdn.example.com/background.jpg',
+    });
+
+    expect(copyFile).not.toHaveBeenCalled();
+    expect(writeFile).toHaveBeenCalledWith(
+      '/tmp/template-make/tmpl_123/config.json',
+      expect.stringContaining('"name": "Asymmetrical Bob"'),
+    );
+    expect(writeFile).toHaveBeenCalledWith(
+      '/tmp/template-make/tmpl_123/content.json',
+      expect.stringContaining('"version": "1.0"'),
+    );
+  });
+});

+ 11 - 6
backend/src/modules/templates/template-task-files.ts

@@ -1,10 +1,11 @@
 import { Injectable } from '@nestjs/common';
-import { mkdtemp, mkdir, readFile, rm, writeFile, copyFile } from 'fs/promises';
+import { mkdtemp, mkdir, readFile, rm, writeFile } from 'fs/promises';
 import { createWriteStream } from 'fs';
 import { tmpdir } from 'os';
-import { basename, join, resolve } from 'path';
+import { basename, join } from 'path';
 import { ZipFile } from 'yazl';
 import { StorageService } from '../storage/storage.service';
+import { STEP2_CONFIG, STEP2_CONTENT } from './step2-assets';
 
 type PrepareStep2BundleInput = {
   totalTaskId: string;
@@ -69,8 +70,6 @@ export class TemplateTaskFiles {
     localFiles: Record<string, string>;
   }> {
     const workDir = await this.createWorkDir(input.totalTaskId);
-    const configPath = resolve(process.cwd(), 'src/step2/config.json');
-    const contentPath = resolve(process.cwd(), 'src/step2/content.json');
     const copiedConfigPath = join(workDir, 'config.json');
     const copiedContentPath = join(workDir, 'content.json');
     const hairstylePath = join(workDir, 'hairstyle.jpg');
@@ -78,8 +77,14 @@ export class TemplateTaskFiles {
     const backgroundPath = join(workDir, 'input-background.jpg');
     const zipPath = join(workDir, 'template.zip');
 
-    await copyFile(configPath, copiedConfigPath);
-    await copyFile(contentPath, copiedContentPath);
+    await writeFile(
+      copiedConfigPath,
+      `${JSON.stringify(STEP2_CONFIG, null, 2)}\n`,
+    );
+    await writeFile(
+      copiedContentPath,
+      `${JSON.stringify(STEP2_CONTENT, null, 2)}\n`,
+    );
     await writeFile(hairstylePath, await this.fetchBuffer(input.step1ResultImg));
     await writeFile(outfitPath, await this.fetchBuffer(input.clothesUrl));
     await writeFile(backgroundPath, await this.fetchBuffer(input.backgroundUrl));

+ 1 - 0
backend/src/modules/templates/templates.service.spec.ts

@@ -411,6 +411,7 @@ describe('TemplatesService', () => {
   });
 
   it('starts step2 with modelUrl as inputs.images when step1 has succeeded', async () => {
+    configService.get.mockReturnValue(Number.MAX_SAFE_INTEGER);
     prisma.hairTemplateMakeTask.findFirst.mockResolvedValue({
       id: BigInt(90),
       totalTaskId: 'tmpl_789',

+ 0 - 6
backend/src/step2/config.json

@@ -1,6 +0,0 @@
-{
-  "name": "Asymmetrical Bob",
-  "type": "Group",
-  "style": "default",
-  "version": "1.0.0"
-}

ファイルの差分が大きいため隠しています
+ 0 - 82
backend/src/step2/content.json


この差分においてかなりの量のファイルが変更されているため、一部のファイルを表示していません