Skip to content

Commit 3296a1b

Browse files
authored
Merge pull request #41 from import-ai/fix/upload_encoding
fix(upload): Fix uploaded filename encoding
2 parents 9af37b9 + 78c8d85 commit 3296a1b

File tree

9 files changed

+57
-16
lines changed

9 files changed

+57
-16
lines changed

.env.example

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,6 @@ OBB_JWT_EXPIRE=2678400s
1515

1616
OBB_MAIL_TRANSPORT=smtps://your-email@example.com:your-email-password@smtp.example.com:465
1717
OBB_MAIL_FROM="No Reply <no-reply@example.com>"
18+
19+
OBB_MINIO_ENDPOINT=http://username:password@localhost:9000
20+
OBB_MINIO_BUCKET=omnibox-default

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,25 @@
1616
docker compose -f compose.yaml -f build.yaml up -d --build
1717
```
1818

19+
+ Run with persistence postgres and minio data
20+
21+
```shell
22+
docker compose ... -f persistence.yaml ...
23+
```
24+
25+
+ Run with pgadmin
26+
27+
```shell
28+
docker compose ... -f pgadmin.yaml ...
29+
```
30+
31+
Then login with:
32+
33+
| Name | Value |
34+
|----------|------------------|
35+
| Username | `omnibox@qq.com` |
36+
| Password | `Passw0rd` |
37+
1938
### Locally
2039

2140
```bash

compose.yaml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ services:
66
- "${OBB_PORT:-8000}:${OBB_PORT:-8000}"
77
env_file:
88
- .env
9+
environment:
10+
OBB_DB_HOST: postgres
11+
OBB_MINIO_ENDPOINT: http://username:password@minio:9000
912
depends_on:
1013
postgres:
1114
condition: service_healthy
@@ -26,8 +29,6 @@ services:
2629
- POSTGRES_USER=${OBB_DB_USERNAME:-omnibox}
2730
- POSTGRES_PASSWORD=${OBB_DB_PASSWORD:-omnibox}
2831
- POSTGRES_PORT=${OBB_DB_PORT:-5432}
29-
volumes:
30-
- ${PWD}/.tmp/data/postgres:/var/lib/postgresql/data
3132
healthcheck:
3233
test: [ "CMD", "pg_isready", "-q", "-d", "${OBB_DB_DATABASE:-omnibox}", "-U", "${OBB_DB_USERNAME:-omnibox}", "-p", "${OBB_DB_PORT:-5432}" ]
3334
interval: 5s
@@ -46,8 +47,6 @@ services:
4647
interval: 5s
4748
timeout: 3s
4849
retries: 5
49-
volumes:
50-
- '${PWD}/.tmp/data/minio:/data'
5150
ports:
5251
- '9001:9001'
5352
- '9000:9000'

dev.yaml

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,3 @@ services:
1212
timeout: 3s
1313
retries: 60
1414
start_period: 30s
15-
16-
pgadmin:
17-
image: dpage/pgadmin4
18-
ports:
19-
- '8888:80'
20-
environment:
21-
PGADMIN_DEFAULT_EMAIL: user-name@domain-name.com
22-
PGADMIN_DEFAULT_PASSWORD: strong-password
23-
volumes:
24-
- ${PWD}/.tmp/data/pgadmin:/var/lib/pgadmin

persistence.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
services:
2+
postgres:
3+
volumes:
4+
- "${PWD}/.tmp/data/postgres:/var/lib/postgresql/data"
5+
6+
minio:
7+
volumes:
8+
- "${PWD}/.tmp/data/minio:/data"

pgadmin.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
services:
2+
pgadmin:
3+
image: dpage/pgadmin4
4+
ports:
5+
- '8888:80'
6+
environment:
7+
PGADMIN_DEFAULT_EMAIL: user-name@domain-name.com
8+
PGADMIN_DEFAULT_PASSWORD: strong-password
9+
volumes:
10+
- ${PWD}/.tmp/data/pgadmin:/var/lib/pgadmin

src/resources/resources.controller.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ export async function fileResponse(
2626
) {
2727
const { fileStream, resource } =
2828
await resourcesService.downloadFile(resourceId);
29+
const encodedName = encodeURIComponent(resource.name);
2930
response.setHeader(
3031
'Content-Disposition',
31-
`attachment; filename="${resource.name}"`,
32+
`attachment; filename="${encodedName}"`,
3233
);
3334
response.setHeader(
3435
'Content-Type',

src/resources/resources.service.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ export interface IQuery {
2727
tags?: string;
2828
}
2929

30+
function decode(text: string) {
31+
if (!text) {
32+
return text;
33+
}
34+
return decodeURIComponent(Buffer.from(text, 'binary').toString('utf-8'));
35+
}
36+
3037
@Injectable()
3138
export class ResourcesService {
3239
constructor(
@@ -194,6 +201,10 @@ export class ResourcesService {
194201
parentId?: string,
195202
resourceId?: string,
196203
) {
204+
// TODO name received is not utf-8 string.
205+
file.originalname = decode(file.originalname);
206+
file.filename = decode(file.filename);
207+
197208
let resource: Resource;
198209
if (resourceId) {
199210
resource = await this.get(resourceId);

test/resources.e2e-spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ describe('ResourcesController (e2e)', () => {
2828
await app.close();
2929
});
3030

31-
it('/api/v1/resources/upload (POST) and /api/v1/resources/download (GET)', async () => {
31+
it('files upload (POST) and files download (GET)', async () => {
3232
// Prepare a test file
3333
const testFilePath = path.join(__dirname, 'test-upload.txt');
3434
fs.writeFileSync(testFilePath, 'hello world');

0 commit comments

Comments
 (0)