Giter VIP home page Giter VIP logo

nestjs-module-file's Introduction

a## how use

ImageModule.forRootAsync({
  imports: [TypeOrmModule.forFeature(FileBaseModule.entities), HttpModule],
  controllers: [...FileBaseModule.controllers],
  providers: [...FileBaseModule.providers],
  destination: 'upload/',
  inject: [ConfigService, HttpService],
  useFactory: async (FileStorageInstall, GuardStore) => {
    const imageMaxSize = 1024 * 1024 * 10; //100
    //需要限制大小和使用后卫在这里限制
    // GuardStore.inject = async (context) => {
    //   //限制大小
    //   const req = context.switchToHttp().getRequest<Request>();
    //   const header = req.headers;
    //   if (+header['content-length'] > imageMaxSize) {
    //     throw new BadRequestException();
    //   }
    //   const auth = RootModule.install.get(AdminAuthGuard);
    //   return await auth.canActivate(context);
    // };
    return new FileFactor({
      domain: () => {
        return 'http://127.0.0.1:3001';
      },
    });
    //使用cos上传,自定义上传需要实现IFileFactory
    // return new FileFactorCos({
    //   SecretKey: configService.get('COS_SECRETKEY'),
    //   SecretId: configService.get('COS_SECRETID'),
    //   bucket: configService.get('COS_BUCKET'),
    //   region: configService.get('COS_REGION'),
    //   Key: (path) => {
    //     console.log(path);
    //     return path;
    //   },
    //   domain: () => {
    //     return 'https://testupload-1256172954.cos.ap-chengdu.myqcloud.com';
    //   },
    // });
  },
});

如何添加cos模块

import { IFileFactory } from './types';
import * as COS from 'cos-nodejs-sdk-v5';
import * as fs from 'fs-extra';
export interface IFileFactorCosOptions {
  SecretId: string;
  SecretKey: string;
  bucket: string;
  region: string;
  Key: (filePath: string) => string;
  domain: () => string;
}
export class FileFactorCos implements IFileFactory {
  domain: () => string;
  cos: COS;
  bucket: string;
  region: string;
  Key: (path: string) => string;
  constructor(options: IFileFactorCosOptions) {
    this.cos = new COS({
      SecretId: options.SecretId,
      SecretKey: options.SecretKey,
    });
    this.bucket = options.bucket;
    this.region = options.region;
    this.Key = options.Key;
    this.domain = options.domain;
  }

  async saveFile(file: Express.Multer.File & { md5: string }) {
    try {
      const key = this.Key(file.path);
      const result = await this.cos.putObject({
        Bucket: this.bucket,
        Region: this.region,
        Key: key,
        Body: fs.createReadStream(file.path),
        ContentLength: file.size,
      });
      if (result?.statusCode == 200) {
        await fs.remove(file.path);
        return {
          path: key.replace(/^\//, ''),
          size: file.size,
          md5: file.md5,
        };
      }
      throw new Error('上传失败');
    } catch (e) {
      throw e;
    }
  }
}

nestjs-module-file's People

Contributors

guojianlan avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.