🛠️ VitNode is still in development! You can try it out, but it is not recommended to use it now in production.
Backend
Files

Files

This documentation is deprecated!

The core_attachemnts is responsible for upload files to the system.

File system will save your files in public folder.

Basic usage

As an example, we will upload a file for a avatar user.

Arguments (DTO)

File: upload/dto/upload-avatar-core_members.args.ts

import { ArgsType, Field } from '@nestjs/graphql';
 
import { FileUpload } from '@/utils/graphql-upload/Upload';
import { GraphQLUpload } from '@/utils/graphql-upload/GraphQLUpload';
 
@ArgsType()
export class UploadAvatarCoreMembersArgs {
  @Field(() => GraphQLUpload)
  file: Promise<FileUpload>;
}

Resolver

File: upload/upload-avatar-core_members.resolver.ts

In this example, we will use the @CurrentUser() decorator to get the user data.

import { Args, Mutation, Resolver } from '@nestjs/graphql';
import { UseGuards } from '@nestjs/common';
 
import { UploadAvatarCoreMembersService } from './upload-avatar-core_members.service';
import { UploadAvatarCoreMembersArgs } from './dto/upload-avatar-core_members.args';
 
import { User, CurrentUser } from '@/utils/decorators/user.decorator';
import { AuthGuards } from '@/utils/guards/auth.guards';
 
@Resolver()
export class UploadAvatarCoreMembersResolver {
  constructor(private readonly service: UploadAvatarCoreMembersService) {}
 
  @Mutation(() => String)
  @UseGuards(AuthGuards)
  async core_members__avatar__upload(
    @CurrentUser() currentUser: User,
    @Args() args: UploadAvatarCoreMembersArgs
  ): Promise<string> {
    return await this.service.uploadAvatar(currentUser, args);
  }
}

Upload service

To upload files to the system, we will use the UploadCoreAttachmentsService service.

await this.attachments.upload({
  files: [
    {
      file,
      description: 'Avatar',
      position: 0
    }
  ],
  maxUploadSizeBytes: 1e6, // 1MB
  acceptMimeType: ['image/png', 'image/jpeg'],
  module: 'core_members',
  module_id: id
});

Arguments for the upload method:

ArgsDescription
filesArray of files to upload. If you want to upload multiple files, just add more objects to the array.
file: File to upload.
position: File position.
description?: File description (optional). If you don't set this value, the system will automatically set the description based on the file name.
maxUploadSizeBytesMaximum file size in bytes.
acceptMimeTypeArray of accepted mime types.
moduleModule name. In this example, we will use core_members.
module_idModule ID. In this example, we will use the user ID.

Returns value

The upload method returns a Promise<UploadCoreAttachmentsObj>.

@ObjectType()
export class UploadCoreAttachmentsObj {
  @Field(() => String)
  module: string;
 
  @Field(() => String)
  module_id: string;
 
  @Field(() => String)
  name: string;
 
  @Field(() => String)
  mimetype: string;
 
  @Field(() => String)
  path: string;
 
  @Field(() => Int)
  created: number;
 
  @Field(() => Int)
  position: number;
 
  @Field(() => String, { nullable: true })
  description: string | null;
 
  @Field(() => String)
  extension: string;
 
  @Field(() => Int)
  file_size: number;
 
  @Field(() => String)
  user_id: string;
}

Full code for Service

File: upload/upload-avatar-core_members.service.ts

import { Injectable } from '@nestjs/common';
 
import { UploadAvatarCoreMembersArgs } from './dto/upload-avatar-core_members.args';
 
import { UploadCoreAttachmentsService } from '@/src/core/attachments/upload/upload-core_attachments.service';
import { User } from '@/utils/decorators/user.decorator';
 
@Injectable()
export class UploadAvatarCoreMembersService {
  constructor(private readonly attachments: UploadCoreAttachmentsService) {}
 
  async uploadAvatar({ id }: User, { file }: UploadAvatarCoreMembersArgs): Promise<string> {
    await this.attachments.upload({
      files: [
        {
          file,
          description: 'Avatar',
          position: 0
        }
      ],
      maxUploadSizeBytes: 1e6, // 1MB
      acceptMimeType: ['image/png', 'image/jpeg'],
      module: 'core_members',
      module_id: id
    });
 
    return 'Success!';
  }
}