Skip to content

Commit

Permalink
fix(storage): use primary key as minio bucket name + persist minio st…
Browse files Browse the repository at this point in the history
…orage in docker-compose
  • Loading branch information
tada5hi committed Jun 14, 2024
1 parent 5fd1515 commit cfc4cbb
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 18 deletions.
4 changes: 4 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ version: '3.9'
volumes:
mysql:
driver: local
minio:
driver: local
services:
mysql:
image: mysql:latest
Expand Down Expand Up @@ -58,6 +60,8 @@ services:
environment:
RABBITMQ_DEFAULT_USER: root
RABBITMQ_DEFAULT_PASS: start123
volumes:
- minio:/data
networks:
hub:
vault:
Expand Down
8 changes: 8 additions & 0 deletions packages/server-storage/src/domains/bucket/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,11 @@ export function isBucketOwnedByActor(entity: BucketEntity, actor: Actor) {
return entity.actor_type === actor.type &&
entity.actor_id === actor.id;
}

export function toBucketName(input: string) : string {
input = input
.toLowerCase()
.replace(/[^a-z0-9.-]/g, '');

return input.slice(0, Math.min(63, input.length));
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
BucketFileEntity,
getActorFromRequest,
isBucketFileOwnedByActor,
isBucketOwnedByActor,
isBucketOwnedByActor, toBucketName,
} from '../../../../domains';

