Giter Club home page Giter Club logo

file-storage's Introduction

 ____|_) |       ___|  |
 |     | |  _ \\___ \  __|  _ \   __| _` |  _` |  _ \
 __|   | |  __/      | |   (   | |   (   | (   |  __/
_|    _|_|\___|_____/ \__|\___/ _|  \__,_|\__, |\___|
A file system abstraction for Node.js     |___/

Test License: MIT

A simple abstraction to interact with file system inspired by Laravel File System, provide one interface for many kind of drivers: local, ftp, sftp, Amazon S3, and Google Cloud Storage, even your custom driver.

Installation

$ yarn add @file-storage/core @file-storage/common

# Or npm
$ npm install @file-storage/core @file-storage/common

And upload a file to local, it will be stored in storage folder in your root project directory by default:

import Storage from '@file-storage/core';

// Upload from file path.
Storage.put('/my-image.png', '/path/of/destination/my-image.png');

// Or from a read-stream/buffer:
Storage.put(stream, '/path/of/destination/my-image.png');

Configuration

By default only local driver is supported, if you want to use another driver, you need to install corresponding package:

  • Amazon S3: yarn add @file-storage/s3
  • FTP: yarn add @file-storage/ftp
  • SFTP: yarn add @file-storage/sftp
  • Google Cloud Storage: yarn add @file-storage/gcs

If there is no configuration, it will uploads to local disk. You can specific yours by using config method:

import Storage, { BuiltInDiskConfig } from '@file-storage/core';
import { DriverName } from '@file-storage/common';

Storage.config<BuiltInDiskConfig>({
  // Default disk that you can access directly via Storage facade.
  defaultDiskName: 'mys3',
  diskConfigs: [
    {
      driver: DriverName.LOCAL,
      name: 'local',
      root: 'public',
    },
    {
      driver: DriverName.S3,
      name: 'mys3',
      bucketName: 'mybucket',
      // Uncomment if you want specify credentials manually.
      // region: 'ap-southeast-1',
      // credentials: {
      //   accessKeyId: '123abc',
      //   secretAccessKey: '123abc',
      // },
    },
  ],
});

// Somewhere in your code...
// Get file from s3:
Storage.get('/path/to/s3-bucket/my-image.png');

Obtain disk instance:

To interact with a specific disk instead of the default, use disk method to get that instance:

Storage.disk('local').get('/path/to/local/my-image.png');

Unique file name

Enable unique file name to prevent a file get replaced when uploading same file (or same name). The unique name generated by uuid to secure your file path.

Storage.config({
  ...
  uniqueFileName: true,
});

// The uploaded path could be like this: /path/to/e8a3e633-fc7f-4dde-b7f0-d2686bcd6836.jpeg

Create your custom driver

If built-in drivers doesn't match your need, just defines a custom driver by extends Driver abstract class:

import Storage from '@file-storage/core';
import { Driver, DiskConfig } from '@file-storage/common';

interface MyCustomDiskConfig extends DiskConfig {
  driver: typeof MyCustomDriver;
  ...
}

class MyCustomDriver extends Driver {
  constructor(config: MyCustomDiskConfig) {
    super(config);
    ...
  }

  // Implement all Driver's methods here.
}

And provide it to Storage.diskConfigs:

Storage.config<MyCustomDiskConfig>({
  diskConfigs: [
    {
      driver: MyCustomDriver,
      name: 'myCustomDisk',
      ...
    }
  ],
});

Image manipulation

To upload image and also creates many diferent sizes for web resonsive, install this package, it is acting as a plugin, will generates those images automatically. Images will be generated if the size reach given breakpoints. We provide 3 breakpoints by default: large: 1000, medium: 750, small: 500. And the thumbnail is also generaged by default.

$ yarn add @file-storage/image-manipulation

NOTE: Image manipulation only available on Storage facade, If you obtain a specific disk instance, set the second parameter to true to obtain a storage instance insteads:

Storage.disk('your-disk', true); // Storage instance.

Image manipulation customize

You can customize responsive formats and thumbnail size:

import ImageManipulation from '@file-storage/image-manipulation';

ImageManipulation.config({
  breakpoints: {
    size1: 500,
    size2: 800,
  },
  thumbnailResizeOptions: {
    width: 333,
    height: 222,
    fit: 'contain',
  },
});

TODO

  • Create interface for all result (Need same result format for all drivers).
  • Refactor customDrivers option: provides disk defination is enough.
  • Implement GCS disk.
  • Put file from a local path.
  • API section: detailed of each driver.
  • Remove customDrivers option, pass custom driver class directly to diskConfigs.driver.
  • Unique file name.
  • Update aws-sdk to v3.
  • Replace request module with another module as it was deprecated.

License

MIT

file-storage's People

Contributors

dangnfq avatar dangnguyenemma avatar googlicius avatar sr258 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.