npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

smart-file-upload

v1.0.6

Published

Smart file upload

Downloads

1

Readme

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.