export async function executeBucketFileRouteDeleteHandler(req: Request, res: Response) : Promise<any> {
Expand Down Expand Up @@ -52,7 +52,7 @@ export async function executeBucketFileRouteDeleteHandler(req: Request, res: Res
}

const minio = useMinio();
await minio.removeObject(entity.bucket.name, entity.hash);
await minio.removeObject(toBucketName(entity.bucket.id), entity.hash);

const { id: entityId } = entity;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { useRequestParam } from 'routup';
import { useDataSource } from 'typeorm-extension';
import { useMinio } from '../../../../core';
import {
BucketFileEntity,
BucketFileEntity, toBucketName,
} from '../../../../domains';

export async function executeBucketFileRouteStreamHandler(req: Request, res: Response) : Promise<any> {
Expand All @@ -38,7 +38,7 @@ export async function executeBucketFileRouteStreamHandler(req: Request, res: Res
// setResponseHeaderAttachment(res, entity.path);

const minio = useMinio();
const stream = await minio.getObject(entity.bucket.name, entity.hash);
const stream = await minio.getObject(toBucketName(entity.bucket.id), entity.hash);

stream.pipe(res);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { sendCreated } from 'routup';
import { useDataSource } from 'typeorm-extension';
import { useRequestEnv } from '@privateaim/server-http-kit';
import { useMinio } from '../../../../core';
import { BucketEntity, getActorFromRequest } from '../../../../domains';
import { BucketEntity, getActorFromRequest, toBucketName } from '../../../../domains';
import { runBucketValidation } from '../utils/validation';

export async function executeBucketRouteCreateHandler(req: Request, res: Response) : Promise<any> {
Expand Down Expand Up @@ -40,9 +40,9 @@ export async function executeBucketRouteCreateHandler(req: Request, res: Respons

const minio = useMinio();
if (entity.region) {
await minio.makeBucket(entity.name, entity.region);
await minio.makeBucket(toBucketName(entity.id), entity.region);
} else {
await minio.makeBucket(entity.name);
await minio.makeBucket(toBucketName(entity.id));
}

return sendCreated(res, entity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import { sendAccepted, useRequestParam } from 'routup';
import { useDataSource } from 'typeorm-extension';
import { useRequestEnv } from '@privateaim/server-http-kit';
import { useMinio } from '../../../../core';
import { BucketEntity, getActorFromRequest, isBucketOwnedByActor } from '../../../../domains';
import {
BucketEntity, getActorFromRequest, isBucketOwnedByActor, toBucketName,
} from '../../../../domains';

export async function executeBucketRouteDeleteHandler(req: Request, res: Response) : Promise<any> {
const id = useRequestParam(req, 'id');
Expand Down Expand Up @@ -45,10 +47,10 @@ export async function executeBucketRouteDeleteHandler(req: Request, res: Respons
}
}

const { id: entityId, name: entityName } = entity;
const { id: entityId } = entity;

const minio = useMinio();
await minio.removeBucket(entityName);
await minio.removeBucket(toBucketName(entityId));

await repository.remove(entity);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import tar from 'tar-stream';
import { useDataSource } from 'typeorm-extension';
import { streamToBuffer, useMinio } from '../../../../core';
import {
BucketEntity, BucketFileEntity,
BucketEntity, BucketFileEntity, toBucketName,
} from '../../../../domains';

async function packFile(
Expand Down Expand Up @@ -87,7 +87,7 @@ export async function executeBucketRouteStreamHandler(req: Request, res: Respons
const pack = tar.pack();
pack.pipe(res);

await packFiles(pack, entity.name, files);
await packFiles(pack, toBucketName(entity.id), files);

pack.finalize();
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import { sendAccepted, useRequestParam } from 'routup';
import { useDataSource } from 'typeorm-extension';
import { useRequestEnv } from '@privateaim/server-http-kit';
import { useMinio } from '../../../../core';
import { BucketEntity, getActorFromRequest, isBucketOwnedByActor } from '../../../../domains';
import {
BucketEntity, getActorFromRequest, isBucketOwnedByActor, toBucketName,
} from '../../../../domains';
import { runBucketValidation } from '../utils/validation';

export async function executeBucketRouteUpdateHandler(req: Request, res: Response) : Promise<any> {
Expand Down Expand Up @@ -55,12 +57,12 @@ export async function executeBucketRouteUpdateHandler(req: Request, res: Respons
await repository.save(entity);

const minio = useMinio();
const hasBucket = await minio.bucketExists(entity.name);
const hasBucket = await minio.bucketExists(entity.id);
if (!hasBucket) {
if (entity.region) {
await minio.makeBucket(entity.name, entity.region);
await minio.makeBucket(toBucketName(entity.id), entity.region);
} else {
await minio.makeBucket(entity.name);
await minio.makeBucket(toBucketName(entity.id));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import type { Request, Response } from 'routup';
import { useDataSource } from 'typeorm-extension';
import { useRequestEnv } from '@privateaim/server-http-kit';
import { streamToBuffer, useMinio } from '../../../../core';
import { BucketEntity, BucketFileEntity, getActorFromRequest } from '../../../../domains';
import {
BucketEntity, BucketFileEntity, getActorFromRequest, toBucketName,
} from '../../../../domains';

export async function uploadRequestFiles(req: Request, bucketName: string) {
const minio = useMinio();
Expand Down Expand Up @@ -104,7 +106,7 @@ export async function executeBucketRouteUploadHandler(req: Request, res: Respons
throw new NotFoundError();
}

let files = await uploadRequestFiles(req, entity.name);
let files = await uploadRequestFiles(req, toBucketName(entity.id));

if (files.length > 0) {
files = files.map((file) => ({
Expand Down
18 changes: 18 additions & 0 deletions packages/server-storage/test/unit/bucket-name.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright (c) 2024.
* Author Peter Placzek (tada5hi)
* For the full copyright and license information,
* view the LICENSE file that was distributed with this source code.
*/

import { toBucketName } from '../../src/domains';

describe('src/domains/bucket', () => {
it('should serialize bucket name', () => {
let output = toBucketName('805735EC-6A00-4368-88CD-83C818806f7A');
expect(output).toEqual('805735ec-6a00-4368-88cd-83c818806f7a');

output = toBucketName('805735ec-6a00-4368-88cd-83c818806f7a-83c818806f7a-83c818806f7a-foo');
expect(output).toEqual('805735ec-6a00-4368-88cd-83c818806f7a-83c818806f7a-83c818806f7a-');
});
});

0 comments on commit cfc4cbb

Please sign in to comment.