Skip to content

Commit

Permalink
chore: handle limit errors and generalize limit parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
V-ed committed Aug 25, 2023
1 parent cf27104 commit 76e0972
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 20 deletions.
9 changes: 8 additions & 1 deletion api/src/@common/minio/minio-client.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import { MinioModule } from 'nestjs-minio-client';
import { EnvironmentConfig } from '~/env.validation';
import { MinioClientService } from './minio-client.service';

export const GRAPHQL_MAX_FILE_SIZE_MB = 10;
export const GRAPHQL_MAX_FILE_COUNT = 10;

@Module({
imports: [
MinioModule.registerAsync({
Expand All @@ -22,6 +25,10 @@ import { MinioClientService } from './minio-client.service';
})
export class MinioClientModule implements NestModule {
configure(consumer: MiddlewareConsumer) {
consumer.apply(graphqlUploadExpress({ maxFileSize: 1000000, maxFiles: 10 })).forRoutes('/graphql');
const byteToMbRatio = 1000000;

consumer
.apply(graphqlUploadExpress({ maxFileSize: GRAPHQL_MAX_FILE_SIZE_MB * byteToMbRatio, maxFiles: GRAPHQL_MAX_FILE_COUNT }))
.forRoutes('/graphql');
}
}
17 changes: 16 additions & 1 deletion api/src/@common/minio/minio-client.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as crypto from 'crypto';
import { FileUpload } from 'graphql-upload/Upload.js';
import { MinioService } from 'nestjs-minio-client';
import { EnvironmentConfig } from '~/env.validation';
import { GRAPHQL_MAX_FILE_COUNT, GRAPHQL_MAX_FILE_SIZE_MB } from './minio-client.module';

@Injectable()
export class MinioClientService {
Expand Down Expand Up @@ -40,6 +41,20 @@ export class MinioClientService {
}

public async upload(file: FileUpload, bucketName: string) {
const fileStream = (() => {
try {
return file.createReadStream();
} catch (error) {
throw new HttpException(
`Invalid file! Make sure that you are sending less than ${GRAPHQL_MAX_FILE_COUNT} files and that the files are less than ${GRAPHQL_MAX_FILE_SIZE_MB} MB.`,
HttpStatus.BAD_REQUEST,
{
cause: error,
},
);
}
})();

const timestamp = Date.now().toString();
const hashedFileName = crypto.createHash('md5').update(timestamp).digest('hex');
const extension = file.filename.substring(file.filename.lastIndexOf('.'), file.filename.length);
Expand All @@ -52,7 +67,7 @@ export class MinioClientService {
await this.verifyBucketExistence(appBucketName);

try {
await this.client.putObject(appBucketName, fileName, file.createReadStream(), {
await this.client.putObject(appBucketName, fileName, fileStream, {
'Content-Type': file.mimetype,
});
} catch (error) {
Expand Down
9 changes: 2 additions & 7 deletions api/src/@common/users/avatar/avatar.resolver.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { AuthSession, LuciaSession } from '$auth/session.decorator';
import { SuccessOutput } from '$graphql/common-dto/success.dto';
import { Args, Mutation, Resolver } from '@nestjs/graphql';
import GraphQLUpload from 'graphql-upload/GraphQLUpload.js';
import { FileUpload } from 'graphql-upload/Upload.js';
Expand All @@ -23,12 +22,8 @@ export class AvatarResolver {
} satisfies UploadAvatarOutput;
}

@Mutation(() => SuccessOutput, { nullable: true })
@Mutation(() => Boolean)
async deleteAvatar(@AuthSession() { user }: LuciaSession) {
await this.avatarService.deleteUserImage(user);

return {
success: true,
} satisfies SuccessOutput;
return this.avatarService.deleteUserImage(user);
}
}
4 changes: 3 additions & 1 deletion api/src/@common/users/avatar/avatar.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export class AvatarService {

async deleteUserImage(user: User) {
if (!user.avatarRef) {
return;
return false;
}

await this.minioClientService.delete(user.avatarRef, AVATAR_BUCKET_NAME);
Expand All @@ -67,5 +67,7 @@ export class AvatarService {
id: user.id,
},
});

return true;
}
}
6 changes: 1 addition & 5 deletions api/tests/@generated/graphql/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ export type MessageWhereUniqueInput = {
export type Mutation = {
addMessage?: Maybe<Message>;
createUser: CreateUserOutput;
deleteAvatar?: Maybe<SuccessOutput>;
deleteAvatar: Scalars['Boolean']['output'];
deleteUser?: Maybe<GetUserOutput>;
editUser: User;
login: LoggedUserOutput;
Expand Down Expand Up @@ -563,10 +563,6 @@ export type SubscriptionMessageAddedArgs = {
where?: InputMaybe<MessageWhereInput>;
};

export type SuccessOutput = {
success: Scalars['Boolean']['output'];
};

export type UnregisteredUserOutput = {
/** Email of the unregistered user */
email: Scalars['String']['output'];
Expand Down
6 changes: 1 addition & 5 deletions client/src/graphql/@generated/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ export type MessageWhereUniqueInput = {
export type Mutation = {
addMessage?: Maybe<Message>;
createUser: CreateUserOutput;
deleteAvatar?: Maybe<SuccessOutput>;
deleteAvatar: Scalars['Boolean']['output'];
deleteUser?: Maybe<GetUserOutput>;
editUser: User;
login: LoggedUserOutput;
Expand Down Expand Up @@ -563,10 +563,6 @@ export type SubscriptionMessageAddedArgs = {
where?: InputMaybe<MessageWhereInput>;
};

export type SuccessOutput = {
success: Scalars['Boolean']['output'];
};

export type UnregisteredUserOutput = {
/** Email of the unregistered user */
email: Scalars['String']['output'];
Expand Down

0 comments on commit 76e0972

Please sign in to comment.