Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@file-storage/core

Package Overview
Dependencies
Maintainers
1
Versions
27
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@file-storage/core

> TODO: description

  • 1.1.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
113
increased by94.83%
Maintainers
1
Weekly downloads
 
Created
Source
 ____|_) |       ___|  |
 |     | |  _ \\___ \  __|  _ \   __| _` |  _` |  _ \
 __|   | |  __/      | |   (   | |   (   | (   |  __/
_|    _|_|\___|_____/ \__|\___/ _|  \__,_|\__, |\___|
A file system abstraction for Node.js     |___/

Test License: MIT

A simple abstraction to interact with file system inspired by Laravel File System, provides 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 from '@file-storage/core';
import { DriverName } from '@file-storage/common';

Storage.config({
  diskConfigs: [
    {
      driver: DriverName.LOCAL,
      name: 'local',
      root: 'public',
    },
    {
      driver: DriverName.S3,
      name: 'mys3',
      bucketName: 'mybucket',
      isDefault: true, // Default disk that you can access directly via Storage facade.
      // 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:

If you want 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');

Hash file name

Enable hash file name to prevent a file get replaced when uploading same file (or same name):

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

// The path uploaded will be like this: /path/to/image/my-image_$ru6eu3.png

Create your custom driver

If bult-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 } from '@file-storage/common';

interface OneDriveConfig {
  name: string; // driver instance name is required.
  ...
}

class OneDrive extends Driver {
  static readonly driverName = 'one_drive';

  constructor(config: OneDriveConfig) {
    super(config);
    ...
  }

  // Define all Driver's methods here.
}

And provide it to Storage.customDrivers:

Storage.config<OneDriveConfig>({
  diskConfigs: [
    {
      driver: 'one_drive',
      name: 'myCustomDisk',
      isDefault: true,
      ...
    }
  ],
  customDrivers: [OneDrive],
});

Image manipulation

If you want to upload image and also creates many diferent sizes: large, medium, small, thumbnail for web resonsive, install this package, it is acting as a plugin, will generates those image sizes automatically:

$ yarn add @file-storage/image-manipulation

HINT: Image manipulation only available on Starage 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.

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.

License

MIT

FAQs

Package last updated on 21 Aug 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc