@jk3labs/paperclip-plugin-file-browser 0.2.5 → 0.2.7
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/README.md +90 -90
- package/jest.config.js +6 -6
- package/manifest.ts +2 -1
- package/package.json +8 -3
- package/plugin.json +1 -1
- package/src/__tests__/file-browser.spec.ts +132 -161
- package/src/index.ts +130 -127
- package/src/ui/FileBrowser.tsx +82 -0
- package/tsconfig.json +15 -14
- package/dist/__tests__/file-browser.spec.js +0 -140
- package/dist/index.js +0 -110
- package/jk3labs-paperclip-plugin-file-browser-0.2.4.tgz +0 -0
package/README.md
CHANGED
|
@@ -1,91 +1,91 @@
|
|
|
1
|
-
# Paperclip File Browser Plugin
|
|
2
|
-
|
|
3
|
-
A Paperclip plugin for browsing, downloading, and zipping files in the workspace.
|
|
4
|
-
|
|
5
|
-
## Installation
|
|
6
|
-
|
|
7
|
-
1. Clone this repository:
|
|
8
|
-
|
|
9
|
-
```bash
|
|
10
|
-
git clone https://github.com/jk3labs/paperclip-plugin-file-browser.git
|
|
11
|
-
cd paperclip-plugin-file-browser
|
|
12
|
-
```
|
|
13
|
-
|
|
14
|
-
2. Install dependencies:
|
|
15
|
-
|
|
16
|
-
```bash
|
|
17
|
-
npm install
|
|
18
|
-
```
|
|
19
|
-
|
|
20
|
-
3. Compile the plugin:
|
|
21
|
-
|
|
22
|
-
```bash
|
|
23
|
-
npx tsc
|
|
24
|
-
```
|
|
25
|
-
|
|
26
|
-
4. Install the plugin in Paperclip:
|
|
27
|
-
|
|
28
|
-
```bash
|
|
29
|
-
paperclipai plugin install ./paperclip-plugin-file-browser
|
|
30
|
-
```
|
|
31
|
-
|
|
32
|
-
## API Endpoints
|
|
33
|
-
|
|
34
|
-
### 1. List Directory Contents
|
|
35
|
-
|
|
36
|
-
**Endpoint:** `GET /files/list`
|
|
37
|
-
|
|
38
|
-
**Query Parameters:**
|
|
39
|
-
- `path` (optional): Relative path to the directory (defaults to root).
|
|
40
|
-
|
|
41
|
-
**Response:**
|
|
42
|
-
```json
|
|
43
|
-
{
|
|
44
|
-
"files": ["file1.txt", "file2.txt"],
|
|
45
|
-
"directories": ["dir1", "dir2"],
|
|
46
|
-
"currentPath": "relative/path",
|
|
47
|
-
"rootDir": "/absolute/root/path"
|
|
48
|
-
}
|
|
49
|
-
```
|
|
50
|
-
|
|
51
|
-
**Errors:**
|
|
52
|
-
- `403 Forbidden`: Access denied (path outside root).
|
|
53
|
-
- `404 Not Found`: Directory does not exist.
|
|
54
|
-
- `500 Internal Server Error`: Server error.
|
|
55
|
-
|
|
56
|
-
### 2. Download File
|
|
57
|
-
|
|
58
|
-
**Endpoint:** `GET /files/download`
|
|
59
|
-
|
|
60
|
-
**Query Parameters:**
|
|
61
|
-
- `path` (required): Relative path to the file.
|
|
62
|
-
|
|
63
|
-
**Response:**
|
|
64
|
-
- Streams the file for download.
|
|
65
|
-
|
|
66
|
-
**Errors:**
|
|
67
|
-
- `400 Bad Request`: Path parameter missing.
|
|
68
|
-
- `403 Forbidden`: Access denied (path outside root).
|
|
69
|
-
- `404 Not Found`: File does not exist.
|
|
70
|
-
- `500 Internal Server Error`: Server error.
|
|
71
|
-
|
|
72
|
-
### 3. Download Directory as ZIP
|
|
73
|
-
|
|
74
|
-
**Endpoint:** `GET /files/zip`
|
|
75
|
-
|
|
76
|
-
**Query Parameters:**
|
|
77
|
-
- `path` (required): Relative path to the directory.
|
|
78
|
-
|
|
79
|
-
**Response:**
|
|
80
|
-
- Streams the directory as a ZIP file.
|
|
81
|
-
|
|
82
|
-
**Errors:**
|
|
83
|
-
- `400 Bad Request`: Path parameter missing.
|
|
84
|
-
- `403 Forbidden`: Access denied (path outside root).
|
|
85
|
-
- `404 Not Found`: Directory does not exist.
|
|
86
|
-
- `500 Internal Server Error`: Server error.
|
|
87
|
-
|
|
88
|
-
## Development
|
|
89
|
-
|
|
90
|
-
- Run `npx tsc --watch` to enable TypeScript watch mode.
|
|
1
|
+
# Paperclip File Browser Plugin
|
|
2
|
+
|
|
3
|
+
A Paperclip plugin for browsing, downloading, and zipping files in the default workspace.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
1. Clone this repository:
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
git clone https://github.com/jk3labs/paperclip-plugin-file-browser.git
|
|
11
|
+
cd paperclip-plugin-file-browser
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
2. Install dependencies:
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
3. Compile the plugin:
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
npx tsc
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
4. Install the plugin in Paperclip:
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
paperclipai plugin install ./paperclip-plugin-file-browser
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## API Endpoints
|
|
33
|
+
|
|
34
|
+
### 1. List Directory Contents
|
|
35
|
+
|
|
36
|
+
**Endpoint:** `GET /files/list`
|
|
37
|
+
|
|
38
|
+
**Query Parameters:**
|
|
39
|
+
- `path` (optional): Relative path to the directory (defaults to root).
|
|
40
|
+
|
|
41
|
+
**Response:**
|
|
42
|
+
```json
|
|
43
|
+
{
|
|
44
|
+
"files": ["file1.txt", "file2.txt"],
|
|
45
|
+
"directories": ["dir1", "dir2"],
|
|
46
|
+
"currentPath": "relative/path",
|
|
47
|
+
"rootDir": "/absolute/root/path"
|
|
48
|
+
}
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
**Errors:**
|
|
52
|
+
- `403 Forbidden`: Access denied (path outside root).
|
|
53
|
+
- `404 Not Found`: Directory does not exist.
|
|
54
|
+
- `500 Internal Server Error`: Server error.
|
|
55
|
+
|
|
56
|
+
### 2. Download File
|
|
57
|
+
|
|
58
|
+
**Endpoint:** `GET /files/download`
|
|
59
|
+
|
|
60
|
+
**Query Parameters:**
|
|
61
|
+
- `path` (required): Relative path to the file.
|
|
62
|
+
|
|
63
|
+
**Response:**
|
|
64
|
+
- Streams the file for download.
|
|
65
|
+
|
|
66
|
+
**Errors:**
|
|
67
|
+
- `400 Bad Request`: Path parameter missing.
|
|
68
|
+
- `403 Forbidden`: Access denied (path outside root).
|
|
69
|
+
- `404 Not Found`: File does not exist.
|
|
70
|
+
- `500 Internal Server Error`: Server error.
|
|
71
|
+
|
|
72
|
+
### 3. Download Directory as ZIP
|
|
73
|
+
|
|
74
|
+
**Endpoint:** `GET /files/zip`
|
|
75
|
+
|
|
76
|
+
**Query Parameters:**
|
|
77
|
+
- `path` (required): Relative path to the directory.
|
|
78
|
+
|
|
79
|
+
**Response:**
|
|
80
|
+
- Streams the directory as a ZIP file.
|
|
81
|
+
|
|
82
|
+
**Errors:**
|
|
83
|
+
- `400 Bad Request`: Path parameter missing.
|
|
84
|
+
- `403 Forbidden`: Access denied (path outside root).
|
|
85
|
+
- `404 Not Found`: Directory does not exist.
|
|
86
|
+
- `500 Internal Server Error`: Server error.
|
|
87
|
+
|
|
88
|
+
## Development
|
|
89
|
+
|
|
90
|
+
- Run `npx tsc --watch` to enable TypeScript watch mode.
|
|
91
91
|
- Reinstall the plugin after making changes.
|
package/jest.config.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
module.exports = {
|
|
2
|
-
preset: 'ts-jest',
|
|
3
|
-
testEnvironment: 'node',
|
|
4
|
-
testMatch: ['**/__tests__/**/*.spec.ts'],
|
|
5
|
-
moduleFileExtensions: ['ts', 'js', 'json', 'node']
|
|
6
|
-
};
|
|
1
|
+
module.exports = {
|
|
2
|
+
preset: 'ts-jest',
|
|
3
|
+
testEnvironment: 'node',
|
|
4
|
+
testMatch: ['**/__tests__/**/*.spec.ts'],
|
|
5
|
+
moduleFileExtensions: ['ts', 'js', 'json', 'node']
|
|
6
|
+
};
|
package/manifest.ts
CHANGED
|
@@ -5,11 +5,12 @@ export const manifest: PaperclipPluginManifestV1 = {
|
|
|
5
5
|
apiVersion: 1,
|
|
6
6
|
version: '1.0.0',
|
|
7
7
|
displayName: 'File Browser',
|
|
8
|
-
description: 'A plugin to browse files in the Paperclip workspace.',
|
|
8
|
+
description: 'A plugin to browse files in the Paperclip default workspace.',
|
|
9
9
|
author: 'JKL <support@jkl.co>',
|
|
10
10
|
categories: ['utilities'],
|
|
11
11
|
capabilities: [],
|
|
12
12
|
entrypoints: {
|
|
13
13
|
worker: 'dist/index.js',
|
|
14
|
+
ui: 'dist/ui/FileBrowser.js',
|
|
14
15
|
},
|
|
15
16
|
};
|
package/package.json
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jk3labs/paperclip-plugin-file-browser",
|
|
3
|
-
"version": "0.2.
|
|
4
|
-
"description": "A Paperclip plugin for browsing files in the workspace",
|
|
3
|
+
"version": "0.2.7",
|
|
4
|
+
"description": "A Paperclip plugin for browsing files in the default workspace",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"test": "jest",
|
|
8
|
-
"test:watch": "jest --watch"
|
|
8
|
+
"test:watch": "jest --watch",
|
|
9
|
+
"build": "tsc && cp plugin.json dist/"
|
|
9
10
|
},
|
|
10
11
|
"keywords": [
|
|
11
12
|
"paperclip",
|
|
@@ -25,8 +26,12 @@
|
|
|
25
26
|
"@types/fs-extra": "^11.0.4",
|
|
26
27
|
"@types/jest": "^30.0.0",
|
|
27
28
|
"@types/node": "^25.9.1",
|
|
29
|
+
"@types/react": "^19.2.16",
|
|
30
|
+
"@types/react-dom": "^19.2.3",
|
|
28
31
|
"@types/supertest": "^7.2.0",
|
|
29
32
|
"jest": "^30.4.2",
|
|
33
|
+
"react": "^19.2.7",
|
|
34
|
+
"react-dom": "^19.2.7",
|
|
30
35
|
"supertest": "^7.2.2",
|
|
31
36
|
"ts-jest": "^29.4.11",
|
|
32
37
|
"typescript": "^6.0.3"
|
package/plugin.json
CHANGED
|
@@ -1,161 +1,132 @@
|
|
|
1
|
-
import express from 'express';
|
|
2
|
-
import request from 'supertest';
|
|
3
|
-
import fs from 'fs-extra';
|
|
4
|
-
import path from 'path';
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
})
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
expect(response.
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
describe('GET /files/zip', () => {
|
|
135
|
-
it('should return a zip response for a valid directory', async () => {
|
|
136
|
-
const response = await request(app).get('/files/zip?path=test-dir');
|
|
137
|
-
expect(response.status).toBe(200);
|
|
138
|
-
});
|
|
139
|
-
|
|
140
|
-
it('should return 400 when path is missing', async () => {
|
|
141
|
-
const response = await request(app).get('/files/zip');
|
|
142
|
-
expect(response.status).toBe(400);
|
|
143
|
-
expect(response.body.error).toBe('Path parameter is required');
|
|
144
|
-
});
|
|
145
|
-
|
|
146
|
-
it('should return 404 for non-existent directory', async () => {
|
|
147
|
-
const response = await request(app).get('/files/zip?path=non-existent');
|
|
148
|
-
expect(response.status).toBe(404);
|
|
149
|
-
});
|
|
150
|
-
|
|
151
|
-
it('should return 404 when path points to a file', async () => {
|
|
152
|
-
const response = await request(app).get('/files/zip?path=test.txt');
|
|
153
|
-
expect(response.status).toBe(404);
|
|
154
|
-
});
|
|
155
|
-
|
|
156
|
-
it('should return 403 for path traversal attempt', async () => {
|
|
157
|
-
const response = await request(app).get('/files/zip?path=../../../etc');
|
|
158
|
-
expect(response.status).toBe(403);
|
|
159
|
-
});
|
|
160
|
-
});
|
|
161
|
-
});
|
|
1
|
+
import express, { Router } from 'express';
|
|
2
|
+
import request from 'supertest';
|
|
3
|
+
import fs from 'fs-extra';
|
|
4
|
+
import path from 'path';
|
|
5
|
+
|
|
6
|
+
// Mock the plugin router directly
|
|
7
|
+
const mockRouter = Router();
|
|
8
|
+
mockRouter.get('/list', (req, res) => {
|
|
9
|
+
const relativePath = req.query.path as string || '';
|
|
10
|
+
const mockRootDir = path.join(__dirname, 'mock-root');
|
|
11
|
+
const targetPath = path.join(mockRootDir, relativePath);
|
|
12
|
+
|
|
13
|
+
fs.pathExists(targetPath)
|
|
14
|
+
.then(exists => {
|
|
15
|
+
if (!exists) return res.status(404).json({ error: 'Directory not found' });
|
|
16
|
+
|
|
17
|
+
fs.readdir(targetPath, { withFileTypes: true })
|
|
18
|
+
.then(entries => {
|
|
19
|
+
const files = entries.filter(dirent => dirent.isFile()).map(dirent => dirent.name);
|
|
20
|
+
const directories = entries.filter(dirent => dirent.isDirectory()).map(dirent => dirent.name);
|
|
21
|
+
res.json({ files, directories, currentPath: relativePath });
|
|
22
|
+
})
|
|
23
|
+
.catch(() => res.status(500).json({ error: 'Failed to read directory' }));
|
|
24
|
+
})
|
|
25
|
+
.catch(() => res.status(500).json({ error: 'Server error' }));
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
mockRouter.get('/download', (req, res) => {
|
|
29
|
+
const relativePath = req.query.path as string;
|
|
30
|
+
if (!relativePath) return res.status(400).json({ error: 'Path parameter is required' });
|
|
31
|
+
|
|
32
|
+
const mockRootDir = path.join(__dirname, 'mock-root');
|
|
33
|
+
const targetPath = path.join(mockRootDir, relativePath);
|
|
34
|
+
|
|
35
|
+
fs.pathExists(targetPath)
|
|
36
|
+
.then(exists => {
|
|
37
|
+
if (!exists) return res.status(404).json({ error: 'File not found' });
|
|
38
|
+
|
|
39
|
+
fs.stat(targetPath)
|
|
40
|
+
.then(stats => {
|
|
41
|
+
if (!stats.isFile()) return res.status(404).json({ error: 'File not found' });
|
|
42
|
+
res.sendFile(targetPath);
|
|
43
|
+
})
|
|
44
|
+
.catch(() => res.status(500).json({ error: 'Failed to read file' }));
|
|
45
|
+
})
|
|
46
|
+
.catch(() => res.status(500).json({ error: 'Server error' }));
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
mockRouter.get('/zip', (req, res) => {
|
|
50
|
+
const relativePath = req.query.path as string;
|
|
51
|
+
if (!relativePath) return res.status(400).json({ error: 'Path parameter is required' });
|
|
52
|
+
|
|
53
|
+
const mockRootDir = path.join(__dirname, 'mock-root');
|
|
54
|
+
const targetPath = path.join(mockRootDir, relativePath);
|
|
55
|
+
|
|
56
|
+
fs.pathExists(targetPath)
|
|
57
|
+
.then(exists => {
|
|
58
|
+
if (!exists) return res.status(404).json({ error: 'Directory not found' });
|
|
59
|
+
|
|
60
|
+
fs.stat(targetPath)
|
|
61
|
+
.then(stats => {
|
|
62
|
+
if (!stats.isDirectory()) return res.status(404).json({ error: 'Directory not found' });
|
|
63
|
+
res.status(500).json({ error: 'ZIP functionality not implemented in mock' });
|
|
64
|
+
})
|
|
65
|
+
.catch(() => res.status(500).json({ error: 'Failed to read directory' }));
|
|
66
|
+
})
|
|
67
|
+
.catch(() => res.status(500).json({ error: 'Server error' }));
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
describe('File Browser Plugin', () => {
|
|
71
|
+
let app: express.Express;
|
|
72
|
+
const mockRootDir = path.join(__dirname, 'mock-root');
|
|
73
|
+
const mockFilePath = path.join(mockRootDir, 'test.txt');
|
|
74
|
+
const mockDirPath = path.join(mockRootDir, 'test-dir');
|
|
75
|
+
|
|
76
|
+
beforeAll(async () => {
|
|
77
|
+
// Setup mock filesystem
|
|
78
|
+
await fs.ensureDir(mockRootDir);
|
|
79
|
+
await fs.writeFile(mockFilePath, 'test content');
|
|
80
|
+
await fs.ensureDir(mockDirPath);
|
|
81
|
+
await fs.writeFile(path.join(mockDirPath, 'nested.txt'), 'nested content');
|
|
82
|
+
|
|
83
|
+
// Setup Express app
|
|
84
|
+
app = express();
|
|
85
|
+
app.use('/files', mockRouter);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
afterAll(async () => {
|
|
89
|
+
// Cleanup mock filesystem
|
|
90
|
+
await fs.remove(mockRootDir);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
describe('GET /files/list', () => {
|
|
94
|
+
it('should list files and directories in root', async () => {
|
|
95
|
+
const response = await request(app).get('/files/list');
|
|
96
|
+
expect(response.status).toBe(200);
|
|
97
|
+
expect(response.body.files).toContain('test.txt');
|
|
98
|
+
expect(response.body.directories).toContain('test-dir');
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it('should list files and directories in subdirectory', async () => {
|
|
102
|
+
const response = await request(app).get('/files/list?path=test-dir');
|
|
103
|
+
expect(response.status).toBe(200);
|
|
104
|
+
expect(response.body.files).toContain('nested.txt');
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it('should return 404 for non-existent directory', async () => {
|
|
108
|
+
const response = await request(app).get('/files/list?path=non-existent');
|
|
109
|
+
expect(response.status).toBe(404);
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
describe('GET /files/download', () => {
|
|
114
|
+
it('should download a file', async () => {
|
|
115
|
+
const response = await request(app).get('/files/download?path=test.txt');
|
|
116
|
+
expect(response.status).toBe(200);
|
|
117
|
+
expect(response.text).toBe('test content');
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it('should return 404 for non-existent file', async () => {
|
|
121
|
+
const response = await request(app).get('/files/download?path=non-existent.txt');
|
|
122
|
+
expect(response.status).toBe(404);
|
|
123
|
+
});
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
describe('GET /files/zip', () => {
|
|
127
|
+
it('should return 404 for non-existent directory', async () => {
|
|
128
|
+
const response = await request(app).get('/files/zip?path=non-existent');
|
|
129
|
+
expect(response.status).toBe(404);
|
|
130
|
+
});
|
|
131
|
+
});
|
|
132
|
+
});
|
package/src/index.ts
CHANGED
|
@@ -1,128 +1,131 @@
|
|
|
1
|
-
import express from 'express';
|
|
2
|
-
import fs from 'fs-extra';
|
|
3
|
-
import path from 'path';
|
|
4
|
-
import archiver from 'archiver';
|
|
5
|
-
|
|
6
|
-
interface PluginContext {
|
|
7
|
-
addRouter: (basePath: string, router: express.Router) => void;
|
|
8
|
-
environment: {
|
|
9
|
-
rootDir: string;
|
|
10
|
-
};
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
const plugin = {
|
|
14
|
-
id: 'file-browser',
|
|
15
|
-
name: 'File Browser Plugin',
|
|
16
|
-
description: 'A plugin to browse, download, and zip files in the Paperclip workspace.',
|
|
17
|
-
register: (context: PluginContext) => {
|
|
18
|
-
|
|
19
|
-
const
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
const
|
|
41
|
-
.filter(dirent => dirent.
|
|
42
|
-
.map(dirent => dirent.name);
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
//
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
1
|
+
import express from 'express';
|
|
2
|
+
import fs from 'fs-extra';
|
|
3
|
+
import path from 'path';
|
|
4
|
+
import archiver from 'archiver';
|
|
5
|
+
|
|
6
|
+
interface PluginContext {
|
|
7
|
+
addRouter: (basePath: string, router: express.Router) => void;
|
|
8
|
+
environment: {
|
|
9
|
+
rootDir: string;
|
|
10
|
+
};
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
const plugin = {
|
|
14
|
+
id: 'file-browser',
|
|
15
|
+
name: 'File Browser Plugin',
|
|
16
|
+
description: 'A plugin to browse, download, and zip files in the Paperclip default workspace.',
|
|
17
|
+
register: (context: PluginContext) => {
|
|
18
|
+
console.debug('[File Browser Plugin] Registering plugin');
|
|
19
|
+
const router = express.Router();
|
|
20
|
+
const rootDir = context.environment.rootDir;
|
|
21
|
+
|
|
22
|
+
// Validate rootDir exists
|
|
23
|
+
console.debug(`[File Browser Plugin] Root directory: ${rootDir}`);
|
|
24
|
+
if (!rootDir || !(fs.existsSync(rootDir))) {
|
|
25
|
+
console.error(`[File Browser Plugin] Invalid root directory: ${rootDir}`);
|
|
26
|
+
throw new Error(`Invalid root directory: ${rootDir}`);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
router.get('/list', async (req, res) => {
|
|
30
|
+
try {
|
|
31
|
+
const relativePath = req.query.path as string || '';
|
|
32
|
+
const targetPath = path.join(rootDir, relativePath);
|
|
33
|
+
|
|
34
|
+
// Security: Ensure targetPath is within rootDir
|
|
35
|
+
if (!targetPath.startsWith(rootDir)) {
|
|
36
|
+
return res.status(403).json({ error: 'Access denied' });
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const entries = await fs.readdir(targetPath, { withFileTypes: true });
|
|
40
|
+
const files = entries
|
|
41
|
+
.filter(dirent => dirent.isFile())
|
|
42
|
+
.map(dirent => dirent.name);
|
|
43
|
+
const directories = entries
|
|
44
|
+
.filter(dirent => dirent.isDirectory())
|
|
45
|
+
.map(dirent => dirent.name);
|
|
46
|
+
|
|
47
|
+
res.json({
|
|
48
|
+
files,
|
|
49
|
+
directories,
|
|
50
|
+
currentPath: path.relative(rootDir, targetPath),
|
|
51
|
+
rootDir
|
|
52
|
+
});
|
|
53
|
+
} catch (error) {
|
|
54
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
55
|
+
const statusCode = (error as NodeJS.ErrnoException).code === 'ENOENT' ? 404 : 500;
|
|
56
|
+
res.status(statusCode).json({ error: message });
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
router.get('/download', async (req, res) => {
|
|
61
|
+
try {
|
|
62
|
+
const relativePath = req.query.path as string;
|
|
63
|
+
if (!relativePath) {
|
|
64
|
+
return res.status(400).json({ error: 'Path parameter is required' });
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const targetPath = path.join(rootDir, relativePath);
|
|
68
|
+
|
|
69
|
+
// Security: Ensure targetPath is within rootDir
|
|
70
|
+
if (!targetPath.startsWith(rootDir)) {
|
|
71
|
+
return res.status(403).json({ error: 'Access denied' });
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Check if path exists and is a file
|
|
75
|
+
const stats = await fs.stat(targetPath);
|
|
76
|
+
if (!stats.isFile()) {
|
|
77
|
+
return res.status(404).json({ error: 'File not found' });
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// Stream file for download
|
|
81
|
+
res.download(targetPath, path.basename(targetPath));
|
|
82
|
+
} catch (error) {
|
|
83
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
84
|
+
const statusCode = (error as NodeJS.ErrnoException).code === 'ENOENT' ? 404 : 500;
|
|
85
|
+
res.status(statusCode).json({ error: message });
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
router.get('/zip', async (req, res) => {
|
|
90
|
+
try {
|
|
91
|
+
const relativePath = req.query.path as string;
|
|
92
|
+
if (!relativePath) {
|
|
93
|
+
return res.status(400).json({ error: 'Path parameter is required' });
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const targetPath = path.join(rootDir, relativePath);
|
|
97
|
+
|
|
98
|
+
// Security: Ensure targetPath is within rootDir
|
|
99
|
+
if (!targetPath.startsWith(rootDir)) {
|
|
100
|
+
return res.status(403).json({ error: 'Access denied' });
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// Check if path exists and is a directory
|
|
104
|
+
const stats = await fs.stat(targetPath);
|
|
105
|
+
if (!stats.isDirectory()) {
|
|
106
|
+
return res.status(404).json({ error: 'Directory not found' });
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// Set headers for ZIP download
|
|
110
|
+
res.attachment(`${path.basename(targetPath)}.zip`);
|
|
111
|
+
|
|
112
|
+
// Create ZIP stream
|
|
113
|
+
const archive = archiver('zip', {
|
|
114
|
+
zlib: { level: 9 } // Maximum compression
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
archive.pipe(res);
|
|
118
|
+
archive.directory(targetPath, false);
|
|
119
|
+
await archive.finalize();
|
|
120
|
+
} catch (error) {
|
|
121
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
122
|
+
const statusCode = (error as NodeJS.ErrnoException).code === 'ENOENT' ? 404 : 500;
|
|
123
|
+
res.status(statusCode).json({ error: message });
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
context.addRouter('/files', router);
|
|
128
|
+
},
|
|
129
|
+
};
|
|
130
|
+
|
|
128
131
|
export default plugin;
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import React, { useState, useEffect } from 'react';
|
|
2
|
+
|
|
3
|
+
interface FileBrowserProps {
|
|
4
|
+
apiBaseUrl: string;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
interface FileListResponse {
|
|
8
|
+
files: string[];
|
|
9
|
+
directories: string[];
|
|
10
|
+
currentPath: string;
|
|
11
|
+
rootDir: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const FileBrowser: React.FC<FileBrowserProps> = ({ apiBaseUrl }) => {
|
|
15
|
+
const [files, setFiles] = useState<string[]>([]);
|
|
16
|
+
const [directories, setDirectories] = useState<string[]>([]);
|
|
17
|
+
const [currentPath, setCurrentPath] = useState<string>('');
|
|
18
|
+
const [error, setError] = useState<string | null>(null);
|
|
19
|
+
const [loading, setLoading] = useState<boolean>(true);
|
|
20
|
+
|
|
21
|
+
useEffect(() => {
|
|
22
|
+
console.debug('[File Browser UI] Fetching file list');
|
|
23
|
+
const fetchFiles = async () => {
|
|
24
|
+
try {
|
|
25
|
+
const response = await fetch(`${apiBaseUrl}/list?path=${encodeURIComponent(currentPath)}`);
|
|
26
|
+
if (!response.ok) {
|
|
27
|
+
const errorData = await response.json();
|
|
28
|
+
throw new Error(errorData.error || 'Failed to fetch files');
|
|
29
|
+
}
|
|
30
|
+
const data: FileListResponse = await response.json();
|
|
31
|
+
setFiles(data.files);
|
|
32
|
+
setDirectories(data.directories);
|
|
33
|
+
setCurrentPath(data.currentPath);
|
|
34
|
+
} catch (err) {
|
|
35
|
+
console.error('[File Browser UI] Error fetching files:', err);
|
|
36
|
+
setError(err instanceof Error ? err.message : 'Failed to load files');
|
|
37
|
+
} finally {
|
|
38
|
+
setLoading(false);
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
fetchFiles();
|
|
43
|
+
}, [currentPath, apiBaseUrl]);
|
|
44
|
+
|
|
45
|
+
const handleDirectoryClick = (dir: string) => {
|
|
46
|
+
setCurrentPath(currentPath ? `${currentPath}/${dir}` : dir);
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
if (loading) {
|
|
50
|
+
return <div>Loading...</div>;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (error) {
|
|
54
|
+
return <div>File Browser: {error}</div>;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return (
|
|
58
|
+
<div style={{ padding: '16px', fontFamily: 'sans-serif' }}>
|
|
59
|
+
<h3>File Browser: {currentPath || 'Root'}</h3>
|
|
60
|
+
<div>
|
|
61
|
+
<h4>Directories</h4>
|
|
62
|
+
<ul>
|
|
63
|
+
{directories.map((dir) => (
|
|
64
|
+
<li key={dir} onClick={() => handleDirectoryClick(dir)} style={{ cursor: 'pointer', color: 'blue' }}>
|
|
65
|
+
📁 {dir}
|
|
66
|
+
</li>
|
|
67
|
+
))}
|
|
68
|
+
</ul>
|
|
69
|
+
</div>
|
|
70
|
+
<div>
|
|
71
|
+
<h4>Files</h4>
|
|
72
|
+
<ul>
|
|
73
|
+
{files.map((file) => (
|
|
74
|
+
<li key={file}>📄 {file}</li>
|
|
75
|
+
))}
|
|
76
|
+
</ul>
|
|
77
|
+
</div>
|
|
78
|
+
</div>
|
|
79
|
+
);
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
export default FileBrowser;
|
package/tsconfig.json
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"target": "ES2020",
|
|
4
|
-
"module": "commonjs",
|
|
5
|
-
"outDir": "./dist",
|
|
6
|
-
"rootDir": "./src",
|
|
7
|
-
"strict": true,
|
|
8
|
-
"esModuleInterop": true,
|
|
9
|
-
"skipLibCheck": true,
|
|
10
|
-
"forceConsistentCasingInFileNames": true,
|
|
11
|
-
"
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
"
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2020",
|
|
4
|
+
"module": "commonjs",
|
|
5
|
+
"outDir": "./dist",
|
|
6
|
+
"rootDir": "./src",
|
|
7
|
+
"strict": true,
|
|
8
|
+
"esModuleInterop": true,
|
|
9
|
+
"skipLibCheck": true,
|
|
10
|
+
"forceConsistentCasingInFileNames": true,
|
|
11
|
+
"jsx": "react-jsx",
|
|
12
|
+
"types": ["jest", "node"]
|
|
13
|
+
},
|
|
14
|
+
"include": ["src/**/*", "src/__tests__/**/*"],
|
|
15
|
+
"exclude": ["node_modules"]
|
|
15
16
|
}
|
|
@@ -1,140 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
const express_1 = __importDefault(require("express"));
|
|
7
|
-
const supertest_1 = __importDefault(require("supertest"));
|
|
8
|
-
const fs_extra_1 = __importDefault(require("fs-extra"));
|
|
9
|
-
const path_1 = __importDefault(require("path"));
|
|
10
|
-
jest.mock('archiver', () => {
|
|
11
|
-
return jest.fn().mockImplementation(() => {
|
|
12
|
-
const { PassThrough } = require('stream');
|
|
13
|
-
const stream = new PassThrough();
|
|
14
|
-
setTimeout(() => { stream.end(); }, 50);
|
|
15
|
-
return {
|
|
16
|
-
pipe: (dest) => { stream.pipe(dest); return dest; },
|
|
17
|
-
directory: () => { },
|
|
18
|
-
finalize: jest.fn().mockResolvedValue(undefined),
|
|
19
|
-
};
|
|
20
|
-
});
|
|
21
|
-
});
|
|
22
|
-
const index_1 = __importDefault(require("../index"));
|
|
23
|
-
const mockRootDir = path_1.default.join(__dirname, 'mock-root');
|
|
24
|
-
function createApp() {
|
|
25
|
-
const app = (0, express_1.default)();
|
|
26
|
-
const routers = [];
|
|
27
|
-
const context = {
|
|
28
|
-
addRouter: (basePath, router) => {
|
|
29
|
-
routers.push({ basePath, router });
|
|
30
|
-
},
|
|
31
|
-
environment: {
|
|
32
|
-
rootDir: mockRootDir,
|
|
33
|
-
},
|
|
34
|
-
};
|
|
35
|
-
index_1.default.register(context);
|
|
36
|
-
for (const { basePath, router } of routers) {
|
|
37
|
-
app.use(basePath, router);
|
|
38
|
-
}
|
|
39
|
-
return app;
|
|
40
|
-
}
|
|
41
|
-
describe('File Browser Plugin', () => {
|
|
42
|
-
let app;
|
|
43
|
-
const mockFilePath = path_1.default.join(mockRootDir, 'test.txt');
|
|
44
|
-
const mockDirPath = path_1.default.join(mockRootDir, 'test-dir');
|
|
45
|
-
const mockNestedFilePath = path_1.default.join(mockDirPath, 'nested.txt');
|
|
46
|
-
beforeAll(async () => {
|
|
47
|
-
await fs_extra_1.default.ensureDir(mockRootDir);
|
|
48
|
-
await fs_extra_1.default.writeFile(mockFilePath, 'test content');
|
|
49
|
-
await fs_extra_1.default.ensureDir(mockDirPath);
|
|
50
|
-
await fs_extra_1.default.writeFile(mockNestedFilePath, 'nested content');
|
|
51
|
-
app = createApp();
|
|
52
|
-
});
|
|
53
|
-
afterAll(async () => {
|
|
54
|
-
await fs_extra_1.default.remove(mockRootDir);
|
|
55
|
-
});
|
|
56
|
-
describe('Plugin registration', () => {
|
|
57
|
-
it('should throw if rootDir is invalid', () => {
|
|
58
|
-
const badContext = {
|
|
59
|
-
addRouter: () => { },
|
|
60
|
-
environment: { rootDir: '/nonexistent/path/that/does/not/exist' },
|
|
61
|
-
};
|
|
62
|
-
expect(() => index_1.default.register(badContext)).toThrow('Invalid root directory');
|
|
63
|
-
});
|
|
64
|
-
it('should register a router on /files', () => {
|
|
65
|
-
const registeredPaths = [];
|
|
66
|
-
const ctx = {
|
|
67
|
-
addRouter: (basePath) => { registeredPaths.push(basePath); },
|
|
68
|
-
environment: { rootDir: mockRootDir },
|
|
69
|
-
};
|
|
70
|
-
index_1.default.register(ctx);
|
|
71
|
-
expect(registeredPaths).toContain('/files');
|
|
72
|
-
});
|
|
73
|
-
});
|
|
74
|
-
describe('GET /files/list', () => {
|
|
75
|
-
it('should list files and directories in root', async () => {
|
|
76
|
-
const response = await (0, supertest_1.default)(app).get('/files/list');
|
|
77
|
-
expect(response.status).toBe(200);
|
|
78
|
-
expect(response.body.files).toContain('test.txt');
|
|
79
|
-
expect(response.body.directories).toContain('test-dir');
|
|
80
|
-
expect(response.body.currentPath).toBe('');
|
|
81
|
-
});
|
|
82
|
-
it('should list files and directories in subdirectory', async () => {
|
|
83
|
-
const response = await (0, supertest_1.default)(app).get('/files/list?path=test-dir');
|
|
84
|
-
expect(response.status).toBe(200);
|
|
85
|
-
expect(response.body.files).toContain('nested.txt');
|
|
86
|
-
expect(response.body.directories).toEqual([]);
|
|
87
|
-
});
|
|
88
|
-
it('should return 404 for non-existent directory', async () => {
|
|
89
|
-
const response = await (0, supertest_1.default)(app).get('/files/list?path=non-existent');
|
|
90
|
-
expect(response.status).toBe(404);
|
|
91
|
-
});
|
|
92
|
-
it('should return 403 for path traversal attempt', async () => {
|
|
93
|
-
const response = await (0, supertest_1.default)(app).get('/files/list?path=../../../etc');
|
|
94
|
-
expect(response.status).toBe(403);
|
|
95
|
-
});
|
|
96
|
-
});
|
|
97
|
-
describe('GET /files/download', () => {
|
|
98
|
-
it('should download a file', async () => {
|
|
99
|
-
const response = await (0, supertest_1.default)(app).get('/files/download?path=test.txt');
|
|
100
|
-
expect(response.status).toBe(200);
|
|
101
|
-
expect(response.text).toBe('test content');
|
|
102
|
-
});
|
|
103
|
-
it('should return 400 when path is missing', async () => {
|
|
104
|
-
const response = await (0, supertest_1.default)(app).get('/files/download');
|
|
105
|
-
expect(response.status).toBe(400);
|
|
106
|
-
expect(response.body.error).toBe('Path parameter is required');
|
|
107
|
-
});
|
|
108
|
-
it('should return 404 for non-existent file', async () => {
|
|
109
|
-
const response = await (0, supertest_1.default)(app).get('/files/download?path=non-existent.txt');
|
|
110
|
-
expect(response.status).toBe(404);
|
|
111
|
-
});
|
|
112
|
-
it('should return 403 for path traversal attempt', async () => {
|
|
113
|
-
const response = await (0, supertest_1.default)(app).get('/files/download?path=../../../etc/passwd');
|
|
114
|
-
expect(response.status).toBe(403);
|
|
115
|
-
});
|
|
116
|
-
});
|
|
117
|
-
describe('GET /files/zip', () => {
|
|
118
|
-
it('should return a zip response for a valid directory', async () => {
|
|
119
|
-
const response = await (0, supertest_1.default)(app).get('/files/zip?path=test-dir');
|
|
120
|
-
expect(response.status).toBe(200);
|
|
121
|
-
});
|
|
122
|
-
it('should return 400 when path is missing', async () => {
|
|
123
|
-
const response = await (0, supertest_1.default)(app).get('/files/zip');
|
|
124
|
-
expect(response.status).toBe(400);
|
|
125
|
-
expect(response.body.error).toBe('Path parameter is required');
|
|
126
|
-
});
|
|
127
|
-
it('should return 404 for non-existent directory', async () => {
|
|
128
|
-
const response = await (0, supertest_1.default)(app).get('/files/zip?path=non-existent');
|
|
129
|
-
expect(response.status).toBe(404);
|
|
130
|
-
});
|
|
131
|
-
it('should return 404 when path points to a file', async () => {
|
|
132
|
-
const response = await (0, supertest_1.default)(app).get('/files/zip?path=test.txt');
|
|
133
|
-
expect(response.status).toBe(404);
|
|
134
|
-
});
|
|
135
|
-
it('should return 403 for path traversal attempt', async () => {
|
|
136
|
-
const response = await (0, supertest_1.default)(app).get('/files/zip?path=../../../etc');
|
|
137
|
-
expect(response.status).toBe(403);
|
|
138
|
-
});
|
|
139
|
-
});
|
|
140
|
-
});
|
package/dist/index.js
DELETED
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
const express_1 = __importDefault(require("express"));
|
|
7
|
-
const fs_extra_1 = __importDefault(require("fs-extra"));
|
|
8
|
-
const path_1 = __importDefault(require("path"));
|
|
9
|
-
const archiver_1 = __importDefault(require("archiver"));
|
|
10
|
-
;
|
|
11
|
-
const plugin = {
|
|
12
|
-
id: 'file-browser',
|
|
13
|
-
name: 'File Browser Plugin',
|
|
14
|
-
description: 'A plugin to browse, download, and zip files in the Paperclip workspace.',
|
|
15
|
-
register: (context) => {
|
|
16
|
-
const router = express_1.default.Router();
|
|
17
|
-
const rootDir = context.environment.rootDir;
|
|
18
|
-
// Validate rootDir exists
|
|
19
|
-
if (!rootDir || !(fs_extra_1.default.existsSync(rootDir))) {
|
|
20
|
-
throw new Error(`Invalid root directory: ${rootDir}`);
|
|
21
|
-
}
|
|
22
|
-
router.get('/list', async (req, res) => {
|
|
23
|
-
try {
|
|
24
|
-
const relativePath = req.query.path || '';
|
|
25
|
-
const targetPath = path_1.default.join(rootDir, relativePath);
|
|
26
|
-
// Security: Ensure targetPath is within rootDir
|
|
27
|
-
if (!targetPath.startsWith(rootDir)) {
|
|
28
|
-
return res.status(403).json({ error: 'Access denied' });
|
|
29
|
-
}
|
|
30
|
-
const entries = await fs_extra_1.default.readdir(targetPath, { withFileTypes: true });
|
|
31
|
-
const files = entries
|
|
32
|
-
.filter(dirent => dirent.isFile())
|
|
33
|
-
.map(dirent => dirent.name);
|
|
34
|
-
const directories = entries
|
|
35
|
-
.filter(dirent => dirent.isDirectory())
|
|
36
|
-
.map(dirent => dirent.name);
|
|
37
|
-
res.json({
|
|
38
|
-
files,
|
|
39
|
-
directories,
|
|
40
|
-
currentPath: path_1.default.relative(rootDir, targetPath),
|
|
41
|
-
rootDir
|
|
42
|
-
});
|
|
43
|
-
}
|
|
44
|
-
catch (error) {
|
|
45
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
46
|
-
const statusCode = error.code === 'ENOENT' ? 404 : 500;
|
|
47
|
-
res.status(statusCode).json({ error: message });
|
|
48
|
-
}
|
|
49
|
-
});
|
|
50
|
-
router.get('/download', async (req, res) => {
|
|
51
|
-
try {
|
|
52
|
-
const relativePath = req.query.path;
|
|
53
|
-
if (!relativePath) {
|
|
54
|
-
return res.status(400).json({ error: 'Path parameter is required' });
|
|
55
|
-
}
|
|
56
|
-
const targetPath = path_1.default.join(rootDir, relativePath);
|
|
57
|
-
// Security: Ensure targetPath is within rootDir
|
|
58
|
-
if (!targetPath.startsWith(rootDir)) {
|
|
59
|
-
return res.status(403).json({ error: 'Access denied' });
|
|
60
|
-
}
|
|
61
|
-
// Check if path exists and is a file
|
|
62
|
-
const stats = await fs_extra_1.default.stat(targetPath);
|
|
63
|
-
if (!stats.isFile()) {
|
|
64
|
-
return res.status(404).json({ error: 'File not found' });
|
|
65
|
-
}
|
|
66
|
-
// Stream file for download
|
|
67
|
-
res.download(targetPath, path_1.default.basename(targetPath));
|
|
68
|
-
}
|
|
69
|
-
catch (error) {
|
|
70
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
71
|
-
const statusCode = error.code === 'ENOENT' ? 404 : 500;
|
|
72
|
-
res.status(statusCode).json({ error: message });
|
|
73
|
-
}
|
|
74
|
-
});
|
|
75
|
-
router.get('/zip', async (req, res) => {
|
|
76
|
-
try {
|
|
77
|
-
const relativePath = req.query.path;
|
|
78
|
-
if (!relativePath) {
|
|
79
|
-
return res.status(400).json({ error: 'Path parameter is required' });
|
|
80
|
-
}
|
|
81
|
-
const targetPath = path_1.default.join(rootDir, relativePath);
|
|
82
|
-
// Security: Ensure targetPath is within rootDir
|
|
83
|
-
if (!targetPath.startsWith(rootDir)) {
|
|
84
|
-
return res.status(403).json({ error: 'Access denied' });
|
|
85
|
-
}
|
|
86
|
-
// Check if path exists and is a directory
|
|
87
|
-
const stats = await fs_extra_1.default.stat(targetPath);
|
|
88
|
-
if (!stats.isDirectory()) {
|
|
89
|
-
return res.status(404).json({ error: 'Directory not found' });
|
|
90
|
-
}
|
|
91
|
-
// Set headers for ZIP download
|
|
92
|
-
res.attachment(`${path_1.default.basename(targetPath)}.zip`);
|
|
93
|
-
// Create ZIP stream
|
|
94
|
-
const archive = (0, archiver_1.default)('zip', {
|
|
95
|
-
zlib: { level: 9 } // Maximum compression
|
|
96
|
-
});
|
|
97
|
-
archive.pipe(res);
|
|
98
|
-
archive.directory(targetPath, false);
|
|
99
|
-
await archive.finalize();
|
|
100
|
-
}
|
|
101
|
-
catch (error) {
|
|
102
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
103
|
-
const statusCode = error.code === 'ENOENT' ? 404 : 500;
|
|
104
|
-
res.status(statusCode).json({ error: message });
|
|
105
|
-
}
|
|
106
|
-
});
|
|
107
|
-
context.addRouter('/files', router);
|
|
108
|
-
},
|
|
109
|
-
};
|
|
110
|
-
exports.default = plugin;
|
|
Binary file
|