0.0.22 • Published 2 years ago

node-filesystem v0.0.22

Weekly downloads
19
License
MIT
Repository
github
Last release
2 years ago

Node File System

This project was inspired on the great PHP package Flysystem, and tries to be API compatible when possible. It's a work in progress. Pull requests are welcome.

Like Flysystem, is a filesystem abstraction which allows you to easily swap out a local filesystem for a remote one.

It's made with Typescript. All methods are async.

Goals

  • Have a generic API for handling common tasks across multiple file storage engines.
  • Have consistent output which you can rely on.
  • Emulate directories in systems that support none, like AwsS3.

Installation

Using YARN:

yarn add node-filesystem

Using NPM:

npm add node-filesystem --save

Core concepts

See Flysysytem docs

The API

Write Files

await filesystem.write('path/to/file.txt', 'contents');

Update Files

await filesystem.update('path/to/file.txt', 'new contents');

Write or Update Files

await filesystem.put('path/to/file.txt', 'contents');

Read Files

const contents = await filesystem.read('path/to/file.txt');

Check if a file exists

const exists = await filesystem.has('path/to/file.txt');

NOTE: This only has consistent behaviour for files, not directories. Directories are less important, they’re created implicitly and often ignored because not every adapter (filesystem type) supports directories.

Delete Files

await filesystem.delete('path/to/file.txt');

Rename Files

await filesystem.rename('filename.txt', 'newname.txt');

Copy Files

await filesystem.copy('filename.txt', 'duplicate.txt');

Get Mimetypes

const mimetype = await filesystem.getMimetype('path/to/file.txt');

Get Timestamps

const timestamp = await filesystem.getTimestamp('path/to/file.txt');

Get File Sizes

const size = await filesystem.getSize('path/to/file.txt');

Create Directories

await filesystem.createDir('path/to/nested/directory');

Directories are also made implicitly when writing to a deeper path

await filesystem.write('path/to/file.txt', 'contents');

Delete Directories

await filesystem.deleteDir('path/to/directory');

The above method will delete directories recursively

NOTE: All paths used are relative to the adapter root directory.

Manage Visibility

Visibility is the abstraction of file permissions across multiple platforms. Visibility can be either public or private.

await filesystem.write('db.backup', backup, {
  visibility: 'private',
});

You can also change and check visibility of existing files

if ((await filesystem.getVisibility('secret.txt')) === 'private') {
  await filesystem.setVisibility('secret.txt', 'public');
}

List Contents

const contents = await filesystem.listContents();

The result of a contents listing is a collection of arrays containing all the metadata the file manager knows at that time. By default you’ll receive path info and file type. Additional info could be supplied by default depending on the adapter used.

Example:

for (const object of contents) {
  console.log(
    object.basename,
    ' is located at ',
    object.path,
    ' and is a ',
    object.type,
  );
}

By default it lists the top directory non-recursively. You can supply a directory name and recursive boolean to get more precise results

const contents = filesystem.listContents('some/dir', true);

Adapters

Local

import { LocalAdapter } from 'node-filesystem';

new LocalAdapter('my-root-folder', 'my-subfolder');

Aws S3

You need install the oficial AWS SDK:

yarn add aws-sdk
import * as AWS from 'aws-sdk';
import { S3Adapter } from 'node-filesystem';

const s3Client = new AWS.S3({
  accessKeyId: 'my-aws-access-key',
  secretAccessKey: 'my-aws-secret-key',
  region: 'my-aws-region',
});

new S3Adapter(s3Client, 'my-bucket', 'my-subfolder');

Google Cloud Storage

import * as Storage from '@google-cloud/storage';
import { GoogleStorage } from 'node-filesystem';

const googleStorageClient = new Storage({
  projectId: '',
});

process.env.GOOGLE_APPLICATION_CREDENTIALS =
  __dirname + '/../gcp-credentials.json';

new GoogleStorage(googleStorageClient, 'my-bucket', 'my-subfolder');

DigitalOcean Spaces

You can use the AWS S3 Adapter

1.0.0-beta.3

2 years ago

1.0.0-beta.2

2 years ago

1.0.0-beta.0

2 years ago

1.0.0-beta.1

2 years ago

0.0.22

2 years ago

0.0.21

3 years ago

0.0.20

5 years ago

0.0.19

5 years ago

0.0.18

5 years ago

0.0.17

5 years ago

0.0.16

5 years ago

0.0.15

6 years ago

0.0.14

6 years ago

0.0.13

6 years ago

0.0.12

6 years ago

0.0.11

6 years ago

0.0.10

6 years ago

0.0.9

6 years ago

0.0.8

6 years ago

0.0.7

6 years ago

0.0.6

6 years ago

0.0.5

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago