mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-19 14:16:29 +00:00
feat(transcode): add PDF preview, sprite generation, and annotation support
This commit is contained in:
@@ -49,6 +49,7 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
|
||||
ca-certificates \
|
||||
bubblewrap \
|
||||
gosu \
|
||||
poppler-utils \
|
||||
&& apt-get clean \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
|
||||
|
||||
@@ -125,23 +125,24 @@ mkdir shumai && cd shumai
|
||||
Shumai requires the following system packages on Linux:
|
||||
|
||||
- **`ffmpeg`** – Used for media transcoding and metadata extraction.
|
||||
- **`poppler`** (`poppler-utils`) – Used for PDF page image extraction and PDF sprite preview generation (`pdftoppm`).
|
||||
- **`bubblewrap`**, **`socat`**, and **`ripgrep`** – Required by the AI agent sandbox (`anthropic-experimental/sandbox-runtime`) for process isolation, networking, and workspace search.
|
||||
|
||||
Install all required packages with one command:
|
||||
|
||||
> Ubuntu/Debian
|
||||
```bash
|
||||
sudo apt install -y ffmpeg bubblewrap socat ripgrep
|
||||
sudo apt install -y ffmpeg poppler-utils bubblewrap socat ripgrep
|
||||
```
|
||||
|
||||
> Fedora
|
||||
```bash
|
||||
sudo dnf install -y ffmpeg bubblewrap socat ripgrep
|
||||
sudo dnf install -y ffmpeg poppler-utils bubblewrap socat ripgrep
|
||||
```
|
||||
|
||||
> Arch Linux
|
||||
```bash
|
||||
sudo pacman -S --noconfirm ffmpeg bubblewrap socat ripgrep
|
||||
sudo pacman -S --noconfirm ffmpeg poppler bubblewrap socat ripgrep
|
||||
```
|
||||
|
||||
> [!NOTE]
|
||||
@@ -162,12 +163,13 @@ sudo pacman -S --noconfirm ffmpeg bubblewrap socat ripgrep
|
||||
Shumai requires:
|
||||
|
||||
- **`ffmpeg`** – Used for media transcoding and metadata extraction.
|
||||
- **`poppler`** – Used for PDF page image extraction and PDF sprite preview generation (`pdftoppm`).
|
||||
- **`ripgrep`** – Required by the AI agent sandbox (`anthropic-experimental/sandbox-runtime`).
|
||||
|
||||
Install the required packages with Homebrew:
|
||||
|
||||
```bash
|
||||
brew install ffmpeg ripgrep
|
||||
brew install ffmpeg poppler ripgrep
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
+6
-4
@@ -139,23 +139,24 @@ mkdir shumai && cd shumai
|
||||
在 Linux 上运行 Shumai 需要安装以下系统依赖:
|
||||
|
||||
- **`ffmpeg`** —— 用于媒体转码和元数据提取。
|
||||
- **`poppler`** (`poppler-utils`) —— 用于 PDF 页面图像提取和 PDF 雪碧图预览生成 (`pdftoppm`)。
|
||||
- **`bubblewrap`**、**`socat`** 和 **`ripgrep`** —— AI Agent 沙箱(`anthropic-experimental/sandbox-runtime`)所需,用于进程隔离、网络通信和工作区搜索。
|
||||
|
||||
可以使用以下命令一次性安装所有依赖:
|
||||
|
||||
> Ubuntu/Debian
|
||||
```bash
|
||||
sudo apt install -y ffmpeg bubblewrap socat ripgrep
|
||||
sudo apt install -y ffmpeg poppler-utils bubblewrap socat ripgrep
|
||||
```
|
||||
|
||||
> Fedora
|
||||
```bash
|
||||
sudo dnf install -y ffmpeg bubblewrap socat ripgrep
|
||||
sudo dnf install -y ffmpeg poppler-utils bubblewrap socat ripgrep
|
||||
```
|
||||
|
||||
> Arch Linux
|
||||
```bash
|
||||
sudo pacman -S --noconfirm ffmpeg bubblewrap socat ripgrep
|
||||
sudo pacman -S --noconfirm ffmpeg poppler bubblewrap socat ripgrep
|
||||
```
|
||||
|
||||
> [!NOTE]
|
||||
@@ -176,12 +177,13 @@ sudo pacman -S --noconfirm ffmpeg bubblewrap socat ripgrep
|
||||
在 macOS 上运行 Shumai 需要:
|
||||
|
||||
- **`ffmpeg`** —— 用于媒体转码和元数据提取。
|
||||
- **`poppler`** —— 用于 PDF 页面图像提取和 PDF 雪碧图预览生成 (`pdftoppm`)。
|
||||
- **`ripgrep`** —— AI Agent 沙箱(`anthropic-experimental/sandbox-runtime`)所需。
|
||||
|
||||
使用 Homebrew 安装所需依赖:
|
||||
|
||||
```bash
|
||||
brew install ffmpeg ripgrep
|
||||
brew install ffmpeg poppler ripgrep
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
"dependencies": {
|
||||
"@tanstack/react-virtual": "3.14.2",
|
||||
"jittered-fractional-indexing": "1.0.1",
|
||||
"pdfjs-dist": "6.1.200",
|
||||
},
|
||||
"devDependencies": {
|
||||
"@eslint/js": "10.0.1",
|
||||
@@ -37,7 +38,7 @@
|
||||
},
|
||||
"apps/agent": {
|
||||
"name": "@shumai/agent-app",
|
||||
"version": "0.1.9",
|
||||
"version": "0.1.10",
|
||||
"bin": {
|
||||
"shumai-agent": "./src/index.ts",
|
||||
},
|
||||
@@ -49,7 +50,7 @@
|
||||
},
|
||||
"apps/cli": {
|
||||
"name": "@shumai/cli",
|
||||
"version": "0.1.9",
|
||||
"version": "0.1.10",
|
||||
"bin": {
|
||||
"shumai-cli": "./src/index.ts",
|
||||
},
|
||||
@@ -64,7 +65,7 @@
|
||||
},
|
||||
"apps/transcode": {
|
||||
"name": "@shumai/transcode-app",
|
||||
"version": "0.1.9",
|
||||
"version": "0.1.10",
|
||||
"bin": {
|
||||
"shumai-transcode": "./src/index.ts",
|
||||
},
|
||||
@@ -76,7 +77,7 @@
|
||||
},
|
||||
"apps/web": {
|
||||
"name": "@shumai/web-app",
|
||||
"version": "0.1.9",
|
||||
"version": "0.1.10",
|
||||
"dependencies": {
|
||||
"@shumai/agent": "workspace:*",
|
||||
"@shumai/api": "workspace:*",
|
||||
@@ -96,7 +97,7 @@
|
||||
},
|
||||
"packages/agent": {
|
||||
"name": "@shumai/agent",
|
||||
"version": "0.1.9",
|
||||
"version": "0.1.10",
|
||||
"dependencies": {
|
||||
"@anthropic-ai/sandbox-runtime": "0.0.52",
|
||||
"@earendil-works/pi-agent-core": "0.78.0",
|
||||
@@ -120,7 +121,7 @@
|
||||
},
|
||||
"packages/api": {
|
||||
"name": "@shumai/api",
|
||||
"version": "0.1.9",
|
||||
"version": "0.1.10",
|
||||
"dependencies": {
|
||||
"@hono/zod-validator": "0.7.6",
|
||||
"@shumai/core": "workspace:*",
|
||||
@@ -134,7 +135,7 @@
|
||||
},
|
||||
"packages/core": {
|
||||
"name": "@shumai/core",
|
||||
"version": "0.1.9",
|
||||
"version": "0.1.10",
|
||||
"dependencies": {
|
||||
"@earendil-works/pi-agent-core": "0.78.0",
|
||||
"@shumai/db": "workspace:*",
|
||||
@@ -162,7 +163,7 @@
|
||||
},
|
||||
"packages/db": {
|
||||
"name": "@shumai/db",
|
||||
"version": "0.1.9",
|
||||
"version": "0.1.10",
|
||||
"dependencies": {
|
||||
"@earendil-works/pi-agent-core": "0.78.0",
|
||||
"@prisma/adapter-pg": "7.8.0",
|
||||
@@ -178,7 +179,7 @@
|
||||
},
|
||||
"packages/dtos": {
|
||||
"name": "@shumai/dtos",
|
||||
"version": "0.1.9",
|
||||
"version": "0.1.10",
|
||||
"dependencies": {
|
||||
"@earendil-works/pi-agent-core": "0.78.0",
|
||||
"zod": "4.4.3",
|
||||
@@ -186,7 +187,7 @@
|
||||
},
|
||||
"packages/e2e": {
|
||||
"name": "@shumai/e2e",
|
||||
"version": "0.1.9",
|
||||
"version": "0.1.10",
|
||||
"dependencies": {
|
||||
"@earendil-works/pi-agent-core": "0.78.0",
|
||||
"@google/genai": "1.52.0",
|
||||
@@ -200,11 +201,11 @@
|
||||
},
|
||||
"packages/tableprinter": {
|
||||
"name": "@shumai/tableprinter",
|
||||
"version": "0.1.9",
|
||||
"version": "0.1.10",
|
||||
},
|
||||
"packages/transcode": {
|
||||
"name": "@shumai/transcode",
|
||||
"version": "0.1.9",
|
||||
"version": "0.1.10",
|
||||
"dependencies": {
|
||||
"@shumai/core": "workspace:*",
|
||||
"@shumai/db": "workspace:*",
|
||||
@@ -218,7 +219,7 @@
|
||||
},
|
||||
"packages/webui": {
|
||||
"name": "@shumai/webui",
|
||||
"version": "0.1.9",
|
||||
"version": "0.1.10",
|
||||
"dependencies": {
|
||||
"@base-ui/react": "1.5.0",
|
||||
"@dnd-kit/abstract": "0.4.0",
|
||||
@@ -315,7 +316,7 @@
|
||||
},
|
||||
"packages/workflow-core": {
|
||||
"name": "@shumai/workflow-core",
|
||||
"version": "0.1.9",
|
||||
"version": "0.1.10",
|
||||
"dependencies": {
|
||||
"@shumai/core": "workspace:*",
|
||||
"@shumai/db": "workspace:*",
|
||||
@@ -732,6 +733,30 @@
|
||||
|
||||
"@mistralai/mistralai": ["@mistralai/mistralai@2.2.1", "", { "dependencies": { "ws": "^8.18.0", "zod": "^3.25.0 || ^4.0.0", "zod-to-json-schema": "^3.25.0" } }, "sha512-uKU8CZmL2RzYKmplsU01hii4p3pe4HqJefpWNRWXm1Tcm0Sm4xXfwSLIy4k7ZCPlbETCGcp69E7hZs+WOJ5itQ=="],
|
||||
|
||||
"@napi-rs/canvas": ["@napi-rs/canvas@1.0.2", "", { "optionalDependencies": { "@napi-rs/canvas-android-arm64": "1.0.2", "@napi-rs/canvas-darwin-arm64": "1.0.2", "@napi-rs/canvas-darwin-x64": "1.0.2", "@napi-rs/canvas-linux-arm-gnueabihf": "1.0.2", "@napi-rs/canvas-linux-arm64-gnu": "1.0.2", "@napi-rs/canvas-linux-arm64-musl": "1.0.2", "@napi-rs/canvas-linux-riscv64-gnu": "1.0.2", "@napi-rs/canvas-linux-x64-gnu": "1.0.2", "@napi-rs/canvas-linux-x64-musl": "1.0.2", "@napi-rs/canvas-win32-arm64-msvc": "1.0.2", "@napi-rs/canvas-win32-x64-msvc": "1.0.2" } }, "sha512-EYEqlMYaCbpZDz+IgDH5xp9MTd3ui4dmGqbQYryhMLnSRxrhHKq5KQWHHKxFUcEP4Hp8/BWgvqXocX4j7iSbOQ=="],
|
||||
|
||||
"@napi-rs/canvas-android-arm64": ["@napi-rs/canvas-android-arm64@1.0.2", "", { "os": "android", "cpu": "arm64" }, "sha512-IMXKVQod0ol4vt3gmClUfXz4JAgHYESGPCUqmH3lQxBoL0K/2greJaQE1HVBVxWWFKfLc4OLZVdxg7kXVyXv+g=="],
|
||||
|
||||
"@napi-rs/canvas-darwin-arm64": ["@napi-rs/canvas-darwin-arm64@1.0.2", "", { "os": "darwin", "cpu": "arm64" }, "sha512-Sc8tPi6cF+5lqOzCCKFALJHhDiRwyMzTPYm3bbhdXsOunU0lQO5f05ucyOzN2r55I23Hg5bsjH63uSCvWp3EgQ=="],
|
||||
|
||||
"@napi-rs/canvas-darwin-x64": ["@napi-rs/canvas-darwin-x64@1.0.2", "", { "os": "darwin", "cpu": "x64" }, "sha512-niDXZ9LhKB1zLrUdYB64RHQFDGz9rr0eGx061qtJJU3U20EMMIx28ADF5fVYbhtOgkWQrBjFicfaye1yM0U62A=="],
|
||||
|
||||
"@napi-rs/canvas-linux-arm-gnueabihf": ["@napi-rs/canvas-linux-arm-gnueabihf@1.0.2", "", { "os": "linux", "cpu": "arm" }, "sha512-sgatQL9JxGRH/Amzcvu0P3t8Am3duou74CisfuJ41Dwt8cWy723z/9KZ8LlgmxfypEwEZxSTNFJtU8d281lmhQ=="],
|
||||
|
||||
"@napi-rs/canvas-linux-arm64-gnu": ["@napi-rs/canvas-linux-arm64-gnu@1.0.2", "", { "os": "linux", "cpu": "arm64" }, "sha512-dgKuX0peF3xwY6ZF5QxGS4wbfDqpoFAJYXiLSp+guZKARQUKMkRqZSDrXKj7nfrec3UCMzC0PFCPte0ES98AiA=="],
|
||||
|
||||
"@napi-rs/canvas-linux-arm64-musl": ["@napi-rs/canvas-linux-arm64-musl@1.0.2", "", { "os": "linux", "cpu": "arm64" }, "sha512-qwROoDIC9upfvDoRLuPn2aNg9CGW1x0Ygr4k2Or+8paA9d0qBLwk87U+g8KQpoOviKoPoiwl97kvBYuYD7qZoA=="],
|
||||
|
||||
"@napi-rs/canvas-linux-riscv64-gnu": ["@napi-rs/canvas-linux-riscv64-gnu@1.0.2", "", { "os": "linux", "cpu": "none" }, "sha512-fXRjnPihdnbO6qy1QQOgxAonb68A0TCEG7rj1x7v7rxNElsE8EVIKIEUTvyDtU+sthYSbX+8e7g3oZiLGnOmxw=="],
|
||||
|
||||
"@napi-rs/canvas-linux-x64-gnu": ["@napi-rs/canvas-linux-x64-gnu@1.0.2", "", { "os": "linux", "cpu": "x64" }, "sha512-nPR97DXhbWIAy7yazF3jc06kEPMqYMLmPzFOVNlwKPfIoSChnI+x7dc0hTLaihz3jxrjL6j4BbA7earxfx4X3g=="],
|
||||
|
||||
"@napi-rs/canvas-linux-x64-musl": ["@napi-rs/canvas-linux-x64-musl@1.0.2", "", { "os": "linux", "cpu": "x64" }, "sha512-l7zZY5+jL5qnBZtDz7CoBtY6p7EkHu422g/0zWwrOrzIwWyWxZFRfZZORY1UG7YApymPLx+UbOkN206xXn/c1Q=="],
|
||||
|
||||
"@napi-rs/canvas-win32-arm64-msvc": ["@napi-rs/canvas-win32-arm64-msvc@1.0.2", "", { "os": "win32", "cpu": "arm64" }, "sha512-yE0koHCFF4PIbMc2o2SEALhnipz7WBISh5glLvQiomtIoCcW0np3H4Lw93ceJAfJttTTeIIWFbwH84F7EVzjMQ=="],
|
||||
|
||||
"@napi-rs/canvas-win32-x64-msvc": ["@napi-rs/canvas-win32-x64-msvc@1.0.2", "", { "os": "win32", "cpu": "x64" }, "sha512-okU8/t2foV6C31n0GtvEMbfD5rOFc70+/6xUNME9Guld29sgSOIGUEDScAWFlcP3k5TYQRl9TNkwJEEjh15w8A=="],
|
||||
|
||||
"@noble/ciphers": ["@noble/ciphers@2.2.0", "", {}, "sha512-Z6pjIZ/8IJcCGzb2S/0Px5J81yij85xASuk1teLNeg75bfT07MV3a/O2Mtn1I2se43k3lkVEcFaR10N4cgQcZA=="],
|
||||
|
||||
"@noble/hashes": ["@noble/hashes@2.2.0", "", {}, "sha512-IYqDGiTXab6FniAgnSdZwgWbomxpy9FtYvLKs7wCUs2a8RkITG+DFGO1DM9cr+E3/RgADRpFjrKVaJ1z6sjtEg=="],
|
||||
@@ -2242,6 +2267,8 @@
|
||||
|
||||
"pathe": ["pathe@2.0.3", "", {}, "sha512-WUjGcAqP1gQacoQe+OBJsFA7Ld4DyXuUIjZ5cc75cLHvJ7dtNsTugphxIADwspS+AraAUePCKrSVtPLFj/F88w=="],
|
||||
|
||||
"pdfjs-dist": ["pdfjs-dist@6.1.200", "", { "optionalDependencies": { "@napi-rs/canvas": "^1.0.0" } }, "sha512-o8MolyzirkkLrcdsae/HEOiIcXWI7DS5zGpvqW8xTC2YUsW30rltFw2bDGvw/fskUdEMrQm2br68jzDS5BH2vw=="],
|
||||
|
||||
"pend": ["pend@1.2.0", "", {}, "sha512-F3asv42UuXchdzt+xXqfW1OGlVBe+mxa2mqI0pg5yAHZPvFmY3Y6drSf/GQ1A86WgWEN9Kzh/WrgKa6iGcHXLg=="],
|
||||
|
||||
"perfect-debounce": ["perfect-debounce@2.1.0", "", {}, "sha512-LjgdTytVFXeUgtHZr9WYViYSM/g8MkcTPYDlPa3cDqMirHjKiSZPYd6DoL7pK8AJQr+uWkQvCjHNdiMqsrJs+g=="],
|
||||
|
||||
@@ -3,12 +3,15 @@ title: Agent Tools
|
||||
description: Configure the tools and capabilities permitted for each agent.
|
||||
---
|
||||
|
||||
Shumai agents are equipped with built-in tools that allow them to interact with the file system, view files, extract video frames, and execute commands.
|
||||
Shumai agents are equipped with built-in tools that allow them to interact with the file system, view files, extract video frames, and execute commands.
|
||||
|
||||
By default, **all tools are enabled** for new agents. Team Owners can deny specific tools to restrict agent capabilities.
|
||||
|
||||
<Warning>
|
||||
**Caution:** Disabling tools can cause agents or skills to fail. For example, disabling the **Sandboxed Terminal** tool will break almost all custom skills that rely on executing commands or script environments. Disable tools only when you have a specific security policy or isolation requirement.
|
||||
**Caution:** Disabling tools can cause agents or skills to fail. For example, disabling the
|
||||
**Sandboxed Terminal** tool will break almost all custom skills that rely on executing commands or
|
||||
script environments. Disable tools only when you have a specific security policy or isolation
|
||||
requirement.
|
||||
</Warning>
|
||||
|
||||
---
|
||||
@@ -18,34 +21,42 @@ By default, **all tools are enabled** for new agents. Team Owners can deny speci
|
||||
The following tools can be individually toggled in the agent configuration dialog:
|
||||
|
||||
### 1. Sandboxed Terminal (`bash`)
|
||||
|
||||
- **Description:** Allows the agent to run terminal/shell commands in a highly isolated environment.
|
||||
- **Why it matters:** Essential for running custom scripts, processing files on disk, or using standard Unix utilities. If disabled, the agent won't be able to run command-line code.
|
||||
|
||||
### 2. Read Skills (`read_skill`)
|
||||
|
||||
- **Description:** Allows the agent to load and read skill definitions to perform specialized actions.
|
||||
- **Why it matters:** Enables the agent to inspect the instructions/configurations of custom skills registered in your workspace before executing them.
|
||||
|
||||
### 3. Analyze Image (`analyze_image`)
|
||||
|
||||
- **Description:** Allows the agent to view and analyze the visual contents of an image file.
|
||||
- **Why it matters:** Enables multimodal agents to "see" image assets uploaded to Shumai, allowing them to answer comments or write metadata about images.
|
||||
|
||||
### 4. Video Frame Extraction (`screenshot`)
|
||||
|
||||
- **Description:** Allows the agent to extract still image frames from video files for analysis.
|
||||
- **Why it matters:** Enables the agent to perform frame-by-frame analysis on videos (e.g. for comments, annotation, or search).
|
||||
|
||||
### 5. Browse Shumai (`list_assets`)
|
||||
|
||||
- **Description:** Allows the agent to list folders and browse files in Shumai.
|
||||
- **Why it matters:** Gives the agent visual mapping of the project structure so it can search for other files or locate related assets.
|
||||
|
||||
### 6. Create Folder (`create_folder`)
|
||||
|
||||
- **Description:** Allows the agent to create new folders in Shumai.
|
||||
- **Why it matters:** Needed if the agent organizes outputs or places files in custom subdirectory folders.
|
||||
|
||||
### 7. Create File (`create_file`)
|
||||
|
||||
- **Description:** Allows the agent to create and upload new files to Shumai.
|
||||
- **Why it matters:** Allows the agent to generate new assets (e.g. text logs, transcoded clips, reports) and save them directly back into your project.
|
||||
|
||||
### 8. Create Version (`create_version`)
|
||||
|
||||
- **Description:** Allows the agent to upload and stack a new version of an existing file in Shumai.
|
||||
- **Why it matters:** Allows the agent to update files in a clean, non-destructive way by stacking revisions rather than creating duplicate files.
|
||||
|
||||
@@ -54,6 +65,7 @@ The following tools can be individually toggled in the agent configuration dialo
|
||||
## Restricting Tools
|
||||
|
||||
To change which tools an agent can use:
|
||||
|
||||
1. Navigate to **Team Settings** > **Agents** (Note: Only Team Owners can view and modify these settings).
|
||||
2. Click on the agent you want to edit.
|
||||
3. Scroll down below the **Skills** section to find the **Tools** configuration panel.
|
||||
|
||||
+2
-1
@@ -69,6 +69,7 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"@tanstack/react-virtual": "3.14.2",
|
||||
"jittered-fractional-indexing": "1.0.1"
|
||||
"jittered-fractional-indexing": "1.0.1",
|
||||
"pdfjs-dist": "6.1.200"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2021,6 +2021,7 @@ export class AssetService {
|
||||
originalWidth: asset.media.metadata?.originalWidth,
|
||||
spriteUrl,
|
||||
duration: asset.media.metadata?.duration,
|
||||
pageCount: asset.media.metadata?.totalFrames,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -287,6 +287,139 @@ export class TranscodeService {
|
||||
await execFileAsync('ffmpeg', ['-y', '-loglevel', 'warning', ...args])
|
||||
}
|
||||
|
||||
async generatePdfSprite(
|
||||
inputFile: string,
|
||||
outputSprite: string,
|
||||
outputPoster: string,
|
||||
): Promise<{ pageCount: number; originalWidth: number; originalHeight: number }> {
|
||||
const tmpDir = this.createTempDir('pdf-sprite-')
|
||||
try {
|
||||
const pagePrefix = path.join(tmpDir, 'page')
|
||||
try {
|
||||
await execFileAsync('pdftoppm', ['-png', '-f', '1', '-l', '100', inputFile, pagePrefix])
|
||||
} catch (err) {
|
||||
const msg = err instanceof Error ? err.message : String(err)
|
||||
if (msg.includes('pdftoppm') || msg.includes('ENOENT') || msg.includes('not found')) {
|
||||
throw new Error(
|
||||
`pdftoppm executable not found in $PATH. Please install poppler-utils / poppler. (${msg})`,
|
||||
{ cause: err },
|
||||
)
|
||||
}
|
||||
throw err
|
||||
}
|
||||
|
||||
const files = fs.readdirSync(tmpDir).filter((f) => f.endsWith('.png'))
|
||||
if (files.length === 0) {
|
||||
throw new Error('pdftoppm produced no image outputs')
|
||||
}
|
||||
|
||||
files.sort((a, b) => {
|
||||
const numA = parseInt(a.replace(/[^0-9]/g, ''), 10)
|
||||
const numB = parseInt(b.replace(/[^0-9]/g, ''), 10)
|
||||
return numA - numB
|
||||
})
|
||||
|
||||
const extractedCount = files.length
|
||||
const firstPagePath = path.join(tmpDir, files[0])
|
||||
const firstMeta = await sharp(firstPagePath, { limitInputPixels: false }).metadata()
|
||||
const originalWidth = firstMeta.width || 800
|
||||
const originalHeight = firstMeta.height || 1000
|
||||
|
||||
for (let f = 1; f <= 100; f++) {
|
||||
const pageIdx = Math.min(extractedCount - 1, Math.floor(((f - 1) * extractedCount) / 100))
|
||||
const srcPath = path.join(tmpDir, files[pageIdx])
|
||||
const destPath = path.join(tmpDir, `frame_${f}.png`)
|
||||
if (srcPath !== destPath) {
|
||||
fs.copyFileSync(srcPath, destPath)
|
||||
}
|
||||
}
|
||||
|
||||
const spriteArgs = [
|
||||
'-i',
|
||||
path.join(tmpDir, 'frame_%d.png'),
|
||||
'-filter_complex',
|
||||
'scale=w=480:h=-2,tile=10x10',
|
||||
'-frames:v',
|
||||
'1',
|
||||
'-c:v',
|
||||
'libwebp',
|
||||
'-q:v',
|
||||
'75',
|
||||
outputSprite,
|
||||
]
|
||||
await execFileAsync('ffmpeg', ['-y', '-loglevel', 'warning', ...spriteArgs])
|
||||
|
||||
await sharp(firstPagePath, { limitInputPixels: false })
|
||||
.resize(480, null, { withoutEnlargement: true, fit: 'inside' })
|
||||
.webp({ quality: 75 })
|
||||
.toFile(outputPoster)
|
||||
|
||||
let totalPages = extractedCount
|
||||
try {
|
||||
const { stdout } = await execFileAsync('pdfinfo', [inputFile])
|
||||
const match = stdout.match(/Pages:\s+(\d+)/)
|
||||
if (match) {
|
||||
totalPages = parseInt(match[1], 10)
|
||||
}
|
||||
} catch {
|
||||
// Fallback to extracted count if pdfinfo is unavailable
|
||||
}
|
||||
|
||||
return {
|
||||
pageCount: totalPages,
|
||||
originalWidth,
|
||||
originalHeight,
|
||||
}
|
||||
} finally {
|
||||
this.removeDir(tmpDir)
|
||||
}
|
||||
}
|
||||
|
||||
async getPdfInfo(inputFile: string): Promise<MediaMetadata> {
|
||||
const tmpDir = this.createTempDir('pdf-info-')
|
||||
let originalWidth = 800
|
||||
let originalHeight = 1000
|
||||
let pageCount = 1
|
||||
|
||||
try {
|
||||
const pagePrefix = path.join(tmpDir, 'page')
|
||||
await execFileAsync('pdftoppm', ['-png', '-f', '1', '-l', '1', inputFile, pagePrefix])
|
||||
const files = fs.readdirSync(tmpDir).filter((f) => f.endsWith('.png'))
|
||||
if (files.length > 0) {
|
||||
const meta = await sharp(path.join(tmpDir, files[0]), {
|
||||
limitInputPixels: false,
|
||||
}).metadata()
|
||||
originalWidth = meta.width || 800
|
||||
originalHeight = meta.height || 1000
|
||||
}
|
||||
} catch (err) {
|
||||
console.warn('pdftoppm not available for PDF dimensions extraction:', err)
|
||||
}
|
||||
|
||||
try {
|
||||
const { stdout } = await execFileAsync('pdfinfo', [inputFile])
|
||||
const match = stdout.match(/Pages:\s+(\d+)/)
|
||||
if (match) {
|
||||
pageCount = parseInt(match[1], 10)
|
||||
}
|
||||
} catch {
|
||||
// Fallback
|
||||
} finally {
|
||||
this.removeDir(tmpDir)
|
||||
}
|
||||
|
||||
return {
|
||||
originalWidth,
|
||||
originalHeight,
|
||||
duration: 0,
|
||||
bitRate: 0,
|
||||
frameRate: 0,
|
||||
totalFrames: pageCount,
|
||||
hasAudio: false,
|
||||
mimeType: 'application/pdf',
|
||||
}
|
||||
}
|
||||
|
||||
async extractAudio(inputFile: string, outputFile: string, bitrate: string): Promise<void> {
|
||||
const args = ['-i', inputFile, '-vn', '-acodec', 'libmp3lame', '-b:a', bitrate, outputFile]
|
||||
await execFileAsync('ffmpeg', ['-y', '-loglevel', 'warning', ...args])
|
||||
|
||||
@@ -18,7 +18,7 @@ import {
|
||||
PresignedUrl,
|
||||
TaskInfo,
|
||||
} from '@shumai/dtos'
|
||||
import { ImageTranscoder, VideoTranscoder } from '@shumai/transcode'
|
||||
import { ImageTranscoder, PdfTranscoder, VideoTranscoder } from '@shumai/transcode'
|
||||
import { generateKeyBetween } from 'jittered-fractional-indexing'
|
||||
import { ulid } from 'ulid'
|
||||
import { sanitizeFilename } from '@shumai/core/src/utils/filename'
|
||||
@@ -303,6 +303,7 @@ export class UploadService {
|
||||
const isVideo = asset.mediaType?.startsWith('video/')
|
||||
const isImage = asset.mediaType?.startsWith('image/')
|
||||
const isAudio = asset.mediaType?.startsWith('audio/')
|
||||
const isPdf = asset.mediaType === 'application/pdf' || asset.name.toLowerCase().endsWith('.pdf')
|
||||
|
||||
if (autofillAgent && (isVideo || isImage)) {
|
||||
await tx.workflowTask.create({
|
||||
@@ -320,7 +321,7 @@ export class UploadService {
|
||||
})
|
||||
}
|
||||
|
||||
if (!isVideo && !isImage && !isAudio) {
|
||||
if (!isVideo && !isImage && !isAudio && !isPdf) {
|
||||
await tx.asset.update({
|
||||
where: { id: asset.id },
|
||||
data: { status: AssetStatus.processed },
|
||||
@@ -341,6 +342,12 @@ export class UploadService {
|
||||
} else if (isImage) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
await new ImageTranscoder(tx as any, asset.id, team.id, projectId).withThumbnail().submit()
|
||||
} else if (isPdf) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
await new PdfTranscoder(tx as any, asset.id, team.id, projectId)
|
||||
.withSprite()
|
||||
.withPoster()
|
||||
.submit()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ export const previewInfoSchema = z.object({
|
||||
originalWidth: z.number().optional(),
|
||||
spriteUrl: z.string().optional(),
|
||||
duration: z.number().optional(),
|
||||
pageCount: z.number().optional(),
|
||||
})
|
||||
export type PreviewInfo = z.infer<typeof previewInfoSchema>
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ import { s3Service } from '@shumai/core/src/s3/s3'
|
||||
import { prisma } from '@shumai/db'
|
||||
import { setupTestDbHooks } from '@shumai/db/test'
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { transcodeService } from '@shumai/core'
|
||||
import { transcodeService } from '@shumai/core/src/transcode/transcode'
|
||||
import * as child_process from 'child_process'
|
||||
|
||||
vi.mock('child_process', () => ({
|
||||
@@ -39,10 +39,25 @@ vi.mock('@shumai/core/src/transcode/transcode', () => ({
|
||||
getVideoInfo: vi.fn(),
|
||||
getAudioInfo: vi.fn(),
|
||||
getImageInfo: vi.fn(),
|
||||
getPdfInfo: vi.fn().mockResolvedValue({
|
||||
originalWidth: 800,
|
||||
originalHeight: 1000,
|
||||
duration: 0,
|
||||
bitRate: 0,
|
||||
frameRate: 0,
|
||||
totalFrames: 10,
|
||||
hasAudio: false,
|
||||
mimeType: 'application/pdf',
|
||||
}),
|
||||
transcodeVideo: vi.fn(),
|
||||
transcodeAudio: vi.fn(),
|
||||
transcodeImage: vi.fn(),
|
||||
generateSprite: vi.fn(),
|
||||
generatePdfSprite: vi.fn().mockResolvedValue({
|
||||
pageCount: 10,
|
||||
originalWidth: 800,
|
||||
originalHeight: 1000,
|
||||
}),
|
||||
createTempDir: vi.fn().mockReturnValue('/tmp'),
|
||||
removeDir: vi.fn(),
|
||||
takeScreenshots: vi.fn(),
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { prisma, WorkflowTaskType, WorkflowTaskStatus } from '@shumai/db'
|
||||
import { s3Service } from '@shumai/core/src/s3/s3'
|
||||
import { transcodeService } from '@shumai/core'
|
||||
import { transcodeService } from '@shumai/core/src/transcode/transcode'
|
||||
import { metadataService } from '@shumai/core/src/metadata/metadata'
|
||||
import { stemFromKey } from '@shumai/core/src/utils/filename'
|
||||
import { ApplicationFailure } from '@temporalio/activity'
|
||||
@@ -163,6 +163,25 @@ export async function getMediaInfoActivity(params: {
|
||||
{ key: 'resolution_width', value: info.originalWidth },
|
||||
{ key: 'resolution_height', value: info.originalHeight },
|
||||
)
|
||||
} else if (params.mediaType === 'application/pdf') {
|
||||
const info = await transcodeService.getPdfInfo(params.filePath)
|
||||
mediaInfo.frames = info.totalFrames
|
||||
mediaInfo.metadata = {
|
||||
originalWidth: info.originalWidth,
|
||||
originalHeight: info.originalHeight,
|
||||
duration: 0,
|
||||
bitRate: 0,
|
||||
frameRate: 0,
|
||||
totalFrames: info.totalFrames,
|
||||
startTimecode: '00:00:00:00',
|
||||
hasAudio: false,
|
||||
format: {},
|
||||
}
|
||||
metadataUpdates.push(
|
||||
{ key: 'resolution_width', value: info.originalWidth },
|
||||
{ key: 'resolution_height', value: info.originalHeight },
|
||||
{ key: 'total_pages', value: info.totalFrames },
|
||||
)
|
||||
}
|
||||
|
||||
await metadataService.updateAssetMetadata(params.assetId, metadataUpdates, true)
|
||||
@@ -401,12 +420,26 @@ export async function generateSpriteActivity(params: GenerateSpriteActivityParam
|
||||
const posterFile = path.join(tmpDir, 'poster.webp')
|
||||
|
||||
try {
|
||||
await transcodeService.generateSprite(
|
||||
params.filePath,
|
||||
spriteFile,
|
||||
posterFile,
|
||||
params.mediaInfo.duration,
|
||||
)
|
||||
if (params.mediaInfo.mimeType === 'application/pdf') {
|
||||
const pdfRes = await transcodeService.generatePdfSprite(
|
||||
params.filePath,
|
||||
spriteFile,
|
||||
posterFile,
|
||||
)
|
||||
if (params.mediaInfo && params.mediaInfo.metadata) {
|
||||
params.mediaInfo.metadata.totalFrames = pdfRes.pageCount
|
||||
params.mediaInfo.metadata.originalWidth = pdfRes.originalWidth
|
||||
params.mediaInfo.metadata.originalHeight = pdfRes.originalHeight
|
||||
params.mediaInfo.frames = pdfRes.pageCount
|
||||
}
|
||||
} else {
|
||||
await transcodeService.generateSprite(
|
||||
params.filePath,
|
||||
spriteFile,
|
||||
posterFile,
|
||||
params.mediaInfo.duration,
|
||||
)
|
||||
}
|
||||
|
||||
const spriteBuffer = fs.readFileSync(spriteFile)
|
||||
await s3Service.putObject(
|
||||
|
||||
@@ -82,3 +82,41 @@ export class ImageTranscoder {
|
||||
return task.id
|
||||
}
|
||||
}
|
||||
|
||||
export class PdfTranscoder {
|
||||
private spec: PrismaJson.TaskSpec = {}
|
||||
|
||||
constructor(
|
||||
private readonly db: TransactionClient,
|
||||
private readonly assetId: string,
|
||||
private readonly teamId: string,
|
||||
private readonly projectId: string,
|
||||
) {}
|
||||
|
||||
withSprite(): this {
|
||||
this.spec.sprite = true
|
||||
return this
|
||||
}
|
||||
|
||||
withPoster(): this {
|
||||
this.spec.poster = true
|
||||
return this
|
||||
}
|
||||
|
||||
async submit(): Promise<string> {
|
||||
const task = await this.db.workflowTask.create({
|
||||
data: {
|
||||
assetId: this.assetId,
|
||||
teamId: this.teamId,
|
||||
projectId: this.projectId,
|
||||
type: WorkflowTaskType.transcode,
|
||||
status: WorkflowTaskStatus.pending,
|
||||
payload: {
|
||||
projectId: this.projectId,
|
||||
transcode: this.spec,
|
||||
},
|
||||
},
|
||||
})
|
||||
return task.id
|
||||
}
|
||||
}
|
||||
|
||||
@@ -245,6 +245,89 @@ describe('Transcode Workflow', () => {
|
||||
})
|
||||
})
|
||||
|
||||
it('should process pdf transcode and sprite generation successfully', async () => {
|
||||
const task: WorkflowTask = {
|
||||
id: 'task-pdf',
|
||||
assetId: 'asset-pdf',
|
||||
type: WorkflowTaskType.transcode,
|
||||
status: WorkflowTaskStatus.pending,
|
||||
sessionId: null,
|
||||
output: null,
|
||||
payload: {
|
||||
projectId: 'proj-1',
|
||||
transcode: {
|
||||
sprite: true,
|
||||
poster: true,
|
||||
},
|
||||
},
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
heartbeat: null,
|
||||
teamId: 'team-1',
|
||||
projectId: 'proj-1',
|
||||
uid: 'task-uid-pdf',
|
||||
model: null,
|
||||
inputTokens: 0,
|
||||
outputTokens: 0,
|
||||
}
|
||||
|
||||
mockActivities.getAssetActivity.mockResolvedValue({
|
||||
id: 'asset-pdf',
|
||||
storageKey: { key: 'document.pdf' },
|
||||
mediaType: 'application/pdf',
|
||||
})
|
||||
|
||||
mockActivities.getMediaInfoActivity.mockResolvedValue({
|
||||
mimeType: 'application/pdf',
|
||||
metadata: {
|
||||
originalWidth: 800,
|
||||
originalHeight: 1000,
|
||||
duration: 0,
|
||||
frameRate: 0,
|
||||
totalFrames: 12,
|
||||
startTimecode: '00:00:00:00',
|
||||
bitRate: 0,
|
||||
hasAudio: false,
|
||||
format: {},
|
||||
},
|
||||
videoTranscodes: [],
|
||||
imageTranscodes: [],
|
||||
})
|
||||
|
||||
mockActivities.generateSpriteActivity.mockResolvedValue({
|
||||
sprite: { key: 'sprite.webp', frames: 100, tileX: 10, tileY: 10 },
|
||||
poster: { key: 'poster.webp' },
|
||||
})
|
||||
|
||||
await transcodeMedia(task)
|
||||
|
||||
expect(mockActivities.updateAssetStatusActivity).toHaveBeenCalledWith({
|
||||
assetId: 'asset-pdf',
|
||||
status: AssetStatus.processing,
|
||||
})
|
||||
|
||||
expect(mockActivities.generateSpriteActivity).toHaveBeenCalledWith({
|
||||
assetKey: 'document.pdf',
|
||||
filePath: '/tmp/video.mp4',
|
||||
spriteSpec: expect.objectContaining({ key: 'sprite.webp' }),
|
||||
posterSpec: expect.objectContaining({ key: 'poster.webp' }),
|
||||
mediaInfo: expect.objectContaining({ mimeType: 'application/pdf' }),
|
||||
})
|
||||
|
||||
expect(mockActivities.updateAssetMediaActivity).toHaveBeenCalledWith({
|
||||
assetId: 'asset-pdf',
|
||||
mediaInfo: expect.objectContaining({
|
||||
sprite: { key: 'sprite.webp', frames: 100, tileX: 10, tileY: 10 },
|
||||
poster: { key: 'poster.webp' },
|
||||
}),
|
||||
})
|
||||
|
||||
expect(mockActivities.updateAssetStatusActivity).toHaveBeenCalledWith({
|
||||
assetId: 'asset-pdf',
|
||||
status: AssetStatus.processed,
|
||||
})
|
||||
})
|
||||
|
||||
it('should handle failures and update task status with error', async () => {
|
||||
const task: WorkflowTask = {
|
||||
id: 'task-fail',
|
||||
|
||||
@@ -11,6 +11,7 @@ type FilePreviewItem = {
|
||||
originalWidth?: number
|
||||
originalHeight?: number
|
||||
duration?: number
|
||||
pageCount?: number
|
||||
} | null
|
||||
}
|
||||
|
||||
@@ -22,23 +23,30 @@ interface FilePreviewProps {
|
||||
export const FilePreview = ({ item, showDuration = false }: FilePreviewProps) => {
|
||||
const isVideo = item.preview?.mediaType?.startsWith('video/')
|
||||
const isAudio = item.preview?.mediaType?.startsWith('audio/')
|
||||
const isPdf = item.preview?.mediaType === 'application/pdf'
|
||||
const hasDuration = isVideo || isAudio
|
||||
const duration = item.preview?.duration
|
||||
const durationOverlay =
|
||||
const pageCount = item.preview?.pageCount
|
||||
|
||||
const overlay =
|
||||
showDuration && hasDuration && typeof duration === 'number' && duration > 0 ? (
|
||||
<span className="pointer-events-none absolute bottom-1 right-1 text-xs font-medium tabular-nums text-white">
|
||||
<span className="pointer-events-none absolute bottom-1 right-1 rounded bg-black/60 px-1 py-0.5 text-xs font-medium tabular-nums text-white">
|
||||
{formatTime(duration)}
|
||||
</span>
|
||||
) : showDuration && isPdf && typeof pageCount === 'number' && pageCount > 0 ? (
|
||||
<span className="pointer-events-none absolute bottom-1 right-1 rounded bg-black/60 px-1 py-0.5 text-xs font-medium tabular-nums text-white">
|
||||
{pageCount} P
|
||||
</span>
|
||||
) : null
|
||||
|
||||
const isVideoWithSprite =
|
||||
isVideo &&
|
||||
const isMediaWithSprite =
|
||||
(isVideo || isPdf) &&
|
||||
item.preview?.spriteUrl &&
|
||||
item.preview.thumbnailUrl &&
|
||||
item.preview.originalWidth &&
|
||||
item.preview.originalHeight
|
||||
|
||||
if (isVideoWithSprite && item.preview) {
|
||||
if (isMediaWithSprite && item.preview) {
|
||||
return (
|
||||
<>
|
||||
<SpriteScrubber
|
||||
@@ -47,7 +55,7 @@ export const FilePreview = ({ item, showDuration = false }: FilePreviewProps) =>
|
||||
videoWidth={item.preview.originalWidth!}
|
||||
videoHeight={item.preview.originalHeight!}
|
||||
/>
|
||||
{durationOverlay}
|
||||
{overlay}
|
||||
</>
|
||||
)
|
||||
}
|
||||
@@ -60,7 +68,7 @@ export const FilePreview = ({ item, showDuration = false }: FilePreviewProps) =>
|
||||
alt="Preview"
|
||||
className="w-full h-full object-contain bg-black"
|
||||
/>
|
||||
{durationOverlay}
|
||||
{overlay}
|
||||
</>
|
||||
)
|
||||
}
|
||||
@@ -71,7 +79,7 @@ export const FilePreview = ({ item, showDuration = false }: FilePreviewProps) =>
|
||||
<div className="w-full h-full flex items-center justify-center bg-zinc-950">
|
||||
<AudioLines className="w-8 h-8 text-zinc-500" />
|
||||
</div>
|
||||
{durationOverlay}
|
||||
{overlay}
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
import { FileTypeDefinition } from '../types'
|
||||
import PdfViewer from './pdf-viewer'
|
||||
|
||||
export const pdfTypeDefinition: FileTypeDefinition = {
|
||||
id: 'pdf',
|
||||
name: 'PDF',
|
||||
match: (file) => file.mediaType === 'application/pdf' || file.name.toLowerCase().endsWith('.pdf'),
|
||||
viewer: PdfViewer,
|
||||
commentsConfig: {
|
||||
hasTimestamp: false,
|
||||
hasAnnotations: true,
|
||||
hasAiBots: true,
|
||||
},
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
import { ChevronLeft, ChevronRight, Download, Minus, Plus } from 'lucide-react'
|
||||
import { m } from '@/ui/paraglide/messages.js'
|
||||
|
||||
export interface PdfControlBarProps {
|
||||
currentPage: number
|
||||
totalPages: number
|
||||
onPageChange: (newPage: number) => void
|
||||
zoom: number
|
||||
onZoomIn: () => void
|
||||
onZoomOut: () => void
|
||||
onFit: () => void
|
||||
onDownload: () => void
|
||||
canDownload: boolean
|
||||
}
|
||||
|
||||
export function PdfControlBar({
|
||||
currentPage,
|
||||
totalPages,
|
||||
onPageChange,
|
||||
zoom,
|
||||
onZoomIn,
|
||||
onZoomOut,
|
||||
onFit,
|
||||
onDownload,
|
||||
canDownload,
|
||||
}: PdfControlBarProps) {
|
||||
return (
|
||||
<div className="relative px-4 py-3 bg-card border-t border-gray-200 dark:border-gray-700 z-10 flex items-center justify-between gap-2 transition-colors duration-200">
|
||||
{/* Page Navigation */}
|
||||
<div className="flex items-center gap-1.5 bg-gray-200/50 dark:bg-white/10 rounded-md p-0.5">
|
||||
<button
|
||||
onClick={() => onPageChange(Math.max(1, currentPage - 1))}
|
||||
disabled={currentPage <= 1}
|
||||
className="p-1.5 rounded hover:bg-gray-300 dark:hover:bg-gray-600 text-gray-700 dark:text-gray-200 transition-colors disabled:opacity-40"
|
||||
title={m.previous_page()}
|
||||
>
|
||||
<ChevronLeft size={16} />
|
||||
</button>
|
||||
<span className="px-2 text-xs font-mono font-medium text-gray-900 dark:text-gray-100 select-none">
|
||||
{m.page_of_pages({ current: currentPage, total: totalPages || 1 })}
|
||||
</span>
|
||||
<button
|
||||
onClick={() => onPageChange(Math.min(totalPages, currentPage + 1))}
|
||||
disabled={currentPage >= totalPages}
|
||||
className="p-1.5 rounded hover:bg-gray-300 dark:hover:bg-gray-600 text-gray-700 dark:text-gray-200 transition-colors disabled:opacity-40"
|
||||
title={m.next_page()}
|
||||
>
|
||||
<ChevronRight size={16} />
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{/* Zoom & Action Controls */}
|
||||
<div className="flex items-center gap-2">
|
||||
<div className="flex items-center gap-1 bg-gray-200/50 dark:bg-white/10 rounded-md p-0.5">
|
||||
<button
|
||||
onClick={onZoomOut}
|
||||
className="p-1.5 rounded hover:bg-gray-300 dark:hover:bg-gray-600 text-gray-700 dark:text-gray-200 transition-colors"
|
||||
title={m.zoom_out()}
|
||||
>
|
||||
<Minus size={16} />
|
||||
</button>
|
||||
<span className="w-12 text-center text-xs font-mono font-medium text-gray-900 dark:text-gray-100 select-none">
|
||||
{Math.round(zoom * 100)}%
|
||||
</span>
|
||||
<button
|
||||
onClick={onZoomIn}
|
||||
className="p-1.5 rounded hover:bg-gray-300 dark:hover:bg-gray-600 text-gray-700 dark:text-gray-200 transition-colors"
|
||||
title={m.zoom_in()}
|
||||
>
|
||||
<Plus size={16} />
|
||||
</button>
|
||||
</div>
|
||||
<button
|
||||
onClick={onFit}
|
||||
className="text-xs font-medium px-3 py-1.5 rounded bg-gray-200/50 dark:bg-white/10 hover:bg-gray-300 dark:hover:bg-gray-600 text-gray-700 dark:text-gray-200 transition-colors border border-transparent"
|
||||
>
|
||||
{m.fit()}
|
||||
</button>
|
||||
<button
|
||||
onClick={onDownload}
|
||||
disabled={!canDownload}
|
||||
className="flex items-center gap-1.5 text-xs font-medium px-3 py-1.5 rounded bg-gray-200/50 dark:bg-white/10 hover:bg-gray-300 dark:hover:bg-gray-600 text-gray-700 dark:text-gray-200 transition-colors border border-transparent disabled:opacity-50"
|
||||
title={m.download_original_file()}
|
||||
>
|
||||
<Download size={14} />
|
||||
{m.download()}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,257 @@
|
||||
import { client } from '@/ui/api/client'
|
||||
import DrawingCanvas from '@/ui/components/drawing-canvas'
|
||||
import { useAnnotationStore } from '@/ui/stores/annotation-store'
|
||||
import * as pdfjsLib from 'pdfjs-dist'
|
||||
import React, { useCallback, useEffect, useImperativeHandle, useRef, useState } from 'react'
|
||||
import { FileViewerProps, MediaController } from '../types'
|
||||
import { PdfControlBar } from './pdf-control-bar'
|
||||
|
||||
if (typeof window !== 'undefined' && pdfjsLib.GlobalWorkerOptions) {
|
||||
pdfjsLib.GlobalWorkerOptions.workerSrc = `https://unpkg.com/pdfjs-dist@${pdfjsLib.version}/build/pdf.worker.min.mjs`
|
||||
}
|
||||
|
||||
export const PdfViewer = React.forwardRef<MediaController, FileViewerProps>(
|
||||
({ file, annotations, shareId, children }, ref) => {
|
||||
useImperativeHandle(ref, () => ({
|
||||
play: () => {},
|
||||
pause: () => {},
|
||||
seekTo: () => {},
|
||||
}))
|
||||
|
||||
const {
|
||||
isDrawing,
|
||||
currentTool,
|
||||
currentColor,
|
||||
addAnnotation,
|
||||
annotations: draftAnnotations,
|
||||
} = useAnnotationStore()
|
||||
|
||||
const [pdfDoc, setPdfDoc] = useState<pdfjsLib.PDFDocumentProxy | null>(null)
|
||||
const [currentPage, setCurrentPage] = useState(1)
|
||||
const [totalPages, setTotalPages] = useState(1)
|
||||
const [pageImageUrl, setPageImageUrl] = useState<string>('')
|
||||
const [pageDimensions, setPageDimensions] = useState({ width: 800, height: 1000 })
|
||||
const [loading, setLoading] = useState(true)
|
||||
const [error, setError] = useState<string | null>(null)
|
||||
|
||||
const [containerSize, setContainerSize] = useState({ width: 0, height: 0 })
|
||||
const [zoom, setZoom] = useState(1)
|
||||
const [pan, setPan] = useState({ x: 0, y: 0 })
|
||||
|
||||
const observerRef = useRef<ResizeObserver | null>(null)
|
||||
|
||||
const containerRef = useCallback((node: HTMLDivElement | null) => {
|
||||
if (observerRef.current) {
|
||||
observerRef.current.disconnect()
|
||||
observerRef.current = null
|
||||
}
|
||||
|
||||
if (node !== null) {
|
||||
const observer = new ResizeObserver((entries) => {
|
||||
const entry = entries[0]
|
||||
if (entry) {
|
||||
setContainerSize({
|
||||
width: entry.contentRect.width,
|
||||
height: entry.contentRect.height,
|
||||
})
|
||||
}
|
||||
})
|
||||
observer.observe(node)
|
||||
observerRef.current = observer
|
||||
}
|
||||
}, [])
|
||||
|
||||
const fileUrl = file.media?.original?.downloadUrl
|
||||
|
||||
// Load PDF Document
|
||||
useEffect(() => {
|
||||
if (!fileUrl) {
|
||||
setError('No PDF URL available')
|
||||
setLoading(false)
|
||||
return
|
||||
}
|
||||
|
||||
let active = true
|
||||
setLoading(true)
|
||||
setError(null)
|
||||
|
||||
const loadingTask = pdfjsLib.getDocument({ url: fileUrl })
|
||||
loadingTask.promise
|
||||
.then((doc) => {
|
||||
if (!active) return
|
||||
setPdfDoc(doc)
|
||||
setTotalPages(doc.numPages)
|
||||
setCurrentPage(1)
|
||||
setLoading(false)
|
||||
})
|
||||
.catch((err) => {
|
||||
if (!active) return
|
||||
console.error('Failed to load PDF:', err)
|
||||
setError('Failed to load PDF document')
|
||||
setLoading(false)
|
||||
})
|
||||
|
||||
return () => {
|
||||
active = false
|
||||
}
|
||||
}, [fileUrl])
|
||||
|
||||
// Render active PDF page into image
|
||||
useEffect(() => {
|
||||
if (!pdfDoc) return
|
||||
|
||||
let active = true
|
||||
pdfDoc
|
||||
.getPage(currentPage)
|
||||
.then((page) => {
|
||||
if (!active) return
|
||||
const unscaledViewport = page.getViewport({ scale: 1.0 })
|
||||
const renderScale = 2.0
|
||||
const viewport = page.getViewport({ scale: renderScale })
|
||||
|
||||
const canvas = document.createElement('canvas')
|
||||
canvas.width = viewport.width
|
||||
canvas.height = viewport.height
|
||||
const ctx = canvas.getContext('2d')
|
||||
|
||||
if (!ctx) return
|
||||
|
||||
page
|
||||
.render({ canvasContext: ctx, viewport, canvas })
|
||||
.promise.then(() => {
|
||||
if (!active) return
|
||||
setPageImageUrl(canvas.toDataURL('image/png'))
|
||||
setPageDimensions({
|
||||
width: unscaledViewport.width,
|
||||
height: unscaledViewport.height,
|
||||
})
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error('Failed to render page:', err)
|
||||
})
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error('Failed to get page:', err)
|
||||
})
|
||||
|
||||
return () => {
|
||||
active = false
|
||||
}
|
||||
}, [pdfDoc, currentPage])
|
||||
|
||||
const conW = containerSize.width
|
||||
const conH = containerSize.height
|
||||
const pdfW = pageDimensions.width
|
||||
const pdfH = pageDimensions.height
|
||||
|
||||
let baseScale = 1
|
||||
if (conW > 0 && conH > 0 && pdfW > 0 && pdfH > 0) {
|
||||
baseScale = Math.min(conW / pdfW, conH / pdfH)
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
if (conW > 0 && conH > 0 && pdfW > 0 && pdfH > 0) {
|
||||
setZoom(baseScale)
|
||||
const x = (conW - pdfW * baseScale) / 2
|
||||
const y = (conH - pdfH * baseScale) / 2
|
||||
setPan({ x, y })
|
||||
}
|
||||
}, [file.id, currentPage, conW, conH, baseScale, pdfW, pdfH])
|
||||
|
||||
const handleZoom = (factor: number) => {
|
||||
const cx = conW / 2
|
||||
const cy = conH / 2
|
||||
|
||||
const newZoom = zoom * factor
|
||||
if (newZoom < 0.01 || newZoom > 50) return
|
||||
|
||||
const newPanX = cx - (cx - pan.x) * (newZoom / zoom)
|
||||
const newPanY = cy - (cy - pan.y) * (newZoom / zoom)
|
||||
|
||||
setZoom(newZoom)
|
||||
setPan({ x: newPanX, y: newPanY })
|
||||
}
|
||||
|
||||
const handleFit = () => {
|
||||
setZoom(baseScale)
|
||||
const x = (conW - pdfW * baseScale) / 2
|
||||
const y = (conH - pdfH * baseScale) / 2
|
||||
setPan({ x, y })
|
||||
}
|
||||
|
||||
const handleDownload = async () => {
|
||||
const key = file.media?.original?.key
|
||||
if (!key || !file.id) return
|
||||
try {
|
||||
const res = shareId
|
||||
? await client.api.shares[':shareId'].files[':fileId']['download-url'].$post({
|
||||
param: { shareId, fileId: file.id },
|
||||
json: { key },
|
||||
})
|
||||
: await client.api.files['download-url'].$post({
|
||||
json: { key, assetId: file.id },
|
||||
})
|
||||
if (!res.ok) return
|
||||
const { url } = await res.json()
|
||||
const link = document.createElement('a')
|
||||
link.href = url
|
||||
link.download = file.name || 'document.pdf'
|
||||
document.body.appendChild(link)
|
||||
link.click()
|
||||
document.body.removeChild(link)
|
||||
} catch {
|
||||
// fail silently
|
||||
}
|
||||
}
|
||||
|
||||
const displayAnnotations = [...(annotations || []), ...draftAnnotations]
|
||||
|
||||
return (
|
||||
<div className="flex flex-col flex-1 h-full overflow-hidden bg-gray-100 dark:bg-gray-950 relative">
|
||||
<div className="flex-1 flex flex-col-reverse md:flex-row min-h-0 relative">
|
||||
{children}
|
||||
<div ref={containerRef} className="flex-1 relative overflow-hidden">
|
||||
{loading ? (
|
||||
<div className="absolute inset-0 flex items-center justify-center bg-black/40 z-20">
|
||||
<div className="w-8 h-8 border-4 border-white/30 border-t-white rounded-full animate-spin" />
|
||||
</div>
|
||||
) : error ? (
|
||||
<div className="absolute inset-0 flex items-center justify-center text-gray-500 text-sm">
|
||||
{error}
|
||||
</div>
|
||||
) : (
|
||||
<DrawingCanvas
|
||||
width={conW}
|
||||
height={conH}
|
||||
mediaDimensions={pageDimensions}
|
||||
imageUrl={pageImageUrl}
|
||||
annotations={displayAnnotations}
|
||||
scale={zoom}
|
||||
offset={pan}
|
||||
onPan={setPan}
|
||||
className="absolute inset-0 z-0"
|
||||
isDrawing={isDrawing}
|
||||
currentTool={currentTool}
|
||||
currentColor={currentColor}
|
||||
onAddAnnotation={addAnnotation}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
<PdfControlBar
|
||||
currentPage={currentPage}
|
||||
totalPages={totalPages}
|
||||
onPageChange={setCurrentPage}
|
||||
zoom={zoom}
|
||||
onZoomIn={() => handleZoom(1.2)}
|
||||
onZoomOut={() => handleZoom(0.8)}
|
||||
onFit={handleFit}
|
||||
onDownload={handleDownload}
|
||||
canDownload={!!file.media?.original?.key}
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
},
|
||||
)
|
||||
|
||||
export default PdfViewer
|
||||
@@ -2,9 +2,10 @@ import type { AssetInfo } from '@shumai/dtos'
|
||||
import { FileTypeDefinition } from './types'
|
||||
import { videoTypeDefinition } from './video'
|
||||
import { imageTypeDefinition } from './image'
|
||||
import { pdfTypeDefinition } from './pdf'
|
||||
import { defaultTypeDefinition } from './default'
|
||||
|
||||
const registry: FileTypeDefinition[] = [videoTypeDefinition, imageTypeDefinition]
|
||||
const registry: FileTypeDefinition[] = [pdfTypeDefinition, videoTypeDefinition, imageTypeDefinition]
|
||||
|
||||
export function getViewerForFile(file: AssetInfo | null | undefined): FileTypeDefinition {
|
||||
if (!file) return defaultTypeDefinition
|
||||
|
||||
Vendored
+5
@@ -2,3 +2,8 @@ declare module '*.css' {
|
||||
const content: { [className: string]: string }
|
||||
export default content
|
||||
}
|
||||
|
||||
declare module '*?url' {
|
||||
const content: string
|
||||
export default content
|
||||
}
|
||||
|
||||
@@ -475,6 +475,9 @@
|
||||
"zoom_out": "Zoom Out",
|
||||
"zoom_in": "Zoom In",
|
||||
"fit": "Fit",
|
||||
"previous_page": "Previous Page",
|
||||
"next_page": "Next Page",
|
||||
"page_of_pages": "Page {current} of {total}",
|
||||
"copy_image_to_clipboard": "Copy optimized image to clipboard",
|
||||
"download_original_image": "Download original image",
|
||||
|
||||
|
||||
@@ -474,7 +474,10 @@
|
||||
"download_original_file": "下载原始文件",
|
||||
"zoom_out": "缩小",
|
||||
"zoom_in": "放大",
|
||||
"fit": "适应",
|
||||
"fit": "适应窗口",
|
||||
"previous_page": "上一页",
|
||||
"next_page": "下一页",
|
||||
"page_of_pages": "第 {current} / {total} 页",
|
||||
"copy_image_to_clipboard": "复制优化图片到剪贴板",
|
||||
"download_original_image": "下载原始图片",
|
||||
|
||||
|
||||
+3
@@ -451,6 +451,9 @@ export * from "./download_original_file.js";
|
||||
export * from "./zoom_out.js";
|
||||
export * from "./zoom_in.js";
|
||||
export * from "./fit.js";
|
||||
export * from "./previous_page.js";
|
||||
export * from "./next_page.js";
|
||||
export * from "./page_of_pages.js";
|
||||
export * from "./copy_image_to_clipboard.js";
|
||||
export * from "./download_original_image.js";
|
||||
export * from "./no_item_selected.js";
|
||||
|
||||
+3
@@ -453,6 +453,9 @@ export * from './download_original_file.js'
|
||||
export * from './zoom_out.js'
|
||||
export * from './zoom_in.js'
|
||||
export * from './fit.js'
|
||||
export * from './previous_page.js'
|
||||
export * from './next_page.js'
|
||||
export * from './page_of_pages.js'
|
||||
export * from './copy_image_to_clipboard.js'
|
||||
export * from './download_original_image.js'
|
||||
export * from './no_item_selected.js'
|
||||
|
||||
Generated
+1
-1
@@ -10,7 +10,7 @@ const en_fit = /** @type {(inputs: FitInputs) => LocalizedString} */ () => {
|
||||
};
|
||||
|
||||
const zh_fit = /** @type {(inputs: FitInputs) => LocalizedString} */ () => {
|
||||
return /** @type {LocalizedString} */ (`适应`)
|
||||
return /** @type {LocalizedString} */ (`适应窗口`)
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
/**
|
||||
* | output |
|
||||
* | --- |
|
||||
* | "Next Page" |
|
||||
*
|
||||
* @param {Next_PageInputs} inputs
|
||||
* @param {{ locale?: "en" | "zh" }} options
|
||||
* @returns {LocalizedString}
|
||||
*/
|
||||
export const next_page: ((inputs?: Next_PageInputs, options?: {
|
||||
locale?: "en" | "zh";
|
||||
}) => LocalizedString) & import("../runtime.js").MessageMetadata<Next_PageInputs, {
|
||||
locale?: "en" | "zh";
|
||||
}, {}>;
|
||||
export type LocalizedString = import("../runtime.js").LocalizedString;
|
||||
export type Next_PageInputs = {};
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
/* eslint-disable */
|
||||
import { getLocale, experimentalStaticLocale } from '../runtime.js';
|
||||
|
||||
/** @typedef {import('../runtime.js').LocalizedString} LocalizedString */
|
||||
|
||||
/** @typedef {{}} Next_PageInputs */
|
||||
|
||||
const en_next_page = /** @type {(inputs: Next_PageInputs) => LocalizedString} */ () => {
|
||||
return /** @type {LocalizedString} */ (`Next Page`)
|
||||
};
|
||||
|
||||
const zh_next_page = /** @type {(inputs: Next_PageInputs) => LocalizedString} */ () => {
|
||||
return /** @type {LocalizedString} */ (`下一页`)
|
||||
};
|
||||
|
||||
/**
|
||||
* | output |
|
||||
* | --- |
|
||||
* | "Next Page" |
|
||||
*
|
||||
* @param {Next_PageInputs} inputs
|
||||
* @param {{ locale?: "en" | "zh" }} options
|
||||
* @returns {LocalizedString}
|
||||
*/
|
||||
export const next_page = /** @type {((inputs?: Next_PageInputs, options?: { locale?: "en" | "zh" }) => LocalizedString) & import('../runtime.js').MessageMetadata<Next_PageInputs, { locale?: "en" | "zh" }, {}>} */ ((inputs = {}, options = {}) => {
|
||||
const locale = experimentalStaticLocale ?? options.locale ?? getLocale()
|
||||
if (locale === "en") return en_next_page(inputs)
|
||||
return zh_next_page(inputs)
|
||||
});
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
/**
|
||||
* | output |
|
||||
* | --- |
|
||||
* | "Page {current} of {total}" |
|
||||
*
|
||||
* @param {Page_Of_PagesInputs} inputs
|
||||
* @param {{ locale?: "en" | "zh" }} options
|
||||
* @returns {LocalizedString}
|
||||
*/
|
||||
export const page_of_pages: ((inputs: Page_Of_PagesInputs, options?: {
|
||||
locale?: "en" | "zh";
|
||||
}) => LocalizedString) & import("../runtime.js").MessageMetadata<Page_Of_PagesInputs, {
|
||||
locale?: "en" | "zh";
|
||||
}, {}>;
|
||||
export type LocalizedString = import("../runtime.js").LocalizedString;
|
||||
export type Page_Of_PagesInputs = {
|
||||
current: NonNullable<unknown>;
|
||||
total: NonNullable<unknown>;
|
||||
};
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
/* eslint-disable */
|
||||
import { getLocale, experimentalStaticLocale } from '../runtime.js';
|
||||
|
||||
/** @typedef {import('../runtime.js').LocalizedString} LocalizedString */
|
||||
|
||||
/** @typedef {{ current: NonNullable<unknown>, total: NonNullable<unknown> }} Page_Of_PagesInputs */
|
||||
|
||||
const en_page_of_pages = /** @type {(inputs: Page_Of_PagesInputs) => LocalizedString} */ (i) => {
|
||||
return /** @type {LocalizedString} */ (`Page ${i?.current} of ${i?.total}`)
|
||||
};
|
||||
|
||||
const zh_page_of_pages = /** @type {(inputs: Page_Of_PagesInputs) => LocalizedString} */ (i) => {
|
||||
return /** @type {LocalizedString} */ (`第 ${i?.current} / ${i?.total} 页`)
|
||||
};
|
||||
|
||||
/**
|
||||
* | output |
|
||||
* | --- |
|
||||
* | "Page {current} of {total}" |
|
||||
*
|
||||
* @param {Page_Of_PagesInputs} inputs
|
||||
* @param {{ locale?: "en" | "zh" }} options
|
||||
* @returns {LocalizedString}
|
||||
*/
|
||||
export const page_of_pages = /** @type {((inputs: Page_Of_PagesInputs, options?: { locale?: "en" | "zh" }) => LocalizedString) & import('../runtime.js').MessageMetadata<Page_Of_PagesInputs, { locale?: "en" | "zh" }, {}>} */ ((inputs, options = {}) => {
|
||||
const locale = experimentalStaticLocale ?? options.locale ?? getLocale()
|
||||
if (locale === "en") return en_page_of_pages(inputs)
|
||||
return zh_page_of_pages(inputs)
|
||||
});
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
/**
|
||||
* | output |
|
||||
* | --- |
|
||||
* | "Previous Page" |
|
||||
*
|
||||
* @param {Previous_PageInputs} inputs
|
||||
* @param {{ locale?: "en" | "zh" }} options
|
||||
* @returns {LocalizedString}
|
||||
*/
|
||||
export const previous_page: ((inputs?: Previous_PageInputs, options?: {
|
||||
locale?: "en" | "zh";
|
||||
}) => LocalizedString) & import("../runtime.js").MessageMetadata<Previous_PageInputs, {
|
||||
locale?: "en" | "zh";
|
||||
}, {}>;
|
||||
export type LocalizedString = import("../runtime.js").LocalizedString;
|
||||
export type Previous_PageInputs = {};
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
/* eslint-disable */
|
||||
import { getLocale, experimentalStaticLocale } from '../runtime.js';
|
||||
|
||||
/** @typedef {import('../runtime.js').LocalizedString} LocalizedString */
|
||||
|
||||
/** @typedef {{}} Previous_PageInputs */
|
||||
|
||||
const en_previous_page = /** @type {(inputs: Previous_PageInputs) => LocalizedString} */ () => {
|
||||
return /** @type {LocalizedString} */ (`Previous Page`)
|
||||
};
|
||||
|
||||
const zh_previous_page = /** @type {(inputs: Previous_PageInputs) => LocalizedString} */ () => {
|
||||
return /** @type {LocalizedString} */ (`上一页`)
|
||||
};
|
||||
|
||||
/**
|
||||
* | output |
|
||||
* | --- |
|
||||
* | "Previous Page" |
|
||||
*
|
||||
* @param {Previous_PageInputs} inputs
|
||||
* @param {{ locale?: "en" | "zh" }} options
|
||||
* @returns {LocalizedString}
|
||||
*/
|
||||
export const previous_page = /** @type {((inputs?: Previous_PageInputs, options?: { locale?: "en" | "zh" }) => LocalizedString) & import('../runtime.js').MessageMetadata<Previous_PageInputs, { locale?: "en" | "zh" }, {}>} */ ((inputs = {}, options = {}) => {
|
||||
const locale = experimentalStaticLocale ?? options.locale ?? getLocale()
|
||||
if (locale === "en") return en_previous_page(inputs)
|
||||
return zh_previous_page(inputs)
|
||||
});
|
||||
Reference in New Issue
Block a user