1.0.6 • Published 11 months ago

smart-file-upload v1.0.6

Weekly downloads
-
License
ISC
Repository
-
Last release
11 months ago

Basics usage flow

  1. Set the below variables in .env file

    - MONGO_DB_URI="paste here mongodb uri"
    - REDIS_HOST=localhost
    - REDIS_PORT=6379
    - REDIS_PASSWORD (this is not compulsion, remove this if your system doesn't ask for redis password)
    - MAX_PART_SIZE=10 (Note: represents in mb) (if the file is 100 mb, it will break in 1000/MAX_PART_SIZE = 100/10 = 10 multipart)
    - S3_BUCKET_NAME="name of you s3 bucket" e.g smartfileupload
    - S3_URL="s3 url of ur bucket" e.g https://s3bucketurl-public.s3.ap-south-1.amazonaws.com
    - SERVER_URL="server url" e.g http://localhost:3000
    - S3_BUCKET_REGION="s3 bucket region" (e.g us-east-1)
    - S3_ACCESS_KEY="access key for ur aws account with s3 previlage"
    - S3_SECRET_ACCESS_KEY="secret accesss key for ur aws account with s3 previlage"
  2. add the above varibales in .env and move to package installation

    • run npm i smart-file-upload
  3. required dependencies

    
    "@aws-sdk/client-s3": "^3.348.0",
    "@nestjs/bull": "^0.6.3",
    "@nestjs/config": "^2.3.2",
    "@nestjs/mongoose": "^9.2.2",
    "@types/multer": "^1.4.7"

    Note: use the latest version

  4. Make a file-controller . You can access these

    • FileUploadService
      • this contains two service named
        • upload() to uplod file
        • getFileDetail ( get file detail based on mongodb id stored)

Helpful code

  1. Nestjs

    exmaple of sample controller file

    import {
    Controller,
    Get,
    HttpStatus,
    Param,
    Post,
    UploadedFile,
    UseGuards,
    UseInterceptors,
    } from "@nestjs/common";
    import { ApiBody, ApiConsumes, ApiTags } from "@nestjs/swagger";
    import { FileInterceptor } from "@nestjs/platform-express";
    import { diskStorage } from "multer";
    import { Request } from "express";
    import _ as path from "path";
    import _ as crypto from "crypto";
    import { DirectoryNameEnum } from "everestwalkgroups-file-upload/common/enum";
    import { FileUploadService } from "everestwalkgroups-file-upload/";
    
    @ApiTags("File Upload")
    @Controller("api/v1/file-upload")
    export class FileUploadController {
    constructor(private readonly fileUploadService: FileUploadService) {}
    
    @ApiConsumes("multipart/form-data")
    @ApiBody({
    schema: {
    type: "object",
    properties: {
    file: {
    type: "string",
    format: "binary",
    },
    },
    },
    })
    @UseInterceptors(
    FileInterceptor("file", {
    storage: diskStorage({
    destination: function (
    req: Request,
    file: Express.Multer.File,
    cb: (error: Error, destination: string) => void
    ) {
    cb(null, path.resolve(DirectoryNameEnum.UPLOADS));
    },
    filename: function (
    req: Request,
    file: Express.Multer.File,
    cb: (error: Error, filename: string) => void
    ) {
    const parsed = path.parse(file.originalname);
    const ext = parsed.ext;
    file.originalname = Buffer.from(parsed.name, "binary").toString();
    file.filename = crypto.randomUUID() + "-" + Date.now() + ext;
    cb(null, file.filename);
    },
    }),
    })
    )
    @Post()
    async upload(@UploadedFile() file: Express.Multer.File) {
    return {
    statusCode: HttpStatus.CREATED,
    data: await this.fileUploadService.upload(file),
    };
    }
    
    @Get(":id")
    async getFileDetail(@Param("id") id: string) {
    return {
    statusCode: HttpStatus.OK,
    data: await this.fileUploadService.getFileDetail(id),
    };
    }
    }
  2. Nodejs Please modify as per as your above code.

1.0.6

11 months ago

1.0.5

11 months ago

1.0.4

11 months ago

1.0.3

11 months ago

1.0.2

11 months ago

1.0.1

11 months ago

1.0.0

11 months ago