diff --git a/CHANGELOG.md b/CHANGELOG.md index 40add3bb..bf719254 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). ### Fixed +* **transcode**: Fix an issue where portrait photos from cameras and phones appeared sideways in thumbnails, previews, watermarked share links and annotated snapshots, and were listed with their width and height swapped + ### Changed ## [0.5.1] - 2026-10-06 diff --git a/packages/core/src/transcode/raw-extract.ts b/packages/core/src/transcode/raw-extract.ts index 54d51012..3b4077a4 100644 --- a/packages/core/src/transcode/raw-extract.ts +++ b/packages/core/src/transcode/raw-extract.ts @@ -25,6 +25,11 @@ export interface RawPreviewResult { width: number height: number orientation?: number + /** + * True when the file is already upright (dcraw_emu output), so downstream sharp must not + * auto-orient it even though `orientation` is undefined. + */ + orientationApplied?: boolean rawWidth?: number rawHeight?: number } @@ -382,6 +387,7 @@ export async function extractAndValidateRawPreview( previewPath: decoded.previewPath, cleanup: decoded.cleanup, orientation: decoded.orientation, + orientationApplied: true, rawWidth: decoded.rawWidth, rawHeight: decoded.rawHeight, ...dims, diff --git a/packages/core/src/transcode/transcode.orientation.test.ts b/packages/core/src/transcode/transcode.orientation.test.ts new file mode 100644 index 00000000..85db3dd4 --- /dev/null +++ b/packages/core/src/transcode/transcode.orientation.test.ts @@ -0,0 +1,252 @@ +import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest' +import * as fs from 'fs' +import * as path from 'path' +import sharp from 'sharp' +import { transcodeService } from './transcode' + +// Only the storage layer is mocked: these tests run the real sharp pipeline end to end. +vi.mock('@shumai/core/src/s3/s3', () => ({ + s3Service: { + downloadToFile: vi.fn(), + putObject: vi.fn(), + resolveInput: vi.fn(), + presign: vi.fn(), + }, +})) + +// Only the RAW preview extraction is replaced, so the RAW branch of transcodeImage can be driven +// with a prepared file (the dcraw_emu fallback result) without ExifTool or dcraw_emu installed. +const extractRawMock = vi.hoisted(() => vi.fn()) +vi.mock('./raw-extract', async (importOriginal) => ({ + ...(await importOriginal()), + extractAndValidateRawPreview: extractRawMock, +})) + +// Windows keeps cached files open, which would make the temp dir cleanup fail with EBUSY. +sharp.cache(false) + +type Corner = 'TL' | 'TR' | 'BL' | 'BR' + +// Stored (sensor) pixels are landscape. Each quadrant has its own color so the direction of the +// rotation or flip can be read back from the corners of the output. +const STORED_WIDTH = 64 +const STORED_HEIGHT = 48 +const QUADRANT_COLORS: Record = { + TL: [220, 30, 30], // red + TR: [30, 200, 30], // green + BL: [30, 30, 220], // blue + BR: [230, 230, 30], // yellow +} + +/** + * For each EXIF orientation: the displayed size and which stored corner ends up at the displayed + * top-left and bottom-right. Orientations 5 to 8 swap width and height. + */ +const ORIENTATIONS: Array<{ + orientation: number + name: string + swapped: boolean + topLeftIs: Corner + bottomRightIs: Corner +}> = [ + { orientation: 1, name: 'normal', swapped: false, topLeftIs: 'TL', bottomRightIs: 'BR' }, + { + orientation: 2, + name: 'mirror horizontal', + swapped: false, + topLeftIs: 'TR', + bottomRightIs: 'BL', + }, + { orientation: 3, name: 'rotate 180', swapped: false, topLeftIs: 'BR', bottomRightIs: 'TL' }, + { orientation: 4, name: 'mirror vertical', swapped: false, topLeftIs: 'BL', bottomRightIs: 'TR' }, + { orientation: 5, name: 'transpose', swapped: true, topLeftIs: 'TL', bottomRightIs: 'BR' }, + { orientation: 6, name: 'rotate 90 CW', swapped: true, topLeftIs: 'BL', bottomRightIs: 'TR' }, + { orientation: 7, name: 'transverse', swapped: true, topLeftIs: 'BR', bottomRightIs: 'TL' }, + { orientation: 8, name: 'rotate 270 CW', swapped: true, topLeftIs: 'TR', bottomRightIs: 'BL' }, +] + +async function makeJpeg(orientation: number): Promise { + const halfW = STORED_WIDTH / 2 + const halfH = STORED_HEIGHT / 2 + const block = (corner: Corner) => ({ + create: { + width: halfW, + height: halfH, + channels: 3 as const, + background: { + r: QUADRANT_COLORS[corner][0], + g: QUADRANT_COLORS[corner][1], + b: QUADRANT_COLORS[corner][2], + }, + }, + }) + const tile = async (corner: Corner) => sharp(block(corner)).png().toBuffer() + return sharp({ + create: { + width: STORED_WIDTH, + height: STORED_HEIGHT, + channels: 3, + background: { r: 0, g: 0, b: 0 }, + }, + }) + .composite([ + { input: await tile('TL'), left: 0, top: 0 }, + { input: await tile('TR'), left: halfW, top: 0 }, + { input: await tile('BL'), left: 0, top: halfH }, + { input: await tile('BR'), left: halfW, top: halfH }, + ]) + .jpeg({ quality: 100, chromaSubsampling: '4:4:4' }) + .withMetadata({ orientation }) + .toBuffer() +} + +/** Which quadrant color is nearest to the pixel a few pixels inside the given output corner. */ +async function cornerColor(file: string, where: 'TL' | 'BR'): Promise { + const { data, info } = await sharp(fs.readFileSync(file)) + .removeAlpha() + .raw() + .toBuffer({ resolveWithObject: true }) + const inset = 3 + const x = where === 'TL' ? inset : info.width - 1 - inset + const y = where === 'TL' ? inset : info.height - 1 - inset + const offset = (y * info.width + x) * info.channels + const pixel = [data[offset], data[offset + 1], data[offset + 2]] + let best: Corner = 'TL' + let bestDistance = Infinity + for (const corner of Object.keys(QUADRANT_COLORS) as Corner[]) { + const distance = QUADRANT_COLORS[corner].reduce( + (sum, channel, i) => sum + (channel - pixel[i]) ** 2, + 0, + ) + if (distance < bestDistance) { + best = corner + bestDistance = distance + } + } + return best +} + +describe('EXIF orientation (real sharp)', () => { + let tempDir: string + + beforeEach(() => { + tempDir = transcodeService.createTempDir('orientation-test-') + }) + + afterEach(() => { + transcodeService.removeDir(tempDir) + }) + + const expectedSize = (swapped: boolean) => + swapped + ? { width: STORED_HEIGHT, height: STORED_WIDTH } + : { width: STORED_WIDTH, height: STORED_HEIGHT } + + it.each(ORIENTATIONS)( + 'getImageInfo reports the displayed size for orientation $orientation ($name)', + async ({ orientation, swapped }) => { + const inputPath = path.join(tempDir, `in-${orientation}.jpg`) + fs.writeFileSync(inputPath, await makeJpeg(orientation)) + + const info = await transcodeService.getImageInfo(inputPath) + const expected = expectedSize(swapped) + + expect(info.originalWidth).toBe(expected.width) + expect(info.originalHeight).toBe(expected.height) + }, + ) + + it.each(ORIENTATIONS)( + 'transcodeImage outputs an upright image for orientation $orientation ($name)', + async ({ orientation, swapped, topLeftIs, bottomRightIs }) => { + const inputPath = path.join(tempDir, `in-${orientation}.jpg`) + const outputPath = path.join(tempDir, `out-${orientation}.webp`) + fs.writeFileSync(inputPath, await makeJpeg(orientation)) + + await transcodeService.transcodeImage(inputPath, outputPath, 1000, 100) + + const meta = await sharp(fs.readFileSync(outputPath)).metadata() + const expected = expectedSize(swapped) + expect(meta.width).toBe(expected.width) + expect(meta.height).toBe(expected.height) + // Direction, not just size: the stored corner that lands top-left and bottom-right. + expect(await cornerColor(outputPath, 'TL')).toBe(topLeftIs) + expect(await cornerColor(outputPath, 'BR')).toBe(bottomRightIs) + }, + ) + + it('transcodeImage sizes a rotated preview from the displayed dimensions', async () => { + // Stored 64x48 with orientation 6 is displayed 48x64 (portrait): the short edge is the width. + const inputPath = path.join(tempDir, 'preview-in.jpg') + const outputPath = path.join(tempDir, 'preview-out.webp') + fs.writeFileSync(inputPath, await makeJpeg(6)) + + await transcodeService.transcodeImage(inputPath, outputPath, 24, 80, { isPreview: true }) + + const meta = await sharp(fs.readFileSync(outputPath)).metadata() + expect(meta.width).toBeLessThan(meta.height ?? 0) + }) + + describe('RAW inputs', () => { + const rawPreview = (previewPath: string, extra: Record) => ({ + previewPath, + cleanup: () => {}, + width: STORED_WIDTH, + height: STORED_HEIGHT, + ...extra, + }) + + it('never auto-orients dcraw_emu output that still carries an Orientation tag', async () => { + // The dcraw fallback reports orientation undefined (already upright) but flags it as applied. + const tiffLike = path.join(tempDir, 'dcraw.jpg') + const outputPath = path.join(tempDir, 'dcraw-out.webp') + fs.writeFileSync(tiffLike, await makeJpeg(6)) + extractRawMock.mockResolvedValueOnce( + rawPreview(tiffLike, { orientation: undefined, orientationApplied: true }), + ) + + await transcodeService.transcodeImage(path.join(tempDir, 'photo.cr2'), outputPath, 1000, 100) + + const meta = await sharp(fs.readFileSync(outputPath)).metadata() + // Stored landscape stays landscape and the stored corners stay put: no second rotation. + expect(meta.width).toBe(STORED_WIDTH) + expect(meta.height).toBe(STORED_HEIGHT) + expect(await cornerColor(outputPath, 'TL')).toBe('TL') + expect(await cornerColor(outputPath, 'BR')).toBe('BR') + }) + + it('still auto-orients an embedded preview that has no container orientation', async () => { + const embedded = path.join(tempDir, 'embedded.jpg') + const outputPath = path.join(tempDir, 'embedded-out.webp') + fs.writeFileSync(embedded, await makeJpeg(6)) + extractRawMock.mockResolvedValueOnce(rawPreview(embedded, { orientation: undefined })) + + await transcodeService.transcodeImage(path.join(tempDir, 'photo.cr2'), outputPath, 1000, 100) + + const meta = await sharp(fs.readFileSync(outputPath)).metadata() + expect(meta.width).toBe(STORED_HEIGHT) + expect(meta.height).toBe(STORED_WIDTH) + }) + }) + + it('overlayAnnotationsOnBuffer keeps the displayed orientation', async () => { + const annotations = [ + { + type: 'box', + color: '#ffffff', + points: [ + [0.1, 0.1], + [0.4, 0.4], + ], + }, + ] as unknown as PrismaJson.AnnotationList + const annotated = await transcodeService.overlayAnnotationsOnBuffer( + await makeJpeg(6), + annotations, + ) + + const meta = await sharp(annotated).metadata() + expect(meta.width).toBe(STORED_HEIGHT) + expect(meta.height).toBe(STORED_WIDTH) + }) +}) diff --git a/packages/core/src/transcode/transcode.test.ts b/packages/core/src/transcode/transcode.test.ts index c6a48168..802c5086 100644 --- a/packages/core/src/transcode/transcode.test.ts +++ b/packages/core/src/transcode/transcode.test.ts @@ -330,7 +330,7 @@ describe('TranscodeService', () => { const outputFile = path.join(tempDir, 'output.webp') await transcodeService.transcodeImage('input.png', outputFile, 300, 80, { isPreview: true }) - expect(sharp).toHaveBeenCalledWith('input.png', { limitInputPixels: false }) + expect(sharp).toHaveBeenCalledWith('input.png', { limitInputPixels: false, autoOrient: true }) const mockSharp = vi.mocked(sharp).mock.results[0].value expect(mockSharp.toColorspace).toHaveBeenCalledWith('srgb') expect(mockSharp.resize).toHaveBeenCalledWith(400, 300, expect.any(Object)) @@ -544,7 +544,11 @@ describe('TranscodeService', () => { await transcodeService.transcodeImage(rawPath, outputFile, 1920, 85) - expect(sharp).toHaveBeenCalledWith('/tmp/extracted-raw.jpg', { limitInputPixels: false }) + // The RAW container orientation (1 here) is authoritative, so no auto-orient on top of it. + expect(sharp).toHaveBeenCalledWith('/tmp/extracted-raw.jpg', { + limitInputPixels: false, + autoOrient: false, + }) const mockSharp = vi.mocked(sharp).mock.results[vi.mocked(sharp).mock.results.length - 1].value expect(mockSharp.toColorspace).toHaveBeenCalledWith('srgb') expect(mockSharp.webp).toHaveBeenCalledWith({ quality: 85 }) @@ -568,7 +572,10 @@ describe('TranscodeService', () => { await transcodeService.transcodeImage(rawPath, outputFile, 1920, 85) - expect(sharp).toHaveBeenCalledWith('/tmp/decoded-dcraw.tiff', { limitInputPixels: false }) + expect(sharp).toHaveBeenCalledWith('/tmp/decoded-dcraw.tiff', { + limitInputPixels: false, + autoOrient: true, + }) const mockSharp = vi.mocked(sharp).mock.results[vi.mocked(sharp).mock.results.length - 1].value expect(mockSharp.rotate).not.toHaveBeenCalled() expect(mockSharp.toColorspace).toHaveBeenCalledWith('srgb') @@ -593,9 +600,67 @@ describe('TranscodeService', () => { const mockSharp = vi.mocked(sharp).mock.results[vi.mocked(sharp).mock.results.length - 1].value expect(mockSharp.rotate).toHaveBeenCalledWith(90) + // Rotated from the container EXIF only, never also auto-oriented (would double-rotate). + expect(sharp).toHaveBeenCalledWith('/tmp/extracted-rotated.jpg', { + limitInputPixels: false, + autoOrient: false, + }) expect(cleanupSpy).toHaveBeenCalled() }) + it('should apply EXIF orientation when transcoding a rotated photo', async () => { + // A camera portrait: stored 7728x5152 (landscape) with EXIF orientation 8, shown 5152x7728. + const mockRotated = { + resize: vi.fn().mockReturnThis(), + toColorspace: vi.fn().mockReturnThis(), + webp: vi.fn().mockReturnThis(), + toFile: vi.fn().mockResolvedValue({}), + metadata: vi.fn().mockResolvedValue({ + width: 7728, + height: 5152, + orientation: 8, + autoOrient: { width: 5152, height: 7728 }, + format: 'jpeg', + }), + } + // eslint-disable-next-line @typescript-eslint/no-explicit-any + vi.mocked(sharp).mockReturnValueOnce(mockRotated as any) + + await transcodeService.transcodeImage( + 'DSCF5056.JPG', + path.join(tempDir, 'rotated.webp'), + 300, + 80, + { isPreview: true }, + ) + + expect(sharp).toHaveBeenCalledWith( + 'DSCF5056.JPG', + expect.objectContaining({ autoOrient: true }), + ) + // Portrait preview: 300 wide on the short edge, not a 450x300 landscape one. + expect(mockRotated.resize).toHaveBeenCalledWith(300, 450, expect.any(Object)) + }) + + it('should report the displayed size of a rotated photo', async () => { + const mockRotated = { + metadata: vi.fn().mockResolvedValue({ + width: 7728, + height: 5152, + orientation: 8, + autoOrient: { width: 5152, height: 7728 }, + format: 'jpeg', + }), + } + // eslint-disable-next-line @typescript-eslint/no-explicit-any + vi.mocked(sharp).mockReturnValueOnce(mockRotated as any) + + const info = await transcodeService.getImageInfo('DSCF5056.JPG') + + expect(info.originalWidth).toBe(5152) + expect(info.originalHeight).toBe(7728) + }) + it('should throw error when transcoding RAW without usable preview', async () => { const rawPath = path.join(tempDir, 'corrupt.raf') const outputFile = path.join(tempDir, 'output-fail.webp') @@ -618,7 +683,10 @@ describe('TranscodeService', () => { await transcodeService.transcodeImage('http://example.com/image.png', outputFile, 480, 80) expect(global.fetch).toHaveBeenCalledWith('http://example.com/image.png') - expect(sharp).toHaveBeenCalledWith(expect.any(Buffer), { limitInputPixels: false }) + expect(sharp).toHaveBeenCalledWith(expect.any(Buffer), { + limitInputPixels: false, + autoOrient: true, + }) }) it('should support Buffer as input for image transcoding', async () => { @@ -626,7 +694,7 @@ describe('TranscodeService', () => { const outputFile = path.join(tempDir, 'output-buffer.webp') await transcodeService.transcodeImage(inputBuffer, outputFile, 480, 80) - expect(sharp).toHaveBeenCalledWith(inputBuffer, { limitInputPixels: false }) + expect(sharp).toHaveBeenCalledWith(inputBuffer, { limitInputPixels: false, autoOrient: true }) const mockSharp = vi.mocked(sharp).mock.results[vi.mocked(sharp).mock.results.length - 1].value expect(mockSharp.toColorspace).toHaveBeenCalledWith('srgb') expect(mockSharp.resize).toHaveBeenCalledWith(400, 300, expect.any(Object)) diff --git a/packages/core/src/transcode/transcode.ts b/packages/core/src/transcode/transcode.ts index 729e6d68..0d81e716 100644 --- a/packages/core/src/transcode/transcode.ts +++ b/packages/core/src/transcode/transcode.ts @@ -8,7 +8,7 @@ import * as fs from 'fs' import * as os from 'os' import * as path from 'path' import PDFDocument from 'pdfkit' -import sharp from 'sharp' +import sharp, { type Metadata as SharpMetadata } from 'sharp' import { ulid } from 'ulid' import { promisify } from 'util' import { mapConcurrent } from '../utils/async' @@ -419,6 +419,28 @@ function normalizeRotation(rotation: number): number { return ((Math.round(rotation) % 360) + 360) % 360 } +interface OrientationOptions { + /** True when a RAW container orientation is applied by hand, so sharp must not also auto-orient. */ + rawOrientationApplied?: boolean +} + +/** + * Sharp input options. The webp output drops the EXIF orientation tag, so camera portraits would + * come out sideways unless sharp applies it; skip that when the RAW orientation is already applied, + * otherwise the image would rotate twice. + */ +function orientedSharpOptions({ rawOrientationApplied = false }: OrientationOptions = {}) { + return { limitInputPixels: false, autoOrient: !rawOrientationApplied } as const +} + +/** Width and height as displayed: sharp reports the EXIF-oriented size under `autoOrient`. */ +function displayedDimensions( + metadata: SharpMetadata, + { rawOrientationApplied = false }: OrientationOptions = {}, +): Pick { + return rawOrientationApplied ? metadata : (metadata.autoOrient ?? metadata) +} + export function parseBitrateKbps(bitrate: string | number): number { if (typeof bitrate === 'number') { return Math.round(bitrate / 1000) @@ -955,9 +977,11 @@ export class TranscodeService { } const metadata = await sharp(input, { limitInputPixels: false }).metadata() + // Cameras store portraits sideways plus an EXIF orientation; report the size as displayed. + const shown = displayedDimensions(metadata) return { - originalWidth: metadata.width || 0, - originalHeight: metadata.height || 0, + originalWidth: shown.width || 0, + originalHeight: shown.height || 0, duration: 0, bitRate: 0, frameRate: 0, @@ -1922,6 +1946,7 @@ export class TranscodeService { // RAW branch — extract embedded preview or decode RAW to temporary file + orientation, then treat as normal image let rawOrientation: number | undefined + let rawOutputUpright = false let rawCleanup: (() => void) | null = null if (typeof input === 'string' && isRawImage(input)) { const extracted = await extractAndValidateRawPreview(input) @@ -1932,6 +1957,7 @@ export class TranscodeService { } input = extracted.previewPath rawOrientation = extracted.orientation + rawOutputUpright = extracted.orientationApplied === true rawCleanup = extracted.cleanup } @@ -1940,11 +1966,17 @@ export class TranscodeService { let targetW = width > 0 ? Math.min(width, WEBP_MAX_DIMENSION) : WEBP_MAX_DIMENSION let targetH = height && height > 0 ? Math.min(height, WEBP_MAX_DIMENSION) : WEBP_MAX_DIMENSION - const sharpInstance = sharp(input, { limitInputPixels: false }) + // RAW previews are oriented from the container EXIF below, so only auto-orient other + // inputs: the webp output drops the tag, which would leave camera portraits sideways. + // dcraw_emu output is already upright, so it is never auto-oriented (no double rotation). + const rawOrientationApplied = rawOrientation !== undefined || rawOutputUpright + const sharpInstance = sharp(input, orientedSharpOptions({ rawOrientationApplied })) if (isPreview) { try { - const meta = await sharpInstance.metadata() + const meta = displayedDimensions(await sharpInstance.metadata(), { + rawOrientationApplied, + }) if (meta.width && meta.height) { const isSwapped = rawOrientation !== undefined && rawOrientation >= 5 && rawOrientation <= 8 @@ -2003,7 +2035,7 @@ export class TranscodeService { // Apply EXIF orientation from the RAW container to the extracted buffer. // The buffer itself often lacks orientation EXIF, so Sharp won't auto-rotate. - // For non-RAW images, Sharp auto-rotates from the image's own EXIF. + // For non-RAW images, autoOrient above applies the image's own EXIF. if (rawOrientation && EXIF_ORIENTATION_TO_ROTATION[rawOrientation]) { const { angle, flip, flop } = EXIF_ORIENTATION_TO_ROTATION[rawOrientation] if (angle) sharpInstance.rotate(angle) @@ -3019,12 +3051,15 @@ export class TranscodeService { return imageBuffer } - const meta = await sharp(imageBuffer, { limitInputPixels: false }).metadata() + // Annotations are drawn in the displayed (EXIF-oriented) coordinate space. + const meta = displayedDimensions( + await sharp(imageBuffer, { limitInputPixels: false }).metadata(), + ) const width = meta.width || 1920 const height = meta.height || 1080 const svgStr = renderAnnotationsToSvg(width, height, annotations) - return await sharp(imageBuffer, { limitInputPixels: false }) + return await sharp(imageBuffer, orientedSharpOptions()) .composite([{ input: Buffer.from(svgStr), top: 0, left: 0 }]) .toColorspace('srgb') .resize(16383, 16383, { fit: 'inside', withoutEnlargement: true }) @@ -3111,7 +3146,7 @@ export class TranscodeService { width: number, height: number, ): Promise { - await sharp(inputPath, { limitInputPixels: false }) + await sharp(inputPath, orientedSharpOptions()) .toColorspace('srgb') .resize(width, height, { fit: 'inside' }) .composite([{ input: overlayPngBuffer }]) diff --git a/packages/e2e/fixtures/portrait-rotated.jpg b/packages/e2e/fixtures/portrait-rotated.jpg new file mode 100644 index 00000000..e7c7a275 Binary files /dev/null and b/packages/e2e/fixtures/portrait-rotated.jpg differ diff --git a/packages/e2e/workflow/transcode-image.test.ts b/packages/e2e/workflow/transcode-image.test.ts index deaf952c..1f1a2f12 100644 --- a/packages/e2e/workflow/transcode-image.test.ts +++ b/packages/e2e/workflow/transcode-image.test.ts @@ -295,5 +295,85 @@ describe.each(['local', 'temporal'] as const)( expect(mediaInfo.imageTranscodes.length).toBeGreaterThan(0) expect(mediaInfo.thumbnail).toBeDefined() }, 50000) + + it('should transcode a rotated camera photo upright', async () => { + // 1. Seed Database + const team = await prisma.team.create({ + data: { name: 'E2E Rotated Transcode Team' }, + }) + + const project = await prisma.project.create({ + data: { name: 'E2E Rotated Transcode Project', teamId: team.id }, + }) + + const storageKey = await prisma.storageKey.create({ + data: { + key: 'projects/e2e/portrait-rotated.jpg', + }, + }) + + const asset = await prisma.asset.create({ + data: { + name: 'portrait-rotated.jpg', + type: 'file', + status: 'uploaded', + mediaType: 'image/jpeg', + projectId: project.id, + storageKeyId: storageKey.id, + }, + }) + + // 2. Seed S3 Storage from fixture portrait-rotated.jpg: stored 600x400 with EXIF + // orientation 6, the way cameras save portrait shots, so it is displayed 400x600. + const jpgPath = path.join(fixturesDir, 'portrait-rotated.jpg') + const jpgBuffer = fs.readFileSync(jpgPath) + await s3Service.putObject( + 'shumai-e2e-test-bucket-transcode', + 'projects/e2e/portrait-rotated.jpg', + jpgBuffer, + jpgBuffer.length, + 'image/jpeg', + ) + + // 3. Create Workflow Task + const task = await prisma.workflowTask.create({ + data: { + type: 'transcode_image', + status: 'pending', + assetId: asset.id, + projectId: project.id, + teamId: team.id, + payload: { + projectId: project.id, + transcode: { + thumbnail: true, + }, + }, + }, + }) + + // 4. Wait for workflow to complete + console.log( + `Submitted E2E Rotated Image Transcode Workflow Task. ID: ${task.id}. Awaiting completion...`, + ) + const completedTask = await workflowService.executeWait(task, 45000) + + // 5. Verification: the asset and its transcode are portrait, not the stored landscape. + expect(completedTask.status).toBe('completed') + + const updatedAsset = await prisma.asset.findUnique({ + where: { id: asset.id }, + }) + expect(updatedAsset?.status).toBe(AssetStatus.processed) + + const mediaInfo = updatedAsset?.media as unknown as { + imageTranscodes: Array<{ width: number; height: number }> + metadata: { originalWidth: number; originalHeight: number } + } + expect(mediaInfo.metadata.originalWidth).toBe(400) + expect(mediaInfo.metadata.originalHeight).toBe(600) + expect(mediaInfo.imageTranscodes[0].width).toBe(400) + expect(mediaInfo.imageTranscodes[0].height).toBe(600) + }, 50000) }, )